blob: a8586e10953c665ddb7d03cf786f78c08befa3cf [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>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050041#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040042#include "ctree.h"
43#include "disk-io.h"
44#include "transaction.h"
45#include "btrfs_inode.h"
46#include "ioctl.h"
47#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040048#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040049#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040050#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040051#include "tree-log.h"
Chris Masonc8b97812008-10-29 14:49:59 -040052#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050053#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050054#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080055#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040056
57struct btrfs_iget_args {
58 u64 ino;
59 struct btrfs_root *root;
60};
61
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070062static const struct inode_operations btrfs_dir_inode_operations;
63static const struct inode_operations btrfs_symlink_inode_operations;
64static const struct inode_operations btrfs_dir_ro_inode_operations;
65static const struct inode_operations btrfs_special_inode_operations;
66static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070067static const struct address_space_operations btrfs_aops;
68static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070069static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050070static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040071
72static struct kmem_cache *btrfs_inode_cachep;
73struct kmem_cache *btrfs_trans_handle_cachep;
74struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050076struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077
78#define S_SHIFT 12
79static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
80 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
81 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
82 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
83 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
84 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
85 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
86 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
87};
88
Josef Bacika41ad392011-01-31 15:30:16 -050089static int btrfs_setsize(struct inode *inode, loff_t newsize);
90static int btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040091static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050092static noinline int cow_file_range(struct inode *inode,
93 struct page *locked_page,
94 u64 start, u64 end, int *page_started,
95 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040096
Yan, Zhengf34f57a2009-11-12 09:35:27 +000097static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -050098 struct inode *inode, struct inode *dir,
99 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500100{
101 int err;
102
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000103 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500104 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500105 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500106 return err;
107}
108
Chris Masond352ac62008-09-29 15:18:18 -0400109/*
Chris Masonc8b97812008-10-29 14:49:59 -0400110 * this does all the hard work for inserting an inline extent into
111 * the btree. The caller should have done a btrfs_drop_extents so that
112 * no overlapping inline items exist in the btree
113 */
Chris Masond3977122009-01-05 21:25:51 -0500114static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400115 struct btrfs_root *root, struct inode *inode,
116 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000117 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400118 struct page **compressed_pages)
119{
120 struct btrfs_key key;
121 struct btrfs_path *path;
122 struct extent_buffer *leaf;
123 struct page *page = NULL;
124 char *kaddr;
125 unsigned long ptr;
126 struct btrfs_file_extent_item *ei;
127 int err = 0;
128 int ret;
129 size_t cur_size = size;
130 size_t datasize;
131 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400132
Li Zefanfe3f5662011-03-28 08:30:38 +0000133 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400134 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400135
Chris Masond3977122009-01-05 21:25:51 -0500136 path = btrfs_alloc_path();
137 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400138 return -ENOMEM;
139
Chris Masonb9473432009-03-13 11:00:37 -0400140 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400141
Li Zefan33345d012011-04-20 10:31:50 +0800142 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400143 key.offset = start;
144 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400145 datasize = btrfs_file_extent_calc_inline_size(cur_size);
146
147 inode_add_bytes(inode, size);
148 ret = btrfs_insert_empty_item(trans, root, path, &key,
149 datasize);
150 BUG_ON(ret);
151 if (ret) {
152 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400153 goto fail;
154 }
155 leaf = path->nodes[0];
156 ei = btrfs_item_ptr(leaf, path->slots[0],
157 struct btrfs_file_extent_item);
158 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
159 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
160 btrfs_set_file_extent_encryption(leaf, ei, 0);
161 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
162 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
163 ptr = btrfs_file_extent_inline_start(ei);
164
Li Zefan261507a02010-12-17 14:21:50 +0800165 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400166 struct page *cpage;
167 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500168 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400169 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500170 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400171 PAGE_CACHE_SIZE);
172
Chris Masonb9473432009-03-13 11:00:37 -0400173 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400174 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400175 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400176
177 i++;
178 ptr += cur_size;
179 compressed_size -= cur_size;
180 }
181 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800182 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400183 } else {
184 page = find_get_page(inode->i_mapping,
185 start >> PAGE_CACHE_SHIFT);
186 btrfs_set_file_extent_compression(leaf, ei, 0);
187 kaddr = kmap_atomic(page, KM_USER0);
188 offset = start & (PAGE_CACHE_SIZE - 1);
189 write_extent_buffer(leaf, kaddr + offset, ptr, size);
190 kunmap_atomic(kaddr, KM_USER0);
191 page_cache_release(page);
192 }
193 btrfs_mark_buffer_dirty(leaf);
194 btrfs_free_path(path);
195
Yan, Zhengc2167752009-11-12 09:34:21 +0000196 /*
197 * we're an inline extent, so nobody can
198 * extend the file past i_size without locking
199 * a page we already have locked.
200 *
201 * We must do any isize and inode updates
202 * before we unlock the pages. Otherwise we
203 * could end up racing with unlink.
204 */
Chris Masonc8b97812008-10-29 14:49:59 -0400205 BTRFS_I(inode)->disk_i_size = inode->i_size;
206 btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000207
Chris Masonc8b97812008-10-29 14:49:59 -0400208 return 0;
209fail:
210 btrfs_free_path(path);
211 return err;
212}
213
214
215/*
216 * conditionally insert an inline extent into the file. This
217 * does the checks required to make sure the data is small enough
218 * to fit as an inline extent.
219 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400220static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400221 struct btrfs_root *root,
222 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000223 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400224 struct page **compressed_pages)
225{
226 u64 isize = i_size_read(inode);
227 u64 actual_end = min(end + 1, isize);
228 u64 inline_len = actual_end - start;
229 u64 aligned_end = (end + root->sectorsize - 1) &
230 ~((u64)root->sectorsize - 1);
231 u64 hint_byte;
232 u64 data_len = inline_len;
233 int ret;
234
235 if (compressed_size)
236 data_len = compressed_size;
237
238 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400239 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400240 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
241 (!compressed_size &&
242 (actual_end & (root->sectorsize - 1)) == 0) ||
243 end + 1 < isize ||
244 data_len > root->fs_info->max_inline) {
245 return 1;
246 }
247
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000248 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400249 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 BUG_ON(ret);
251
252 if (isize > actual_end)
253 inline_len = min_t(u64, isize, actual_end);
254 ret = insert_inline_extent(trans, root, inode, start,
255 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000256 compress_type, compressed_pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400257 BUG_ON(ret);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400258 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400259 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400260 return 0;
261}
262
Chris Mason771ed682008-11-06 22:02:51 -0500263struct async_extent {
264 u64 start;
265 u64 ram_size;
266 u64 compressed_size;
267 struct page **pages;
268 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800269 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500270 struct list_head list;
271};
272
273struct async_cow {
274 struct inode *inode;
275 struct btrfs_root *root;
276 struct page *locked_page;
277 u64 start;
278 u64 end;
279 struct list_head extents;
280 struct btrfs_work work;
281};
282
283static noinline int add_async_extent(struct async_cow *cow,
284 u64 start, u64 ram_size,
285 u64 compressed_size,
286 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800287 unsigned long nr_pages,
288 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500289{
290 struct async_extent *async_extent;
291
292 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Yoshinori Sanodac97e52011-02-15 12:01:42 +0000293 BUG_ON(!async_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500294 async_extent->start = start;
295 async_extent->ram_size = ram_size;
296 async_extent->compressed_size = compressed_size;
297 async_extent->pages = pages;
298 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800299 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500300 list_add_tail(&async_extent->list, &cow->extents);
301 return 0;
302}
303
Chris Masonc8b97812008-10-29 14:49:59 -0400304/*
Chris Mason771ed682008-11-06 22:02:51 -0500305 * we create compressed extents in two phases. The first
306 * phase compresses a range of pages that have already been
307 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400308 *
Chris Mason771ed682008-11-06 22:02:51 -0500309 * This is done inside an ordered work queue, and the compression
310 * is spread across many cpus. The actual IO submission is step
311 * two, and the ordered work queue takes care of making sure that
312 * happens in the same order things were put onto the queue by
313 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400314 *
Chris Mason771ed682008-11-06 22:02:51 -0500315 * If this code finds it can't get good compression, it puts an
316 * entry onto the work queue to write the uncompressed bytes. This
317 * makes sure that both compressed inodes and uncompressed inodes
318 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400319 */
Chris Mason771ed682008-11-06 22:02:51 -0500320static noinline int compress_file_range(struct inode *inode,
321 struct page *locked_page,
322 u64 start, u64 end,
323 struct async_cow *async_cow,
324 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400325{
326 struct btrfs_root *root = BTRFS_I(inode)->root;
327 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400328 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400329 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400330 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500331 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400332 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400333 struct page **pages = NULL;
334 unsigned long nr_pages;
335 unsigned long nr_pages_ret = 0;
336 unsigned long total_compressed = 0;
337 unsigned long total_in = 0;
338 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500339 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400340 int i;
341 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800342 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400343
Chris Mason4cb53002011-05-24 15:35:30 -0400344 /* if this is a small write inside eof, kick off a defragbot */
345 if (end <= BTRFS_I(inode)->disk_i_size && (end - start + 1) < 16 * 1024)
346 btrfs_add_inode_defrag(NULL, inode);
347
Chris Mason42dc7ba2008-12-15 11:44:56 -0500348 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400349again:
350 will_compress = 0;
351 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
352 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
353
Chris Masonf03d9302009-02-04 09:31:06 -0500354 /*
355 * we don't want to send crud past the end of i_size through
356 * compression, that's just a waste of CPU time. So, if the
357 * end of the file is before the start of our current
358 * requested range of bytes, we bail out to the uncompressed
359 * cleanup code that can deal with all of this.
360 *
361 * It isn't really the fastest way to fix things, but this is a
362 * very uncommon corner.
363 */
364 if (actual_end <= start)
365 goto cleanup_and_bail_uncompressed;
366
Chris Masonc8b97812008-10-29 14:49:59 -0400367 total_compressed = actual_end - start;
368
369 /* we want to make sure that amount of ram required to uncompress
370 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500371 * of a compressed extent to 128k. This is a crucial number
372 * because it also controls how easily we can spread reads across
373 * cpus for decompression.
374 *
375 * We also want to make sure the amount of IO required to do
376 * a random read is reasonably small, so we limit the size of
377 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400378 */
379 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400380 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500381 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400382 total_in = 0;
383 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400384
Chris Mason771ed682008-11-06 22:02:51 -0500385 /*
386 * we do compression for mount -o compress and when the
387 * inode has not been flagged as nocompress. This flag can
388 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400389 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200390 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500391 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000392 (BTRFS_I(inode)->force_compress) ||
393 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400394 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400395 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800396 if (!pages) {
397 /* just bail out to the uncompressed code */
398 goto cont;
399 }
Chris Mason179e29e2007-11-01 11:28:41 -0400400
Li Zefan261507a02010-12-17 14:21:50 +0800401 if (BTRFS_I(inode)->force_compress)
402 compress_type = BTRFS_I(inode)->force_compress;
403
404 ret = btrfs_compress_pages(compress_type,
405 inode->i_mapping, start,
406 total_compressed, pages,
407 nr_pages, &nr_pages_ret,
408 &total_in,
409 &total_compressed,
410 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400411
412 if (!ret) {
413 unsigned long offset = total_compressed &
414 (PAGE_CACHE_SIZE - 1);
415 struct page *page = pages[nr_pages_ret - 1];
416 char *kaddr;
417
418 /* zero the tail end of the last page, we might be
419 * sending it down to disk
420 */
421 if (offset) {
422 kaddr = kmap_atomic(page, KM_USER0);
423 memset(kaddr + offset, 0,
424 PAGE_CACHE_SIZE - offset);
425 kunmap_atomic(kaddr, KM_USER0);
426 }
427 will_compress = 1;
428 }
429 }
Li Zefan560f7d72011-09-08 10:22:01 +0800430cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400431 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400432 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000433 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400434 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500435
Chris Masonc8b97812008-10-29 14:49:59 -0400436 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500437 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400438 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500439 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400440 */
441 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000442 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400443 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500444 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400445 ret = cow_file_range_inline(trans, root, inode,
446 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000447 total_compressed,
448 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400449 }
450 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500451 /*
452 * inline extent creation worked, we don't need
453 * to create any more async work items. Unlock
454 * and free up our temp pages.
455 */
Chris Masonc8b97812008-10-29 14:49:59 -0400456 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400457 &BTRFS_I(inode)->io_tree,
458 start, end, NULL,
459 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400460 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400461 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000462
463 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400464 goto free_pages_out;
465 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000466 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400467 }
468
469 if (will_compress) {
470 /*
471 * we aren't doing an inline extent round the compressed size
472 * up to a block size boundary so the allocator does sane
473 * things
474 */
475 total_compressed = (total_compressed + blocksize - 1) &
476 ~(blocksize - 1);
477
478 /*
479 * one last check to make sure the compression is really a
480 * win, compare the page count read with the blocks on disk
481 */
482 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
483 ~(PAGE_CACHE_SIZE - 1);
484 if (total_compressed >= total_in) {
485 will_compress = 0;
486 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400487 num_bytes = total_in;
488 }
489 }
490 if (!will_compress && pages) {
491 /*
492 * the compression code ran but failed to make things smaller,
493 * free any pages it allocated and our page pointer array
494 */
495 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400496 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400497 page_cache_release(pages[i]);
498 }
499 kfree(pages);
500 pages = NULL;
501 total_compressed = 0;
502 nr_pages_ret = 0;
503
504 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500505 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
506 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500507 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500508 }
Chris Masonc8b97812008-10-29 14:49:59 -0400509 }
Chris Mason771ed682008-11-06 22:02:51 -0500510 if (will_compress) {
511 *num_added += 1;
512
513 /* the async work queues will take care of doing actual
514 * allocation on disk for these compressed pages,
515 * and will submit them to the elevator.
516 */
517 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800518 total_compressed, pages, nr_pages_ret,
519 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500520
Yan, Zheng24ae6362010-12-06 07:02:36 +0000521 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500522 start += num_bytes;
523 pages = NULL;
524 cond_resched();
525 goto again;
526 }
527 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500528cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500529 /*
530 * No compression, but we still need to write the pages in
531 * the file we've been given so far. redirty the locked
532 * page if it corresponds to our extent and set things up
533 * for the async work queue to run cow_file_range to do
534 * the normal delalloc dance
535 */
536 if (page_offset(locked_page) >= start &&
537 page_offset(locked_page) <= end) {
538 __set_page_dirty_nobuffers(locked_page);
539 /* unlocked later on in the async handlers */
540 }
Li Zefan261507a02010-12-17 14:21:50 +0800541 add_async_extent(async_cow, start, end - start + 1,
542 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500543 *num_added += 1;
544 }
545
546out:
547 return 0;
548
549free_pages_out:
550 for (i = 0; i < nr_pages_ret; i++) {
551 WARN_ON(pages[i]->mapping);
552 page_cache_release(pages[i]);
553 }
Chris Masond3977122009-01-05 21:25:51 -0500554 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500555
556 goto out;
557}
558
559/*
560 * phase two of compressed writeback. This is the ordered portion
561 * of the code, which only gets called in the order the work was
562 * queued. We walk all the async extents created by compress_file_range
563 * and send them down to the disk.
564 */
565static noinline int submit_compressed_extents(struct inode *inode,
566 struct async_cow *async_cow)
567{
568 struct async_extent *async_extent;
569 u64 alloc_hint = 0;
570 struct btrfs_trans_handle *trans;
571 struct btrfs_key ins;
572 struct extent_map *em;
573 struct btrfs_root *root = BTRFS_I(inode)->root;
574 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
575 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500576 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500577
578 if (list_empty(&async_cow->extents))
579 return 0;
580
Chris Mason771ed682008-11-06 22:02:51 -0500581
Chris Masond3977122009-01-05 21:25:51 -0500582 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500583 async_extent = list_entry(async_cow->extents.next,
584 struct async_extent, list);
585 list_del(&async_extent->list);
586
587 io_tree = &BTRFS_I(inode)->io_tree;
588
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500589retry:
Chris Mason771ed682008-11-06 22:02:51 -0500590 /* did the compression code fall back to uncompressed IO? */
591 if (!async_extent->pages) {
592 int page_started = 0;
593 unsigned long nr_written = 0;
594
595 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000596 async_extent->start +
597 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500598
599 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500600 ret = cow_file_range(inode, async_cow->locked_page,
601 async_extent->start,
602 async_extent->start +
603 async_extent->ram_size - 1,
604 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500605
606 /*
607 * if page_started, cow_file_range inserted an
608 * inline extent and took care of all the unlocking
609 * and IO for us. Otherwise, we need to submit
610 * all those pages down to the drive.
611 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500612 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500613 extent_write_locked_range(io_tree,
614 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500615 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500616 async_extent->ram_size - 1,
617 btrfs_get_extent,
618 WB_SYNC_ALL);
619 kfree(async_extent);
620 cond_resched();
621 continue;
622 }
623
624 lock_extent(io_tree, async_extent->start,
625 async_extent->start + async_extent->ram_size - 1,
626 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500627
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400628 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000629 BUG_ON(IS_ERR(trans));
Josef Bacik74b21072011-04-13 12:02:53 -0400630 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500631 ret = btrfs_reserve_extent(trans, root,
632 async_extent->compressed_size,
633 async_extent->compressed_size,
634 0, alloc_hint,
635 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000636 btrfs_end_transaction(trans, root);
637
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500638 if (ret) {
639 int i;
640 for (i = 0; i < async_extent->nr_pages; i++) {
641 WARN_ON(async_extent->pages[i]->mapping);
642 page_cache_release(async_extent->pages[i]);
643 }
644 kfree(async_extent->pages);
645 async_extent->nr_pages = 0;
646 async_extent->pages = NULL;
647 unlock_extent(io_tree, async_extent->start,
648 async_extent->start +
649 async_extent->ram_size - 1, GFP_NOFS);
650 goto retry;
651 }
652
Yan, Zhengc2167752009-11-12 09:34:21 +0000653 /*
654 * here we're doing allocation and writeback of the
655 * compressed pages
656 */
657 btrfs_drop_extent_cache(inode, async_extent->start,
658 async_extent->start +
659 async_extent->ram_size - 1, 0);
660
David Sterba172ddd62011-04-21 00:48:27 +0200661 em = alloc_extent_map();
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000662 BUG_ON(!em);
Chris Mason771ed682008-11-06 22:02:51 -0500663 em->start = async_extent->start;
664 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500665 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500666
667 em->block_start = ins.objectid;
668 em->block_len = ins.offset;
669 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800670 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500671 set_bit(EXTENT_FLAG_PINNED, &em->flags);
672 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
673
Chris Masond3977122009-01-05 21:25:51 -0500674 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400675 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500676 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400677 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500678 if (ret != -EEXIST) {
679 free_extent_map(em);
680 break;
681 }
682 btrfs_drop_extent_cache(inode, async_extent->start,
683 async_extent->start +
684 async_extent->ram_size - 1, 0);
685 }
686
Li Zefan261507a02010-12-17 14:21:50 +0800687 ret = btrfs_add_ordered_extent_compress(inode,
688 async_extent->start,
689 ins.objectid,
690 async_extent->ram_size,
691 ins.offset,
692 BTRFS_ORDERED_COMPRESSED,
693 async_extent->compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500694 BUG_ON(ret);
695
Chris Mason771ed682008-11-06 22:02:51 -0500696 /*
697 * clear dirty, set writeback and unlock the pages.
698 */
699 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400700 &BTRFS_I(inode)->io_tree,
701 async_extent->start,
702 async_extent->start +
703 async_extent->ram_size - 1,
704 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
705 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400706 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400707 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500708
709 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500710 async_extent->start,
711 async_extent->ram_size,
712 ins.objectid,
713 ins.offset, async_extent->pages,
714 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500715
716 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500717 alloc_hint = ins.objectid + ins.offset;
718 kfree(async_extent);
719 cond_resched();
720 }
721
Chris Mason771ed682008-11-06 22:02:51 -0500722 return 0;
723}
724
Josef Bacik4b46fce2010-05-23 11:00:55 -0400725static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
726 u64 num_bytes)
727{
728 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
729 struct extent_map *em;
730 u64 alloc_hint = 0;
731
732 read_lock(&em_tree->lock);
733 em = search_extent_mapping(em_tree, start, num_bytes);
734 if (em) {
735 /*
736 * if block start isn't an actual block number then find the
737 * first block in this inode and use that as a hint. If that
738 * block is also bogus then just don't worry about it.
739 */
740 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
741 free_extent_map(em);
742 em = search_extent_mapping(em_tree, 0, 0);
743 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
744 alloc_hint = em->block_start;
745 if (em)
746 free_extent_map(em);
747 } else {
748 alloc_hint = em->block_start;
749 free_extent_map(em);
750 }
751 }
752 read_unlock(&em_tree->lock);
753
754 return alloc_hint;
755}
756
Chris Mason771ed682008-11-06 22:02:51 -0500757/*
758 * when extent_io.c finds a delayed allocation range in the file,
759 * the call backs end up in this code. The basic idea is to
760 * allocate extents on disk for the range, and create ordered data structs
761 * in ram to track those extents.
762 *
763 * locked_page is the page that writepage had locked already. We use
764 * it to make sure we don't do extra locks or unlocks.
765 *
766 * *page_started is set to one if we unlock locked_page and do everything
767 * required to start IO on it. It may be clean and already done with
768 * IO when we return.
769 */
770static noinline int cow_file_range(struct inode *inode,
771 struct page *locked_page,
772 u64 start, u64 end, int *page_started,
773 unsigned long *nr_written,
774 int unlock)
775{
776 struct btrfs_root *root = BTRFS_I(inode)->root;
777 struct btrfs_trans_handle *trans;
778 u64 alloc_hint = 0;
779 u64 num_bytes;
780 unsigned long ram_size;
781 u64 disk_num_bytes;
782 u64 cur_alloc_size;
783 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500784 struct btrfs_key ins;
785 struct extent_map *em;
786 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
787 int ret = 0;
788
Chris Mason2cf85722011-07-26 15:35:09 -0400789 BUG_ON(btrfs_is_free_space_inode(root, inode));
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400790 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000791 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400792 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500793
Chris Mason771ed682008-11-06 22:02:51 -0500794 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
795 num_bytes = max(blocksize, num_bytes);
796 disk_num_bytes = num_bytes;
797 ret = 0;
798
Chris Mason4cb53002011-05-24 15:35:30 -0400799 /* if this is a small write inside eof, kick off defrag */
800 if (end <= BTRFS_I(inode)->disk_i_size && num_bytes < 64 * 1024)
801 btrfs_add_inode_defrag(trans, inode);
802
Chris Mason771ed682008-11-06 22:02:51 -0500803 if (start == 0) {
804 /* lets try to make an inline extent */
805 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000806 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500807 if (ret == 0) {
808 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400809 &BTRFS_I(inode)->io_tree,
810 start, end, NULL,
811 EXTENT_CLEAR_UNLOCK_PAGE |
812 EXTENT_CLEAR_UNLOCK |
813 EXTENT_CLEAR_DELALLOC |
814 EXTENT_CLEAR_DIRTY |
815 EXTENT_SET_WRITEBACK |
816 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000817
Chris Mason771ed682008-11-06 22:02:51 -0500818 *nr_written = *nr_written +
819 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
820 *page_started = 1;
821 ret = 0;
822 goto out;
823 }
824 }
Chris Masonc8b97812008-10-29 14:49:59 -0400825
826 BUG_ON(disk_num_bytes >
827 btrfs_super_total_bytes(&root->fs_info->super_copy));
828
Josef Bacik4b46fce2010-05-23 11:00:55 -0400829 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400830 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400831
Chris Masond3977122009-01-05 21:25:51 -0500832 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400833 unsigned long op;
834
Josef Bacik287a0ab2010-03-19 18:07:23 +0000835 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400836 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500837 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400838 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500839 BUG_ON(ret);
840
David Sterba172ddd62011-04-21 00:48:27 +0200841 em = alloc_extent_map();
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000842 BUG_ON(!em);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400843 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500844 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500845 ram_size = ins.offset;
846 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400847
Chris Masone6dcd2d2008-07-17 12:53:50 -0400848 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400849 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400850 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400851 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400852
Chris Masond3977122009-01-05 21:25:51 -0500853 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400854 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400855 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400856 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400857 if (ret != -EEXIST) {
858 free_extent_map(em);
859 break;
860 }
861 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400862 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400863 }
864
Chris Mason98d20f62008-04-14 09:46:10 -0400865 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400866 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500867 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400868 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400869
Yan Zheng17d217f2008-12-12 10:03:38 -0500870 if (root->root_key.objectid ==
871 BTRFS_DATA_RELOC_TREE_OBJECTID) {
872 ret = btrfs_reloc_clone_csums(inode, start,
873 cur_alloc_size);
874 BUG_ON(ret);
875 }
876
Chris Masond3977122009-01-05 21:25:51 -0500877 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400878 break;
Chris Masond3977122009-01-05 21:25:51 -0500879
Chris Masonc8b97812008-10-29 14:49:59 -0400880 /* we're not doing compressed IO, don't unlock the first
881 * page (which the caller expects to stay locked), don't
882 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400883 *
884 * Do set the Private2 bit so we know this page was properly
885 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400886 */
Chris Masona791e352009-10-08 11:27:10 -0400887 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
888 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
889 EXTENT_SET_PRIVATE2;
890
Chris Masonc8b97812008-10-29 14:49:59 -0400891 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
892 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400893 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400894 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500895 num_bytes -= cur_alloc_size;
896 alloc_hint = ins.objectid + ins.offset;
897 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400898 }
Chris Masonb888db22007-08-27 16:49:44 -0400899out:
Chris Mason771ed682008-11-06 22:02:51 -0500900 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400901 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400902
Chris Masonbe20aa92007-12-17 20:14:01 -0500903 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500904}
Chris Masonc8b97812008-10-29 14:49:59 -0400905
Chris Mason771ed682008-11-06 22:02:51 -0500906/*
907 * work queue call back to started compression on a file and pages
908 */
909static noinline void async_cow_start(struct btrfs_work *work)
910{
911 struct async_cow *async_cow;
912 int num_added = 0;
913 async_cow = container_of(work, struct async_cow, work);
914
915 compress_file_range(async_cow->inode, async_cow->locked_page,
916 async_cow->start, async_cow->end, async_cow,
917 &num_added);
918 if (num_added == 0)
919 async_cow->inode = NULL;
920}
921
922/*
923 * work queue call back to submit previously compressed pages
924 */
925static noinline void async_cow_submit(struct btrfs_work *work)
926{
927 struct async_cow *async_cow;
928 struct btrfs_root *root;
929 unsigned long nr_pages;
930
931 async_cow = container_of(work, struct async_cow, work);
932
933 root = async_cow->root;
934 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
935 PAGE_CACHE_SHIFT;
936
937 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
938
939 if (atomic_read(&root->fs_info->async_delalloc_pages) <
940 5 * 1042 * 1024 &&
941 waitqueue_active(&root->fs_info->async_submit_wait))
942 wake_up(&root->fs_info->async_submit_wait);
943
Chris Masond3977122009-01-05 21:25:51 -0500944 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500945 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500946}
Chris Masonc8b97812008-10-29 14:49:59 -0400947
Chris Mason771ed682008-11-06 22:02:51 -0500948static noinline void async_cow_free(struct btrfs_work *work)
949{
950 struct async_cow *async_cow;
951 async_cow = container_of(work, struct async_cow, work);
952 kfree(async_cow);
953}
954
955static int cow_file_range_async(struct inode *inode, struct page *locked_page,
956 u64 start, u64 end, int *page_started,
957 unsigned long *nr_written)
958{
959 struct async_cow *async_cow;
960 struct btrfs_root *root = BTRFS_I(inode)->root;
961 unsigned long nr_pages;
962 u64 cur_end;
963 int limit = 10 * 1024 * 1042;
964
Chris Masona3429ab2009-10-08 12:30:20 -0400965 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
966 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500967 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500968 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -0400969 BUG_ON(!async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500970 async_cow->inode = inode;
971 async_cow->root = root;
972 async_cow->locked_page = locked_page;
973 async_cow->start = start;
974
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200975 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500976 cur_end = end;
977 else
978 cur_end = min(end, start + 512 * 1024 - 1);
979
980 async_cow->end = cur_end;
981 INIT_LIST_HEAD(&async_cow->extents);
982
983 async_cow->work.func = async_cow_start;
984 async_cow->work.ordered_func = async_cow_submit;
985 async_cow->work.ordered_free = async_cow_free;
986 async_cow->work.flags = 0;
987
Chris Mason771ed682008-11-06 22:02:51 -0500988 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
989 PAGE_CACHE_SHIFT;
990 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
991
992 btrfs_queue_worker(&root->fs_info->delalloc_workers,
993 &async_cow->work);
994
995 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
996 wait_event(root->fs_info->async_submit_wait,
997 (atomic_read(&root->fs_info->async_delalloc_pages) <
998 limit));
999 }
1000
Chris Masond3977122009-01-05 21:25:51 -05001001 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001002 atomic_read(&root->fs_info->async_delalloc_pages)) {
1003 wait_event(root->fs_info->async_submit_wait,
1004 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1005 0));
1006 }
1007
1008 *nr_written += nr_pages;
1009 start = cur_end + 1;
1010 }
1011 *page_started = 1;
1012 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001013}
1014
Chris Masond3977122009-01-05 21:25:51 -05001015static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001016 u64 bytenr, u64 num_bytes)
1017{
1018 int ret;
1019 struct btrfs_ordered_sum *sums;
1020 LIST_HEAD(list);
1021
Yan Zheng07d400a2009-01-06 11:42:00 -05001022 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001023 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001024 if (ret == 0 && list_empty(&list))
1025 return 0;
1026
1027 while (!list_empty(&list)) {
1028 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1029 list_del(&sums->list);
1030 kfree(sums);
1031 }
1032 return 1;
1033}
1034
Chris Masond352ac62008-09-29 15:18:18 -04001035/*
1036 * when nowcow writeback call back. This checks for snapshots or COW copies
1037 * of the extents that exist in the file, and COWs the file as required.
1038 *
1039 * If no cow copies or snapshots exist, we write directly to the existing
1040 * blocks on disk
1041 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001042static noinline int run_delalloc_nocow(struct inode *inode,
1043 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001044 u64 start, u64 end, int *page_started, int force,
1045 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001046{
Chris Masonbe20aa92007-12-17 20:14:01 -05001047 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001048 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001049 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001050 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001051 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001052 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001053 u64 cow_start;
1054 u64 cur_offset;
1055 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001056 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001057 u64 disk_bytenr;
1058 u64 num_bytes;
1059 int extent_type;
1060 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001061 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001062 int nocow;
1063 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001064 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001065 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001066
1067 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001068 if (!path)
1069 return -ENOMEM;
Li Zefan82d59022011-04-20 10:33:24 +08001070
Chris Mason2cf85722011-07-26 15:35:09 -04001071 nolock = btrfs_is_free_space_inode(root, inode);
Li Zefan82d59022011-04-20 10:33:24 +08001072
1073 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001074 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001075 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001076 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001077
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001078 BUG_ON(IS_ERR(trans));
Josef Bacik74b21072011-04-13 12:02:53 -04001079 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001080
Yan Zheng80ff3852008-10-30 14:20:02 -04001081 cow_start = (u64)-1;
1082 cur_offset = start;
1083 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001084 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001085 cur_offset, 0);
1086 BUG_ON(ret < 0);
1087 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1088 leaf = path->nodes[0];
1089 btrfs_item_key_to_cpu(leaf, &found_key,
1090 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001091 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001092 found_key.type == BTRFS_EXTENT_DATA_KEY)
1093 path->slots[0]--;
1094 }
1095 check_prev = 0;
1096next_slot:
1097 leaf = path->nodes[0];
1098 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1099 ret = btrfs_next_leaf(root, path);
1100 if (ret < 0)
1101 BUG_ON(1);
1102 if (ret > 0)
1103 break;
1104 leaf = path->nodes[0];
1105 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001106
Yan Zheng80ff3852008-10-30 14:20:02 -04001107 nocow = 0;
1108 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001109 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001110 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001111
Li Zefan33345d012011-04-20 10:31:50 +08001112 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001113 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1114 found_key.offset > end)
1115 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001116
Yan Zheng80ff3852008-10-30 14:20:02 -04001117 if (found_key.offset > cur_offset) {
1118 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001119 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001120 goto out_check;
1121 }
Chris Masonc31f8832008-01-08 15:46:31 -05001122
Yan Zheng80ff3852008-10-30 14:20:02 -04001123 fi = btrfs_item_ptr(leaf, path->slots[0],
1124 struct btrfs_file_extent_item);
1125 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001126
Yan Zhengd899e052008-10-30 14:25:28 -04001127 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1128 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001129 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001130 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001131 extent_end = found_key.offset +
1132 btrfs_file_extent_num_bytes(leaf, fi);
1133 if (extent_end <= start) {
1134 path->slots[0]++;
1135 goto next_slot;
1136 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001137 if (disk_bytenr == 0)
1138 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001139 if (btrfs_file_extent_compression(leaf, fi) ||
1140 btrfs_file_extent_encryption(leaf, fi) ||
1141 btrfs_file_extent_other_encoding(leaf, fi))
1142 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001143 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1144 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001145 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001146 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001147 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001148 found_key.offset -
1149 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001150 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001151 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001152 disk_bytenr += cur_offset - found_key.offset;
1153 num_bytes = min(end + 1, extent_end) - cur_offset;
1154 /*
1155 * force cow if csum exists in the range.
1156 * this ensure that csum for a given extent are
1157 * either valid or do not exist.
1158 */
1159 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1160 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001161 nocow = 1;
1162 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1163 extent_end = found_key.offset +
1164 btrfs_file_extent_inline_len(leaf, fi);
1165 extent_end = ALIGN(extent_end, root->sectorsize);
1166 } else {
1167 BUG_ON(1);
1168 }
1169out_check:
1170 if (extent_end <= start) {
1171 path->slots[0]++;
1172 goto next_slot;
1173 }
1174 if (!nocow) {
1175 if (cow_start == (u64)-1)
1176 cow_start = cur_offset;
1177 cur_offset = extent_end;
1178 if (cur_offset > end)
1179 break;
1180 path->slots[0]++;
1181 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001182 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001183
David Sterbab3b4aa72011-04-21 01:20:15 +02001184 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001185 if (cow_start != (u64)-1) {
1186 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001187 found_key.offset - 1, page_started,
1188 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001189 BUG_ON(ret);
1190 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001191 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001192
Yan Zhengd899e052008-10-30 14:25:28 -04001193 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1194 struct extent_map *em;
1195 struct extent_map_tree *em_tree;
1196 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001197 em = alloc_extent_map();
Tsutomu Itohc26a9202011-02-14 00:45:29 +00001198 BUG_ON(!em);
Yan Zhengd899e052008-10-30 14:25:28 -04001199 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001200 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001201 em->len = num_bytes;
1202 em->block_len = num_bytes;
1203 em->block_start = disk_bytenr;
1204 em->bdev = root->fs_info->fs_devices->latest_bdev;
1205 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1206 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001207 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001208 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001209 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001210 if (ret != -EEXIST) {
1211 free_extent_map(em);
1212 break;
1213 }
1214 btrfs_drop_extent_cache(inode, em->start,
1215 em->start + em->len - 1, 0);
1216 }
1217 type = BTRFS_ORDERED_PREALLOC;
1218 } else {
1219 type = BTRFS_ORDERED_NOCOW;
1220 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001221
1222 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001223 num_bytes, num_bytes, type);
1224 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001225
Yan, Zhengefa56462010-05-16 10:49:59 -04001226 if (root->root_key.objectid ==
1227 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1228 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1229 num_bytes);
1230 BUG_ON(ret);
1231 }
1232
Yan Zhengd899e052008-10-30 14:25:28 -04001233 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001234 cur_offset, cur_offset + num_bytes - 1,
1235 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1236 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1237 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 cur_offset = extent_end;
1239 if (cur_offset > end)
1240 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001241 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001242 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001243
1244 if (cur_offset <= end && cow_start == (u64)-1)
1245 cow_start = cur_offset;
1246 if (cow_start != (u64)-1) {
1247 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001248 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001249 BUG_ON(ret);
1250 }
1251
Josef Bacik0cb59c92010-07-02 12:14:14 -04001252 if (nolock) {
1253 ret = btrfs_end_transaction_nolock(trans, root);
1254 BUG_ON(ret);
1255 } else {
1256 ret = btrfs_end_transaction(trans, root);
1257 BUG_ON(ret);
1258 }
Yan Zheng7ea394f2008-08-05 13:05:02 -04001259 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001260 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001261}
1262
Chris Masond352ac62008-09-29 15:18:18 -04001263/*
1264 * extent_io.c call back to do delayed allocation processing
1265 */
Chris Masonc8b97812008-10-29 14:49:59 -04001266static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001267 u64 start, u64 end, int *page_started,
1268 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001269{
Chris Masonbe20aa92007-12-17 20:14:01 -05001270 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001271 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001272
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001273 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001274 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001275 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001276 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001277 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001278 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001279 else if (!btrfs_test_opt(root, COMPRESS) &&
Liu Bo75e7cb72011-03-22 10:12:20 +00001280 !(BTRFS_I(inode)->force_compress) &&
1281 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
Chris Mason7f366cf2009-03-12 20:12:45 -04001282 ret = cow_file_range(inode, locked_page, start, end,
1283 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001284 else
Chris Mason771ed682008-11-06 22:02:51 -05001285 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001286 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001287 return ret;
1288}
1289
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001290static void btrfs_split_extent_hook(struct inode *inode,
1291 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001292{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001293 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001294 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001295 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001296
Josef Bacik9e0baf62011-07-15 15:16:44 +00001297 spin_lock(&BTRFS_I(inode)->lock);
1298 BTRFS_I(inode)->outstanding_extents++;
1299 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001300}
1301
1302/*
1303 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1304 * extents so we can keep track of new extents that are just merged onto old
1305 * extents, such as when we are doing sequential writes, so we can properly
1306 * account for the metadata space we'll need.
1307 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001308static void btrfs_merge_extent_hook(struct inode *inode,
1309 struct extent_state *new,
1310 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001311{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001312 /* not delalloc, ignore it */
1313 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001314 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001315
Josef Bacik9e0baf62011-07-15 15:16:44 +00001316 spin_lock(&BTRFS_I(inode)->lock);
1317 BTRFS_I(inode)->outstanding_extents--;
1318 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001319}
1320
Chris Masond352ac62008-09-29 15:18:18 -04001321/*
1322 * extent_io.c set_bit_hook, used to track delayed allocation
1323 * bytes in this file, and to maintain the list of inodes that
1324 * have pending delalloc work to be done.
1325 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001326static void btrfs_set_bit_hook(struct inode *inode,
1327 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001328{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001329
Chris Mason75eff682008-12-15 15:54:40 -05001330 /*
1331 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001332 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001333 * bit, which is only set or cleared with irqs on
1334 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001335 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001336 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001337 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001338 bool do_list = !btrfs_is_free_space_inode(root, inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001339
Josef Bacik9e0baf62011-07-15 15:16:44 +00001340 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001341 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001342 } else {
1343 spin_lock(&BTRFS_I(inode)->lock);
1344 BTRFS_I(inode)->outstanding_extents++;
1345 spin_unlock(&BTRFS_I(inode)->lock);
1346 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001347
Chris Mason75eff682008-12-15 15:54:40 -05001348 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001349 BTRFS_I(inode)->delalloc_bytes += len;
1350 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001351 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001352 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1353 &root->fs_info->delalloc_inodes);
1354 }
Chris Mason75eff682008-12-15 15:54:40 -05001355 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001356 }
Chris Mason291d6732008-01-29 15:55:23 -05001357}
1358
Chris Masond352ac62008-09-29 15:18:18 -04001359/*
1360 * extent_io.c clear_bit_hook, see set_bit_hook for why
1361 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001362static void btrfs_clear_bit_hook(struct inode *inode,
1363 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001364{
Chris Mason75eff682008-12-15 15:54:40 -05001365 /*
1366 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001367 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001368 * bit, which is only set or cleared with irqs on
1369 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001370 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001371 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001372 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001373 bool do_list = !btrfs_is_free_space_inode(root, inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001374
Josef Bacik9e0baf62011-07-15 15:16:44 +00001375 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001376 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001377 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1378 spin_lock(&BTRFS_I(inode)->lock);
1379 BTRFS_I(inode)->outstanding_extents--;
1380 spin_unlock(&BTRFS_I(inode)->lock);
1381 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001382
1383 if (*bits & EXTENT_DO_ACCOUNTING)
1384 btrfs_delalloc_release_metadata(inode, len);
1385
Josef Bacik0cb59c92010-07-02 12:14:14 -04001386 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1387 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001388 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001389
Chris Mason75eff682008-12-15 15:54:40 -05001390 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001391 root->fs_info->delalloc_bytes -= len;
1392 BTRFS_I(inode)->delalloc_bytes -= len;
1393
Josef Bacik0cb59c92010-07-02 12:14:14 -04001394 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001395 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1396 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1397 }
Chris Mason75eff682008-12-15 15:54:40 -05001398 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001399 }
Chris Mason291d6732008-01-29 15:55:23 -05001400}
1401
Chris Masond352ac62008-09-29 15:18:18 -04001402/*
1403 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1404 * we don't create bios that span stripes or chunks
1405 */
Chris Mason239b14b2008-03-24 15:02:07 -04001406int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001407 size_t size, struct bio *bio,
1408 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001409{
1410 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1411 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001412 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001413 u64 length = 0;
1414 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001415 int ret;
1416
Chris Mason771ed682008-11-06 22:02:51 -05001417 if (bio_flags & EXTENT_BIO_COMPRESSED)
1418 return 0;
1419
Chris Masonf2d8d742008-04-21 10:03:05 -04001420 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001421 map_tree = &root->fs_info->mapping_tree;
1422 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001423 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001424 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001425
Chris Masond3977122009-01-05 21:25:51 -05001426 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001427 return 1;
Andi Kleen411fc6b2010-10-29 15:14:31 -04001428 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04001429}
1430
Chris Masond352ac62008-09-29 15:18:18 -04001431/*
1432 * in order to insert checksums into the metadata in large chunks,
1433 * we wait until bio submission time. All the pages in the bio are
1434 * checksummed and sums are attached onto the ordered extent record.
1435 *
1436 * At IO completion time the cums attached on the ordered extent record
1437 * are inserted into the btree
1438 */
Chris Masond3977122009-01-05 21:25:51 -05001439static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1440 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001441 unsigned long bio_flags,
1442 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001443{
Chris Mason065631f2008-02-20 12:07:25 -05001444 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001445 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001446
Chris Masond20f7042008-12-08 16:58:54 -05001447 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001448 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001449 return 0;
1450}
Chris Masone0156402008-04-16 11:15:20 -04001451
Chris Mason4a69a412008-11-06 22:03:00 -05001452/*
1453 * in order to insert checksums into the metadata in large chunks,
1454 * we wait until bio submission time. All the pages in the bio are
1455 * checksummed and sums are attached onto the ordered extent record.
1456 *
1457 * At IO completion time the cums attached on the ordered extent record
1458 * are inserted into the btree
1459 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001460static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001461 int mirror_num, unsigned long bio_flags,
1462 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001463{
1464 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001465 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001466}
1467
Chris Masond352ac62008-09-29 15:18:18 -04001468/*
Chris Masoncad321a2008-12-17 14:51:42 -05001469 * extent_io.c submission hook. This does the right thing for csum calculation
1470 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001471 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001472static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001473 int mirror_num, unsigned long bio_flags,
1474 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001475{
1476 struct btrfs_root *root = BTRFS_I(inode)->root;
1477 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001478 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001479
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001480 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001481
Chris Mason2cf85722011-07-26 15:35:09 -04001482 if (btrfs_is_free_space_inode(root, inode))
Josef Bacik0cb59c92010-07-02 12:14:14 -04001483 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1484 else
1485 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001486 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001487
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001488 if (!(rw & REQ_WRITE)) {
Chris Masond20f7042008-12-08 16:58:54 -05001489 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001490 return btrfs_submit_compressed_read(inode, bio,
1491 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001492 } else if (!skip_sum) {
1493 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1494 if (ret)
1495 return ret;
1496 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001497 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001498 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001499 /* csum items have already been cloned */
1500 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1501 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001502 /* we're doing a write, do the async checksumming */
1503 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001504 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001505 bio_flags, bio_offset,
1506 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001507 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001508 }
1509
Chris Mason0b86a832008-03-24 15:01:56 -04001510mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001511 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001512}
Chris Mason6885f302008-02-20 16:11:05 -05001513
Chris Masond352ac62008-09-29 15:18:18 -04001514/*
1515 * given a list of ordered sums record them in the inode. This happens
1516 * at IO completion time based on sums calculated at bio submission time.
1517 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001518static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001519 struct inode *inode, u64 file_offset,
1520 struct list_head *list)
1521{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001522 struct btrfs_ordered_sum *sum;
1523
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001524 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001525 btrfs_csum_file_blocks(trans,
1526 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001527 }
1528 return 0;
1529}
1530
Josef Bacik2ac55d42010-02-03 19:33:23 +00001531int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1532 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001533{
Chris Masond3977122009-01-05 21:25:51 -05001534 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001535 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001536 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001537 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001538}
1539
Chris Masond352ac62008-09-29 15:18:18 -04001540/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001541struct btrfs_writepage_fixup {
1542 struct page *page;
1543 struct btrfs_work work;
1544};
1545
Christoph Hellwigb2950862008-12-02 09:54:17 -05001546static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001547{
1548 struct btrfs_writepage_fixup *fixup;
1549 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001550 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001551 struct page *page;
1552 struct inode *inode;
1553 u64 page_start;
1554 u64 page_end;
1555
1556 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1557 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001558again:
Chris Mason247e7432008-07-17 12:53:51 -04001559 lock_page(page);
1560 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1561 ClearPageChecked(page);
1562 goto out_page;
1563 }
1564
1565 inode = page->mapping->host;
1566 page_start = page_offset(page);
1567 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1568
Josef Bacik2ac55d42010-02-03 19:33:23 +00001569 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1570 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001571
1572 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001573 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001574 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001575
1576 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1577 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001578 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1579 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001580 unlock_page(page);
1581 btrfs_start_ordered_extent(inode, ordered, 1);
1582 goto again;
1583 }
Chris Mason247e7432008-07-17 12:53:51 -04001584
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001585 BUG();
Josef Bacik2ac55d42010-02-03 19:33:23 +00001586 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001587 ClearPageChecked(page);
1588out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001589 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1590 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001591out_page:
1592 unlock_page(page);
1593 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001594 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001595}
1596
1597/*
1598 * There are a few paths in the higher layers of the kernel that directly
1599 * set the page dirty bit without asking the filesystem if it is a
1600 * good idea. This causes problems because we want to make sure COW
1601 * properly happens and the data=ordered rules are followed.
1602 *
Chris Masonc8b97812008-10-29 14:49:59 -04001603 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001604 * hasn't been properly setup for IO. We kick off an async process
1605 * to fix it up. The async helper will wait for ordered extents, set
1606 * the delalloc bit and make it safe to write the page.
1607 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001608static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001609{
1610 struct inode *inode = page->mapping->host;
1611 struct btrfs_writepage_fixup *fixup;
1612 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001613
Chris Mason8b62b722009-09-02 16:53:46 -04001614 /* this page is properly in the ordered list */
1615 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001616 return 0;
1617
1618 if (PageChecked(page))
1619 return -EAGAIN;
1620
1621 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1622 if (!fixup)
1623 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001624
Chris Mason247e7432008-07-17 12:53:51 -04001625 SetPageChecked(page);
1626 page_cache_get(page);
1627 fixup->work.func = btrfs_writepage_fixup_worker;
1628 fixup->page = page;
1629 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1630 return -EAGAIN;
1631}
1632
Yan Zhengd899e052008-10-30 14:25:28 -04001633static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1634 struct inode *inode, u64 file_pos,
1635 u64 disk_bytenr, u64 disk_num_bytes,
1636 u64 num_bytes, u64 ram_bytes,
1637 u8 compression, u8 encryption,
1638 u16 other_encoding, int extent_type)
1639{
1640 struct btrfs_root *root = BTRFS_I(inode)->root;
1641 struct btrfs_file_extent_item *fi;
1642 struct btrfs_path *path;
1643 struct extent_buffer *leaf;
1644 struct btrfs_key ins;
1645 u64 hint;
1646 int ret;
1647
1648 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001649 if (!path)
1650 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001651
Chris Masonb9473432009-03-13 11:00:37 -04001652 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001653
1654 /*
1655 * we may be replacing one extent in the tree with another.
1656 * The new extent is pinned in the extent map, and we don't want
1657 * to drop it from the cache until it is completely in the btree.
1658 *
1659 * So, tell btrfs_drop_extents to leave this extent in the cache.
1660 * the caller is expected to unpin it and allow it to be merged
1661 * with the others.
1662 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001663 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1664 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001665 BUG_ON(ret);
1666
Li Zefan33345d012011-04-20 10:31:50 +08001667 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001668 ins.offset = file_pos;
1669 ins.type = BTRFS_EXTENT_DATA_KEY;
1670 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1671 BUG_ON(ret);
1672 leaf = path->nodes[0];
1673 fi = btrfs_item_ptr(leaf, path->slots[0],
1674 struct btrfs_file_extent_item);
1675 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1676 btrfs_set_file_extent_type(leaf, fi, extent_type);
1677 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1678 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1679 btrfs_set_file_extent_offset(leaf, fi, 0);
1680 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1681 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1682 btrfs_set_file_extent_compression(leaf, fi, compression);
1683 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1684 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001685
1686 btrfs_unlock_up_safe(path, 1);
1687 btrfs_set_lock_blocking(leaf);
1688
Yan Zhengd899e052008-10-30 14:25:28 -04001689 btrfs_mark_buffer_dirty(leaf);
1690
1691 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001692
1693 ins.objectid = disk_bytenr;
1694 ins.offset = disk_num_bytes;
1695 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001696 ret = btrfs_alloc_reserved_file_extent(trans, root,
1697 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001698 btrfs_ino(inode), file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001699 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001700 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001701
Yan Zhengd899e052008-10-30 14:25:28 -04001702 return 0;
1703}
1704
Chris Mason5d13a982009-03-13 11:41:46 -04001705/*
1706 * helper function for btrfs_finish_ordered_io, this
1707 * just reads in some of the csum leaves to prime them into ram
1708 * before we start the transaction. It limits the amount of btree
1709 * reads required while inside the transaction.
1710 */
Chris Masond352ac62008-09-29 15:18:18 -04001711/* as ordered data IO finishes, this gets called so we can finish
1712 * an ordered extent if the range of bytes in the file it covers are
1713 * fully written.
1714 */
Chris Mason211f90e2008-07-18 11:56:15 -04001715static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001716{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001717 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001718 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001719 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001720 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001721 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001722 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001723 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001724 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001725
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001726 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1727 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001728 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001729 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001730 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001731
Chris Mason2cf85722011-07-26 15:35:09 -04001732 nolock = btrfs_is_free_space_inode(root, inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001733
Yan, Zhengc2167752009-11-12 09:34:21 +00001734 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1735 BUG_ON(!list_empty(&ordered_extent->list));
1736 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1737 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001738 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001739 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001740 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001741 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001742 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001743 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001744 ret = btrfs_update_inode(trans, root, inode);
1745 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001746 }
1747 goto out;
1748 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001749
Josef Bacik2ac55d42010-02-03 19:33:23 +00001750 lock_extent_bits(io_tree, ordered_extent->file_offset,
1751 ordered_extent->file_offset + ordered_extent->len - 1,
1752 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001753
Josef Bacik0cb59c92010-07-02 12:14:14 -04001754 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001755 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001756 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001757 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001758 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001759 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001760
Chris Masonc8b97812008-10-29 14:49:59 -04001761 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001762 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001763 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001764 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001765 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001766 ordered_extent->file_offset,
1767 ordered_extent->file_offset +
1768 ordered_extent->len);
1769 BUG_ON(ret);
1770 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001771 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001772 ret = insert_reserved_file_extent(trans, inode,
1773 ordered_extent->file_offset,
1774 ordered_extent->start,
1775 ordered_extent->disk_len,
1776 ordered_extent->len,
1777 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001778 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001779 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001780 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1781 ordered_extent->file_offset,
1782 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001783 BUG_ON(ret);
1784 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001785 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1786 ordered_extent->file_offset +
1787 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1788
Chris Masone6dcd2d2008-07-17 12:53:50 -04001789 add_pending_csums(trans, inode, ordered_extent->file_offset,
1790 &ordered_extent->list);
1791
Josef Bacik1ef30be2011-04-05 19:25:36 -04001792 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Miao Xiea39f7522011-09-11 10:52:25 -04001793 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Josef Bacik1ef30be2011-04-05 19:25:36 -04001794 ret = btrfs_update_inode(trans, root, inode);
1795 BUG_ON(ret);
1796 }
1797 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00001798out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001799 if (root != root->fs_info->tree_root)
1800 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1801 if (trans) {
1802 if (nolock)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001803 btrfs_end_transaction_nolock(trans, root);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001804 else
Josef Bacik0cb59c92010-07-02 12:14:14 -04001805 btrfs_end_transaction(trans, root);
1806 }
1807
Chris Masone6dcd2d2008-07-17 12:53:50 -04001808 /* once for us */
1809 btrfs_put_ordered_extent(ordered_extent);
1810 /* once for the tree */
1811 btrfs_put_ordered_extent(ordered_extent);
1812
Chris Masone6dcd2d2008-07-17 12:53:50 -04001813 return 0;
1814}
1815
Christoph Hellwigb2950862008-12-02 09:54:17 -05001816static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001817 struct extent_state *state, int uptodate)
1818{
liubo1abe9b82011-03-24 11:18:59 +00001819 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1820
Chris Mason8b62b722009-09-02 16:53:46 -04001821 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001822 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1823}
1824
Chris Masond352ac62008-09-29 15:18:18 -04001825/*
1826 * When IO fails, either with EIO or csum verification fails, we
1827 * try other mirrors that might have a good copy of the data. This
1828 * io_failure_record is used to record state as we go through all the
1829 * mirrors. If another mirror has good data, the page is set up to date
1830 * and things continue. If a good mirror can't be found, the original
1831 * bio end_io callback is called to indicate things have failed.
1832 */
Chris Mason7e383262008-04-09 16:28:12 -04001833struct io_failure_record {
1834 struct page *page;
1835 u64 start;
1836 u64 len;
1837 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001838 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001839 int last_mirror;
1840};
1841
Christoph Hellwigb2950862008-12-02 09:54:17 -05001842static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001843 struct page *page, u64 start, u64 end,
1844 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001845{
1846 struct io_failure_record *failrec = NULL;
1847 u64 private;
1848 struct extent_map *em;
1849 struct inode *inode = page->mapping->host;
1850 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001851 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001852 struct bio *bio;
1853 int num_copies;
1854 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001855 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001856 u64 logical;
1857
1858 ret = get_state_private(failure_tree, start, &private);
1859 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001860 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1861 if (!failrec)
1862 return -ENOMEM;
1863 failrec->start = start;
1864 failrec->len = end - start + 1;
1865 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001866 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001867
Chris Mason890871b2009-09-02 16:24:52 -04001868 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001869 em = lookup_extent_mapping(em_tree, start, failrec->len);
1870 if (em->start > start || em->start + em->len < start) {
1871 free_extent_map(em);
1872 em = NULL;
1873 }
Chris Mason890871b2009-09-02 16:24:52 -04001874 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001875
David Sterbac7040052011-04-19 18:00:01 +02001876 if (IS_ERR_OR_NULL(em)) {
Chris Mason7e383262008-04-09 16:28:12 -04001877 kfree(failrec);
1878 return -EIO;
1879 }
1880 logical = start - em->start;
1881 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001882 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1883 logical = em->block_start;
1884 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08001885 extent_set_compress_type(&failrec->bio_flags,
1886 em->compress_type);
Chris Masond20f7042008-12-08 16:58:54 -05001887 }
Chris Mason7e383262008-04-09 16:28:12 -04001888 failrec->logical = logical;
1889 free_extent_map(em);
1890 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1891 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001892 set_state_private(failure_tree, start,
1893 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001894 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001895 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001896 }
1897 num_copies = btrfs_num_copies(
1898 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1899 failrec->logical, failrec->len);
1900 failrec->last_mirror++;
1901 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001902 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001903 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1904 failrec->start,
1905 EXTENT_LOCKED);
1906 if (state && state->start != failrec->start)
1907 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001908 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001909 }
1910 if (!state || failrec->last_mirror > num_copies) {
1911 set_state_private(failure_tree, failrec->start, 0);
1912 clear_extent_bits(failure_tree, failrec->start,
1913 failrec->start + failrec->len - 1,
1914 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1915 kfree(failrec);
1916 return -EIO;
1917 }
1918 bio = bio_alloc(GFP_NOFS, 1);
1919 bio->bi_private = state;
1920 bio->bi_end_io = failed_bio->bi_end_io;
1921 bio->bi_sector = failrec->logical >> 9;
1922 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001923 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001924
Chris Mason7e383262008-04-09 16:28:12 -04001925 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001926 if (failed_bio->bi_rw & REQ_WRITE)
Chris Mason1259ab72008-05-12 13:39:03 -04001927 rw = WRITE;
1928 else
1929 rw = READ;
1930
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001931 ret = BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001932 failrec->last_mirror,
Chris Masoneaf25d92010-05-25 09:48:28 -04001933 failrec->bio_flags, 0);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001934 return ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001935}
1936
Chris Masond352ac62008-09-29 15:18:18 -04001937/*
1938 * each time an IO finishes, we do a fast check in the IO failure tree
1939 * to see if we need to process or clean up an io_failure_record
1940 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001941static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001942{
1943 u64 private;
1944 u64 private_failure;
1945 struct io_failure_record *failure;
1946 int ret;
1947
1948 private = 0;
1949 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
Chris Masonec29ed52011-02-23 16:23:20 -05001950 (u64)-1, 1, EXTENT_DIRTY, 0)) {
Chris Mason1259ab72008-05-12 13:39:03 -04001951 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1952 start, &private_failure);
1953 if (ret == 0) {
1954 failure = (struct io_failure_record *)(unsigned long)
1955 private_failure;
1956 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1957 failure->start, 0);
1958 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1959 failure->start,
1960 failure->start + failure->len - 1,
1961 EXTENT_DIRTY | EXTENT_LOCKED,
1962 GFP_NOFS);
1963 kfree(failure);
1964 }
1965 }
Chris Mason7e383262008-04-09 16:28:12 -04001966 return 0;
1967}
1968
Chris Masond352ac62008-09-29 15:18:18 -04001969/*
1970 * when reads are done, we need to check csums to verify the data is correct
1971 * if there's a match, we allow the bio to finish. If not, we go through
1972 * the io_failure_record routines to find good copies
1973 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001974static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001975 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001976{
Chris Mason35ebb932007-10-30 16:56:53 -04001977 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001978 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001979 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001980 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001981 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001982 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001983 struct btrfs_root *root = BTRFS_I(inode)->root;
1984 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001985
Chris Masond20f7042008-12-08 16:58:54 -05001986 if (PageChecked(page)) {
1987 ClearPageChecked(page);
1988 goto good;
1989 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001990
1991 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02001992 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05001993
Yan Zheng17d217f2008-12-12 10:03:38 -05001994 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001995 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001996 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1997 GFP_NOFS);
1998 return 0;
1999 }
2000
Yanc2e639f2008-02-04 08:57:25 -05002001 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002002 private = state->private;
2003 ret = 0;
2004 } else {
2005 ret = get_state_private(io_tree, start, &private);
2006 }
Chris Mason9ab86c82009-01-07 09:48:51 -05002007 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05002008 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002009 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002010
Chris Masonff79f812007-10-15 16:22:25 -04002011 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2012 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002013 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002014 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002015
Chris Mason9ab86c82009-01-07 09:48:51 -05002016 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05002017good:
Chris Mason7e383262008-04-09 16:28:12 -04002018 /* if the io failure tree for this inode is non-empty,
2019 * check to see if we've recovered from a failed IO
2020 */
Chris Mason1259ab72008-05-12 13:39:03 -04002021 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04002022 return 0;
2023
2024zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002025 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002026 "private %llu\n",
2027 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002028 (unsigned long long)start, csum,
2029 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002030 memset(kaddr + offset, 1, end - start + 1);
2031 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05002032 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04002033 if (private == 0)
2034 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002035 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002036}
Chris Masonb888db22007-08-27 16:49:44 -04002037
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002038struct delayed_iput {
2039 struct list_head list;
2040 struct inode *inode;
2041};
2042
2043void btrfs_add_delayed_iput(struct inode *inode)
2044{
2045 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2046 struct delayed_iput *delayed;
2047
2048 if (atomic_add_unless(&inode->i_count, -1, 1))
2049 return;
2050
2051 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2052 delayed->inode = inode;
2053
2054 spin_lock(&fs_info->delayed_iput_lock);
2055 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2056 spin_unlock(&fs_info->delayed_iput_lock);
2057}
2058
2059void btrfs_run_delayed_iputs(struct btrfs_root *root)
2060{
2061 LIST_HEAD(list);
2062 struct btrfs_fs_info *fs_info = root->fs_info;
2063 struct delayed_iput *delayed;
2064 int empty;
2065
2066 spin_lock(&fs_info->delayed_iput_lock);
2067 empty = list_empty(&fs_info->delayed_iputs);
2068 spin_unlock(&fs_info->delayed_iput_lock);
2069 if (empty)
2070 return;
2071
2072 down_read(&root->fs_info->cleanup_work_sem);
2073 spin_lock(&fs_info->delayed_iput_lock);
2074 list_splice_init(&fs_info->delayed_iputs, &list);
2075 spin_unlock(&fs_info->delayed_iput_lock);
2076
2077 while (!list_empty(&list)) {
2078 delayed = list_entry(list.next, struct delayed_iput, list);
2079 list_del(&delayed->list);
2080 iput(delayed->inode);
2081 kfree(delayed);
2082 }
2083 up_read(&root->fs_info->cleanup_work_sem);
2084}
2085
Yan, Zhengd68fc572010-05-16 10:49:58 -04002086enum btrfs_orphan_cleanup_state {
2087 ORPHAN_CLEANUP_STARTED = 1,
2088 ORPHAN_CLEANUP_DONE = 2,
2089};
2090
2091/*
2092 * This is called in transaction commmit time. If there are no orphan
2093 * files in the subvolume, it removes orphan item and frees block_rsv
2094 * structure.
2095 */
2096void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2097 struct btrfs_root *root)
2098{
2099 int ret;
2100
2101 if (!list_empty(&root->orphan_list) ||
2102 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2103 return;
2104
2105 if (root->orphan_item_inserted &&
2106 btrfs_root_refs(&root->root_item) > 0) {
2107 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2108 root->root_key.objectid);
2109 BUG_ON(ret);
2110 root->orphan_item_inserted = 0;
2111 }
2112
2113 if (root->orphan_block_rsv) {
2114 WARN_ON(root->orphan_block_rsv->size > 0);
2115 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2116 root->orphan_block_rsv = NULL;
2117 }
2118}
2119
2120/*
Josef Bacik7b128762008-07-24 12:17:14 -04002121 * This creates an orphan entry for the given inode in case something goes
2122 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002123 *
2124 * NOTE: caller of this function should reserve 5 units of metadata for
2125 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002126 */
2127int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2128{
2129 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002130 struct btrfs_block_rsv *block_rsv = NULL;
2131 int reserve = 0;
2132 int insert = 0;
2133 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002134
Yan, Zhengd68fc572010-05-16 10:49:58 -04002135 if (!root->orphan_block_rsv) {
2136 block_rsv = btrfs_alloc_block_rsv(root);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002137 if (!block_rsv)
2138 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002139 }
2140
Yan, Zhengd68fc572010-05-16 10:49:58 -04002141 spin_lock(&root->orphan_lock);
2142 if (!root->orphan_block_rsv) {
2143 root->orphan_block_rsv = block_rsv;
2144 } else if (block_rsv) {
2145 btrfs_free_block_rsv(root, block_rsv);
2146 block_rsv = NULL;
2147 }
Josef Bacik7b128762008-07-24 12:17:14 -04002148
Yan, Zhengd68fc572010-05-16 10:49:58 -04002149 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2150 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2151#if 0
2152 /*
2153 * For proper ENOSPC handling, we should do orphan
2154 * cleanup when mounting. But this introduces backward
2155 * compatibility issue.
2156 */
2157 if (!xchg(&root->orphan_item_inserted, 1))
2158 insert = 2;
2159 else
2160 insert = 1;
2161#endif
2162 insert = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002163 }
Josef Bacik7b128762008-07-24 12:17:14 -04002164
Yan, Zhengd68fc572010-05-16 10:49:58 -04002165 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2166 BTRFS_I(inode)->orphan_meta_reserved = 1;
2167 reserve = 1;
2168 }
2169 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002170
Yan, Zhengd68fc572010-05-16 10:49:58 -04002171 /* grab metadata reservation from transaction handle */
2172 if (reserve) {
2173 ret = btrfs_orphan_reserve_metadata(trans, inode);
2174 BUG_ON(ret);
2175 }
2176
2177 /* insert an orphan item to track this unlinked/truncated file */
2178 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002179 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Yan, Zhengd68fc572010-05-16 10:49:58 -04002180 BUG_ON(ret);
2181 }
2182
2183 /* insert an orphan item to track subvolume contains orphan files */
2184 if (insert >= 2) {
2185 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2186 root->root_key.objectid);
2187 BUG_ON(ret);
2188 }
2189 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002190}
2191
2192/*
2193 * We have done the truncate/delete so we can go ahead and remove the orphan
2194 * item for this particular inode.
2195 */
2196int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2197{
2198 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002199 int delete_item = 0;
2200 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002201 int ret = 0;
2202
Yan, Zhengd68fc572010-05-16 10:49:58 -04002203 spin_lock(&root->orphan_lock);
2204 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2205 list_del_init(&BTRFS_I(inode)->i_orphan);
2206 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002207 }
2208
Yan, Zhengd68fc572010-05-16 10:49:58 -04002209 if (BTRFS_I(inode)->orphan_meta_reserved) {
2210 BTRFS_I(inode)->orphan_meta_reserved = 0;
2211 release_rsv = 1;
2212 }
2213 spin_unlock(&root->orphan_lock);
2214
2215 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002216 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Yan, Zhengd68fc572010-05-16 10:49:58 -04002217 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002218 }
2219
Yan, Zhengd68fc572010-05-16 10:49:58 -04002220 if (release_rsv)
2221 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002222
Yan, Zhengd68fc572010-05-16 10:49:58 -04002223 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002224}
2225
2226/*
2227 * this cleans up any orphans that may be left on the list from the last use
2228 * of this root.
2229 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002230int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002231{
2232 struct btrfs_path *path;
2233 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002234 struct btrfs_key key, found_key;
2235 struct btrfs_trans_handle *trans;
2236 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002237 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002238 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2239
Yan, Zhengd68fc572010-05-16 10:49:58 -04002240 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002241 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002242
2243 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002244 if (!path) {
2245 ret = -ENOMEM;
2246 goto out;
2247 }
Josef Bacik7b128762008-07-24 12:17:14 -04002248 path->reada = -1;
2249
2250 key.objectid = BTRFS_ORPHAN_OBJECTID;
2251 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2252 key.offset = (u64)-1;
2253
Josef Bacik7b128762008-07-24 12:17:14 -04002254 while (1) {
2255 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002256 if (ret < 0)
2257 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002258
2259 /*
2260 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002261 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002262 * find the key and see if we have stuff that matches
2263 */
2264 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002265 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002266 if (path->slots[0] == 0)
2267 break;
2268 path->slots[0]--;
2269 }
2270
2271 /* pull out the item */
2272 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002273 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2274
2275 /* make sure the item matches what we want */
2276 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2277 break;
2278 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2279 break;
2280
2281 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002282 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002283
2284 /*
2285 * this is where we are basically btrfs_lookup, without the
2286 * crossing root thing. we store the inode number in the
2287 * offset of the orphan item.
2288 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002289
2290 if (found_key.offset == last_objectid) {
2291 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2292 "stopping orphan cleanup\n");
2293 ret = -EINVAL;
2294 goto out;
2295 }
2296
2297 last_objectid = found_key.offset;
2298
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299 found_key.objectid = found_key.offset;
2300 found_key.type = BTRFS_INODE_ITEM_KEY;
2301 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002302 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002303 ret = PTR_RET(inode);
2304 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002305 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002306
2307 /*
2308 * Inode is already gone but the orphan item is still there,
2309 * kill the orphan item.
2310 */
2311 if (ret == -ESTALE) {
2312 trans = btrfs_start_transaction(root, 1);
2313 if (IS_ERR(trans)) {
2314 ret = PTR_ERR(trans);
2315 goto out;
2316 }
2317 ret = btrfs_del_orphan_item(trans, root,
2318 found_key.objectid);
2319 BUG_ON(ret);
2320 btrfs_end_transaction(trans, root);
2321 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002322 }
Josef Bacik7b128762008-07-24 12:17:14 -04002323
Josef Bacik7b128762008-07-24 12:17:14 -04002324 /*
2325 * add this inode to the orphan list so btrfs_orphan_del does
2326 * the proper thing when we hit it
2327 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002328 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002329 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002330 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002331
Josef Bacik7b128762008-07-24 12:17:14 -04002332 /* if we have links, this was a truncate, lets do that */
2333 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002334 if (!S_ISREG(inode->i_mode)) {
2335 WARN_ON(1);
2336 iput(inode);
2337 continue;
2338 }
Josef Bacik7b128762008-07-24 12:17:14 -04002339 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002340 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002341 } else {
2342 nr_unlink++;
2343 }
2344
2345 /* this will do delete_inode and everything for us */
2346 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002347 if (ret)
2348 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002349 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002350 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2351
2352 if (root->orphan_block_rsv)
2353 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2354 (u64)-1);
2355
2356 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002357 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002358 if (!IS_ERR(trans))
2359 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002360 }
Josef Bacik7b128762008-07-24 12:17:14 -04002361
2362 if (nr_unlink)
2363 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2364 if (nr_truncate)
2365 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002366
2367out:
2368 if (ret)
2369 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2370 btrfs_free_path(path);
2371 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002372}
2373
Chris Masond352ac62008-09-29 15:18:18 -04002374/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002375 * very simple check to peek ahead in the leaf looking for xattrs. If we
2376 * don't find any xattrs, we know there can't be any acls.
2377 *
2378 * slot is the slot the inode is in, objectid is the objectid of the inode
2379 */
2380static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2381 int slot, u64 objectid)
2382{
2383 u32 nritems = btrfs_header_nritems(leaf);
2384 struct btrfs_key found_key;
2385 int scanned = 0;
2386
2387 slot++;
2388 while (slot < nritems) {
2389 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2390
2391 /* we found a different objectid, there must not be acls */
2392 if (found_key.objectid != objectid)
2393 return 0;
2394
2395 /* we found an xattr, assume we've got an acl */
2396 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2397 return 1;
2398
2399 /*
2400 * we found a key greater than an xattr key, there can't
2401 * be any acls later on
2402 */
2403 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2404 return 0;
2405
2406 slot++;
2407 scanned++;
2408
2409 /*
2410 * it goes inode, inode backrefs, xattrs, extents,
2411 * so if there are a ton of hard links to an inode there can
2412 * be a lot of backrefs. Don't waste time searching too hard,
2413 * this is just an optimization
2414 */
2415 if (scanned >= 8)
2416 break;
2417 }
2418 /* we hit the end of the leaf before we found an xattr or
2419 * something larger than an xattr. We have to assume the inode
2420 * has acls
2421 */
2422 return 1;
2423}
2424
2425/*
Chris Masond352ac62008-09-29 15:18:18 -04002426 * read an inode from the btree into the in-memory inode
2427 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002428static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002429{
2430 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002431 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002432 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002433 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002434 struct btrfs_root *root = BTRFS_I(inode)->root;
2435 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002436 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002437 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002438 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002439 bool filled = false;
2440
2441 ret = btrfs_fill_inode(inode, &rdev);
2442 if (!ret)
2443 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002444
2445 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002446 if (!path)
2447 goto make_bad;
2448
Josef Bacikd90c7322011-05-17 09:50:54 -04002449 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002450 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002451
Chris Mason39279cc2007-06-12 06:35:45 -04002452 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002453 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002454 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002455
Chris Mason5f39d392007-10-15 16:14:19 -04002456 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002457
2458 if (filled)
2459 goto cache_acl;
2460
Chris Mason5f39d392007-10-15 16:14:19 -04002461 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2462 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002463 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2464 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2465 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2466 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002467 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002468
2469 tspec = btrfs_inode_atime(inode_item);
2470 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2471 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2472
2473 tspec = btrfs_inode_mtime(inode_item);
2474 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2475 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2476
2477 tspec = btrfs_inode_ctime(inode_item);
2478 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2479 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2480
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002481 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002482 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002483 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002484 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002485 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002486 rdev = btrfs_inode_rdev(leaf, inode_item);
2487
Josef Bacikaec74772008-07-24 12:12:38 -04002488 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002489 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002490cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002491 /*
2492 * try to precache a NULL acl entry for files that don't have
2493 * any xattrs or acls
2494 */
Li Zefan33345d012011-04-20 10:31:50 +08002495 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2496 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002497 if (!maybe_acls)
2498 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002499
Chris Mason39279cc2007-06-12 06:35:45 -04002500 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002501
Chris Mason39279cc2007-06-12 06:35:45 -04002502 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002503 case S_IFREG:
2504 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002505 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002506 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002507 inode->i_fop = &btrfs_file_operations;
2508 inode->i_op = &btrfs_file_inode_operations;
2509 break;
2510 case S_IFDIR:
2511 inode->i_fop = &btrfs_dir_file_operations;
2512 if (root == root->fs_info->tree_root)
2513 inode->i_op = &btrfs_dir_ro_inode_operations;
2514 else
2515 inode->i_op = &btrfs_dir_inode_operations;
2516 break;
2517 case S_IFLNK:
2518 inode->i_op = &btrfs_symlink_inode_operations;
2519 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002520 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002521 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002522 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002523 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002524 init_special_inode(inode, inode->i_mode, rdev);
2525 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002526 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002527
2528 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002529 return;
2530
2531make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002532 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002533 make_bad_inode(inode);
2534}
2535
Chris Masond352ac62008-09-29 15:18:18 -04002536/*
2537 * given a leaf and an inode, copy the inode fields into the leaf
2538 */
Chris Masone02119d2008-09-05 16:13:11 -04002539static void fill_inode_item(struct btrfs_trans_handle *trans,
2540 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002541 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002542 struct inode *inode)
2543{
Chris Mason5f39d392007-10-15 16:14:19 -04002544 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2545 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002546 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002547 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2548 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2549
2550 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2551 inode->i_atime.tv_sec);
2552 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2553 inode->i_atime.tv_nsec);
2554
2555 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2556 inode->i_mtime.tv_sec);
2557 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2558 inode->i_mtime.tv_nsec);
2559
2560 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2561 inode->i_ctime.tv_sec);
2562 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2563 inode->i_ctime.tv_nsec);
2564
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002565 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002566 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002567 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002568 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002569 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002570 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f12011-05-11 15:26:06 -04002571 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002572}
2573
Chris Masond352ac62008-09-29 15:18:18 -04002574/*
2575 * copy everything in the in-memory inode into the btree.
2576 */
Chris Masond3977122009-01-05 21:25:51 -05002577noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2578 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002579{
2580 struct btrfs_inode_item *inode_item;
2581 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002582 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002583 int ret;
2584
Miao Xie16cdcec2011-04-22 18:12:22 +08002585 /*
Miao Xie149e2d72011-07-06 18:51:53 -04002586 * If the inode is a free space inode, we can deadlock during commit
2587 * if we put it into the delayed code.
2588 *
2589 * The data relocation inode should also be directly updated
2590 * without delay
Miao Xie16cdcec2011-04-22 18:12:22 +08002591 */
Chris Mason2cf85722011-07-26 15:35:09 -04002592 if (!btrfs_is_free_space_inode(root, inode)
Miao Xie149e2d72011-07-06 18:51:53 -04002593 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Miao Xie16cdcec2011-04-22 18:12:22 +08002594 ret = btrfs_delayed_update_inode(trans, root, inode);
2595 if (!ret)
2596 btrfs_set_inode_last_trans(trans, inode);
2597 return ret;
2598 }
2599
Chris Mason39279cc2007-06-12 06:35:45 -04002600 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002601 if (!path)
2602 return -ENOMEM;
2603
Chris Masonb9473432009-03-13 11:00:37 -04002604 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002605 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2606 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002607 if (ret) {
2608 if (ret > 0)
2609 ret = -ENOENT;
2610 goto failed;
2611 }
2612
Chris Masonb4ce94d2009-02-04 09:25:08 -05002613 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002614 leaf = path->nodes[0];
2615 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002616 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002617
Chris Masone02119d2008-09-05 16:13:11 -04002618 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002619 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002620 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002621 ret = 0;
2622failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002623 btrfs_free_path(path);
2624 return ret;
2625}
2626
Chris Masond352ac62008-09-29 15:18:18 -04002627/*
2628 * unlink helper that gets used here in inode.c and in the tree logging
2629 * recovery code. It remove a link in a directory with a given name, and
2630 * also drops the back refs in the inode to the directory
2631 */
Al Viro92986792011-03-04 17:14:37 +00002632static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2633 struct btrfs_root *root,
2634 struct inode *dir, struct inode *inode,
2635 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002636{
2637 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002638 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002639 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002640 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002641 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002642 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002643 u64 ino = btrfs_ino(inode);
2644 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002645
2646 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002647 if (!path) {
2648 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002649 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002650 }
2651
Chris Masonb9473432009-03-13 11:00:37 -04002652 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002653 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002654 name, name_len, -1);
2655 if (IS_ERR(di)) {
2656 ret = PTR_ERR(di);
2657 goto err;
2658 }
2659 if (!di) {
2660 ret = -ENOENT;
2661 goto err;
2662 }
Chris Mason5f39d392007-10-15 16:14:19 -04002663 leaf = path->nodes[0];
2664 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002665 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002666 if (ret)
2667 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002668 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002669
Li Zefan33345d012011-04-20 10:31:50 +08002670 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2671 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002672 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002673 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002674 "inode %llu parent %llu\n", name_len, name,
2675 (unsigned long long)ino, (unsigned long long)dir_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002676 goto err;
2677 }
2678
Miao Xie16cdcec2011-04-22 18:12:22 +08002679 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2680 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002681 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002682
Chris Masone02119d2008-09-05 16:13:11 -04002683 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002684 inode, dir_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002685 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002686
2687 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2688 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002689 if (ret == -ENOENT)
2690 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002691err:
2692 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002693 if (ret)
2694 goto out;
2695
2696 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2697 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2698 btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002699out:
Chris Mason39279cc2007-06-12 06:35:45 -04002700 return ret;
2701}
2702
Al Viro92986792011-03-04 17:14:37 +00002703int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2704 struct btrfs_root *root,
2705 struct inode *dir, struct inode *inode,
2706 const char *name, int name_len)
2707{
2708 int ret;
2709 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2710 if (!ret) {
2711 btrfs_drop_nlink(inode);
2712 ret = btrfs_update_inode(trans, root, inode);
2713 }
2714 return ret;
2715}
2716
2717
Yan, Zhenga22285a2010-05-16 10:48:46 -04002718/* helper to check if there is any shared block in the path */
2719static int check_path_shared(struct btrfs_root *root,
2720 struct btrfs_path *path)
2721{
2722 struct extent_buffer *eb;
2723 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00002724 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002725
2726 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002727 int ret;
2728
Yan, Zhenga22285a2010-05-16 10:48:46 -04002729 if (!path->nodes[level])
2730 break;
2731 eb = path->nodes[level];
2732 if (!btrfs_block_can_be_shared(root, eb))
2733 continue;
2734 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2735 &refs, NULL);
2736 if (refs > 1)
2737 return 1;
2738 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002739 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002740}
2741
2742/*
2743 * helper to start transaction for unlink and rmdir.
2744 *
2745 * unlink and rmdir are special in btrfs, they do not always free space.
2746 * so in enospc case, we should make sure they will free space before
2747 * allowing them to use the global metadata reservation.
2748 */
2749static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2750 struct dentry *dentry)
2751{
2752 struct btrfs_trans_handle *trans;
2753 struct btrfs_root *root = BTRFS_I(dir)->root;
2754 struct btrfs_path *path;
2755 struct btrfs_inode_ref *ref;
2756 struct btrfs_dir_item *di;
2757 struct inode *inode = dentry->d_inode;
2758 u64 index;
2759 int check_link = 1;
2760 int err = -ENOSPC;
2761 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002762 u64 ino = btrfs_ino(inode);
2763 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002764
Josef Bacike70bea52011-10-11 14:18:24 -04002765 /*
2766 * 1 for the possible orphan item
2767 * 1 for the dir item
2768 * 1 for the dir index
2769 * 1 for the inode ref
2770 * 1 for the inode ref in the tree log
2771 * 2 for the dir entries in the log
2772 * 1 for the inode
2773 */
2774 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002775 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2776 return trans;
2777
Li Zefan33345d012011-04-20 10:31:50 +08002778 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002779 return ERR_PTR(-ENOSPC);
2780
2781 /* check if there is someone else holds reference */
2782 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2783 return ERR_PTR(-ENOSPC);
2784
2785 if (atomic_read(&inode->i_count) > 2)
2786 return ERR_PTR(-ENOSPC);
2787
2788 if (xchg(&root->fs_info->enospc_unlink, 1))
2789 return ERR_PTR(-ENOSPC);
2790
2791 path = btrfs_alloc_path();
2792 if (!path) {
2793 root->fs_info->enospc_unlink = 0;
2794 return ERR_PTR(-ENOMEM);
2795 }
2796
Josef Bacik3880a1b2011-10-14 14:46:51 -04002797 /* 1 for the orphan item */
2798 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002799 if (IS_ERR(trans)) {
2800 btrfs_free_path(path);
2801 root->fs_info->enospc_unlink = 0;
2802 return trans;
2803 }
2804
2805 path->skip_locking = 1;
2806 path->search_commit_root = 1;
2807
2808 ret = btrfs_lookup_inode(trans, root, path,
2809 &BTRFS_I(dir)->location, 0);
2810 if (ret < 0) {
2811 err = ret;
2812 goto out;
2813 }
2814 if (ret == 0) {
2815 if (check_path_shared(root, path))
2816 goto out;
2817 } else {
2818 check_link = 0;
2819 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002820 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002821
2822 ret = btrfs_lookup_inode(trans, root, path,
2823 &BTRFS_I(inode)->location, 0);
2824 if (ret < 0) {
2825 err = ret;
2826 goto out;
2827 }
2828 if (ret == 0) {
2829 if (check_path_shared(root, path))
2830 goto out;
2831 } else {
2832 check_link = 0;
2833 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002834 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002835
2836 if (ret == 0 && S_ISREG(inode->i_mode)) {
2837 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002838 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002839 if (ret < 0) {
2840 err = ret;
2841 goto out;
2842 }
2843 BUG_ON(ret == 0);
2844 if (check_path_shared(root, path))
2845 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02002846 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002847 }
2848
2849 if (!check_link) {
2850 err = 0;
2851 goto out;
2852 }
2853
Li Zefan33345d012011-04-20 10:31:50 +08002854 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002855 dentry->d_name.name, dentry->d_name.len, 0);
2856 if (IS_ERR(di)) {
2857 err = PTR_ERR(di);
2858 goto out;
2859 }
2860 if (di) {
2861 if (check_path_shared(root, path))
2862 goto out;
2863 } else {
2864 err = 0;
2865 goto out;
2866 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002867 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002868
2869 ref = btrfs_lookup_inode_ref(trans, root, path,
2870 dentry->d_name.name, dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08002871 ino, dir_ino, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002872 if (IS_ERR(ref)) {
2873 err = PTR_ERR(ref);
2874 goto out;
2875 }
2876 BUG_ON(!ref);
2877 if (check_path_shared(root, path))
2878 goto out;
2879 index = btrfs_inode_ref_index(path->nodes[0], ref);
David Sterbab3b4aa72011-04-21 01:20:15 +02002880 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002881
Miao Xie16cdcec2011-04-22 18:12:22 +08002882 /*
2883 * This is a commit root search, if we can lookup inode item and other
2884 * relative items in the commit root, it means the transaction of
2885 * dir/file creation has been committed, and the dir index item that we
2886 * delay to insert has also been inserted into the commit root. So
2887 * we needn't worry about the delayed insertion of the dir index item
2888 * here.
2889 */
Li Zefan33345d012011-04-20 10:31:50 +08002890 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002891 dentry->d_name.name, dentry->d_name.len, 0);
2892 if (IS_ERR(di)) {
2893 err = PTR_ERR(di);
2894 goto out;
2895 }
2896 BUG_ON(ret == -ENOENT);
2897 if (check_path_shared(root, path))
2898 goto out;
2899
2900 err = 0;
2901out:
2902 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04002903 /* Migrate the orphan reservation over */
2904 if (!err)
2905 err = btrfs_block_rsv_migrate(trans->block_rsv,
2906 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04002907 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04002908
Yan, Zhenga22285a2010-05-16 10:48:46 -04002909 if (err) {
2910 btrfs_end_transaction(trans, root);
2911 root->fs_info->enospc_unlink = 0;
2912 return ERR_PTR(err);
2913 }
2914
2915 trans->block_rsv = &root->fs_info->global_block_rsv;
2916 return trans;
2917}
2918
2919static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2920 struct btrfs_root *root)
2921{
2922 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
Josef Bacik5a77d762011-11-01 14:32:23 -04002923 btrfs_block_rsv_release(root, trans->block_rsv,
2924 trans->bytes_reserved);
2925 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002926 BUG_ON(!root->fs_info->enospc_unlink);
2927 root->fs_info->enospc_unlink = 0;
2928 }
2929 btrfs_end_transaction_throttle(trans, root);
2930}
2931
Chris Mason39279cc2007-06-12 06:35:45 -04002932static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2933{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002934 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002935 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002936 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002937 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002938 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002939
Yan, Zhenga22285a2010-05-16 10:48:46 -04002940 trans = __unlink_start_trans(dir, dentry);
2941 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002942 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002943
Chris Mason12fcfd22009-03-24 10:24:20 -04002944 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2945
Chris Masone02119d2008-09-05 16:13:11 -04002946 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2947 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002948 if (ret)
2949 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002950
Yan, Zhenga22285a2010-05-16 10:48:46 -04002951 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002952 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002953 if (ret)
2954 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002955 }
Josef Bacik7b128762008-07-24 12:17:14 -04002956
Tsutomu Itohb5324022011-07-19 07:27:20 +00002957out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002958 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002959 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002960 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002961 return ret;
2962}
2963
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002964int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2965 struct btrfs_root *root,
2966 struct inode *dir, u64 objectid,
2967 const char *name, int name_len)
2968{
2969 struct btrfs_path *path;
2970 struct extent_buffer *leaf;
2971 struct btrfs_dir_item *di;
2972 struct btrfs_key key;
2973 u64 index;
2974 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002975 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002976
2977 path = btrfs_alloc_path();
2978 if (!path)
2979 return -ENOMEM;
2980
Li Zefan33345d012011-04-20 10:31:50 +08002981 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002982 name, name_len, -1);
David Sterbac7040052011-04-19 18:00:01 +02002983 BUG_ON(IS_ERR_OR_NULL(di));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002984
2985 leaf = path->nodes[0];
2986 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2987 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2988 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2989 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02002990 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002991
2992 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2993 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002994 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002995 if (ret < 0) {
2996 BUG_ON(ret != -ENOENT);
Li Zefan33345d012011-04-20 10:31:50 +08002997 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002998 name, name_len);
David Sterbac7040052011-04-19 18:00:01 +02002999 BUG_ON(IS_ERR_OR_NULL(di));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003000
3001 leaf = path->nodes[0];
3002 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003003 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003004 index = key.offset;
3005 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003006 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003007
Miao Xie16cdcec2011-04-22 18:12:22 +08003008 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3009 BUG_ON(ret);
3010
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003011 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3012 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3013 ret = btrfs_update_inode(trans, root, dir);
3014 BUG_ON(ret);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003015
Josef Bacik71d7aed2011-06-14 14:24:32 -04003016 btrfs_free_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003017 return 0;
3018}
3019
Chris Mason39279cc2007-06-12 06:35:45 -04003020static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3021{
3022 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003023 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003024 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003025 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003026 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003027
Chris Mason3394e162008-11-17 20:42:26 -05003028 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Li Zefan33345d012011-04-20 10:31:50 +08003029 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003030 return -ENOTEMPTY;
3031
Yan, Zhenga22285a2010-05-16 10:48:46 -04003032 trans = __unlink_start_trans(dir, dentry);
3033 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003034 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003035
Li Zefan33345d012011-04-20 10:31:50 +08003036 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003037 err = btrfs_unlink_subvol(trans, root, dir,
3038 BTRFS_I(inode)->location.objectid,
3039 dentry->d_name.name,
3040 dentry->d_name.len);
3041 goto out;
3042 }
3043
Josef Bacik7b128762008-07-24 12:17:14 -04003044 err = btrfs_orphan_add(trans, inode);
3045 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003046 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003047
Chris Mason39279cc2007-06-12 06:35:45 -04003048 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003049 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3050 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003051 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003052 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003053out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003054 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003055 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003056 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003057
Chris Mason39279cc2007-06-12 06:35:45 -04003058 return err;
3059}
3060
Chris Mason323ac952008-10-01 19:05:46 -04003061/*
Chris Mason39279cc2007-06-12 06:35:45 -04003062 * this can truncate away extent items, csum items and directory items.
3063 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003064 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003065 *
3066 * csum items that cross the new i_size are truncated to the new size
3067 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003068 *
3069 * min_type is the minimum key type to truncate down to. If set to 0, this
3070 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003071 */
Yan, Zheng80825102009-11-12 09:35:36 +00003072int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3073 struct btrfs_root *root,
3074 struct inode *inode,
3075 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003076{
Chris Mason39279cc2007-06-12 06:35:45 -04003077 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003078 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003079 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003080 struct btrfs_key key;
3081 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003082 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003083 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003084 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003085 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003086 u64 mask = root->sectorsize - 1;
3087 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003088 int found_extent;
3089 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003090 int pending_del_nr = 0;
3091 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003092 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003093 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003094 int ret;
3095 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003096 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003097
3098 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003099
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003100 path = btrfs_alloc_path();
3101 if (!path)
3102 return -ENOMEM;
3103 path->reada = -1;
3104
Josef Bacik0af3d002010-06-21 14:48:16 -04003105 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003106 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003107
Miao Xie16cdcec2011-04-22 18:12:22 +08003108 /*
3109 * This function is also used to drop the items in the log tree before
3110 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3111 * it is used to drop the loged items. So we shouldn't kill the delayed
3112 * items.
3113 */
3114 if (min_type == 0 && root == BTRFS_I(inode)->root)
3115 btrfs_kill_delayed_inode_items(inode);
3116
Li Zefan33345d012011-04-20 10:31:50 +08003117 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003118 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003119 key.type = (u8)-1;
3120
Chris Mason85e21ba2008-01-29 15:11:36 -05003121search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003122 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003123 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003124 if (ret < 0) {
3125 err = ret;
3126 goto out;
3127 }
Chris Masond3977122009-01-05 21:25:51 -05003128
Chris Mason85e21ba2008-01-29 15:11:36 -05003129 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003130 /* there are no items in the tree for us to truncate, we're
3131 * done
3132 */
Yan, Zheng80825102009-11-12 09:35:36 +00003133 if (path->slots[0] == 0)
3134 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003135 path->slots[0]--;
3136 }
3137
Chris Masond3977122009-01-05 21:25:51 -05003138 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003139 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003140 leaf = path->nodes[0];
3141 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3142 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003143 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003144
Li Zefan33345d012011-04-20 10:31:50 +08003145 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003146 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003147
Chris Mason85e21ba2008-01-29 15:11:36 -05003148 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003149 break;
3150
Chris Mason5f39d392007-10-15 16:14:19 -04003151 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003152 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003153 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003154 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003155 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003156 encoding = btrfs_file_extent_compression(leaf, fi);
3157 encoding |= btrfs_file_extent_encryption(leaf, fi);
3158 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3159
Chris Mason179e29e2007-11-01 11:28:41 -04003160 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003161 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003162 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003163 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003164 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003165 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003166 }
Yan008630c2007-11-07 13:31:09 -05003167 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003168 }
Yan, Zheng80825102009-11-12 09:35:36 +00003169 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003170 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003171 } else {
3172 if (item_end < new_size)
3173 break;
3174 if (found_key.offset >= new_size)
3175 del_item = 1;
3176 else
3177 del_item = 0;
3178 }
Chris Mason39279cc2007-06-12 06:35:45 -04003179 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003180 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003181 if (found_type != BTRFS_EXTENT_DATA_KEY)
3182 goto delete;
3183
3184 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003185 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003186 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003187 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003188 u64 orig_num_bytes =
3189 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003190 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003191 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003192 extent_num_bytes = extent_num_bytes &
3193 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003194 btrfs_set_file_extent_num_bytes(leaf, fi,
3195 extent_num_bytes);
3196 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003197 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003198 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003199 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003200 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003201 } else {
Chris Masondb945352007-10-15 16:15:53 -04003202 extent_num_bytes =
3203 btrfs_file_extent_disk_num_bytes(leaf,
3204 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003205 extent_offset = found_key.offset -
3206 btrfs_file_extent_offset(leaf, fi);
3207
Chris Mason39279cc2007-06-12 06:35:45 -04003208 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003209 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003210 if (extent_start != 0) {
3211 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003212 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003213 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003214 }
3215 }
Chris Mason90692182008-02-08 13:49:28 -05003216 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003217 /*
3218 * we can't truncate inline items that have had
3219 * special encodings
3220 */
3221 if (!del_item &&
3222 btrfs_file_extent_compression(leaf, fi) == 0 &&
3223 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3224 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003225 u32 size = new_size - found_key.offset;
3226
3227 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003228 inode_sub_bytes(inode, item_end + 1 -
3229 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003230 }
3231 size =
3232 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003233 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003234 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003235 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003236 inode_sub_bytes(inode, item_end + 1 -
3237 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003238 }
Chris Mason39279cc2007-06-12 06:35:45 -04003239 }
Chris Mason179e29e2007-11-01 11:28:41 -04003240delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003241 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003242 if (!pending_del_nr) {
3243 /* no pending yet, add ourselves */
3244 pending_del_slot = path->slots[0];
3245 pending_del_nr = 1;
3246 } else if (pending_del_nr &&
3247 path->slots[0] + 1 == pending_del_slot) {
3248 /* hop on the pending chunk */
3249 pending_del_nr++;
3250 pending_del_slot = path->slots[0];
3251 } else {
Chris Masond3977122009-01-05 21:25:51 -05003252 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003253 }
Chris Mason39279cc2007-06-12 06:35:45 -04003254 } else {
3255 break;
3256 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003257 if (found_extent && (root->ref_cows ||
3258 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003259 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003260 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003261 extent_num_bytes, 0,
3262 btrfs_header_owner(leaf),
Li Zefan33345d012011-04-20 10:31:50 +08003263 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003264 BUG_ON(ret);
3265 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003266
Yan, Zheng80825102009-11-12 09:35:36 +00003267 if (found_type == BTRFS_INODE_ITEM_KEY)
3268 break;
3269
3270 if (path->slots[0] == 0 ||
3271 path->slots[0] != pending_del_slot) {
Li Zefan82d59022011-04-20 10:33:24 +08003272 if (root->ref_cows &&
3273 BTRFS_I(inode)->location.objectid !=
3274 BTRFS_FREE_INO_OBJECTID) {
Yan, Zheng80825102009-11-12 09:35:36 +00003275 err = -EAGAIN;
3276 goto out;
3277 }
3278 if (pending_del_nr) {
3279 ret = btrfs_del_items(trans, root, path,
3280 pending_del_slot,
3281 pending_del_nr);
3282 BUG_ON(ret);
3283 pending_del_nr = 0;
3284 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003285 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003286 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003287 } else {
3288 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003289 }
Chris Mason39279cc2007-06-12 06:35:45 -04003290 }
Yan, Zheng80825102009-11-12 09:35:36 +00003291out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003292 if (pending_del_nr) {
3293 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3294 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003295 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003296 }
Chris Mason39279cc2007-06-12 06:35:45 -04003297 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003298 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003299}
3300
Chris Masona52d9a82007-08-27 16:49:44 -04003301/*
3302 * taken from block_truncate_page, but does cow as it zeros out
3303 * any bytes left in the last page in the file.
3304 */
3305static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3306{
3307 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003308 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003309 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3310 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003311 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003312 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003313 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003314 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3315 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3316 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003317 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003318 int ret = 0;
3319 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003320 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003321
3322 if ((offset & (blocksize - 1)) == 0)
3323 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003324 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003325 if (ret)
3326 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003327
3328 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003329again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003330 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003331 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003332 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003333 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003334 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003335
3336 page_start = page_offset(page);
3337 page_end = page_start + PAGE_CACHE_SIZE - 1;
3338
Chris Masona52d9a82007-08-27 16:49:44 -04003339 if (!PageUptodate(page)) {
3340 ret = btrfs_readpage(NULL, page);
3341 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003342 if (page->mapping != mapping) {
3343 unlock_page(page);
3344 page_cache_release(page);
3345 goto again;
3346 }
Chris Masona52d9a82007-08-27 16:49:44 -04003347 if (!PageUptodate(page)) {
3348 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003349 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003350 }
3351 }
Chris Mason211c17f2008-05-15 09:13:45 -04003352 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003353
Josef Bacik2ac55d42010-02-03 19:33:23 +00003354 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3355 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003356 set_page_extent_mapped(page);
3357
3358 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3359 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003360 unlock_extent_cached(io_tree, page_start, page_end,
3361 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003362 unlock_page(page);
3363 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003364 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003365 btrfs_put_ordered_extent(ordered);
3366 goto again;
3367 }
3368
Josef Bacik2ac55d42010-02-03 19:33:23 +00003369 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003370 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003371 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003372
Josef Bacik2ac55d42010-02-03 19:33:23 +00003373 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3374 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003375 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003376 unlock_extent_cached(io_tree, page_start, page_end,
3377 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003378 goto out_unlock;
3379 }
3380
Chris Masone6dcd2d2008-07-17 12:53:50 -04003381 ret = 0;
3382 if (offset != PAGE_CACHE_SIZE) {
3383 kaddr = kmap(page);
3384 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3385 flush_dcache_page(page);
3386 kunmap(page);
3387 }
Chris Mason247e7432008-07-17 12:53:51 -04003388 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003389 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003390 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3391 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003392
Chris Mason89642222008-07-24 09:41:53 -04003393out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003394 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003395 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003396 unlock_page(page);
3397 page_cache_release(page);
3398out:
3399 return ret;
3400}
3401
Josef Bacik695a0d02011-03-04 15:46:53 -05003402/*
3403 * This function puts in dummy file extents for the area we're creating a hole
3404 * for. So if we are truncating this file to a larger size we need to insert
3405 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3406 * the range between oldsize and size
3407 */
Josef Bacika41ad392011-01-31 15:30:16 -05003408int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003409{
3410 struct btrfs_trans_handle *trans;
3411 struct btrfs_root *root = BTRFS_I(inode)->root;
3412 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003413 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003414 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003415 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003416 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003417 u64 block_end = (size + mask) & ~mask;
3418 u64 last_byte;
3419 u64 cur_offset;
3420 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003421 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003422
3423 if (size <= hole_start)
3424 return 0;
3425
Yan Zheng9036c102008-10-30 14:19:41 -04003426 while (1) {
3427 struct btrfs_ordered_extent *ordered;
3428 btrfs_wait_ordered_range(inode, hole_start,
3429 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003430 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3431 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003432 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3433 if (!ordered)
3434 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003435 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3436 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003437 btrfs_put_ordered_extent(ordered);
3438 }
3439
Yan Zheng9036c102008-10-30 14:19:41 -04003440 cur_offset = hole_start;
3441 while (1) {
3442 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3443 block_end - cur_offset, 0);
David Sterbac7040052011-04-19 18:00:01 +02003444 BUG_ON(IS_ERR_OR_NULL(em));
Yan Zheng9036c102008-10-30 14:19:41 -04003445 last_byte = min(extent_map_end(em), block_end);
3446 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003447 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003448 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003449 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003450
Yan, Zhenga22285a2010-05-16 10:48:46 -04003451 trans = btrfs_start_transaction(root, 2);
3452 if (IS_ERR(trans)) {
3453 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003454 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003455 }
Yan, Zheng80825102009-11-12 09:35:36 +00003456
3457 err = btrfs_drop_extents(trans, inode, cur_offset,
3458 cur_offset + hole_size,
3459 &hint_byte, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003460 if (err) {
3461 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003462 break;
Miao Xie5b397372011-09-11 10:52:24 -04003463 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003464
Yan Zheng9036c102008-10-30 14:19:41 -04003465 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003466 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003467 0, hole_size, 0, hole_size,
3468 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003469 if (err) {
3470 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003471 break;
Miao Xie5b397372011-09-11 10:52:24 -04003472 }
Yan, Zheng80825102009-11-12 09:35:36 +00003473
Yan Zheng9036c102008-10-30 14:19:41 -04003474 btrfs_drop_extent_cache(inode, hole_start,
3475 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003476
3477 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003478 }
3479 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003480 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003481 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003482 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003483 break;
3484 }
3485
Yan, Zhenga22285a2010-05-16 10:48:46 -04003486 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003487 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3488 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003489 return err;
3490}
3491
Josef Bacika41ad392011-01-31 15:30:16 -05003492static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003493{
Josef Bacika41ad392011-01-31 15:30:16 -05003494 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003495 int ret;
3496
Josef Bacika41ad392011-01-31 15:30:16 -05003497 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003498 return 0;
3499
Josef Bacika41ad392011-01-31 15:30:16 -05003500 if (newsize > oldsize) {
3501 i_size_write(inode, newsize);
3502 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3503 truncate_pagecache(inode, oldsize, newsize);
3504 ret = btrfs_cont_expand(inode, oldsize, newsize);
Yan, Zheng80825102009-11-12 09:35:36 +00003505 if (ret) {
Josef Bacika41ad392011-01-31 15:30:16 -05003506 btrfs_setsize(inode, oldsize);
Yan, Zheng80825102009-11-12 09:35:36 +00003507 return ret;
3508 }
3509
Josef Bacik930f0282011-03-04 14:41:41 -05003510 mark_inode_dirty(inode);
Josef Bacika41ad392011-01-31 15:30:16 -05003511 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003512
Josef Bacika41ad392011-01-31 15:30:16 -05003513 /*
3514 * We're truncating a file that used to have good data down to
3515 * zero. Make sure it gets into the ordered flush list so that
3516 * any new writes get down to disk quickly.
3517 */
3518 if (newsize == 0)
3519 BTRFS_I(inode)->ordered_data_close = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003520
Josef Bacika41ad392011-01-31 15:30:16 -05003521 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3522 truncate_setsize(inode, newsize);
3523 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003524 }
3525
Josef Bacika41ad392011-01-31 15:30:16 -05003526 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003527}
3528
Chris Mason39279cc2007-06-12 06:35:45 -04003529static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3530{
3531 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003532 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003533 int err;
3534
Li Zefanb83cc962010-12-20 16:04:08 +08003535 if (btrfs_root_readonly(root))
3536 return -EROFS;
3537
Chris Mason39279cc2007-06-12 06:35:45 -04003538 err = inode_change_ok(inode, attr);
3539 if (err)
3540 return err;
3541
Chris Mason5a3f23d2009-03-31 13:27:11 -04003542 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003543 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003544 if (err)
3545 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003546 }
Yan Zheng9036c102008-10-30 14:19:41 -04003547
Christoph Hellwig10257742010-06-04 11:30:02 +02003548 if (attr->ia_valid) {
3549 setattr_copy(inode, attr);
3550 mark_inode_dirty(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003551
Christoph Hellwig10257742010-06-04 11:30:02 +02003552 if (attr->ia_valid & ATTR_MODE)
3553 err = btrfs_acl_chmod(inode);
3554 }
3555
Chris Mason39279cc2007-06-12 06:35:45 -04003556 return err;
3557}
Chris Mason61295eb2008-01-14 16:24:38 -05003558
Al Virobd555972010-06-07 11:35:40 -04003559void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003560{
3561 struct btrfs_trans_handle *trans;
3562 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003563 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003564 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003565 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003566 int ret;
3567
liubo1abe9b82011-03-24 11:18:59 +00003568 trace_btrfs_inode_evict(inode);
3569
Chris Mason39279cc2007-06-12 06:35:45 -04003570 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003571 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Chris Mason2cf85722011-07-26 15:35:09 -04003572 btrfs_is_free_space_inode(root, inode)))
Al Virobd555972010-06-07 11:35:40 -04003573 goto no_delete;
3574
Chris Mason39279cc2007-06-12 06:35:45 -04003575 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003576 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003577 goto no_delete;
3578 }
Al Virobd555972010-06-07 11:35:40 -04003579 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003580 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003581
Yan, Zhengc71bf092009-11-12 09:34:40 +00003582 if (root->fs_info->log_root_recovering) {
3583 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3584 goto no_delete;
3585 }
3586
Yan, Zheng76dda932009-09-21 16:00:26 -04003587 if (inode->i_nlink > 0) {
3588 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3589 goto no_delete;
3590 }
3591
Josef Bacik4289a662011-08-05 13:22:24 -04003592 rsv = btrfs_alloc_block_rsv(root);
3593 if (!rsv) {
3594 btrfs_orphan_del(NULL, inode);
3595 goto no_delete;
3596 }
Josef Bacik4a338542011-08-29 11:01:31 -04003597 rsv->size = min_size;
Josef Bacik726c35f2011-09-26 15:46:06 -04003598 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003599
Chris Masondbe674a2008-07-17 12:54:05 -04003600 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003601
Josef Bacik4289a662011-08-05 13:22:24 -04003602 /*
3603 * This is a bit simpler than btrfs_truncate since
3604 *
3605 * 1) We've already reserved our space for our orphan item in the
3606 * unlink.
3607 * 2) We're going to delete the inode item, so we don't need to update
3608 * it at all.
3609 *
3610 * So we just need to reserve some slack space in case we add bytes when
3611 * doing the truncate.
3612 */
Yan, Zheng80825102009-11-12 09:35:36 +00003613 while (1) {
Josef Bacik36ba0222011-10-18 12:15:48 -04003614 ret = btrfs_block_rsv_refill(root, rsv, min_size);
Josef Bacik726c35f2011-09-26 15:46:06 -04003615
3616 /*
3617 * Try and steal from the global reserve since we will
3618 * likely not use this space anyway, we want to try as
3619 * hard as possible to get this to work.
3620 */
3621 if (ret)
3622 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3623
Yan, Zhengd68fc572010-05-16 10:49:58 -04003624 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003625 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003626 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003627 btrfs_orphan_del(NULL, inode);
3628 btrfs_free_block_rsv(root, rsv);
3629 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003630 }
3631
Josef Bacik4289a662011-08-05 13:22:24 -04003632 trans = btrfs_start_transaction(root, 0);
3633 if (IS_ERR(trans)) {
3634 btrfs_orphan_del(NULL, inode);
3635 btrfs_free_block_rsv(root, rsv);
3636 goto no_delete;
3637 }
3638
3639 trans->block_rsv = rsv;
3640
Yan, Zhengd68fc572010-05-16 10:49:58 -04003641 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003642 if (ret != -EAGAIN)
3643 break;
3644
3645 nr = trans->blocks_used;
3646 btrfs_end_transaction(trans, root);
3647 trans = NULL;
3648 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003649 }
3650
Josef Bacik4289a662011-08-05 13:22:24 -04003651 btrfs_free_block_rsv(root, rsv);
3652
Yan, Zheng80825102009-11-12 09:35:36 +00003653 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003654 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003655 ret = btrfs_orphan_del(trans, inode);
3656 BUG_ON(ret);
3657 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003658
Josef Bacik4289a662011-08-05 13:22:24 -04003659 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003660 if (!(root == root->fs_info->tree_root ||
3661 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003662 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003663
Chris Masond3c2fdc2007-09-17 10:58:06 -04003664 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003665 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003666 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003667no_delete:
Al Virobd555972010-06-07 11:35:40 -04003668 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003669 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003670}
3671
3672/*
3673 * this returns the key found in the dir entry in the location pointer.
3674 * If no dir entries were found, location->objectid is 0.
3675 */
3676static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3677 struct btrfs_key *location)
3678{
3679 const char *name = dentry->d_name.name;
3680 int namelen = dentry->d_name.len;
3681 struct btrfs_dir_item *di;
3682 struct btrfs_path *path;
3683 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003684 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003685
3686 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003687 if (!path)
3688 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003689
Li Zefan33345d012011-04-20 10:31:50 +08003690 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003691 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003692 if (IS_ERR(di))
3693 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003694
David Sterbac7040052011-04-19 18:00:01 +02003695 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003696 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003697
Chris Mason5f39d392007-10-15 16:14:19 -04003698 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003699out:
Chris Mason39279cc2007-06-12 06:35:45 -04003700 btrfs_free_path(path);
3701 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003702out_err:
3703 location->objectid = 0;
3704 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003705}
3706
3707/*
3708 * when we hit a tree root in a directory, the btrfs part of the inode
3709 * needs to be changed to reflect the root directory of the tree root. This
3710 * is kind of like crossing a mount point.
3711 */
3712static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003713 struct inode *dir,
3714 struct dentry *dentry,
3715 struct btrfs_key *location,
3716 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003717{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003718 struct btrfs_path *path;
3719 struct btrfs_root *new_root;
3720 struct btrfs_root_ref *ref;
3721 struct extent_buffer *leaf;
3722 int ret;
3723 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003724
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003725 path = btrfs_alloc_path();
3726 if (!path) {
3727 err = -ENOMEM;
3728 goto out;
3729 }
Chris Mason39279cc2007-06-12 06:35:45 -04003730
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003731 err = -ENOENT;
3732 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3733 BTRFS_I(dir)->root->root_key.objectid,
3734 location->objectid);
3735 if (ret) {
3736 if (ret < 0)
3737 err = ret;
3738 goto out;
3739 }
Chris Mason39279cc2007-06-12 06:35:45 -04003740
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003741 leaf = path->nodes[0];
3742 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003743 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003744 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3745 goto out;
3746
3747 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3748 (unsigned long)(ref + 1),
3749 dentry->d_name.len);
3750 if (ret)
3751 goto out;
3752
David Sterbab3b4aa72011-04-21 01:20:15 +02003753 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003754
3755 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3756 if (IS_ERR(new_root)) {
3757 err = PTR_ERR(new_root);
3758 goto out;
3759 }
3760
3761 if (btrfs_root_refs(&new_root->root_item) == 0) {
3762 err = -ENOENT;
3763 goto out;
3764 }
3765
3766 *sub_root = new_root;
3767 location->objectid = btrfs_root_dirid(&new_root->root_item);
3768 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003769 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003770 err = 0;
3771out:
3772 btrfs_free_path(path);
3773 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003774}
3775
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003776static void inode_tree_add(struct inode *inode)
3777{
3778 struct btrfs_root *root = BTRFS_I(inode)->root;
3779 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003780 struct rb_node **p;
3781 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08003782 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003783again:
3784 p = &root->inode_tree.rb_node;
3785 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003786
Al Viro1d3382c2010-10-23 15:19:20 -04003787 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003788 return;
3789
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003790 spin_lock(&root->inode_lock);
3791 while (*p) {
3792 parent = *p;
3793 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3794
Li Zefan33345d012011-04-20 10:31:50 +08003795 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003796 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003797 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003798 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003799 else {
3800 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003801 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003802 rb_erase(parent, &root->inode_tree);
3803 RB_CLEAR_NODE(parent);
3804 spin_unlock(&root->inode_lock);
3805 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003806 }
3807 }
3808 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3809 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3810 spin_unlock(&root->inode_lock);
3811}
3812
3813static void inode_tree_del(struct inode *inode)
3814{
3815 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003816 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003817
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003818 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003819 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003820 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003821 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003822 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003823 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003824 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003825
Josef Bacik0af3d002010-06-21 14:48:16 -04003826 /*
3827 * Free space cache has inodes in the tree root, but the tree root has a
3828 * root_refs of 0, so this could end up dropping the tree root as a
3829 * snapshot, so we need the extra !root->fs_info->tree_root check to
3830 * make sure we don't drop it.
3831 */
3832 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3833 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003834 synchronize_srcu(&root->fs_info->subvol_srcu);
3835 spin_lock(&root->inode_lock);
3836 empty = RB_EMPTY_ROOT(&root->inode_tree);
3837 spin_unlock(&root->inode_lock);
3838 if (empty)
3839 btrfs_add_dead_root(root);
3840 }
3841}
3842
3843int btrfs_invalidate_inodes(struct btrfs_root *root)
3844{
3845 struct rb_node *node;
3846 struct rb_node *prev;
3847 struct btrfs_inode *entry;
3848 struct inode *inode;
3849 u64 objectid = 0;
3850
3851 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3852
3853 spin_lock(&root->inode_lock);
3854again:
3855 node = root->inode_tree.rb_node;
3856 prev = NULL;
3857 while (node) {
3858 prev = node;
3859 entry = rb_entry(node, struct btrfs_inode, rb_node);
3860
Li Zefan33345d012011-04-20 10:31:50 +08003861 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003862 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003863 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003864 node = node->rb_right;
3865 else
3866 break;
3867 }
3868 if (!node) {
3869 while (prev) {
3870 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08003871 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003872 node = prev;
3873 break;
3874 }
3875 prev = rb_next(prev);
3876 }
3877 }
3878 while (node) {
3879 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08003880 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04003881 inode = igrab(&entry->vfs_inode);
3882 if (inode) {
3883 spin_unlock(&root->inode_lock);
3884 if (atomic_read(&inode->i_count) > 1)
3885 d_prune_aliases(inode);
3886 /*
Al Viro45321ac2010-06-07 13:43:19 -04003887 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04003888 * the inode cache when its usage count
3889 * hits zero.
3890 */
3891 iput(inode);
3892 cond_resched();
3893 spin_lock(&root->inode_lock);
3894 goto again;
3895 }
3896
3897 if (cond_resched_lock(&root->inode_lock))
3898 goto again;
3899
3900 node = rb_next(node);
3901 }
3902 spin_unlock(&root->inode_lock);
3903 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003904}
3905
Chris Masone02119d2008-09-05 16:13:11 -04003906static int btrfs_init_locked_inode(struct inode *inode, void *p)
3907{
3908 struct btrfs_iget_args *args = p;
3909 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04003910 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003911 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003912 return 0;
3913}
3914
3915static int btrfs_find_actor(struct inode *inode, void *opaque)
3916{
3917 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08003918 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05003919 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003920}
3921
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003922static struct inode *btrfs_iget_locked(struct super_block *s,
3923 u64 objectid,
3924 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003925{
3926 struct inode *inode;
3927 struct btrfs_iget_args args;
3928 args.ino = objectid;
3929 args.root = root;
3930
3931 inode = iget5_locked(s, objectid, btrfs_find_actor,
3932 btrfs_init_locked_inode,
3933 (void *)&args);
3934 return inode;
3935}
3936
Balaji Rao1a54ef82008-07-21 02:01:04 +05303937/* Get an inode object given its location and corresponding root.
3938 * Returns in *is_new if the inode was read from disk
3939 */
3940struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00003941 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303942{
3943 struct inode *inode;
3944
3945 inode = btrfs_iget_locked(s, location->objectid, root);
3946 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003947 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303948
3949 if (inode->i_state & I_NEW) {
3950 BTRFS_I(inode)->root = root;
3951 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3952 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07003953 if (!is_bad_inode(inode)) {
3954 inode_tree_add(inode);
3955 unlock_new_inode(inode);
3956 if (new)
3957 *new = 1;
3958 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04003959 unlock_new_inode(inode);
3960 iput(inode);
3961 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07003962 }
3963 }
3964
Balaji Rao1a54ef82008-07-21 02:01:04 +05303965 return inode;
3966}
3967
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003968static struct inode *new_simple_dir(struct super_block *s,
3969 struct btrfs_key *key,
3970 struct btrfs_root *root)
3971{
3972 struct inode *inode = new_inode(s);
3973
3974 if (!inode)
3975 return ERR_PTR(-ENOMEM);
3976
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003977 BTRFS_I(inode)->root = root;
3978 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3979 BTRFS_I(inode)->dummy_inode = 1;
3980
3981 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3982 inode->i_op = &simple_dir_inode_operations;
3983 inode->i_fop = &simple_dir_operations;
3984 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3985 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3986
3987 return inode;
3988}
3989
Chris Mason3de45862008-11-17 21:02:50 -05003990struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003991{
Chris Masond3977122009-01-05 21:25:51 -05003992 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003993 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003994 struct btrfs_root *sub_root = root;
3995 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003996 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04003997 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003998
3999 if (dentry->d_name.len > BTRFS_NAME_LEN)
4000 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004001
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004002 if (unlikely(d_need_lookup(dentry))) {
4003 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4004 kfree(dentry->d_fsdata);
4005 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004006 /* This thing is hashed, drop it for now */
4007 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004008 } else {
4009 ret = btrfs_inode_by_name(dir, dentry, &location);
4010 }
Chris Mason5f39d392007-10-15 16:14:19 -04004011
Chris Mason39279cc2007-06-12 06:35:45 -04004012 if (ret < 0)
4013 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004014
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004015 if (location.objectid == 0)
4016 return NULL;
4017
4018 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004019 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004020 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004021 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004022
4023 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4024
Yan, Zheng76dda932009-09-21 16:00:26 -04004025 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004026 ret = fixup_tree_root_location(root, dir, dentry,
4027 &location, &sub_root);
4028 if (ret < 0) {
4029 if (ret != -ENOENT)
4030 inode = ERR_PTR(ret);
4031 else
4032 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4033 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004034 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004035 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004036 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4037
Julia Lawall34d19ba2011-01-24 19:55:19 +00004038 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004039 down_read(&root->fs_info->cleanup_work_sem);
4040 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004041 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004042 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004043 if (ret)
4044 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004045 }
4046
Chris Mason3de45862008-11-17 21:02:50 -05004047 return inode;
4048}
4049
Nick Pigginfe15ce42011-01-07 17:49:23 +11004050static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004051{
4052 struct btrfs_root *root;
4053
Yan, Zhengefefb142009-10-09 09:25:16 -04004054 if (!dentry->d_inode && !IS_ROOT(dentry))
4055 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004056
Yan, Zhengefefb142009-10-09 09:25:16 -04004057 if (dentry->d_inode) {
4058 root = BTRFS_I(dentry->d_inode)->root;
4059 if (btrfs_root_refs(&root->root_item) == 0)
4060 return 1;
4061 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004062 return 0;
4063}
4064
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004065static void btrfs_dentry_release(struct dentry *dentry)
4066{
4067 if (dentry->d_fsdata)
4068 kfree(dentry->d_fsdata);
4069}
4070
Chris Mason3de45862008-11-17 21:02:50 -05004071static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4072 struct nameidata *nd)
4073{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004074 struct dentry *ret;
4075
4076 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4077 if (unlikely(d_need_lookup(dentry))) {
4078 spin_lock(&dentry->d_lock);
4079 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4080 spin_unlock(&dentry->d_lock);
4081 }
4082 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004083}
4084
Miao Xie16cdcec2011-04-22 18:12:22 +08004085unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004086 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4087};
4088
David Woodhousecbdf5a22008-08-06 19:42:33 +01004089static int btrfs_real_readdir(struct file *filp, void *dirent,
4090 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004091{
Chris Mason6da6aba2007-12-18 16:15:09 -05004092 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004093 struct btrfs_root *root = BTRFS_I(inode)->root;
4094 struct btrfs_item *item;
4095 struct btrfs_dir_item *di;
4096 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004097 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004098 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004099 struct list_head ins_list;
4100 struct list_head del_list;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004101 struct qstr q;
Chris Mason39279cc2007-06-12 06:35:45 -04004102 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004103 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004104 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004105 unsigned char d_type;
4106 int over = 0;
4107 u32 di_cur;
4108 u32 di_total;
4109 u32 di_len;
4110 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004111 char tmp_name[32];
4112 char *name_ptr;
4113 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004114 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004115
4116 /* FIXME, use a real flag for deciding about the key type */
4117 if (root->fs_info->tree_root == root)
4118 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004119
Chris Mason39544012007-12-12 14:38:19 -05004120 /* special case for "." */
4121 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004122 over = filldir(dirent, ".", 1,
4123 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004124 if (over)
4125 return 0;
4126 filp->f_pos = 1;
4127 }
Chris Mason39544012007-12-12 14:38:19 -05004128 /* special case for .., just use the back ref */
4129 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004130 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004131 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004132 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004133 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004134 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004135 filp->f_pos = 2;
4136 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004137 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004138 if (!path)
4139 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004140
Josef Bacik026fd312011-05-13 10:32:11 -04004141 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004142
Miao Xie16cdcec2011-04-22 18:12:22 +08004143 if (key_type == BTRFS_DIR_INDEX_KEY) {
4144 INIT_LIST_HEAD(&ins_list);
4145 INIT_LIST_HEAD(&del_list);
4146 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4147 }
4148
Chris Mason39279cc2007-06-12 06:35:45 -04004149 btrfs_set_key_type(&key, key_type);
4150 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004151 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004152
Chris Mason39279cc2007-06-12 06:35:45 -04004153 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4154 if (ret < 0)
4155 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004156
4157 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004158 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004159 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004160 if (slot >= btrfs_header_nritems(leaf)) {
4161 ret = btrfs_next_leaf(root, path);
4162 if (ret < 0)
4163 goto err;
4164 else if (ret > 0)
4165 break;
4166 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004167 }
Chris Mason3de45862008-11-17 21:02:50 -05004168
Chris Mason5f39d392007-10-15 16:14:19 -04004169 item = btrfs_item_nr(leaf, slot);
4170 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4171
4172 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004173 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004174 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004175 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004176 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004177 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004178 if (key_type == BTRFS_DIR_INDEX_KEY &&
4179 btrfs_should_delete_dir_index(&del_list,
4180 found_key.offset))
4181 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004182
4183 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004184 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004185
Chris Mason39279cc2007-06-12 06:35:45 -04004186 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4187 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004188 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004189
4190 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004191 struct btrfs_key location;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004192 struct dentry *tmp;
Chris Mason5f39d392007-10-15 16:14:19 -04004193
Josef Bacik22a94d42011-03-16 16:47:17 -04004194 if (verify_dir_item(root, leaf, di))
4195 break;
4196
Chris Mason5f39d392007-10-15 16:14:19 -04004197 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004198 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004199 name_ptr = tmp_name;
4200 } else {
4201 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004202 if (!name_ptr) {
4203 ret = -ENOMEM;
4204 goto err;
4205 }
Chris Mason5f39d392007-10-15 16:14:19 -04004206 }
4207 read_extent_buffer(leaf, name_ptr,
4208 (unsigned long)(di + 1), name_len);
4209
4210 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4211 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004212
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004213 q.name = name_ptr;
4214 q.len = name_len;
4215 q.hash = full_name_hash(q.name, q.len);
4216 tmp = d_lookup(filp->f_dentry, &q);
4217 if (!tmp) {
4218 struct btrfs_key *newkey;
4219
4220 newkey = kzalloc(sizeof(struct btrfs_key),
4221 GFP_NOFS);
4222 if (!newkey)
4223 goto no_dentry;
4224 tmp = d_alloc(filp->f_dentry, &q);
4225 if (!tmp) {
4226 kfree(newkey);
4227 dput(tmp);
4228 goto no_dentry;
4229 }
4230 memcpy(newkey, &location,
4231 sizeof(struct btrfs_key));
4232 tmp->d_fsdata = newkey;
4233 tmp->d_flags |= DCACHE_NEED_LOOKUP;
4234 d_rehash(tmp);
4235 dput(tmp);
4236 } else {
4237 dput(tmp);
4238 }
4239no_dentry:
Chris Mason3de45862008-11-17 21:02:50 -05004240 /* is this a reference to our own snapshot? If so
4241 * skip it
4242 */
4243 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4244 location.objectid == root->root_key.objectid) {
4245 over = 0;
4246 goto skip;
4247 }
Chris Mason5f39d392007-10-15 16:14:19 -04004248 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004249 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004250 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004251
Chris Mason3de45862008-11-17 21:02:50 -05004252skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004253 if (name_ptr != tmp_name)
4254 kfree(name_ptr);
4255
Chris Mason39279cc2007-06-12 06:35:45 -04004256 if (over)
4257 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004258 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004259 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004260 di_cur += di_len;
4261 di = (struct btrfs_dir_item *)((char *)di + di_len);
4262 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004263next:
4264 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004265 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004266
Miao Xie16cdcec2011-04-22 18:12:22 +08004267 if (key_type == BTRFS_DIR_INDEX_KEY) {
4268 if (is_curr)
4269 filp->f_pos++;
4270 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4271 &ins_list);
4272 if (ret)
4273 goto nopos;
4274 }
4275
David Woodhouse49593bf2008-08-17 17:08:36 +01004276 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004277 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004278 /*
4279 * 32-bit glibc will use getdents64, but then strtol -
4280 * so the last number we can serve is this.
4281 */
4282 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004283 else
4284 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004285nopos:
4286 ret = 0;
4287err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004288 if (key_type == BTRFS_DIR_INDEX_KEY)
4289 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004290 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004291 return ret;
4292}
4293
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004294int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004295{
4296 struct btrfs_root *root = BTRFS_I(inode)->root;
4297 struct btrfs_trans_handle *trans;
4298 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004299 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004300
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004301 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004302 return 0;
4303
Chris Mason2cf85722011-07-26 15:35:09 -04004304 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(root, inode))
Li Zefan82d59022011-04-20 10:33:24 +08004305 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004306
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004307 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004308 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004309 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004310 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004311 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004312 if (IS_ERR(trans))
4313 return PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04004314 if (nolock)
4315 ret = btrfs_end_transaction_nolock(trans, root);
4316 else
4317 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004318 }
4319 return ret;
4320}
4321
4322/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004323 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004324 * inode changes. But, it is most likely to find the inode in cache.
4325 * FIXME, needs more benchmarking...there are no reasons other than performance
4326 * to keep or drop this code.
4327 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04004328void btrfs_dirty_inode(struct inode *inode, int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04004329{
4330 struct btrfs_root *root = BTRFS_I(inode)->root;
4331 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004332 int ret;
4333
4334 if (BTRFS_I(inode)->dummy_inode)
4335 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004336
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004337 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004338 BUG_ON(IS_ERR(trans));
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004339
4340 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004341 if (ret && ret == -ENOSPC) {
4342 /* whoops, lets try again with the full transaction */
4343 btrfs_end_transaction(trans, root);
4344 trans = btrfs_start_transaction(root, 1);
Chris Mason9aeead72010-05-27 10:23:00 -04004345 if (IS_ERR(trans)) {
David Sterba7a36dde2011-05-06 15:33:15 +02004346 printk_ratelimited(KERN_ERR "btrfs: fail to "
Li Zefan33345d012011-04-20 10:31:50 +08004347 "dirty inode %llu error %ld\n",
4348 (unsigned long long)btrfs_ino(inode),
4349 PTR_ERR(trans));
Chris Mason9aeead72010-05-27 10:23:00 -04004350 return;
4351 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004352
Chris Mason94b60442010-05-26 11:02:00 -04004353 ret = btrfs_update_inode(trans, root, inode);
4354 if (ret) {
David Sterba7a36dde2011-05-06 15:33:15 +02004355 printk_ratelimited(KERN_ERR "btrfs: fail to "
Li Zefan33345d012011-04-20 10:31:50 +08004356 "dirty inode %llu error %d\n",
4357 (unsigned long long)btrfs_ino(inode),
4358 ret);
Chris Mason94b60442010-05-26 11:02:00 -04004359 }
4360 }
Chris Mason39279cc2007-06-12 06:35:45 -04004361 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004362 if (BTRFS_I(inode)->delayed_node)
4363 btrfs_balance_delayed_items(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004364}
4365
Chris Masond352ac62008-09-29 15:18:18 -04004366/*
4367 * find the highest existing sequence number in a directory
4368 * and then set the in-memory index_cnt variable to reflect
4369 * free sequence numbers
4370 */
Josef Bacikaec74772008-07-24 12:12:38 -04004371static int btrfs_set_inode_index_count(struct inode *inode)
4372{
4373 struct btrfs_root *root = BTRFS_I(inode)->root;
4374 struct btrfs_key key, found_key;
4375 struct btrfs_path *path;
4376 struct extent_buffer *leaf;
4377 int ret;
4378
Li Zefan33345d012011-04-20 10:31:50 +08004379 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004380 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4381 key.offset = (u64)-1;
4382
4383 path = btrfs_alloc_path();
4384 if (!path)
4385 return -ENOMEM;
4386
4387 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4388 if (ret < 0)
4389 goto out;
4390 /* FIXME: we should be able to handle this */
4391 if (ret == 0)
4392 goto out;
4393 ret = 0;
4394
4395 /*
4396 * MAGIC NUMBER EXPLANATION:
4397 * since we search a directory based on f_pos we have to start at 2
4398 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4399 * else has to start at 2
4400 */
4401 if (path->slots[0] == 0) {
4402 BTRFS_I(inode)->index_cnt = 2;
4403 goto out;
4404 }
4405
4406 path->slots[0]--;
4407
4408 leaf = path->nodes[0];
4409 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4410
Li Zefan33345d012011-04-20 10:31:50 +08004411 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004412 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4413 BTRFS_I(inode)->index_cnt = 2;
4414 goto out;
4415 }
4416
4417 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4418out:
4419 btrfs_free_path(path);
4420 return ret;
4421}
4422
Chris Masond352ac62008-09-29 15:18:18 -04004423/*
4424 * helper to find a free sequence number in a given directory. This current
4425 * code is very simple, later versions will do smarter things in the btree
4426 */
Chris Mason3de45862008-11-17 21:02:50 -05004427int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004428{
4429 int ret = 0;
4430
4431 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004432 ret = btrfs_inode_delayed_dir_index_count(dir);
4433 if (ret) {
4434 ret = btrfs_set_inode_index_count(dir);
4435 if (ret)
4436 return ret;
4437 }
Josef Bacikaec74772008-07-24 12:12:38 -04004438 }
4439
Chris Mason00e4e6b2008-08-05 11:18:09 -04004440 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004441 BTRFS_I(dir)->index_cnt++;
4442
4443 return ret;
4444}
4445
Chris Mason39279cc2007-06-12 06:35:45 -04004446static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4447 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004448 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004449 const char *name, int name_len,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004450 u64 ref_objectid, u64 objectid, int mode,
4451 u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004452{
4453 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004454 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004455 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004456 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004457 struct btrfs_inode_ref *ref;
4458 struct btrfs_key key[2];
4459 u32 sizes[2];
4460 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004461 int ret;
4462 int owner;
4463
Chris Mason5f39d392007-10-15 16:14:19 -04004464 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004465 if (!path)
4466 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004467
Chris Mason39279cc2007-06-12 06:35:45 -04004468 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004469 if (!inode) {
4470 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004471 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004472 }
Chris Mason39279cc2007-06-12 06:35:45 -04004473
Li Zefan581bb052011-04-20 10:06:11 +08004474 /*
4475 * we have to initialize this early, so we can reclaim the inode
4476 * number if we fail afterwards in this function.
4477 */
4478 inode->i_ino = objectid;
4479
Josef Bacikaec74772008-07-24 12:12:38 -04004480 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004481 trace_btrfs_inode_request(dir);
4482
Chris Mason3de45862008-11-17 21:02:50 -05004483 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004484 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004485 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004486 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004487 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004488 }
Josef Bacikaec74772008-07-24 12:12:38 -04004489 }
4490 /*
4491 * index_cnt is ignored for everything but a dir,
4492 * btrfs_get_inode_index_count has an explanation for the magic
4493 * number
4494 */
4495 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004496 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004497 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004498 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004499 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004500
Al Viro569254b2011-07-24 17:08:40 -04004501 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004502 owner = 0;
4503 else
4504 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004505
4506 key[0].objectid = objectid;
4507 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4508 key[0].offset = 0;
4509
4510 key[1].objectid = objectid;
4511 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4512 key[1].offset = ref_objectid;
4513
4514 sizes[0] = sizeof(struct btrfs_inode_item);
4515 sizes[1] = name_len + sizeof(*ref);
4516
Chris Masonb9473432009-03-13 11:00:37 -04004517 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004518 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4519 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004520 goto fail;
4521
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004522 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004523 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004524 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004525 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4526 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004527 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004528
4529 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4530 struct btrfs_inode_ref);
4531 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004532 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004533 ptr = (unsigned long)(ref + 1);
4534 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4535
Chris Mason5f39d392007-10-15 16:14:19 -04004536 btrfs_mark_buffer_dirty(path->nodes[0]);
4537 btrfs_free_path(path);
4538
Chris Mason39279cc2007-06-12 06:35:45 -04004539 location = &BTRFS_I(inode)->location;
4540 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004541 location->offset = 0;
4542 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4543
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004544 btrfs_inherit_iflags(inode, dir);
4545
Al Viro569254b2011-07-24 17:08:40 -04004546 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004547 if (btrfs_test_opt(root, NODATASUM))
4548 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004549 if (btrfs_test_opt(root, NODATACOW) ||
4550 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004551 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4552 }
4553
Chris Mason39279cc2007-06-12 06:35:45 -04004554 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004555 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004556
4557 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004558 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004559
Chris Mason39279cc2007-06-12 06:35:45 -04004560 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004561fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004562 if (dir)
4563 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004564 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004565 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004566 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004567}
4568
4569static inline u8 btrfs_inode_type(struct inode *inode)
4570{
4571 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4572}
4573
Chris Masond352ac62008-09-29 15:18:18 -04004574/*
4575 * utility function to add 'inode' into 'parent_inode' with
4576 * a give name and a given sequence number.
4577 * if 'add_backref' is true, also insert a backref from the
4578 * inode to the parent directory.
4579 */
Chris Masone02119d2008-09-05 16:13:11 -04004580int btrfs_add_link(struct btrfs_trans_handle *trans,
4581 struct inode *parent_inode, struct inode *inode,
4582 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004583{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004584 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004585 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004586 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004587 u64 ino = btrfs_ino(inode);
4588 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004589
Li Zefan33345d012011-04-20 10:31:50 +08004590 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004591 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4592 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004593 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004594 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4595 key.offset = 0;
4596 }
Chris Mason39279cc2007-06-12 06:35:45 -04004597
Li Zefan33345d012011-04-20 10:31:50 +08004598 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004599 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4600 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004601 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004602 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004603 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4604 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004605 }
4606
Chris Mason39279cc2007-06-12 06:35:45 -04004607 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004608 ret = btrfs_insert_dir_item(trans, root, name, name_len,
Miao Xie16cdcec2011-04-22 18:12:22 +08004609 parent_inode, &key,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004610 btrfs_inode_type(inode), index);
4611 BUG_ON(ret);
4612
Chris Masondbe674a2008-07-17 12:54:05 -04004613 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004614 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004615 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004616 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004617 }
4618 return ret;
4619}
4620
4621static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004622 struct inode *dir, struct dentry *dentry,
4623 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004624{
Josef Bacika1b075d2010-11-19 20:36:11 +00004625 int err = btrfs_add_link(trans, dir, inode,
4626 dentry->d_name.name, dentry->d_name.len,
4627 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004628 if (!err) {
4629 d_instantiate(dentry, inode);
4630 return 0;
4631 }
4632 if (err > 0)
4633 err = -EEXIST;
4634 return err;
4635}
4636
Josef Bacik618e21d2007-07-11 10:18:17 -04004637static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4638 int mode, dev_t rdev)
4639{
4640 struct btrfs_trans_handle *trans;
4641 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004642 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004643 int err;
4644 int drop_inode = 0;
4645 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004646 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004647 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004648
4649 if (!new_valid_dev(rdev))
4650 return -EINVAL;
4651
Josef Bacik9ed74f22009-09-11 16:12:44 -04004652 /*
4653 * 2 for inode item and ref
4654 * 2 for dir items
4655 * 1 for xattr if selinux is on
4656 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004657 trans = btrfs_start_transaction(root, 5);
4658 if (IS_ERR(trans))
4659 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004660
Li Zefan581bb052011-04-20 10:06:11 +08004661 err = btrfs_find_free_ino(root, &objectid);
4662 if (err)
4663 goto out_unlock;
4664
Josef Bacikaec74772008-07-24 12:12:38 -04004665 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004666 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004667 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004668 if (IS_ERR(inode)) {
4669 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004670 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004671 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004672
Eric Paris2a7dba32011-02-01 11:05:39 -05004673 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004674 if (err) {
4675 drop_inode = 1;
4676 goto out_unlock;
4677 }
4678
Josef Bacika1b075d2010-11-19 20:36:11 +00004679 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004680 if (err)
4681 drop_inode = 1;
4682 else {
4683 inode->i_op = &btrfs_special_inode_operations;
4684 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004685 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004686 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004687out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004688 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004689 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004690 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004691 if (drop_inode) {
4692 inode_dec_link_count(inode);
4693 iput(inode);
4694 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004695 return err;
4696}
4697
Chris Mason39279cc2007-06-12 06:35:45 -04004698static int btrfs_create(struct inode *dir, struct dentry *dentry,
4699 int mode, struct nameidata *nd)
4700{
4701 struct btrfs_trans_handle *trans;
4702 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004703 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004704 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004705 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004706 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004707 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004708 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004709
Josef Bacik9ed74f22009-09-11 16:12:44 -04004710 /*
4711 * 2 for inode item and ref
4712 * 2 for dir items
4713 * 1 for xattr if selinux is on
4714 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004715 trans = btrfs_start_transaction(root, 5);
4716 if (IS_ERR(trans))
4717 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004718
Li Zefan581bb052011-04-20 10:06:11 +08004719 err = btrfs_find_free_ino(root, &objectid);
4720 if (err)
4721 goto out_unlock;
4722
Josef Bacikaec74772008-07-24 12:12:38 -04004723 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004724 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004725 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004726 if (IS_ERR(inode)) {
4727 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004728 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004729 }
Chris Mason39279cc2007-06-12 06:35:45 -04004730
Eric Paris2a7dba32011-02-01 11:05:39 -05004731 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004732 if (err) {
4733 drop_inode = 1;
4734 goto out_unlock;
4735 }
4736
Josef Bacika1b075d2010-11-19 20:36:11 +00004737 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004738 if (err)
4739 drop_inode = 1;
4740 else {
4741 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004742 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004743 inode->i_fop = &btrfs_file_operations;
4744 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004745 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004746 }
Chris Mason39279cc2007-06-12 06:35:45 -04004747out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004748 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004749 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004750 if (drop_inode) {
4751 inode_dec_link_count(inode);
4752 iput(inode);
4753 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004754 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004755 return err;
4756}
4757
4758static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4759 struct dentry *dentry)
4760{
4761 struct btrfs_trans_handle *trans;
4762 struct btrfs_root *root = BTRFS_I(dir)->root;
4763 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004764 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004765 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004766 int err;
4767 int drop_inode = 0;
4768
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004769 /* do not allow sys_link's with other subvols of the same device */
4770 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00004771 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004772
Al Viroc055e992011-03-04 17:15:18 +00004773 if (inode->i_nlink == ~0U)
4774 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004775
Chris Mason3de45862008-11-17 21:02:50 -05004776 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004777 if (err)
4778 goto fail;
4779
Yan, Zhenga22285a2010-05-16 10:48:46 -04004780 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00004781 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04004782 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00004783 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04004784 */
Miao Xie7e6b6462011-02-18 09:21:17 +00004785 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004786 if (IS_ERR(trans)) {
4787 err = PTR_ERR(trans);
4788 goto fail;
4789 }
Chris Mason5f39d392007-10-15 16:14:19 -04004790
Miao Xie31534952011-04-13 13:19:21 +08004791 btrfs_inc_nlink(inode);
4792 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04004793 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004794
Josef Bacika1b075d2010-11-19 20:36:11 +00004795 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004796
Yan, Zhenga5719522009-09-24 09:17:31 -04004797 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004798 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004799 } else {
Al Viro10d9f302011-07-16 23:09:10 -04004800 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04004801 err = btrfs_update_inode(trans, root, inode);
4802 BUG_ON(err);
Josef Bacik6a912212010-11-20 09:48:00 +00004803 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004804 }
Chris Mason39279cc2007-06-12 06:35:45 -04004805
Chris Masond3c2fdc2007-09-17 10:58:06 -04004806 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004807 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004808fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004809 if (drop_inode) {
4810 inode_dec_link_count(inode);
4811 iput(inode);
4812 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004813 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004814 return err;
4815}
4816
Chris Mason39279cc2007-06-12 06:35:45 -04004817static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4818{
Chris Masonb9d86662008-05-02 16:13:49 -04004819 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004820 struct btrfs_trans_handle *trans;
4821 struct btrfs_root *root = BTRFS_I(dir)->root;
4822 int err = 0;
4823 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004824 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004825 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04004826 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004827
Josef Bacik9ed74f22009-09-11 16:12:44 -04004828 /*
4829 * 2 items for inode and ref
4830 * 2 items for dir items
4831 * 1 for xattr if selinux is on
4832 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004833 trans = btrfs_start_transaction(root, 5);
4834 if (IS_ERR(trans))
4835 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04004836
Li Zefan581bb052011-04-20 10:06:11 +08004837 err = btrfs_find_free_ino(root, &objectid);
4838 if (err)
4839 goto out_fail;
4840
Josef Bacikaec74772008-07-24 12:12:38 -04004841 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004842 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004843 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004844 if (IS_ERR(inode)) {
4845 err = PTR_ERR(inode);
4846 goto out_fail;
4847 }
Chris Mason5f39d392007-10-15 16:14:19 -04004848
Chris Mason39279cc2007-06-12 06:35:45 -04004849 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004850
Eric Paris2a7dba32011-02-01 11:05:39 -05004851 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004852 if (err)
4853 goto out_fail;
4854
Chris Mason39279cc2007-06-12 06:35:45 -04004855 inode->i_op = &btrfs_dir_inode_operations;
4856 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04004857
Chris Masondbe674a2008-07-17 12:54:05 -04004858 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004859 err = btrfs_update_inode(trans, root, inode);
4860 if (err)
4861 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004862
Josef Bacika1b075d2010-11-19 20:36:11 +00004863 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4864 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004865 if (err)
4866 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004867
Chris Mason39279cc2007-06-12 06:35:45 -04004868 d_instantiate(dentry, inode);
4869 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004870
4871out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004872 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004873 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004874 if (drop_on_err)
4875 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04004876 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004877 return err;
4878}
4879
Chris Masond352ac62008-09-29 15:18:18 -04004880/* helper for btfs_get_extent. Given an existing extent in the tree,
4881 * and an extent that you want to insert, deal with overlap and insert
4882 * the new extent into the tree.
4883 */
Chris Mason3b951512008-04-17 11:29:12 -04004884static int merge_extent_mapping(struct extent_map_tree *em_tree,
4885 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004886 struct extent_map *em,
4887 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004888{
4889 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004890
Chris Masone6dcd2d2008-07-17 12:53:50 -04004891 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4892 start_diff = map_start - em->start;
4893 em->start = map_start;
4894 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004895 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4896 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004897 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004898 em->block_len -= start_diff;
4899 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004900 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004901}
4902
Chris Masonc8b97812008-10-29 14:49:59 -04004903static noinline int uncompress_inline(struct btrfs_path *path,
4904 struct inode *inode, struct page *page,
4905 size_t pg_offset, u64 extent_offset,
4906 struct btrfs_file_extent_item *item)
4907{
4908 int ret;
4909 struct extent_buffer *leaf = path->nodes[0];
4910 char *tmp;
4911 size_t max_size;
4912 unsigned long inline_size;
4913 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08004914 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04004915
4916 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08004917 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04004918 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4919 inline_size = btrfs_file_extent_inline_item_len(leaf,
4920 btrfs_item_nr(leaf, path->slots[0]));
4921 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04004922 if (!tmp)
4923 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04004924 ptr = btrfs_file_extent_inline_start(item);
4925
4926 read_extent_buffer(leaf, tmp, ptr, inline_size);
4927
Chris Mason5b050f02008-11-11 09:34:41 -05004928 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08004929 ret = btrfs_decompress(compress_type, tmp, page,
4930 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004931 if (ret) {
4932 char *kaddr = kmap_atomic(page, KM_USER0);
4933 unsigned long copy_size = min_t(u64,
4934 PAGE_CACHE_SIZE - pg_offset,
4935 max_size - extent_offset);
4936 memset(kaddr + pg_offset, 0, copy_size);
4937 kunmap_atomic(kaddr, KM_USER0);
4938 }
4939 kfree(tmp);
4940 return 0;
4941}
4942
Chris Masond352ac62008-09-29 15:18:18 -04004943/*
4944 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004945 * the ugly parts come from merging extents from the disk with the in-ram
4946 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004947 * where the in-ram extents might be locked pending data=ordered completion.
4948 *
4949 * This also copies inline extents directly into the page.
4950 */
Chris Masond3977122009-01-05 21:25:51 -05004951
Chris Masona52d9a82007-08-27 16:49:44 -04004952struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004953 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004954 int create)
4955{
4956 int ret;
4957 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004958 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004959 u64 extent_start = 0;
4960 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004961 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04004962 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004963 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004964 struct btrfs_root *root = BTRFS_I(inode)->root;
4965 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004966 struct extent_buffer *leaf;
4967 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004968 struct extent_map *em = NULL;
4969 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004970 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004971 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08004972 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04004973
Chris Masona52d9a82007-08-27 16:49:44 -04004974again:
Chris Mason890871b2009-09-02 16:24:52 -04004975 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004976 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004977 if (em)
4978 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004979 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004980
Chris Masona52d9a82007-08-27 16:49:44 -04004981 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004982 if (em->start > start || em->start + em->len <= start)
4983 free_extent_map(em);
4984 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004985 free_extent_map(em);
4986 else
4987 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004988 }
David Sterba172ddd62011-04-21 00:48:27 +02004989 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04004990 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004991 err = -ENOMEM;
4992 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004993 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004994 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004995 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004996 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004997 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004998 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004999
5000 if (!path) {
5001 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005002 if (!path) {
5003 err = -ENOMEM;
5004 goto out;
5005 }
5006 /*
5007 * Chances are we'll be called again, so go ahead and do
5008 * readahead
5009 */
5010 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005011 }
5012
Chris Mason179e29e2007-11-01 11:28:41 -04005013 ret = btrfs_lookup_file_extent(trans, root, path,
5014 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005015 if (ret < 0) {
5016 err = ret;
5017 goto out;
5018 }
5019
5020 if (ret != 0) {
5021 if (path->slots[0] == 0)
5022 goto not_found;
5023 path->slots[0]--;
5024 }
5025
Chris Mason5f39d392007-10-15 16:14:19 -04005026 leaf = path->nodes[0];
5027 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005028 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005029 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005030 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5031 found_type = btrfs_key_type(&found_key);
5032 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005033 found_type != BTRFS_EXTENT_DATA_KEY) {
5034 goto not_found;
5035 }
5036
Chris Mason5f39d392007-10-15 16:14:19 -04005037 found_type = btrfs_file_extent_type(leaf, item);
5038 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005039 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005040 if (found_type == BTRFS_FILE_EXTENT_REG ||
5041 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005042 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005043 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005044 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5045 size_t size;
5046 size = btrfs_file_extent_inline_len(leaf, item);
5047 extent_end = (extent_start + size + root->sectorsize - 1) &
5048 ~((u64)root->sectorsize - 1);
5049 }
5050
5051 if (start >= extent_end) {
5052 path->slots[0]++;
5053 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5054 ret = btrfs_next_leaf(root, path);
5055 if (ret < 0) {
5056 err = ret;
5057 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005058 }
Yan Zheng9036c102008-10-30 14:19:41 -04005059 if (ret > 0)
5060 goto not_found;
5061 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005062 }
Yan Zheng9036c102008-10-30 14:19:41 -04005063 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5064 if (found_key.objectid != objectid ||
5065 found_key.type != BTRFS_EXTENT_DATA_KEY)
5066 goto not_found;
5067 if (start + len <= found_key.offset)
5068 goto not_found;
5069 em->start = start;
5070 em->len = found_key.offset - start;
5071 goto not_found_em;
5072 }
5073
Yan Zhengd899e052008-10-30 14:25:28 -04005074 if (found_type == BTRFS_FILE_EXTENT_REG ||
5075 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005076 em->start = extent_start;
5077 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005078 em->orig_start = extent_start -
5079 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005080 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5081 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005082 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005083 goto insert;
5084 }
Li Zefan261507a02010-12-17 14:21:50 +08005085 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005086 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005087 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005088 em->block_start = bytenr;
5089 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5090 item);
5091 } else {
5092 bytenr += btrfs_file_extent_offset(leaf, item);
5093 em->block_start = bytenr;
5094 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005095 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5096 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005097 }
Chris Masona52d9a82007-08-27 16:49:44 -04005098 goto insert;
5099 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005100 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005101 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005102 size_t size;
5103 size_t extent_offset;
5104 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005105
Chris Masona52d9a82007-08-27 16:49:44 -04005106 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005107 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005108 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005109 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005110 goto out;
5111 }
5112
Yan Zheng9036c102008-10-30 14:19:41 -04005113 size = btrfs_file_extent_inline_len(leaf, item);
5114 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005115 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005116 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005117 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005118 em->len = (copy_size + root->sectorsize - 1) &
5119 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005120 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005121 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005122 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005123 em->compress_type = compress_type;
5124 }
Yan689f9342007-10-29 11:41:07 -04005125 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005126 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005127 if (btrfs_file_extent_compression(leaf, item) !=
5128 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005129 ret = uncompress_inline(path, inode, page,
5130 pg_offset,
5131 extent_offset, item);
5132 BUG_ON(ret);
5133 } else {
5134 map = kmap(page);
5135 read_extent_buffer(leaf, map + pg_offset, ptr,
5136 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005137 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5138 memset(map + pg_offset + copy_size, 0,
5139 PAGE_CACHE_SIZE - pg_offset -
5140 copy_size);
5141 }
Chris Masonc8b97812008-10-29 14:49:59 -04005142 kunmap(page);
5143 }
Chris Mason179e29e2007-11-01 11:28:41 -04005144 flush_dcache_page(page);
5145 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005146 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005147 if (!trans) {
5148 kunmap(page);
5149 free_extent_map(em);
5150 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005151
David Sterbab3b4aa72011-04-21 01:20:15 +02005152 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005153 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005154
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005155 if (IS_ERR(trans))
5156 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005157 goto again;
5158 }
Chris Masonc8b97812008-10-29 14:49:59 -04005159 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005160 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005161 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005162 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005163 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005164 }
Chris Masond1310b22008-01-24 16:13:08 -05005165 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005166 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005167 goto insert;
5168 } else {
Chris Masond3977122009-01-05 21:25:51 -05005169 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005170 WARN_ON(1);
5171 }
5172not_found:
5173 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005174 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005175not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005176 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005177 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005178insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005179 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005180 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005181 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5182 "[%llu %llu]\n", (unsigned long long)em->start,
5183 (unsigned long long)em->len,
5184 (unsigned long long)start,
5185 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005186 err = -EIO;
5187 goto out;
5188 }
Chris Masond1310b22008-01-24 16:13:08 -05005189
5190 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005191 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005192 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005193 /* it is possible that someone inserted the extent into the tree
5194 * while we had the lock dropped. It is also possible that
5195 * an overlapping map exists in the tree
5196 */
Chris Masona52d9a82007-08-27 16:49:44 -04005197 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005198 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005199
5200 ret = 0;
5201
Chris Mason3b951512008-04-17 11:29:12 -04005202 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005203 if (existing && (existing->start > start ||
5204 existing->start + existing->len <= start)) {
5205 free_extent_map(existing);
5206 existing = NULL;
5207 }
Chris Mason3b951512008-04-17 11:29:12 -04005208 if (!existing) {
5209 existing = lookup_extent_mapping(em_tree, em->start,
5210 em->len);
5211 if (existing) {
5212 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005213 em, start,
5214 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005215 free_extent_map(existing);
5216 if (err) {
5217 free_extent_map(em);
5218 em = NULL;
5219 }
5220 } else {
5221 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005222 free_extent_map(em);
5223 em = NULL;
5224 }
5225 } else {
5226 free_extent_map(em);
5227 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005228 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005229 }
Chris Masona52d9a82007-08-27 16:49:44 -04005230 }
Chris Mason890871b2009-09-02 16:24:52 -04005231 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005232out:
liubo1abe9b82011-03-24 11:18:59 +00005233
5234 trace_btrfs_get_extent(root, em);
5235
Chris Masonf4219502008-07-22 11:18:09 -04005236 if (path)
5237 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005238 if (trans) {
5239 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005240 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005241 err = ret;
5242 }
Chris Masona52d9a82007-08-27 16:49:44 -04005243 if (err) {
5244 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005245 return ERR_PTR(err);
5246 }
5247 return em;
5248}
5249
Chris Masonec29ed52011-02-23 16:23:20 -05005250struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5251 size_t pg_offset, u64 start, u64 len,
5252 int create)
5253{
5254 struct extent_map *em;
5255 struct extent_map *hole_em = NULL;
5256 u64 range_start = start;
5257 u64 end;
5258 u64 found;
5259 u64 found_end;
5260 int err = 0;
5261
5262 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5263 if (IS_ERR(em))
5264 return em;
5265 if (em) {
5266 /*
5267 * if our em maps to a hole, there might
5268 * actually be delalloc bytes behind it
5269 */
5270 if (em->block_start != EXTENT_MAP_HOLE)
5271 return em;
5272 else
5273 hole_em = em;
5274 }
5275
5276 /* check to see if we've wrapped (len == -1 or similar) */
5277 end = start + len;
5278 if (end < start)
5279 end = (u64)-1;
5280 else
5281 end -= 1;
5282
5283 em = NULL;
5284
5285 /* ok, we didn't find anything, lets look for delalloc */
5286 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5287 end, len, EXTENT_DELALLOC, 1);
5288 found_end = range_start + found;
5289 if (found_end < range_start)
5290 found_end = (u64)-1;
5291
5292 /*
5293 * we didn't find anything useful, return
5294 * the original results from get_extent()
5295 */
5296 if (range_start > end || found_end <= start) {
5297 em = hole_em;
5298 hole_em = NULL;
5299 goto out;
5300 }
5301
5302 /* adjust the range_start to make sure it doesn't
5303 * go backwards from the start they passed in
5304 */
5305 range_start = max(start,range_start);
5306 found = found_end - range_start;
5307
5308 if (found > 0) {
5309 u64 hole_start = start;
5310 u64 hole_len = len;
5311
David Sterba172ddd62011-04-21 00:48:27 +02005312 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005313 if (!em) {
5314 err = -ENOMEM;
5315 goto out;
5316 }
5317 /*
5318 * when btrfs_get_extent can't find anything it
5319 * returns one huge hole
5320 *
5321 * make sure what it found really fits our range, and
5322 * adjust to make sure it is based on the start from
5323 * the caller
5324 */
5325 if (hole_em) {
5326 u64 calc_end = extent_map_end(hole_em);
5327
5328 if (calc_end <= start || (hole_em->start > end)) {
5329 free_extent_map(hole_em);
5330 hole_em = NULL;
5331 } else {
5332 hole_start = max(hole_em->start, start);
5333 hole_len = calc_end - hole_start;
5334 }
5335 }
5336 em->bdev = NULL;
5337 if (hole_em && range_start > hole_start) {
5338 /* our hole starts before our delalloc, so we
5339 * have to return just the parts of the hole
5340 * that go until the delalloc starts
5341 */
5342 em->len = min(hole_len,
5343 range_start - hole_start);
5344 em->start = hole_start;
5345 em->orig_start = hole_start;
5346 /*
5347 * don't adjust block start at all,
5348 * it is fixed at EXTENT_MAP_HOLE
5349 */
5350 em->block_start = hole_em->block_start;
5351 em->block_len = hole_len;
5352 } else {
5353 em->start = range_start;
5354 em->len = found;
5355 em->orig_start = range_start;
5356 em->block_start = EXTENT_MAP_DELALLOC;
5357 em->block_len = found;
5358 }
5359 } else if (hole_em) {
5360 return hole_em;
5361 }
5362out:
5363
5364 free_extent_map(hole_em);
5365 if (err) {
5366 free_extent_map(em);
5367 return ERR_PTR(err);
5368 }
5369 return em;
5370}
5371
Josef Bacik4b46fce2010-05-23 11:00:55 -04005372static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005373 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005374 u64 start, u64 len)
5375{
5376 struct btrfs_root *root = BTRFS_I(inode)->root;
5377 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005378 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5379 struct btrfs_key ins;
5380 u64 alloc_hint;
5381 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005382 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005383
Josef Bacik16d299a2011-04-06 14:53:07 -04005384 /*
5385 * Ok if the extent map we looked up is a hole and is for the exact
5386 * range we want, there is no reason to allocate a new one, however if
5387 * it is not right then we need to free this one and drop the cache for
5388 * our range.
5389 */
5390 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5391 em->len != len) {
5392 free_extent_map(em);
5393 em = NULL;
5394 insert = true;
5395 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5396 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005397
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005398 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005399 if (IS_ERR(trans))
5400 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005401
Chris Mason4cb53002011-05-24 15:35:30 -04005402 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5403 btrfs_add_inode_defrag(trans, inode);
5404
Josef Bacik4b46fce2010-05-23 11:00:55 -04005405 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5406
5407 alloc_hint = get_extent_allocation_hint(inode, start, len);
5408 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5409 alloc_hint, (u64)-1, &ins, 1);
5410 if (ret) {
5411 em = ERR_PTR(ret);
5412 goto out;
5413 }
5414
Josef Bacik4b46fce2010-05-23 11:00:55 -04005415 if (!em) {
David Sterba172ddd62011-04-21 00:48:27 +02005416 em = alloc_extent_map();
Josef Bacik16d299a2011-04-06 14:53:07 -04005417 if (!em) {
5418 em = ERR_PTR(-ENOMEM);
5419 goto out;
5420 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005421 }
5422
5423 em->start = start;
5424 em->orig_start = em->start;
5425 em->len = ins.offset;
5426
5427 em->block_start = ins.objectid;
5428 em->block_len = ins.offset;
5429 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005430
5431 /*
5432 * We need to do this because if we're using the original em we searched
5433 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5434 */
5435 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005436 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5437
Josef Bacik16d299a2011-04-06 14:53:07 -04005438 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005439 write_lock(&em_tree->lock);
5440 ret = add_extent_mapping(em_tree, em);
5441 write_unlock(&em_tree->lock);
5442 if (ret != -EEXIST)
5443 break;
5444 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5445 }
5446
5447 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5448 ins.offset, ins.offset, 0);
5449 if (ret) {
5450 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5451 em = ERR_PTR(ret);
5452 }
5453out:
5454 btrfs_end_transaction(trans, root);
5455 return em;
5456}
5457
Chris Mason46bfbb52010-05-26 11:04:10 -04005458/*
5459 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5460 * block must be cow'd
5461 */
5462static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5463 struct inode *inode, u64 offset, u64 len)
5464{
5465 struct btrfs_path *path;
5466 int ret;
5467 struct extent_buffer *leaf;
5468 struct btrfs_root *root = BTRFS_I(inode)->root;
5469 struct btrfs_file_extent_item *fi;
5470 struct btrfs_key key;
5471 u64 disk_bytenr;
5472 u64 backref_offset;
5473 u64 extent_end;
5474 u64 num_bytes;
5475 int slot;
5476 int found_type;
5477
5478 path = btrfs_alloc_path();
5479 if (!path)
5480 return -ENOMEM;
5481
Li Zefan33345d012011-04-20 10:31:50 +08005482 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005483 offset, 0);
5484 if (ret < 0)
5485 goto out;
5486
5487 slot = path->slots[0];
5488 if (ret == 1) {
5489 if (slot == 0) {
5490 /* can't find the item, must cow */
5491 ret = 0;
5492 goto out;
5493 }
5494 slot--;
5495 }
5496 ret = 0;
5497 leaf = path->nodes[0];
5498 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005499 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005500 key.type != BTRFS_EXTENT_DATA_KEY) {
5501 /* not our file or wrong item type, must cow */
5502 goto out;
5503 }
5504
5505 if (key.offset > offset) {
5506 /* Wrong offset, must cow */
5507 goto out;
5508 }
5509
5510 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5511 found_type = btrfs_file_extent_type(leaf, fi);
5512 if (found_type != BTRFS_FILE_EXTENT_REG &&
5513 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5514 /* not a regular extent, must cow */
5515 goto out;
5516 }
5517 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5518 backref_offset = btrfs_file_extent_offset(leaf, fi);
5519
5520 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5521 if (extent_end < offset + len) {
5522 /* extent doesn't include our full range, must cow */
5523 goto out;
5524 }
5525
5526 if (btrfs_extent_readonly(root, disk_bytenr))
5527 goto out;
5528
5529 /*
5530 * look for other files referencing this extent, if we
5531 * find any we must cow
5532 */
Li Zefan33345d012011-04-20 10:31:50 +08005533 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005534 key.offset - backref_offset, disk_bytenr))
5535 goto out;
5536
5537 /*
5538 * adjust disk_bytenr and num_bytes to cover just the bytes
5539 * in this extent we are about to write. If there
5540 * are any csums in that range we have to cow in order
5541 * to keep the csums correct
5542 */
5543 disk_bytenr += backref_offset;
5544 disk_bytenr += offset - key.offset;
5545 num_bytes = min(offset + len, extent_end) - offset;
5546 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5547 goto out;
5548 /*
5549 * all of the above have passed, it is safe to overwrite this extent
5550 * without cow
5551 */
5552 ret = 1;
5553out:
5554 btrfs_free_path(path);
5555 return ret;
5556}
5557
Josef Bacik4b46fce2010-05-23 11:00:55 -04005558static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5559 struct buffer_head *bh_result, int create)
5560{
5561 struct extent_map *em;
5562 struct btrfs_root *root = BTRFS_I(inode)->root;
5563 u64 start = iblock << inode->i_blkbits;
5564 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005565 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005566
5567 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5568 if (IS_ERR(em))
5569 return PTR_ERR(em);
5570
5571 /*
5572 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5573 * io. INLINE is special, and we could probably kludge it in here, but
5574 * it's still buffered so for safety lets just fall back to the generic
5575 * buffered path.
5576 *
5577 * For COMPRESSED we _have_ to read the entire extent in so we can
5578 * decompress it, so there will be buffering required no matter what we
5579 * do, so go ahead and fallback to buffered.
5580 *
5581 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5582 * to buffered IO. Don't blame me, this is the price we pay for using
5583 * the generic code.
5584 */
5585 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5586 em->block_start == EXTENT_MAP_INLINE) {
5587 free_extent_map(em);
5588 return -ENOTBLK;
5589 }
5590
5591 /* Just a good old fashioned hole, return */
5592 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5593 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5594 free_extent_map(em);
5595 /* DIO will do one hole at a time, so just unlock a sector */
5596 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5597 start + root->sectorsize - 1, GFP_NOFS);
5598 return 0;
5599 }
5600
5601 /*
5602 * We don't allocate a new extent in the following cases
5603 *
5604 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5605 * existing extent.
5606 * 2) The extent is marked as PREALLOC. We're good to go here and can
5607 * just use the extent.
5608 *
5609 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005610 if (!create) {
5611 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005612 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005613 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005614
5615 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5616 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5617 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005618 int type;
5619 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005620 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005621
5622 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5623 type = BTRFS_ORDERED_PREALLOC;
5624 else
5625 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005626 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005627 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005628
5629 /*
5630 * we're not going to log anything, but we do need
5631 * to make sure the current transaction stays open
5632 * while we look for nocow cross refs
5633 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005634 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005635 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005636 goto must_cow;
5637
5638 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5639 ret = btrfs_add_ordered_extent_dio(inode, start,
5640 block_start, len, len, type);
5641 btrfs_end_transaction(trans, root);
5642 if (ret) {
5643 free_extent_map(em);
5644 return ret;
5645 }
5646 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005647 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005648 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005649 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005650must_cow:
5651 /*
5652 * this will cow the extent, reset the len in case we changed
5653 * it above
5654 */
5655 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04005656 em = btrfs_new_extent_direct(inode, em, start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04005657 if (IS_ERR(em))
5658 return PTR_ERR(em);
5659 len = min(len, em->len - (start - em->start));
5660unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005661 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5662 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5663 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005664map:
5665 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5666 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005667 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005668 bh_result->b_bdev = em->bdev;
5669 set_buffer_mapped(bh_result);
5670 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5671 set_buffer_new(bh_result);
5672
5673 free_extent_map(em);
5674
5675 return 0;
5676}
5677
5678struct btrfs_dio_private {
5679 struct inode *inode;
5680 u64 logical_offset;
5681 u64 disk_bytenr;
5682 u64 bytes;
5683 u32 *csums;
5684 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005685
5686 /* number of bios pending for this dio */
5687 atomic_t pending_bios;
5688
5689 /* IO errors */
5690 int errors;
5691
5692 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005693};
5694
5695static void btrfs_endio_direct_read(struct bio *bio, int err)
5696{
Miao Xiee65e1532010-11-22 03:04:43 +00005697 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005698 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5699 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005700 struct inode *inode = dip->inode;
5701 struct btrfs_root *root = BTRFS_I(inode)->root;
5702 u64 start;
5703 u32 *private = dip->csums;
5704
5705 start = dip->logical_offset;
5706 do {
5707 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5708 struct page *page = bvec->bv_page;
5709 char *kaddr;
5710 u32 csum = ~(u32)0;
5711 unsigned long flags;
5712
5713 local_irq_save(flags);
5714 kaddr = kmap_atomic(page, KM_IRQ0);
5715 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5716 csum, bvec->bv_len);
5717 btrfs_csum_final(csum, (char *)&csum);
5718 kunmap_atomic(kaddr, KM_IRQ0);
5719 local_irq_restore(flags);
5720
5721 flush_dcache_page(bvec->bv_page);
5722 if (csum != *private) {
Li Zefan33345d012011-04-20 10:31:50 +08005723 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04005724 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08005725 (unsigned long long)btrfs_ino(inode),
5726 (unsigned long long)start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005727 csum, *private);
5728 err = -EIO;
5729 }
5730 }
5731
5732 start += bvec->bv_len;
5733 private++;
5734 bvec++;
5735 } while (bvec <= bvec_end);
5736
5737 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5738 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5739 bio->bi_private = dip->private;
5740
5741 kfree(dip->csums);
5742 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005743
5744 /* If we had a csum failure make sure to clear the uptodate flag */
5745 if (err)
5746 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005747 dio_end_io(bio, err);
5748}
5749
5750static void btrfs_endio_direct_write(struct bio *bio, int err)
5751{
5752 struct btrfs_dio_private *dip = bio->bi_private;
5753 struct inode *inode = dip->inode;
5754 struct btrfs_root *root = BTRFS_I(inode)->root;
5755 struct btrfs_trans_handle *trans;
5756 struct btrfs_ordered_extent *ordered = NULL;
5757 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005758 u64 ordered_offset = dip->logical_offset;
5759 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005760 int ret;
5761
5762 if (err)
5763 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005764again:
5765 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5766 &ordered_offset,
5767 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005768 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005769 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005770
5771 BUG_ON(!ordered);
5772
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005773 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005774 if (IS_ERR(trans)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005775 err = -ENOMEM;
5776 goto out;
5777 }
5778 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5779
5780 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5781 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5782 if (!ret)
Li Zefanf0dd9592011-09-08 10:26:51 +08005783 err = btrfs_update_inode(trans, root, inode);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005784 goto out;
5785 }
5786
5787 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5788 ordered->file_offset + ordered->len - 1, 0,
5789 &cached_state, GFP_NOFS);
5790
5791 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5792 ret = btrfs_mark_extent_written(trans, inode,
5793 ordered->file_offset,
5794 ordered->file_offset +
5795 ordered->len);
5796 if (ret) {
5797 err = ret;
5798 goto out_unlock;
5799 }
5800 } else {
5801 ret = insert_reserved_file_extent(trans, inode,
5802 ordered->file_offset,
5803 ordered->start,
5804 ordered->disk_len,
5805 ordered->len,
5806 ordered->len,
5807 0, 0, 0,
5808 BTRFS_FILE_EXTENT_REG);
5809 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5810 ordered->file_offset, ordered->len);
5811 if (ret) {
5812 err = ret;
5813 WARN_ON(1);
5814 goto out_unlock;
5815 }
5816 }
5817
5818 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005819 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
Miao Xiea39f7522011-09-11 10:52:25 -04005820 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags))
Josef Bacik1ef30be2011-04-05 19:25:36 -04005821 btrfs_update_inode(trans, root, inode);
5822 ret = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005823out_unlock:
5824 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5825 ordered->file_offset + ordered->len - 1,
5826 &cached_state, GFP_NOFS);
5827out:
5828 btrfs_delalloc_release_metadata(inode, ordered->len);
5829 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005830 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005831 btrfs_put_ordered_extent(ordered);
5832 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05005833
5834out_test:
5835 /*
5836 * our bio might span multiple ordered extents. If we haven't
5837 * completed the accounting for the whole dio, go back and try again
5838 */
5839 if (ordered_offset < dip->logical_offset + dip->bytes) {
5840 ordered_bytes = dip->logical_offset + dip->bytes -
5841 ordered_offset;
5842 goto again;
5843 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005844out_done:
5845 bio->bi_private = dip->private;
5846
5847 kfree(dip->csums);
5848 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005849
5850 /* If we had an error make sure to clear the uptodate flag */
5851 if (err)
5852 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005853 dio_end_io(bio, err);
5854}
5855
Chris Masoneaf25d92010-05-25 09:48:28 -04005856static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5857 struct bio *bio, int mirror_num,
5858 unsigned long bio_flags, u64 offset)
5859{
5860 int ret;
5861 struct btrfs_root *root = BTRFS_I(inode)->root;
5862 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5863 BUG_ON(ret);
5864 return 0;
5865}
5866
Miao Xiee65e1532010-11-22 03:04:43 +00005867static void btrfs_end_dio_bio(struct bio *bio, int err)
5868{
5869 struct btrfs_dio_private *dip = bio->bi_private;
5870
5871 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08005872 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00005873 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08005874 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00005875 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00005876 dip->errors = 1;
5877
5878 /*
5879 * before atomic variable goto zero, we must make sure
5880 * dip->errors is perceived to be set.
5881 */
5882 smp_mb__before_atomic_dec();
5883 }
5884
5885 /* if there are more bios still pending for this dio, just exit */
5886 if (!atomic_dec_and_test(&dip->pending_bios))
5887 goto out;
5888
5889 if (dip->errors)
5890 bio_io_error(dip->orig_bio);
5891 else {
5892 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
5893 bio_endio(dip->orig_bio, 0);
5894 }
5895out:
5896 bio_put(bio);
5897}
5898
5899static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
5900 u64 first_sector, gfp_t gfp_flags)
5901{
5902 int nr_vecs = bio_get_nr_vecs(bdev);
5903 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
5904}
5905
5906static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
5907 int rw, u64 file_offset, int skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04005908 u32 *csums, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00005909{
5910 int write = rw & REQ_WRITE;
5911 struct btrfs_root *root = BTRFS_I(inode)->root;
5912 int ret;
5913
5914 bio_get(bio);
5915 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
5916 if (ret)
5917 goto err;
5918
Josef Bacik1ae39932011-04-06 14:41:34 -04005919 if (skip_sum)
5920 goto map;
5921
5922 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00005923 ret = btrfs_wq_submit_bio(root->fs_info,
5924 inode, rw, bio, 0, 0,
5925 file_offset,
5926 __btrfs_submit_bio_start_direct_io,
5927 __btrfs_submit_bio_done);
5928 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04005929 } else if (write) {
5930 /*
5931 * If we aren't doing async submit, calculate the csum of the
5932 * bio now.
5933 */
5934 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
5935 if (ret)
5936 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00005937 } else if (!skip_sum) {
5938 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
Miao Xiee65e1532010-11-22 03:04:43 +00005939 file_offset, csums);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00005940 if (ret)
5941 goto err;
5942 }
Miao Xiee65e1532010-11-22 03:04:43 +00005943
Josef Bacik1ae39932011-04-06 14:41:34 -04005944map:
5945 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00005946err:
5947 bio_put(bio);
5948 return ret;
5949}
5950
5951static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
5952 int skip_sum)
5953{
5954 struct inode *inode = dip->inode;
5955 struct btrfs_root *root = BTRFS_I(inode)->root;
5956 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5957 struct bio *bio;
5958 struct bio *orig_bio = dip->orig_bio;
5959 struct bio_vec *bvec = orig_bio->bi_io_vec;
5960 u64 start_sector = orig_bio->bi_sector;
5961 u64 file_offset = dip->logical_offset;
5962 u64 submit_len = 0;
5963 u64 map_length;
5964 int nr_pages = 0;
5965 u32 *csums = dip->csums;
5966 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04005967 int async_submit = 0;
Josef Bacik98bc3142011-03-22 11:00:46 -04005968 int write = rw & REQ_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00005969
Miao Xiee65e1532010-11-22 03:04:43 +00005970 map_length = orig_bio->bi_size;
5971 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5972 &map_length, NULL, 0);
5973 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04005974 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00005975 return -EIO;
5976 }
5977
Josef Bacik02f57c72011-04-06 14:25:44 -04005978 if (map_length >= orig_bio->bi_size) {
5979 bio = orig_bio;
5980 goto submit;
5981 }
5982
Josef Bacik1ae39932011-04-06 14:41:34 -04005983 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04005984 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
5985 if (!bio)
5986 return -ENOMEM;
5987 bio->bi_private = dip;
5988 bio->bi_end_io = btrfs_end_dio_bio;
5989 atomic_inc(&dip->pending_bios);
5990
Miao Xiee65e1532010-11-22 03:04:43 +00005991 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
5992 if (unlikely(map_length < submit_len + bvec->bv_len ||
5993 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5994 bvec->bv_offset) < bvec->bv_len)) {
5995 /*
5996 * inc the count before we submit the bio so
5997 * we know the end IO handler won't happen before
5998 * we inc the count. Otherwise, the dip might get freed
5999 * before we're done setting it up
6000 */
6001 atomic_inc(&dip->pending_bios);
6002 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6003 file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006004 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006005 if (ret) {
6006 bio_put(bio);
6007 atomic_dec(&dip->pending_bios);
6008 goto out_err;
6009 }
6010
Josef Bacik98bc3142011-03-22 11:00:46 -04006011 /* Write's use the ordered csums */
6012 if (!write && !skip_sum)
Miao Xiee65e1532010-11-22 03:04:43 +00006013 csums = csums + nr_pages;
6014 start_sector += submit_len >> 9;
6015 file_offset += submit_len;
6016
6017 submit_len = 0;
6018 nr_pages = 0;
6019
6020 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6021 start_sector, GFP_NOFS);
6022 if (!bio)
6023 goto out_err;
6024 bio->bi_private = dip;
6025 bio->bi_end_io = btrfs_end_dio_bio;
6026
6027 map_length = orig_bio->bi_size;
6028 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6029 &map_length, NULL, 0);
6030 if (ret) {
6031 bio_put(bio);
6032 goto out_err;
6033 }
6034 } else {
6035 submit_len += bvec->bv_len;
6036 nr_pages ++;
6037 bvec++;
6038 }
6039 }
6040
Josef Bacik02f57c72011-04-06 14:25:44 -04006041submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006042 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006043 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006044 if (!ret)
6045 return 0;
6046
6047 bio_put(bio);
6048out_err:
6049 dip->errors = 1;
6050 /*
6051 * before atomic variable goto zero, we must
6052 * make sure dip->errors is perceived to be set.
6053 */
6054 smp_mb__before_atomic_dec();
6055 if (atomic_dec_and_test(&dip->pending_bios))
6056 bio_io_error(dip->orig_bio);
6057
6058 /* bio_end_io() will handle error, so we needn't return it */
6059 return 0;
6060}
6061
Josef Bacik4b46fce2010-05-23 11:00:55 -04006062static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6063 loff_t file_offset)
6064{
6065 struct btrfs_root *root = BTRFS_I(inode)->root;
6066 struct btrfs_dio_private *dip;
6067 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006068 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006069 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006070 int ret = 0;
6071
6072 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6073
6074 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6075 if (!dip) {
6076 ret = -ENOMEM;
6077 goto free_ordered;
6078 }
6079 dip->csums = NULL;
6080
Josef Bacik98bc3142011-03-22 11:00:46 -04006081 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6082 if (!write && !skip_sum) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006083 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6084 if (!dip->csums) {
Daniel J Bluemanb4966b72011-03-09 16:46:42 +00006085 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006086 ret = -ENOMEM;
6087 goto free_ordered;
6088 }
6089 }
6090
6091 dip->private = bio->bi_private;
6092 dip->inode = inode;
6093 dip->logical_offset = file_offset;
6094
Josef Bacik4b46fce2010-05-23 11:00:55 -04006095 dip->bytes = 0;
6096 do {
6097 dip->bytes += bvec->bv_len;
6098 bvec++;
6099 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6100
Chris Mason46bfbb52010-05-26 11:04:10 -04006101 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006102 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006103 dip->errors = 0;
6104 dip->orig_bio = bio;
6105 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006106
6107 if (write)
6108 bio->bi_end_io = btrfs_endio_direct_write;
6109 else
6110 bio->bi_end_io = btrfs_endio_direct_read;
6111
Miao Xiee65e1532010-11-22 03:04:43 +00006112 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6113 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006114 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006115free_ordered:
6116 /*
6117 * If this is a write, we need to clean up the reserved space and kill
6118 * the ordered extent.
6119 */
6120 if (write) {
6121 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006122 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006123 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6124 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6125 btrfs_free_reserved_extent(root, ordered->start,
6126 ordered->disk_len);
6127 btrfs_put_ordered_extent(ordered);
6128 btrfs_put_ordered_extent(ordered);
6129 }
6130 bio_endio(bio, ret);
6131}
6132
Chris Mason5a5f79b2010-05-26 21:33:37 -04006133static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6134 const struct iovec *iov, loff_t offset,
6135 unsigned long nr_segs)
6136{
6137 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006138 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006139 size_t size;
6140 unsigned long addr;
6141 unsigned blocksize_mask = root->sectorsize - 1;
6142 ssize_t retval = -EINVAL;
6143 loff_t end = offset;
6144
6145 if (offset & blocksize_mask)
6146 goto out;
6147
6148 /* Check the memory alignment. Blocks cannot straddle pages */
6149 for (seg = 0; seg < nr_segs; seg++) {
6150 addr = (unsigned long)iov[seg].iov_base;
6151 size = iov[seg].iov_len;
6152 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006153 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006154 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006155
6156 /* If this is a write we don't need to check anymore */
6157 if (rw & WRITE)
6158 continue;
6159
6160 /*
6161 * Check to make sure we don't have duplicate iov_base's in this
6162 * iovec, if so return EINVAL, otherwise we'll get csum errors
6163 * when reading back.
6164 */
6165 for (i = seg + 1; i < nr_segs; i++) {
6166 if (iov[seg].iov_base == iov[i].iov_base)
6167 goto out;
6168 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006169 }
6170 retval = 0;
6171out:
6172 return retval;
6173}
Chris Mason16432982008-04-10 10:23:21 -04006174static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6175 const struct iovec *iov, loff_t offset,
6176 unsigned long nr_segs)
6177{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006178 struct file *file = iocb->ki_filp;
6179 struct inode *inode = file->f_mapping->host;
6180 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006181 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006182 u64 lockstart, lockend;
6183 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006184 int writing = rw & WRITE;
6185 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006186 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006187
Chris Mason5a5f79b2010-05-26 21:33:37 -04006188 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6189 offset, nr_segs)) {
6190 return 0;
6191 }
6192
Josef Bacik4b46fce2010-05-23 11:00:55 -04006193 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006194 lockend = offset + count - 1;
6195
6196 if (writing) {
6197 ret = btrfs_delalloc_reserve_space(inode, count);
6198 if (ret)
6199 goto out;
6200 }
Chris Mason4845e442010-05-25 20:56:50 -04006201
Josef Bacik4b46fce2010-05-23 11:00:55 -04006202 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006203 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6204 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006205 /*
6206 * We're concerned with the entire range that we're going to be
6207 * doing DIO to, so we need to make sure theres no ordered
6208 * extents in this range.
6209 */
6210 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6211 lockend - lockstart + 1);
6212 if (!ordered)
6213 break;
Chris Mason4845e442010-05-25 20:56:50 -04006214 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6215 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006216 btrfs_start_ordered_extent(inode, ordered, 1);
6217 btrfs_put_ordered_extent(ordered);
6218 cond_resched();
6219 }
6220
Chris Mason4845e442010-05-25 20:56:50 -04006221 /*
6222 * we don't use btrfs_set_extent_delalloc because we don't want
6223 * the dirty or uptodate bits
6224 */
6225 if (writing) {
6226 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6227 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6228 EXTENT_DELALLOC, 0, NULL, &cached_state,
6229 GFP_NOFS);
6230 if (ret) {
6231 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6232 lockend, EXTENT_LOCKED | write_bits,
6233 1, 0, &cached_state, GFP_NOFS);
6234 goto out;
6235 }
6236 }
6237
6238 free_extent_state(cached_state);
6239 cached_state = NULL;
6240
Chris Mason5a5f79b2010-05-26 21:33:37 -04006241 ret = __blockdev_direct_IO(rw, iocb, inode,
6242 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6243 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6244 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006245
6246 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006247 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6248 offset + iov_length(iov, nr_segs) - 1,
6249 EXTENT_LOCKED | write_bits, 1, 0,
6250 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006251 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6252 /*
6253 * We're falling back to buffered, unlock the section we didn't
6254 * do IO on.
6255 */
Chris Mason4845e442010-05-25 20:56:50 -04006256 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6257 offset + iov_length(iov, nr_segs) - 1,
6258 EXTENT_LOCKED | write_bits, 1, 0,
6259 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006260 }
Chris Mason4845e442010-05-25 20:56:50 -04006261out:
6262 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006263 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006264}
6265
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006266static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6267 __u64 start, __u64 len)
6268{
Chris Masonec29ed52011-02-23 16:23:20 -05006269 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006270}
6271
Chris Mason9ebefb182007-06-15 13:50:00 -04006272int btrfs_readpage(struct file *file, struct page *page)
6273{
Chris Masond1310b22008-01-24 16:13:08 -05006274 struct extent_io_tree *tree;
6275 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006276 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04006277}
Chris Mason1832a6d2007-12-21 16:27:21 -05006278
Chris Mason39279cc2007-06-12 06:35:45 -04006279static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6280{
Chris Masond1310b22008-01-24 16:13:08 -05006281 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006282
6283
6284 if (current->flags & PF_MEMALLOC) {
6285 redirty_page_for_writepage(wbc, page);
6286 unlock_page(page);
6287 return 0;
6288 }
Chris Masond1310b22008-01-24 16:13:08 -05006289 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006290 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6291}
Chris Mason39279cc2007-06-12 06:35:45 -04006292
Chris Masonf4219502008-07-22 11:18:09 -04006293int btrfs_writepages(struct address_space *mapping,
6294 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006295{
Chris Masond1310b22008-01-24 16:13:08 -05006296 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006297
Chris Masond1310b22008-01-24 16:13:08 -05006298 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006299 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6300}
6301
Chris Mason3ab2fb52007-11-08 10:59:22 -05006302static int
6303btrfs_readpages(struct file *file, struct address_space *mapping,
6304 struct list_head *pages, unsigned nr_pages)
6305{
Chris Masond1310b22008-01-24 16:13:08 -05006306 struct extent_io_tree *tree;
6307 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006308 return extent_readpages(tree, mapping, pages, nr_pages,
6309 btrfs_get_extent);
6310}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006311static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006312{
Chris Masond1310b22008-01-24 16:13:08 -05006313 struct extent_io_tree *tree;
6314 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006315 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006316
Chris Masond1310b22008-01-24 16:13:08 -05006317 tree = &BTRFS_I(page->mapping->host)->io_tree;
6318 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006319 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006320 if (ret == 1) {
6321 ClearPagePrivate(page);
6322 set_page_private(page, 0);
6323 page_cache_release(page);
6324 }
6325 return ret;
6326}
Chris Mason39279cc2007-06-12 06:35:45 -04006327
Chris Masone6dcd2d2008-07-17 12:53:50 -04006328static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6329{
Chris Mason98509cf2008-09-11 15:51:43 -04006330 if (PageWriteback(page) || PageDirty(page))
6331 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006332 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006333}
6334
Chris Masona52d9a82007-08-27 16:49:44 -04006335static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6336{
Chris Masond1310b22008-01-24 16:13:08 -05006337 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006338 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006339 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006340 u64 page_start = page_offset(page);
6341 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006342
Chris Mason8b62b722009-09-02 16:53:46 -04006343
6344 /*
6345 * we have the page locked, so new writeback can't start,
6346 * and the dirty bit won't be cleared while we are here.
6347 *
6348 * Wait for IO on this page so that we can safely clear
6349 * the PagePrivate2 bit and do ordered accounting
6350 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006351 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006352
Chris Masond1310b22008-01-24 16:13:08 -05006353 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006354 if (offset) {
6355 btrfs_releasepage(page, GFP_NOFS);
6356 return;
6357 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006358 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6359 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006360 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6361 page_offset(page));
6362 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006363 /*
6364 * IO on this page will never be started, so we need
6365 * to account for any ordered extents now
6366 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006367 clear_extent_bit(tree, page_start, page_end,
6368 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006369 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006370 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006371 /*
6372 * whoever cleared the private bit is responsible
6373 * for the finish_ordered_io
6374 */
6375 if (TestClearPagePrivate2(page)) {
6376 btrfs_finish_ordered_io(page->mapping->host,
6377 page_start, page_end);
6378 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006379 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006380 cached_state = NULL;
6381 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6382 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006383 }
6384 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006385 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006386 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006387 __btrfs_releasepage(page, GFP_NOFS);
6388
Chris Mason4a096752008-07-21 10:29:44 -04006389 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006390 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006391 ClearPagePrivate(page);
6392 set_page_private(page, 0);
6393 page_cache_release(page);
6394 }
Chris Mason39279cc2007-06-12 06:35:45 -04006395}
6396
Chris Mason9ebefb182007-06-15 13:50:00 -04006397/*
6398 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6399 * called from a page fault handler when a page is first dirtied. Hence we must
6400 * be careful to check for EOF conditions here. We set the page up correctly
6401 * for a written page which means we get ENOSPC checking when writing into
6402 * holes and correct delalloc and unwritten extent mapping on filesystems that
6403 * support these features.
6404 *
6405 * We are not allowed to take the i_mutex here so we have to play games to
6406 * protect against truncate races as the page could now be beyond EOF. Because
6407 * vmtruncate() writes the inode size before removing pages, once we have the
6408 * page lock we can determine safely if the page is beyond EOF. If it is not
6409 * beyond EOF, then the page is guaranteed safe against truncation until we
6410 * unlock the page.
6411 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006412int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006413{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006414 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006415 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006416 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006417 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6418 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006419 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006420 char *kaddr;
6421 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006422 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006423 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04006424 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006425 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006426
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006427 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07006428 if (ret) {
6429 if (ret == -ENOMEM)
6430 ret = VM_FAULT_OOM;
6431 else /* -ENOSPC, -EIO, etc */
6432 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05006433 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07006434 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006435
Nick Piggin56a76f82009-03-31 15:23:23 -07006436 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006437again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006438 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006439 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006440 page_start = page_offset(page);
6441 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006442
Chris Mason9ebefb182007-06-15 13:50:00 -04006443 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006444 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006445 /* page got truncated out from underneath us */
6446 goto out_unlock;
6447 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006448 wait_on_page_writeback(page);
6449
Josef Bacik2ac55d42010-02-03 19:33:23 +00006450 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6451 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006452 set_page_extent_mapped(page);
6453
Chris Masoneb84ae02008-07-17 13:53:27 -04006454 /*
6455 * we can't set the delalloc bits if there are pending ordered
6456 * extents. Drop our locks and wait for them to finish
6457 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006458 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6459 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006460 unlock_extent_cached(io_tree, page_start, page_end,
6461 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006462 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006463 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006464 btrfs_put_ordered_extent(ordered);
6465 goto again;
6466 }
6467
Josef Bacikfbf19082009-10-01 17:10:23 -04006468 /*
6469 * XXX - page_mkwrite gets called every time the page is dirtied, even
6470 * if it was already dirty, so for space accounting reasons we need to
6471 * clear any delalloc bits for the range we are fixing to save. There
6472 * is probably a better way to do this, but for now keep consistent with
6473 * prepare_pages in the normal write path.
6474 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006475 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006476 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006477 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006478
Josef Bacik2ac55d42010-02-03 19:33:23 +00006479 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6480 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006481 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006482 unlock_extent_cached(io_tree, page_start, page_end,
6483 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006484 ret = VM_FAULT_SIGBUS;
6485 goto out_unlock;
6486 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006487 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006488
6489 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006490 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006491 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006492 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006493 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006494
Chris Masone6dcd2d2008-07-17 12:53:50 -04006495 if (zero_start != PAGE_CACHE_SIZE) {
6496 kaddr = kmap(page);
6497 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6498 flush_dcache_page(page);
6499 kunmap(page);
6500 }
Chris Mason247e7432008-07-17 12:53:51 -04006501 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006502 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006503 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006504
Chris Mason257c62e2009-10-13 13:21:08 -04006505 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6506 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6507
Josef Bacik2ac55d42010-02-03 19:33:23 +00006508 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006509
6510out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006511 if (!ret)
6512 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006513 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006514 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05006515out:
Chris Mason9ebefb182007-06-15 13:50:00 -04006516 return ret;
6517}
6518
Josef Bacika41ad392011-01-31 15:30:16 -05006519static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006520{
6521 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006522 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006523 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006524 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006525 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04006526 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006527 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006528 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006529
Josef Bacik5d5e1032009-10-13 16:46:49 -04006530 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6531 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006532 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006533
Chris Mason4a096752008-07-21 10:29:44 -04006534 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006535 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006536
Josef Bacikfcb80c22011-05-03 10:40:22 -04006537 /*
6538 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6539 * 3 things going on here
6540 *
6541 * 1) We need to reserve space for our orphan item and the space to
6542 * delete our orphan item. Lord knows we don't want to have a dangling
6543 * orphan item because we didn't reserve space to remove it.
6544 *
6545 * 2) We need to reserve space to update our inode.
6546 *
6547 * 3) We need to have something to cache all the space that is going to
6548 * be free'd up by the truncate operation, but also have some slack
6549 * space reserved in case it uses space during the truncate (thank you
6550 * very much snapshotting).
6551 *
6552 * And we need these to all be seperate. The fact is we can use alot of
6553 * space doing the truncate, and we have no earthly idea how much space
6554 * we will use, so we need the truncate reservation to be seperate so it
6555 * doesn't end up using space reserved for updating the inode or
6556 * removing the orphan item. We also need to be able to stop the
6557 * transaction and start a new one, which means we need to be able to
6558 * update the inode several times, and we have no idea of knowing how
6559 * many times that will be, so we can't just reserve 1 item for the
6560 * entirety of the opration, so that has to be done seperately as well.
6561 * Then there is the orphan item, which does indeed need to be held on
6562 * to for the whole operation, and we need nobody to touch this reserved
6563 * space except the orphan code.
6564 *
6565 * So that leaves us with
6566 *
6567 * 1) root->orphan_block_rsv - for the orphan deletion.
6568 * 2) rsv - for the truncate reservation, which we will steal from the
6569 * transaction reservation.
6570 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6571 * updating the inode.
6572 */
6573 rsv = btrfs_alloc_block_rsv(root);
6574 if (!rsv)
6575 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006576 rsv->size = min_size;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006577
Josef Bacik907cbce2011-08-08 13:46:15 -04006578 /*
Josef Bacik07127182011-08-19 10:29:59 -04006579 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006580 * 1 for the orphan item we're going to add
6581 * 1 for the orphan item deletion
6582 * 1 for updating the inode.
6583 */
Josef Bacik07127182011-08-19 10:29:59 -04006584 trans = btrfs_start_transaction(root, 4);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006585 if (IS_ERR(trans)) {
6586 err = PTR_ERR(trans);
6587 goto out;
6588 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006589
Josef Bacik907cbce2011-08-08 13:46:15 -04006590 /* Migrate the slack space for the truncate to our reserve */
6591 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6592 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006593 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006594
6595 ret = btrfs_orphan_add(trans, inode);
6596 if (ret) {
6597 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006598 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006599 }
6600
Chris Mason5a3f23d2009-03-31 13:27:11 -04006601 /*
6602 * setattr is responsible for setting the ordered_data_close flag,
6603 * but that is only tested during the last file release. That
6604 * could happen well after the next commit, leaving a great big
6605 * window where new writes may get lost if someone chooses to write
6606 * to this file after truncating to zero
6607 *
6608 * The inode doesn't have any dirty data here, and so if we commit
6609 * this is a noop. If someone immediately starts writing to the inode
6610 * it is very likely we'll catch some of their writes in this
6611 * transaction, and the commit will find this file on the ordered
6612 * data list with good things to send down.
6613 *
6614 * This is a best effort solution, there is still a window where
6615 * using truncate to replace the contents of the file will
6616 * end up with a zero length file after a crash.
6617 */
6618 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6619 btrfs_add_ordered_operation(trans, root, inode);
6620
Yan, Zheng80825102009-11-12 09:35:36 +00006621 while (1) {
Josef Bacik36ba0222011-10-18 12:15:48 -04006622 ret = btrfs_block_rsv_refill(root, rsv, min_size);
Josef Bacik907cbce2011-08-08 13:46:15 -04006623 if (ret) {
6624 /*
6625 * This can only happen with the original transaction we
6626 * started above, every other time we shouldn't have a
6627 * transaction started yet.
6628 */
6629 if (ret == -EAGAIN)
6630 goto end_trans;
6631 err = ret;
6632 break;
6633 }
6634
Yan, Zhengd68fc572010-05-16 10:49:58 -04006635 if (!trans) {
Josef Bacik907cbce2011-08-08 13:46:15 -04006636 /* Just need the 1 for updating the inode */
6637 trans = btrfs_start_transaction(root, 1);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006638 if (IS_ERR(trans)) {
6639 err = PTR_ERR(trans);
6640 goto out;
6641 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006642 }
6643
Josef Bacik907cbce2011-08-08 13:46:15 -04006644 trans->block_rsv = rsv;
6645
Yan, Zheng80825102009-11-12 09:35:36 +00006646 ret = btrfs_truncate_inode_items(trans, root, inode,
6647 inode->i_size,
6648 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006649 if (ret != -EAGAIN) {
6650 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006651 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006652 }
Chris Mason39279cc2007-06-12 06:35:45 -04006653
Josef Bacikfcb80c22011-05-03 10:40:22 -04006654 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006655 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006656 if (ret) {
6657 err = ret;
6658 break;
6659 }
Josef Bacik907cbce2011-08-08 13:46:15 -04006660end_trans:
Yan, Zheng80825102009-11-12 09:35:36 +00006661 nr = trans->blocks_used;
6662 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006663 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006664 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006665 }
6666
6667 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04006668 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006669 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006670 if (ret)
6671 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006672 } else if (ret && inode->i_nlink > 0) {
6673 /*
6674 * Failed to do the truncate, remove us from the in memory
6675 * orphan list.
6676 */
6677 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006678 }
6679
Josef Bacikfcb80c22011-05-03 10:40:22 -04006680 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006681 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006682 if (ret && !err)
6683 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006684
Josef Bacik7b128762008-07-24 12:17:14 -04006685 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04006686 ret = btrfs_end_transaction_throttle(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006687 btrfs_btree_balance_dirty(root, nr);
6688
6689out:
6690 btrfs_free_block_rsv(root, rsv);
6691
Josef Bacik3893e332011-01-31 16:03:11 -05006692 if (ret && !err)
6693 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05006694
Josef Bacik3893e332011-01-31 16:03:11 -05006695 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006696}
6697
Sven Wegener3b963622008-06-09 21:57:42 -04006698/*
Chris Masond352ac62008-09-29 15:18:18 -04006699 * create a new subvolume directory/inode (helper for the ioctl).
6700 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006701int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006702 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04006703{
Chris Mason39279cc2007-06-12 06:35:45 -04006704 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006705 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006706 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006707
Josef Bacikaec74772008-07-24 12:12:38 -04006708 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006709 new_dirid, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006710 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006711 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006712 inode->i_op = &btrfs_dir_inode_operations;
6713 inode->i_fop = &btrfs_dir_file_operations;
6714
Chris Mason39279cc2007-06-12 06:35:45 -04006715 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006716 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006717
Yan, Zheng76dda932009-09-21 16:00:26 -04006718 err = btrfs_update_inode(trans, new_root, inode);
6719 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006720
Yan, Zheng76dda932009-09-21 16:00:26 -04006721 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006722 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006723}
6724
Chris Mason39279cc2007-06-12 06:35:45 -04006725struct inode *btrfs_alloc_inode(struct super_block *sb)
6726{
6727 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006728 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006729
6730 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6731 if (!ei)
6732 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006733
6734 ei->root = NULL;
6735 ei->space_info = NULL;
6736 ei->generation = 0;
6737 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006738 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006739 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006740 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006741 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006742 ei->disk_i_size = 0;
6743 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04006744 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006745 ei->index_cnt = (u64)-1;
6746 ei->last_unlink_trans = 0;
6747
Josef Bacik9e0baf62011-07-15 15:16:44 +00006748 spin_lock_init(&ei->lock);
6749 ei->outstanding_extents = 0;
6750 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006751
6752 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006753 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006754 ei->dummy_inode = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04006755 ei->in_defrag = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006756 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006757
Miao Xie16cdcec2011-04-22 18:12:22 +08006758 ei->delayed_node = NULL;
6759
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006760 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02006761 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02006762 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6763 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006764 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006765 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006766 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006767 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006768 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006769 RB_CLEAR_NODE(&ei->rb_node);
6770
6771 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006772}
6773
Nick Pigginfa0d7e32011-01-07 17:49:49 +11006774static void btrfs_i_callback(struct rcu_head *head)
6775{
6776 struct inode *inode = container_of(head, struct inode, i_rcu);
6777 INIT_LIST_HEAD(&inode->i_dentry);
6778 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6779}
6780
Chris Mason39279cc2007-06-12 06:35:45 -04006781void btrfs_destroy_inode(struct inode *inode)
6782{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006783 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006784 struct btrfs_root *root = BTRFS_I(inode)->root;
6785
Chris Mason39279cc2007-06-12 06:35:45 -04006786 WARN_ON(!list_empty(&inode->i_dentry));
6787 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006788 WARN_ON(BTRFS_I(inode)->outstanding_extents);
6789 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04006790 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
6791 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04006792
Chris Mason5a3f23d2009-03-31 13:27:11 -04006793 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006794 * This can happen where we create an inode, but somebody else also
6795 * created the same inode and we need to destroy the one we already
6796 * created.
6797 */
6798 if (!root)
6799 goto free;
6800
6801 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006802 * Make sure we're properly removed from the ordered operation
6803 * lists.
6804 */
6805 smp_mb();
6806 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6807 spin_lock(&root->fs_info->ordered_extent_lock);
6808 list_del_init(&BTRFS_I(inode)->ordered_operations);
6809 spin_unlock(&root->fs_info->ordered_extent_lock);
6810 }
6811
Yan, Zhengd68fc572010-05-16 10:49:58 -04006812 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006813 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Li Zefan33345d012011-04-20 10:31:50 +08006814 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6815 (unsigned long long)btrfs_ino(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00006816 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006817 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006818 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006819
Chris Masond3977122009-01-05 21:25:51 -05006820 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006821 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6822 if (!ordered)
6823 break;
6824 else {
Chris Masond3977122009-01-05 21:25:51 -05006825 printk(KERN_ERR "btrfs found ordered "
6826 "extent %llu %llu on inode cleanup\n",
6827 (unsigned long long)ordered->file_offset,
6828 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006829 btrfs_remove_ordered_extent(inode, ordered);
6830 btrfs_put_ordered_extent(ordered);
6831 btrfs_put_ordered_extent(ordered);
6832 }
6833 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006834 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006835 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006836free:
Miao Xie16cdcec2011-04-22 18:12:22 +08006837 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e32011-01-07 17:49:49 +11006838 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04006839}
6840
Al Viro45321ac2010-06-07 13:43:19 -04006841int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006842{
6843 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006844
Josef Bacik0af3d002010-06-21 14:48:16 -04006845 if (btrfs_root_refs(&root->root_item) == 0 &&
Chris Mason2cf85722011-07-26 15:35:09 -04006846 !btrfs_is_free_space_inode(root, inode))
Al Viro45321ac2010-06-07 13:43:19 -04006847 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006848 else
Al Viro45321ac2010-06-07 13:43:19 -04006849 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006850}
6851
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006852static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006853{
6854 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6855
6856 inode_init_once(&ei->vfs_inode);
6857}
6858
6859void btrfs_destroy_cachep(void)
6860{
6861 if (btrfs_inode_cachep)
6862 kmem_cache_destroy(btrfs_inode_cachep);
6863 if (btrfs_trans_handle_cachep)
6864 kmem_cache_destroy(btrfs_trans_handle_cachep);
6865 if (btrfs_transaction_cachep)
6866 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006867 if (btrfs_path_cachep)
6868 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05006869 if (btrfs_free_space_cachep)
6870 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006871}
6872
6873int btrfs_init_cachep(void)
6874{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006875 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6876 sizeof(struct btrfs_inode), 0,
6877 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006878 if (!btrfs_inode_cachep)
6879 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006880
6881 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6882 sizeof(struct btrfs_trans_handle), 0,
6883 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006884 if (!btrfs_trans_handle_cachep)
6885 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006886
6887 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6888 sizeof(struct btrfs_transaction), 0,
6889 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006890 if (!btrfs_transaction_cachep)
6891 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006892
6893 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6894 sizeof(struct btrfs_path), 0,
6895 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006896 if (!btrfs_path_cachep)
6897 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006898
Josef Bacikdc89e982011-01-28 17:05:48 -05006899 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
6900 sizeof(struct btrfs_free_space), 0,
6901 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
6902 if (!btrfs_free_space_cachep)
6903 goto fail;
6904
Chris Mason39279cc2007-06-12 06:35:45 -04006905 return 0;
6906fail:
6907 btrfs_destroy_cachep();
6908 return -ENOMEM;
6909}
6910
6911static int btrfs_getattr(struct vfsmount *mnt,
6912 struct dentry *dentry, struct kstat *stat)
6913{
6914 struct inode *inode = dentry->d_inode;
6915 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04006916 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05006917 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006918 stat->blocks = (inode_get_bytes(inode) +
6919 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04006920 return 0;
6921}
6922
Liu Bo75e7cb72011-03-22 10:12:20 +00006923/*
6924 * If a file is moved, it will inherit the cow and compression flags of the new
6925 * directory.
6926 */
6927static void fixup_inode_flags(struct inode *dir, struct inode *inode)
6928{
6929 struct btrfs_inode *b_dir = BTRFS_I(dir);
6930 struct btrfs_inode *b_inode = BTRFS_I(inode);
6931
6932 if (b_dir->flags & BTRFS_INODE_NODATACOW)
6933 b_inode->flags |= BTRFS_INODE_NODATACOW;
6934 else
6935 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
6936
6937 if (b_dir->flags & BTRFS_INODE_COMPRESS)
6938 b_inode->flags |= BTRFS_INODE_COMPRESS;
6939 else
6940 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
6941}
6942
Chris Masond3977122009-01-05 21:25:51 -05006943static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6944 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04006945{
6946 struct btrfs_trans_handle *trans;
6947 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006948 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04006949 struct inode *new_inode = new_dentry->d_inode;
6950 struct inode *old_inode = old_dentry->d_inode;
6951 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006952 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006953 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04006954 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08006955 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006956
Li Zefan33345d012011-04-20 10:31:50 +08006957 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04006958 return -EPERM;
6959
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006960 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08006961 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05006962 return -EXDEV;
6963
Li Zefan33345d012011-04-20 10:31:50 +08006964 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
6965 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006966 return -ENOTEMPTY;
6967
Chris Mason39279cc2007-06-12 06:35:45 -04006968 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006969 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04006970 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006971 /*
6972 * we're using rename to replace one file with another.
6973 * and the replacement file is large. Start IO on it now so
6974 * we don't add too much work to the end of the transaction
6975 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04006976 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04006977 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
6978 filemap_flush(old_inode->i_mapping);
6979
Yan, Zheng76dda932009-09-21 16:00:26 -04006980 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08006981 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04006982 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006983 /*
6984 * We want to reserve the absolute worst case amount of items. So if
6985 * both inodes are subvols and we need to unlink them then that would
6986 * require 4 item modifications, but if they are both normal inodes it
6987 * would require 5 item modifications, so we'll assume their normal
6988 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6989 * should cover the worst case number of items we'll modify.
6990 */
6991 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00006992 if (IS_ERR(trans)) {
6993 ret = PTR_ERR(trans);
6994 goto out_notrans;
6995 }
Chris Mason5f39d392007-10-15 16:14:19 -04006996
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006997 if (dest != root)
6998 btrfs_record_root_in_trans(trans, dest);
6999
Yan, Zhenga5719522009-09-24 09:17:31 -04007000 ret = btrfs_set_inode_index(new_dir, &index);
7001 if (ret)
7002 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007003
Li Zefan33345d012011-04-20 10:31:50 +08007004 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007005 /* force full log commit if subvolume involved. */
7006 root->fs_info->last_trans_log_full_commit = trans->transid;
7007 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007008 ret = btrfs_insert_inode_ref(trans, dest,
7009 new_dentry->d_name.name,
7010 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007011 old_ino,
7012 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007013 if (ret)
7014 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007015 /*
7016 * this is an ugly little race, but the rename is required
7017 * to make sure that if we crash, the inode is either at the
7018 * old name or the new one. pinning the log transaction lets
7019 * us make sure we don't allow a log commit to come in after
7020 * we unlink the name but before we add the new name back in.
7021 */
7022 btrfs_pin_log_trans(root);
7023 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007024 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007025 * make sure the inode gets flushed if it is replacing
7026 * something.
7027 */
Li Zefan33345d012011-04-20 10:31:50 +08007028 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007029 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007030
Chris Mason39279cc2007-06-12 06:35:45 -04007031 old_dir->i_ctime = old_dir->i_mtime = ctime;
7032 new_dir->i_ctime = new_dir->i_mtime = ctime;
7033 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007034
Chris Mason12fcfd22009-03-24 10:24:20 -04007035 if (old_dentry->d_parent != new_dentry->d_parent)
7036 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7037
Li Zefan33345d012011-04-20 10:31:50 +08007038 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007039 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7040 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7041 old_dentry->d_name.name,
7042 old_dentry->d_name.len);
7043 } else {
Al Viro92986792011-03-04 17:14:37 +00007044 ret = __btrfs_unlink_inode(trans, root, old_dir,
7045 old_dentry->d_inode,
7046 old_dentry->d_name.name,
7047 old_dentry->d_name.len);
7048 if (!ret)
7049 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007050 }
7051 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04007052
7053 if (new_inode) {
7054 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007055 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007056 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7057 root_objectid = BTRFS_I(new_inode)->location.objectid;
7058 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7059 root_objectid,
7060 new_dentry->d_name.name,
7061 new_dentry->d_name.len);
7062 BUG_ON(new_inode->i_nlink == 0);
7063 } else {
7064 ret = btrfs_unlink_inode(trans, dest, new_dir,
7065 new_dentry->d_inode,
7066 new_dentry->d_name.name,
7067 new_dentry->d_name.len);
7068 }
7069 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007070 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007071 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007072 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007073 }
Chris Mason39279cc2007-06-12 06:35:45 -04007074 }
Josef Bacikaec74772008-07-24 12:12:38 -04007075
Liu Bo75e7cb72011-03-22 10:12:20 +00007076 fixup_inode_flags(new_dir, old_inode);
7077
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007078 ret = btrfs_add_link(trans, new_dir, old_inode,
7079 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007080 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007081 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04007082
Li Zefan33345d012011-04-20 10:31:50 +08007083 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007084 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007085 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007086 btrfs_end_log_trans(root);
7087 }
Chris Mason39279cc2007-06-12 06:35:45 -04007088out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04007089 btrfs_end_transaction_throttle(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007090out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007091 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007092 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007093
Chris Mason39279cc2007-06-12 06:35:45 -04007094 return ret;
7095}
7096
Chris Masond352ac62008-09-29 15:18:18 -04007097/*
7098 * some fairly slow code that needs optimization. This walks the list
7099 * of all the inodes with pending delalloc and forces them to disk.
7100 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007101int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007102{
7103 struct list_head *head = &root->fs_info->delalloc_inodes;
7104 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007105 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007106
Yan Zhengc146afa2008-11-12 14:34:12 -05007107 if (root->fs_info->sb->s_flags & MS_RDONLY)
7108 return -EROFS;
7109
Chris Mason75eff682008-12-15 15:54:40 -05007110 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007111 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007112 binode = list_entry(head->next, struct btrfs_inode,
7113 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007114 inode = igrab(&binode->vfs_inode);
7115 if (!inode)
7116 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007117 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007118 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007119 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007120 if (delay_iput)
7121 btrfs_add_delayed_iput(inode);
7122 else
7123 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007124 }
7125 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007126 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007127 }
Chris Mason75eff682008-12-15 15:54:40 -05007128 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007129
7130 /* the filemap_flush will queue IO into the worker threads, but
7131 * we have to make sure the IO is actually started and that
7132 * ordered extents get created before we return
7133 */
7134 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007135 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007136 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007137 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007138 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7139 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007140 }
7141 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007142 return 0;
7143}
7144
Chris Mason39279cc2007-06-12 06:35:45 -04007145static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7146 const char *symname)
7147{
7148 struct btrfs_trans_handle *trans;
7149 struct btrfs_root *root = BTRFS_I(dir)->root;
7150 struct btrfs_path *path;
7151 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007152 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007153 int err;
7154 int drop_inode = 0;
7155 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007156 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007157 int name_len;
7158 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007159 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007160 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007161 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007162 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007163
7164 name_len = strlen(symname) + 1;
7165 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7166 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007167
Josef Bacik9ed74f22009-09-11 16:12:44 -04007168 /*
7169 * 2 items for inode item and ref
7170 * 2 items for dir items
7171 * 1 item for xattr if selinux is on
7172 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007173 trans = btrfs_start_transaction(root, 5);
7174 if (IS_ERR(trans))
7175 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007176
Li Zefan581bb052011-04-20 10:06:11 +08007177 err = btrfs_find_free_ino(root, &objectid);
7178 if (err)
7179 goto out_unlock;
7180
Josef Bacikaec74772008-07-24 12:12:38 -04007181 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007182 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007183 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007184 if (IS_ERR(inode)) {
7185 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007186 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007187 }
Chris Mason39279cc2007-06-12 06:35:45 -04007188
Eric Paris2a7dba32011-02-01 11:05:39 -05007189 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007190 if (err) {
7191 drop_inode = 1;
7192 goto out_unlock;
7193 }
7194
Josef Bacika1b075d2010-11-19 20:36:11 +00007195 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007196 if (err)
7197 drop_inode = 1;
7198 else {
7199 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007200 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04007201 inode->i_fop = &btrfs_file_operations;
7202 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05007203 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007204 }
Chris Mason39279cc2007-06-12 06:35:45 -04007205 if (drop_inode)
7206 goto out_unlock;
7207
7208 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007209 if (!path) {
7210 err = -ENOMEM;
7211 drop_inode = 1;
7212 goto out_unlock;
7213 }
Li Zefan33345d012011-04-20 10:31:50 +08007214 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007215 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007216 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7217 datasize = btrfs_file_extent_calc_inline_size(name_len);
7218 err = btrfs_insert_empty_item(trans, root, path, &key,
7219 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007220 if (err) {
7221 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007222 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007223 goto out_unlock;
7224 }
Chris Mason5f39d392007-10-15 16:14:19 -04007225 leaf = path->nodes[0];
7226 ei = btrfs_item_ptr(leaf, path->slots[0],
7227 struct btrfs_file_extent_item);
7228 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7229 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007230 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007231 btrfs_set_file_extent_encryption(leaf, ei, 0);
7232 btrfs_set_file_extent_compression(leaf, ei, 0);
7233 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7234 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7235
Chris Mason39279cc2007-06-12 06:35:45 -04007236 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007237 write_extent_buffer(leaf, symname, ptr, name_len);
7238 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007239 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007240
Chris Mason39279cc2007-06-12 06:35:45 -04007241 inode->i_op = &btrfs_symlink_inode_operations;
7242 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007243 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007244 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007245 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007246 err = btrfs_update_inode(trans, root, inode);
7247 if (err)
7248 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007249
7250out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04007251 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04007252 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007253 if (drop_inode) {
7254 inode_dec_link_count(inode);
7255 iput(inode);
7256 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04007257 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007258 return err;
7259}
Chris Mason16432982008-04-10 10:23:21 -04007260
Josef Bacik0af3d002010-06-21 14:48:16 -04007261static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7262 u64 start, u64 num_bytes, u64 min_size,
7263 loff_t actual_len, u64 *alloc_hint,
7264 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007265{
Yan Zhengd899e052008-10-30 14:25:28 -04007266 struct btrfs_root *root = BTRFS_I(inode)->root;
7267 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007268 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007269 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007270 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007271 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007272
Josef Bacik0af3d002010-06-21 14:48:16 -04007273 if (trans)
7274 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007275 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007276 if (own_trans) {
7277 trans = btrfs_start_transaction(root, 3);
7278 if (IS_ERR(trans)) {
7279 ret = PTR_ERR(trans);
7280 break;
7281 }
Yan Zhengd899e052008-10-30 14:25:28 -04007282 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007283
Yan, Zhengefa56462010-05-16 10:49:59 -04007284 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7285 0, *alloc_hint, (u64)-1, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007286 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007287 if (own_trans)
7288 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007289 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007290 }
7291
Yan Zhengd899e052008-10-30 14:25:28 -04007292 ret = insert_reserved_file_extent(trans, inode,
7293 cur_offset, ins.objectid,
7294 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007295 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007296 BTRFS_FILE_EXTENT_PREALLOC);
7297 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04007298 btrfs_drop_extent_cache(inode, cur_offset,
7299 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007300
Yan Zhengd899e052008-10-30 14:25:28 -04007301 num_bytes -= ins.offset;
7302 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007303 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007304
Yan Zhengd899e052008-10-30 14:25:28 -04007305 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007306 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007307 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007308 (actual_len > inode->i_size) &&
7309 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007310 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007311 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007312 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007313 i_size = cur_offset;
7314 i_size_write(inode, i_size);
7315 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007316 }
7317
Yan Zhengd899e052008-10-30 14:25:28 -04007318 ret = btrfs_update_inode(trans, root, inode);
7319 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04007320
Josef Bacik0af3d002010-06-21 14:48:16 -04007321 if (own_trans)
7322 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007323 }
Yan Zhengd899e052008-10-30 14:25:28 -04007324 return ret;
7325}
7326
Josef Bacik0af3d002010-06-21 14:48:16 -04007327int btrfs_prealloc_file_range(struct inode *inode, int mode,
7328 u64 start, u64 num_bytes, u64 min_size,
7329 loff_t actual_len, u64 *alloc_hint)
7330{
7331 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7332 min_size, actual_len, alloc_hint,
7333 NULL);
7334}
7335
7336int btrfs_prealloc_file_range_trans(struct inode *inode,
7337 struct btrfs_trans_handle *trans, int mode,
7338 u64 start, u64 num_bytes, u64 min_size,
7339 loff_t actual_len, u64 *alloc_hint)
7340{
7341 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7342 min_size, actual_len, alloc_hint, trans);
7343}
7344
Chris Masone6dcd2d2008-07-17 12:53:50 -04007345static int btrfs_set_page_dirty(struct page *page)
7346{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007347 return __set_page_dirty_nobuffers(page);
7348}
7349
Al Viro10556cb2011-06-20 19:28:19 -04007350static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007351{
Li Zefanb83cc962010-12-20 16:04:08 +08007352 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007353 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007354
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007355 if (mask & MAY_WRITE &&
7356 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7357 if (btrfs_root_readonly(root))
7358 return -EROFS;
7359 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7360 return -EACCES;
7361 }
Al Viro2830ba72011-06-20 19:16:29 -04007362 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007363}
Chris Mason39279cc2007-06-12 06:35:45 -04007364
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007365static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007366 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007367 .lookup = btrfs_lookup,
7368 .create = btrfs_create,
7369 .unlink = btrfs_unlink,
7370 .link = btrfs_link,
7371 .mkdir = btrfs_mkdir,
7372 .rmdir = btrfs_rmdir,
7373 .rename = btrfs_rename,
7374 .symlink = btrfs_symlink,
7375 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007376 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007377 .setxattr = btrfs_setxattr,
7378 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007379 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007380 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007381 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007382 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007383};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007384static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007385 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007386 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007387 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007388};
Yan, Zheng76dda932009-09-21 16:00:26 -04007389
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007390static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007391 .llseek = generic_file_llseek,
7392 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007393 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007394 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007395#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007396 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007397#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007398 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007399 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007400};
7401
Chris Masond1310b22008-01-24 16:13:08 -05007402static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007403 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007404 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007405 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007406 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007407 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007408 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04007409 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007410 .set_bit_hook = btrfs_set_bit_hook,
7411 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007412 .merge_extent_hook = btrfs_merge_extent_hook,
7413 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007414};
7415
Chris Mason35054392009-01-21 13:11:13 -05007416/*
7417 * btrfs doesn't support the bmap operation because swapfiles
7418 * use bmap to make a mapping of extents in the file. They assume
7419 * these extents won't change over the life of the file and they
7420 * use the bmap result to do IO directly to the drive.
7421 *
7422 * the btrfs bmap call would return logical addresses that aren't
7423 * suitable for IO and they also will change frequently as COW
7424 * operations happen. So, swapfile + btrfs == corruption.
7425 *
7426 * For now we're avoiding this by dropping bmap.
7427 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007428static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007429 .readpage = btrfs_readpage,
7430 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007431 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007432 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007433 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007434 .invalidatepage = btrfs_invalidatepage,
7435 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007436 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007437 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007438};
7439
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007440static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007441 .readpage = btrfs_readpage,
7442 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007443 .invalidatepage = btrfs_invalidatepage,
7444 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007445};
7446
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007447static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007448 .getattr = btrfs_getattr,
7449 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007450 .setxattr = btrfs_setxattr,
7451 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007452 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007453 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007454 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007455 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007456 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007457};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007458static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007459 .getattr = btrfs_getattr,
7460 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007461 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007462 .setxattr = btrfs_setxattr,
7463 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007464 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007465 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007466 .get_acl = btrfs_get_acl,
Josef Bacik618e21d2007-07-11 10:18:17 -04007467};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007468static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007469 .readlink = generic_readlink,
7470 .follow_link = page_follow_link_light,
7471 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007472 .getattr = btrfs_getattr,
Yanfdebe2b2008-01-14 13:26:08 -05007473 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007474 .setxattr = btrfs_setxattr,
7475 .getxattr = btrfs_getxattr,
7476 .listxattr = btrfs_listxattr,
7477 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007478 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007479};
Yan, Zheng76dda932009-09-21 16:00:26 -04007480
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007481const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007482 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007483 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04007484};