blob: d4a9195c7f0d0221d0eac073da01fb2e8918923f [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 Masone6dcd2d2008-07-17 12:53:50 -040048#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040049#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040050#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020051#include "volumes.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);
Chris Mason21151332011-11-10 20:39:08 -050096static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
97 struct btrfs_root *root, struct inode *inode);
Josef Bacik7b128762008-07-24 12:17:14 -040098
Yan, Zhengf34f57a2009-11-12 09:35:27 +000099static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500100 struct inode *inode, struct inode *dir,
101 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500102{
103 int err;
104
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000105 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500106 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500107 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500108 return err;
109}
110
Chris Masond352ac62008-09-29 15:18:18 -0400111/*
Chris Masonc8b97812008-10-29 14:49:59 -0400112 * this does all the hard work for inserting an inline extent into
113 * the btree. The caller should have done a btrfs_drop_extents so that
114 * no overlapping inline items exist in the btree
115 */
Chris Masond3977122009-01-05 21:25:51 -0500116static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400117 struct btrfs_root *root, struct inode *inode,
118 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000119 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400120 struct page **compressed_pages)
121{
122 struct btrfs_key key;
123 struct btrfs_path *path;
124 struct extent_buffer *leaf;
125 struct page *page = NULL;
126 char *kaddr;
127 unsigned long ptr;
128 struct btrfs_file_extent_item *ei;
129 int err = 0;
130 int ret;
131 size_t cur_size = size;
132 size_t datasize;
133 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400134
Li Zefanfe3f5662011-03-28 08:30:38 +0000135 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400136 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400137
Chris Masond3977122009-01-05 21:25:51 -0500138 path = btrfs_alloc_path();
139 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400140 return -ENOMEM;
141
Chris Masonb9473432009-03-13 11:00:37 -0400142 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400143
Li Zefan33345d012011-04-20 10:31:50 +0800144 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400145 key.offset = start;
146 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400147 datasize = btrfs_file_extent_calc_inline_size(cur_size);
148
149 inode_add_bytes(inode, size);
150 ret = btrfs_insert_empty_item(trans, root, path, &key,
151 datasize);
152 BUG_ON(ret);
153 if (ret) {
154 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400155 goto fail;
156 }
157 leaf = path->nodes[0];
158 ei = btrfs_item_ptr(leaf, path->slots[0],
159 struct btrfs_file_extent_item);
160 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
161 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
162 btrfs_set_file_extent_encryption(leaf, ei, 0);
163 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
164 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
165 ptr = btrfs_file_extent_inline_start(ei);
166
Li Zefan261507a02010-12-17 14:21:50 +0800167 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400168 struct page *cpage;
169 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500170 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400171 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500172 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400173 PAGE_CACHE_SIZE);
174
Chris Masonb9473432009-03-13 11:00:37 -0400175 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400176 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400177 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400178
179 i++;
180 ptr += cur_size;
181 compressed_size -= cur_size;
182 }
183 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800184 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400185 } else {
186 page = find_get_page(inode->i_mapping,
187 start >> PAGE_CACHE_SHIFT);
188 btrfs_set_file_extent_compression(leaf, ei, 0);
189 kaddr = kmap_atomic(page, KM_USER0);
190 offset = start & (PAGE_CACHE_SIZE - 1);
191 write_extent_buffer(leaf, kaddr + offset, ptr, size);
192 kunmap_atomic(kaddr, KM_USER0);
193 page_cache_release(page);
194 }
195 btrfs_mark_buffer_dirty(leaf);
196 btrfs_free_path(path);
197
Yan, Zhengc2167752009-11-12 09:34:21 +0000198 /*
199 * we're an inline extent, so nobody can
200 * extend the file past i_size without locking
201 * a page we already have locked.
202 *
203 * We must do any isize and inode updates
204 * before we unlock the pages. Otherwise we
205 * could end up racing with unlink.
206 */
Chris Masonc8b97812008-10-29 14:49:59 -0400207 BTRFS_I(inode)->disk_i_size = inode->i_size;
208 btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000209
Chris Masonc8b97812008-10-29 14:49:59 -0400210 return 0;
211fail:
212 btrfs_free_path(path);
213 return err;
214}
215
216
217/*
218 * conditionally insert an inline extent into the file. This
219 * does the checks required to make sure the data is small enough
220 * to fit as an inline extent.
221 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400222static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400223 struct btrfs_root *root,
224 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000225 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400226 struct page **compressed_pages)
227{
228 u64 isize = i_size_read(inode);
229 u64 actual_end = min(end + 1, isize);
230 u64 inline_len = actual_end - start;
231 u64 aligned_end = (end + root->sectorsize - 1) &
232 ~((u64)root->sectorsize - 1);
233 u64 hint_byte;
234 u64 data_len = inline_len;
235 int ret;
236
237 if (compressed_size)
238 data_len = compressed_size;
239
240 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400241 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400242 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
243 (!compressed_size &&
244 (actual_end & (root->sectorsize - 1)) == 0) ||
245 end + 1 < isize ||
246 data_len > root->fs_info->max_inline) {
247 return 1;
248 }
249
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000250 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400251 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400252 BUG_ON(ret);
253
254 if (isize > actual_end)
255 inline_len = min_t(u64, isize, actual_end);
256 ret = insert_inline_extent(trans, root, inode, start,
257 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000258 compress_type, compressed_pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400259 BUG_ON(ret);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400260 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400261 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400262 return 0;
263}
264
Chris Mason771ed682008-11-06 22:02:51 -0500265struct async_extent {
266 u64 start;
267 u64 ram_size;
268 u64 compressed_size;
269 struct page **pages;
270 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800271 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500272 struct list_head list;
273};
274
275struct async_cow {
276 struct inode *inode;
277 struct btrfs_root *root;
278 struct page *locked_page;
279 u64 start;
280 u64 end;
281 struct list_head extents;
282 struct btrfs_work work;
283};
284
285static noinline int add_async_extent(struct async_cow *cow,
286 u64 start, u64 ram_size,
287 u64 compressed_size,
288 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800289 unsigned long nr_pages,
290 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500291{
292 struct async_extent *async_extent;
293
294 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Yoshinori Sanodac97e52011-02-15 12:01:42 +0000295 BUG_ON(!async_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500296 async_extent->start = start;
297 async_extent->ram_size = ram_size;
298 async_extent->compressed_size = compressed_size;
299 async_extent->pages = pages;
300 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800301 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500302 list_add_tail(&async_extent->list, &cow->extents);
303 return 0;
304}
305
Chris Masonc8b97812008-10-29 14:49:59 -0400306/*
Chris Mason771ed682008-11-06 22:02:51 -0500307 * we create compressed extents in two phases. The first
308 * phase compresses a range of pages that have already been
309 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400310 *
Chris Mason771ed682008-11-06 22:02:51 -0500311 * This is done inside an ordered work queue, and the compression
312 * is spread across many cpus. The actual IO submission is step
313 * two, and the ordered work queue takes care of making sure that
314 * happens in the same order things were put onto the queue by
315 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400316 *
Chris Mason771ed682008-11-06 22:02:51 -0500317 * If this code finds it can't get good compression, it puts an
318 * entry onto the work queue to write the uncompressed bytes. This
319 * makes sure that both compressed inodes and uncompressed inodes
320 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400321 */
Chris Mason771ed682008-11-06 22:02:51 -0500322static noinline int compress_file_range(struct inode *inode,
323 struct page *locked_page,
324 u64 start, u64 end,
325 struct async_cow *async_cow,
326 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400327{
328 struct btrfs_root *root = BTRFS_I(inode)->root;
329 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400330 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400331 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400332 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500333 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400334 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400335 struct page **pages = NULL;
336 unsigned long nr_pages;
337 unsigned long nr_pages_ret = 0;
338 unsigned long total_compressed = 0;
339 unsigned long total_in = 0;
340 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500341 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400342 int i;
343 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800344 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400345
Chris Mason4cb53002011-05-24 15:35:30 -0400346 /* if this is a small write inside eof, kick off a defragbot */
347 if (end <= BTRFS_I(inode)->disk_i_size && (end - start + 1) < 16 * 1024)
348 btrfs_add_inode_defrag(NULL, inode);
349
Chris Mason42dc7ba2008-12-15 11:44:56 -0500350 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400351again:
352 will_compress = 0;
353 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
354 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
355
Chris Masonf03d9302009-02-04 09:31:06 -0500356 /*
357 * we don't want to send crud past the end of i_size through
358 * compression, that's just a waste of CPU time. So, if the
359 * end of the file is before the start of our current
360 * requested range of bytes, we bail out to the uncompressed
361 * cleanup code that can deal with all of this.
362 *
363 * It isn't really the fastest way to fix things, but this is a
364 * very uncommon corner.
365 */
366 if (actual_end <= start)
367 goto cleanup_and_bail_uncompressed;
368
Chris Masonc8b97812008-10-29 14:49:59 -0400369 total_compressed = actual_end - start;
370
371 /* we want to make sure that amount of ram required to uncompress
372 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500373 * of a compressed extent to 128k. This is a crucial number
374 * because it also controls how easily we can spread reads across
375 * cpus for decompression.
376 *
377 * We also want to make sure the amount of IO required to do
378 * a random read is reasonably small, so we limit the size of
379 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400380 */
381 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400382 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500383 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400384 total_in = 0;
385 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400386
Chris Mason771ed682008-11-06 22:02:51 -0500387 /*
388 * we do compression for mount -o compress and when the
389 * inode has not been flagged as nocompress. This flag can
390 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400391 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200392 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500393 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000394 (BTRFS_I(inode)->force_compress) ||
395 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400396 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400397 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800398 if (!pages) {
399 /* just bail out to the uncompressed code */
400 goto cont;
401 }
Chris Mason179e29e2007-11-01 11:28:41 -0400402
Li Zefan261507a02010-12-17 14:21:50 +0800403 if (BTRFS_I(inode)->force_compress)
404 compress_type = BTRFS_I(inode)->force_compress;
405
406 ret = btrfs_compress_pages(compress_type,
407 inode->i_mapping, start,
408 total_compressed, pages,
409 nr_pages, &nr_pages_ret,
410 &total_in,
411 &total_compressed,
412 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400413
414 if (!ret) {
415 unsigned long offset = total_compressed &
416 (PAGE_CACHE_SIZE - 1);
417 struct page *page = pages[nr_pages_ret - 1];
418 char *kaddr;
419
420 /* zero the tail end of the last page, we might be
421 * sending it down to disk
422 */
423 if (offset) {
424 kaddr = kmap_atomic(page, KM_USER0);
425 memset(kaddr + offset, 0,
426 PAGE_CACHE_SIZE - offset);
427 kunmap_atomic(kaddr, KM_USER0);
428 }
429 will_compress = 1;
430 }
431 }
Li Zefan560f7d72011-09-08 10:22:01 +0800432cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400433 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400434 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000435 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400436 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500437
Chris Masonc8b97812008-10-29 14:49:59 -0400438 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500439 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400440 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500441 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400442 */
443 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000444 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400445 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500446 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400447 ret = cow_file_range_inline(trans, root, inode,
448 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000449 total_compressed,
450 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400451 }
452 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500453 /*
454 * inline extent creation worked, we don't need
455 * to create any more async work items. Unlock
456 * and free up our temp pages.
457 */
Chris Masonc8b97812008-10-29 14:49:59 -0400458 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400459 &BTRFS_I(inode)->io_tree,
460 start, end, NULL,
461 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400462 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400463 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000464
465 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400466 goto free_pages_out;
467 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000468 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400469 }
470
471 if (will_compress) {
472 /*
473 * we aren't doing an inline extent round the compressed size
474 * up to a block size boundary so the allocator does sane
475 * things
476 */
477 total_compressed = (total_compressed + blocksize - 1) &
478 ~(blocksize - 1);
479
480 /*
481 * one last check to make sure the compression is really a
482 * win, compare the page count read with the blocks on disk
483 */
484 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
485 ~(PAGE_CACHE_SIZE - 1);
486 if (total_compressed >= total_in) {
487 will_compress = 0;
488 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400489 num_bytes = total_in;
490 }
491 }
492 if (!will_compress && pages) {
493 /*
494 * the compression code ran but failed to make things smaller,
495 * free any pages it allocated and our page pointer array
496 */
497 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400498 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400499 page_cache_release(pages[i]);
500 }
501 kfree(pages);
502 pages = NULL;
503 total_compressed = 0;
504 nr_pages_ret = 0;
505
506 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500507 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
508 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500509 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500510 }
Chris Masonc8b97812008-10-29 14:49:59 -0400511 }
Chris Mason771ed682008-11-06 22:02:51 -0500512 if (will_compress) {
513 *num_added += 1;
514
515 /* the async work queues will take care of doing actual
516 * allocation on disk for these compressed pages,
517 * and will submit them to the elevator.
518 */
519 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800520 total_compressed, pages, nr_pages_ret,
521 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500522
Yan, Zheng24ae6362010-12-06 07:02:36 +0000523 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500524 start += num_bytes;
525 pages = NULL;
526 cond_resched();
527 goto again;
528 }
529 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500530cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500531 /*
532 * No compression, but we still need to write the pages in
533 * the file we've been given so far. redirty the locked
534 * page if it corresponds to our extent and set things up
535 * for the async work queue to run cow_file_range to do
536 * the normal delalloc dance
537 */
538 if (page_offset(locked_page) >= start &&
539 page_offset(locked_page) <= end) {
540 __set_page_dirty_nobuffers(locked_page);
541 /* unlocked later on in the async handlers */
542 }
Li Zefan261507a02010-12-17 14:21:50 +0800543 add_async_extent(async_cow, start, end - start + 1,
544 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500545 *num_added += 1;
546 }
547
548out:
549 return 0;
550
551free_pages_out:
552 for (i = 0; i < nr_pages_ret; i++) {
553 WARN_ON(pages[i]->mapping);
554 page_cache_release(pages[i]);
555 }
Chris Masond3977122009-01-05 21:25:51 -0500556 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500557
558 goto out;
559}
560
561/*
562 * phase two of compressed writeback. This is the ordered portion
563 * of the code, which only gets called in the order the work was
564 * queued. We walk all the async extents created by compress_file_range
565 * and send them down to the disk.
566 */
567static noinline int submit_compressed_extents(struct inode *inode,
568 struct async_cow *async_cow)
569{
570 struct async_extent *async_extent;
571 u64 alloc_hint = 0;
572 struct btrfs_trans_handle *trans;
573 struct btrfs_key ins;
574 struct extent_map *em;
575 struct btrfs_root *root = BTRFS_I(inode)->root;
576 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
577 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500578 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500579
580 if (list_empty(&async_cow->extents))
581 return 0;
582
Chris Mason771ed682008-11-06 22:02:51 -0500583
Chris Masond3977122009-01-05 21:25:51 -0500584 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500585 async_extent = list_entry(async_cow->extents.next,
586 struct async_extent, list);
587 list_del(&async_extent->list);
588
589 io_tree = &BTRFS_I(inode)->io_tree;
590
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500591retry:
Chris Mason771ed682008-11-06 22:02:51 -0500592 /* did the compression code fall back to uncompressed IO? */
593 if (!async_extent->pages) {
594 int page_started = 0;
595 unsigned long nr_written = 0;
596
597 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000598 async_extent->start +
599 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500600
601 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500602 ret = cow_file_range(inode, async_cow->locked_page,
603 async_extent->start,
604 async_extent->start +
605 async_extent->ram_size - 1,
606 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500607
608 /*
609 * if page_started, cow_file_range inserted an
610 * inline extent and took care of all the unlocking
611 * and IO for us. Otherwise, we need to submit
612 * all those pages down to the drive.
613 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500614 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500615 extent_write_locked_range(io_tree,
616 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500617 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500618 async_extent->ram_size - 1,
619 btrfs_get_extent,
620 WB_SYNC_ALL);
621 kfree(async_extent);
622 cond_resched();
623 continue;
624 }
625
626 lock_extent(io_tree, async_extent->start,
627 async_extent->start + async_extent->ram_size - 1,
628 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500629
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400630 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000631 BUG_ON(IS_ERR(trans));
Josef Bacik74b21072011-04-13 12:02:53 -0400632 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500633 ret = btrfs_reserve_extent(trans, root,
634 async_extent->compressed_size,
635 async_extent->compressed_size,
636 0, alloc_hint,
637 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000638 btrfs_end_transaction(trans, root);
639
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500640 if (ret) {
641 int i;
642 for (i = 0; i < async_extent->nr_pages; i++) {
643 WARN_ON(async_extent->pages[i]->mapping);
644 page_cache_release(async_extent->pages[i]);
645 }
646 kfree(async_extent->pages);
647 async_extent->nr_pages = 0;
648 async_extent->pages = NULL;
649 unlock_extent(io_tree, async_extent->start,
650 async_extent->start +
651 async_extent->ram_size - 1, GFP_NOFS);
652 goto retry;
653 }
654
Yan, Zhengc2167752009-11-12 09:34:21 +0000655 /*
656 * here we're doing allocation and writeback of the
657 * compressed pages
658 */
659 btrfs_drop_extent_cache(inode, async_extent->start,
660 async_extent->start +
661 async_extent->ram_size - 1, 0);
662
David Sterba172ddd62011-04-21 00:48:27 +0200663 em = alloc_extent_map();
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000664 BUG_ON(!em);
Chris Mason771ed682008-11-06 22:02:51 -0500665 em->start = async_extent->start;
666 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500667 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500668
669 em->block_start = ins.objectid;
670 em->block_len = ins.offset;
671 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800672 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500673 set_bit(EXTENT_FLAG_PINNED, &em->flags);
674 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
675
Chris Masond3977122009-01-05 21:25:51 -0500676 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400677 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500678 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400679 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500680 if (ret != -EEXIST) {
681 free_extent_map(em);
682 break;
683 }
684 btrfs_drop_extent_cache(inode, async_extent->start,
685 async_extent->start +
686 async_extent->ram_size - 1, 0);
687 }
688
Li Zefan261507a02010-12-17 14:21:50 +0800689 ret = btrfs_add_ordered_extent_compress(inode,
690 async_extent->start,
691 ins.objectid,
692 async_extent->ram_size,
693 ins.offset,
694 BTRFS_ORDERED_COMPRESSED,
695 async_extent->compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500696 BUG_ON(ret);
697
Chris Mason771ed682008-11-06 22:02:51 -0500698 /*
699 * clear dirty, set writeback and unlock the pages.
700 */
701 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400702 &BTRFS_I(inode)->io_tree,
703 async_extent->start,
704 async_extent->start +
705 async_extent->ram_size - 1,
706 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
707 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400708 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400709 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500710
711 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500712 async_extent->start,
713 async_extent->ram_size,
714 ins.objectid,
715 ins.offset, async_extent->pages,
716 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500717
718 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500719 alloc_hint = ins.objectid + ins.offset;
720 kfree(async_extent);
721 cond_resched();
722 }
723
Chris Mason771ed682008-11-06 22:02:51 -0500724 return 0;
725}
726
Josef Bacik4b46fce2010-05-23 11:00:55 -0400727static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
728 u64 num_bytes)
729{
730 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
731 struct extent_map *em;
732 u64 alloc_hint = 0;
733
734 read_lock(&em_tree->lock);
735 em = search_extent_mapping(em_tree, start, num_bytes);
736 if (em) {
737 /*
738 * if block start isn't an actual block number then find the
739 * first block in this inode and use that as a hint. If that
740 * block is also bogus then just don't worry about it.
741 */
742 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
743 free_extent_map(em);
744 em = search_extent_mapping(em_tree, 0, 0);
745 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
746 alloc_hint = em->block_start;
747 if (em)
748 free_extent_map(em);
749 } else {
750 alloc_hint = em->block_start;
751 free_extent_map(em);
752 }
753 }
754 read_unlock(&em_tree->lock);
755
756 return alloc_hint;
757}
758
Chris Mason771ed682008-11-06 22:02:51 -0500759/*
760 * when extent_io.c finds a delayed allocation range in the file,
761 * the call backs end up in this code. The basic idea is to
762 * allocate extents on disk for the range, and create ordered data structs
763 * in ram to track those extents.
764 *
765 * locked_page is the page that writepage had locked already. We use
766 * it to make sure we don't do extra locks or unlocks.
767 *
768 * *page_started is set to one if we unlock locked_page and do everything
769 * required to start IO on it. It may be clean and already done with
770 * IO when we return.
771 */
772static noinline int cow_file_range(struct inode *inode,
773 struct page *locked_page,
774 u64 start, u64 end, int *page_started,
775 unsigned long *nr_written,
776 int unlock)
777{
778 struct btrfs_root *root = BTRFS_I(inode)->root;
779 struct btrfs_trans_handle *trans;
780 u64 alloc_hint = 0;
781 u64 num_bytes;
782 unsigned long ram_size;
783 u64 disk_num_bytes;
784 u64 cur_alloc_size;
785 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500786 struct btrfs_key ins;
787 struct extent_map *em;
788 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
789 int ret = 0;
790
Chris Mason2cf85722011-07-26 15:35:09 -0400791 BUG_ON(btrfs_is_free_space_inode(root, inode));
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400792 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000793 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400794 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500795
Chris Mason771ed682008-11-06 22:02:51 -0500796 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
797 num_bytes = max(blocksize, num_bytes);
798 disk_num_bytes = num_bytes;
799 ret = 0;
800
Chris Mason4cb53002011-05-24 15:35:30 -0400801 /* if this is a small write inside eof, kick off defrag */
802 if (end <= BTRFS_I(inode)->disk_i_size && num_bytes < 64 * 1024)
803 btrfs_add_inode_defrag(trans, inode);
804
Chris Mason771ed682008-11-06 22:02:51 -0500805 if (start == 0) {
806 /* lets try to make an inline extent */
807 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000808 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500809 if (ret == 0) {
810 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400811 &BTRFS_I(inode)->io_tree,
812 start, end, NULL,
813 EXTENT_CLEAR_UNLOCK_PAGE |
814 EXTENT_CLEAR_UNLOCK |
815 EXTENT_CLEAR_DELALLOC |
816 EXTENT_CLEAR_DIRTY |
817 EXTENT_SET_WRITEBACK |
818 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000819
Chris Mason771ed682008-11-06 22:02:51 -0500820 *nr_written = *nr_written +
821 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
822 *page_started = 1;
823 ret = 0;
824 goto out;
825 }
826 }
Chris Masonc8b97812008-10-29 14:49:59 -0400827
828 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200829 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400830
Josef Bacik4b46fce2010-05-23 11:00:55 -0400831 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400832 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400833
Chris Masond3977122009-01-05 21:25:51 -0500834 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400835 unsigned long op;
836
Josef Bacik287a0ab2010-03-19 18:07:23 +0000837 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400838 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500839 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400840 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500841 BUG_ON(ret);
842
David Sterba172ddd62011-04-21 00:48:27 +0200843 em = alloc_extent_map();
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000844 BUG_ON(!em);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400845 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500846 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500847 ram_size = ins.offset;
848 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400849
Chris Masone6dcd2d2008-07-17 12:53:50 -0400850 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400851 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400852 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400853 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400854
Chris Masond3977122009-01-05 21:25:51 -0500855 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400856 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400857 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400858 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400859 if (ret != -EEXIST) {
860 free_extent_map(em);
861 break;
862 }
863 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400864 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400865 }
866
Chris Mason98d20f62008-04-14 09:46:10 -0400867 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400868 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500869 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400870 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400871
Yan Zheng17d217f2008-12-12 10:03:38 -0500872 if (root->root_key.objectid ==
873 BTRFS_DATA_RELOC_TREE_OBJECTID) {
874 ret = btrfs_reloc_clone_csums(inode, start,
875 cur_alloc_size);
876 BUG_ON(ret);
877 }
878
Chris Masond3977122009-01-05 21:25:51 -0500879 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400880 break;
Chris Masond3977122009-01-05 21:25:51 -0500881
Chris Masonc8b97812008-10-29 14:49:59 -0400882 /* we're not doing compressed IO, don't unlock the first
883 * page (which the caller expects to stay locked), don't
884 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400885 *
886 * Do set the Private2 bit so we know this page was properly
887 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400888 */
Chris Masona791e352009-10-08 11:27:10 -0400889 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
890 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
891 EXTENT_SET_PRIVATE2;
892
Chris Masonc8b97812008-10-29 14:49:59 -0400893 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
894 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400895 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400896 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500897 num_bytes -= cur_alloc_size;
898 alloc_hint = ins.objectid + ins.offset;
899 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400900 }
Chris Masonb888db22007-08-27 16:49:44 -0400901out:
Chris Mason771ed682008-11-06 22:02:51 -0500902 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400903 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400904
Chris Masonbe20aa92007-12-17 20:14:01 -0500905 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500906}
Chris Masonc8b97812008-10-29 14:49:59 -0400907
Chris Mason771ed682008-11-06 22:02:51 -0500908/*
909 * work queue call back to started compression on a file and pages
910 */
911static noinline void async_cow_start(struct btrfs_work *work)
912{
913 struct async_cow *async_cow;
914 int num_added = 0;
915 async_cow = container_of(work, struct async_cow, work);
916
917 compress_file_range(async_cow->inode, async_cow->locked_page,
918 async_cow->start, async_cow->end, async_cow,
919 &num_added);
920 if (num_added == 0)
921 async_cow->inode = NULL;
922}
923
924/*
925 * work queue call back to submit previously compressed pages
926 */
927static noinline void async_cow_submit(struct btrfs_work *work)
928{
929 struct async_cow *async_cow;
930 struct btrfs_root *root;
931 unsigned long nr_pages;
932
933 async_cow = container_of(work, struct async_cow, work);
934
935 root = async_cow->root;
936 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
937 PAGE_CACHE_SHIFT;
938
939 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
940
941 if (atomic_read(&root->fs_info->async_delalloc_pages) <
942 5 * 1042 * 1024 &&
943 waitqueue_active(&root->fs_info->async_submit_wait))
944 wake_up(&root->fs_info->async_submit_wait);
945
Chris Masond3977122009-01-05 21:25:51 -0500946 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500947 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500948}
Chris Masonc8b97812008-10-29 14:49:59 -0400949
Chris Mason771ed682008-11-06 22:02:51 -0500950static noinline void async_cow_free(struct btrfs_work *work)
951{
952 struct async_cow *async_cow;
953 async_cow = container_of(work, struct async_cow, work);
954 kfree(async_cow);
955}
956
957static int cow_file_range_async(struct inode *inode, struct page *locked_page,
958 u64 start, u64 end, int *page_started,
959 unsigned long *nr_written)
960{
961 struct async_cow *async_cow;
962 struct btrfs_root *root = BTRFS_I(inode)->root;
963 unsigned long nr_pages;
964 u64 cur_end;
965 int limit = 10 * 1024 * 1042;
966
Chris Masona3429ab2009-10-08 12:30:20 -0400967 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
968 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500969 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500970 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -0400971 BUG_ON(!async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500972 async_cow->inode = inode;
973 async_cow->root = root;
974 async_cow->locked_page = locked_page;
975 async_cow->start = start;
976
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200977 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500978 cur_end = end;
979 else
980 cur_end = min(end, start + 512 * 1024 - 1);
981
982 async_cow->end = cur_end;
983 INIT_LIST_HEAD(&async_cow->extents);
984
985 async_cow->work.func = async_cow_start;
986 async_cow->work.ordered_func = async_cow_submit;
987 async_cow->work.ordered_free = async_cow_free;
988 async_cow->work.flags = 0;
989
Chris Mason771ed682008-11-06 22:02:51 -0500990 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
991 PAGE_CACHE_SHIFT;
992 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
993
994 btrfs_queue_worker(&root->fs_info->delalloc_workers,
995 &async_cow->work);
996
997 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
998 wait_event(root->fs_info->async_submit_wait,
999 (atomic_read(&root->fs_info->async_delalloc_pages) <
1000 limit));
1001 }
1002
Chris Masond3977122009-01-05 21:25:51 -05001003 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001004 atomic_read(&root->fs_info->async_delalloc_pages)) {
1005 wait_event(root->fs_info->async_submit_wait,
1006 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1007 0));
1008 }
1009
1010 *nr_written += nr_pages;
1011 start = cur_end + 1;
1012 }
1013 *page_started = 1;
1014 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001015}
1016
Chris Masond3977122009-01-05 21:25:51 -05001017static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001018 u64 bytenr, u64 num_bytes)
1019{
1020 int ret;
1021 struct btrfs_ordered_sum *sums;
1022 LIST_HEAD(list);
1023
Yan Zheng07d400a2009-01-06 11:42:00 -05001024 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001025 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001026 if (ret == 0 && list_empty(&list))
1027 return 0;
1028
1029 while (!list_empty(&list)) {
1030 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1031 list_del(&sums->list);
1032 kfree(sums);
1033 }
1034 return 1;
1035}
1036
Chris Masond352ac62008-09-29 15:18:18 -04001037/*
1038 * when nowcow writeback call back. This checks for snapshots or COW copies
1039 * of the extents that exist in the file, and COWs the file as required.
1040 *
1041 * If no cow copies or snapshots exist, we write directly to the existing
1042 * blocks on disk
1043 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001044static noinline int run_delalloc_nocow(struct inode *inode,
1045 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001046 u64 start, u64 end, int *page_started, int force,
1047 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001048{
Chris Masonbe20aa92007-12-17 20:14:01 -05001049 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001050 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001051 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001052 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001053 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001054 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001055 u64 cow_start;
1056 u64 cur_offset;
1057 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001058 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001059 u64 disk_bytenr;
1060 u64 num_bytes;
1061 int extent_type;
1062 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001063 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001064 int nocow;
1065 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001066 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001067 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001068
1069 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001070 if (!path)
1071 return -ENOMEM;
Li Zefan82d59022011-04-20 10:33:24 +08001072
Chris Mason2cf85722011-07-26 15:35:09 -04001073 nolock = btrfs_is_free_space_inode(root, inode);
Li Zefan82d59022011-04-20 10:33:24 +08001074
1075 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001076 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001077 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001078 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001079
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001080 BUG_ON(IS_ERR(trans));
Josef Bacik74b21072011-04-13 12:02:53 -04001081 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001082
Yan Zheng80ff3852008-10-30 14:20:02 -04001083 cow_start = (u64)-1;
1084 cur_offset = start;
1085 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001086 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001087 cur_offset, 0);
1088 BUG_ON(ret < 0);
1089 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1090 leaf = path->nodes[0];
1091 btrfs_item_key_to_cpu(leaf, &found_key,
1092 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001093 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001094 found_key.type == BTRFS_EXTENT_DATA_KEY)
1095 path->slots[0]--;
1096 }
1097 check_prev = 0;
1098next_slot:
1099 leaf = path->nodes[0];
1100 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1101 ret = btrfs_next_leaf(root, path);
1102 if (ret < 0)
1103 BUG_ON(1);
1104 if (ret > 0)
1105 break;
1106 leaf = path->nodes[0];
1107 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001108
Yan Zheng80ff3852008-10-30 14:20:02 -04001109 nocow = 0;
1110 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001111 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001112 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001113
Li Zefan33345d012011-04-20 10:31:50 +08001114 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001115 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1116 found_key.offset > end)
1117 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001118
Yan Zheng80ff3852008-10-30 14:20:02 -04001119 if (found_key.offset > cur_offset) {
1120 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001121 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001122 goto out_check;
1123 }
Chris Masonc31f8832008-01-08 15:46:31 -05001124
Yan Zheng80ff3852008-10-30 14:20:02 -04001125 fi = btrfs_item_ptr(leaf, path->slots[0],
1126 struct btrfs_file_extent_item);
1127 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001128
Yan Zhengd899e052008-10-30 14:25:28 -04001129 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1130 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001131 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001132 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001133 extent_end = found_key.offset +
1134 btrfs_file_extent_num_bytes(leaf, fi);
1135 if (extent_end <= start) {
1136 path->slots[0]++;
1137 goto next_slot;
1138 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001139 if (disk_bytenr == 0)
1140 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001141 if (btrfs_file_extent_compression(leaf, fi) ||
1142 btrfs_file_extent_encryption(leaf, fi) ||
1143 btrfs_file_extent_other_encoding(leaf, fi))
1144 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001145 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1146 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001147 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001148 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001149 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001150 found_key.offset -
1151 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001152 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001153 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001154 disk_bytenr += cur_offset - found_key.offset;
1155 num_bytes = min(end + 1, extent_end) - cur_offset;
1156 /*
1157 * force cow if csum exists in the range.
1158 * this ensure that csum for a given extent are
1159 * either valid or do not exist.
1160 */
1161 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1162 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001163 nocow = 1;
1164 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1165 extent_end = found_key.offset +
1166 btrfs_file_extent_inline_len(leaf, fi);
1167 extent_end = ALIGN(extent_end, root->sectorsize);
1168 } else {
1169 BUG_ON(1);
1170 }
1171out_check:
1172 if (extent_end <= start) {
1173 path->slots[0]++;
1174 goto next_slot;
1175 }
1176 if (!nocow) {
1177 if (cow_start == (u64)-1)
1178 cow_start = cur_offset;
1179 cur_offset = extent_end;
1180 if (cur_offset > end)
1181 break;
1182 path->slots[0]++;
1183 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001184 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001185
David Sterbab3b4aa72011-04-21 01:20:15 +02001186 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001187 if (cow_start != (u64)-1) {
1188 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001189 found_key.offset - 1, page_started,
1190 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001191 BUG_ON(ret);
1192 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001193 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001194
Yan Zhengd899e052008-10-30 14:25:28 -04001195 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1196 struct extent_map *em;
1197 struct extent_map_tree *em_tree;
1198 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001199 em = alloc_extent_map();
Tsutomu Itohc26a9202011-02-14 00:45:29 +00001200 BUG_ON(!em);
Yan Zhengd899e052008-10-30 14:25:28 -04001201 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001202 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001203 em->len = num_bytes;
1204 em->block_len = num_bytes;
1205 em->block_start = disk_bytenr;
1206 em->bdev = root->fs_info->fs_devices->latest_bdev;
1207 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1208 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001209 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001210 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001211 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001212 if (ret != -EEXIST) {
1213 free_extent_map(em);
1214 break;
1215 }
1216 btrfs_drop_extent_cache(inode, em->start,
1217 em->start + em->len - 1, 0);
1218 }
1219 type = BTRFS_ORDERED_PREALLOC;
1220 } else {
1221 type = BTRFS_ORDERED_NOCOW;
1222 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001223
1224 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001225 num_bytes, num_bytes, type);
1226 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001227
Yan, Zhengefa56462010-05-16 10:49:59 -04001228 if (root->root_key.objectid ==
1229 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1230 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1231 num_bytes);
1232 BUG_ON(ret);
1233 }
1234
Yan Zhengd899e052008-10-30 14:25:28 -04001235 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001236 cur_offset, cur_offset + num_bytes - 1,
1237 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1238 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1239 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 cur_offset = extent_end;
1241 if (cur_offset > end)
1242 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001243 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001244 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001245
1246 if (cur_offset <= end && cow_start == (u64)-1)
1247 cow_start = cur_offset;
1248 if (cow_start != (u64)-1) {
1249 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001250 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 BUG_ON(ret);
1252 }
1253
Josef Bacik0cb59c92010-07-02 12:14:14 -04001254 if (nolock) {
1255 ret = btrfs_end_transaction_nolock(trans, root);
1256 BUG_ON(ret);
1257 } else {
1258 ret = btrfs_end_transaction(trans, root);
1259 BUG_ON(ret);
1260 }
Yan Zheng7ea394f2008-08-05 13:05:02 -04001261 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001263}
1264
Chris Masond352ac62008-09-29 15:18:18 -04001265/*
1266 * extent_io.c call back to do delayed allocation processing
1267 */
Chris Masonc8b97812008-10-29 14:49:59 -04001268static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001269 u64 start, u64 end, int *page_started,
1270 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001271{
Chris Masonbe20aa92007-12-17 20:14:01 -05001272 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001273 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001274
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001275 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001276 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001277 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001278 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001279 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001280 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001281 else if (!btrfs_test_opt(root, COMPRESS) &&
Liu Bo75e7cb72011-03-22 10:12:20 +00001282 !(BTRFS_I(inode)->force_compress) &&
1283 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
Chris Mason7f366cf2009-03-12 20:12:45 -04001284 ret = cow_file_range(inode, locked_page, start, end,
1285 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001286 else
Chris Mason771ed682008-11-06 22:02:51 -05001287 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001288 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001289 return ret;
1290}
1291
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001292static void btrfs_split_extent_hook(struct inode *inode,
1293 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001294{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001295 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001296 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001297 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001298
Josef Bacik9e0baf62011-07-15 15:16:44 +00001299 spin_lock(&BTRFS_I(inode)->lock);
1300 BTRFS_I(inode)->outstanding_extents++;
1301 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001302}
1303
1304/*
1305 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1306 * extents so we can keep track of new extents that are just merged onto old
1307 * extents, such as when we are doing sequential writes, so we can properly
1308 * account for the metadata space we'll need.
1309 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001310static void btrfs_merge_extent_hook(struct inode *inode,
1311 struct extent_state *new,
1312 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001313{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001314 /* not delalloc, ignore it */
1315 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001316 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001317
Josef Bacik9e0baf62011-07-15 15:16:44 +00001318 spin_lock(&BTRFS_I(inode)->lock);
1319 BTRFS_I(inode)->outstanding_extents--;
1320 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001321}
1322
Chris Masond352ac62008-09-29 15:18:18 -04001323/*
1324 * extent_io.c set_bit_hook, used to track delayed allocation
1325 * bytes in this file, and to maintain the list of inodes that
1326 * have pending delalloc work to be done.
1327 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001328static void btrfs_set_bit_hook(struct inode *inode,
1329 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001330{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001331
Chris Mason75eff682008-12-15 15:54:40 -05001332 /*
1333 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001334 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001335 * bit, which is only set or cleared with irqs on
1336 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001337 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001338 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001339 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001340 bool do_list = !btrfs_is_free_space_inode(root, inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001341
Josef Bacik9e0baf62011-07-15 15:16:44 +00001342 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001343 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001344 } else {
1345 spin_lock(&BTRFS_I(inode)->lock);
1346 BTRFS_I(inode)->outstanding_extents++;
1347 spin_unlock(&BTRFS_I(inode)->lock);
1348 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001349
Chris Mason75eff682008-12-15 15:54:40 -05001350 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001351 BTRFS_I(inode)->delalloc_bytes += len;
1352 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001353 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001354 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1355 &root->fs_info->delalloc_inodes);
1356 }
Chris Mason75eff682008-12-15 15:54:40 -05001357 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001358 }
Chris Mason291d6732008-01-29 15:55:23 -05001359}
1360
Chris Masond352ac62008-09-29 15:18:18 -04001361/*
1362 * extent_io.c clear_bit_hook, see set_bit_hook for why
1363 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001364static void btrfs_clear_bit_hook(struct inode *inode,
1365 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001366{
Chris Mason75eff682008-12-15 15:54:40 -05001367 /*
1368 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001369 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001370 * bit, which is only set or cleared with irqs on
1371 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001372 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001373 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001374 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001375 bool do_list = !btrfs_is_free_space_inode(root, inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001376
Josef Bacik9e0baf62011-07-15 15:16:44 +00001377 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001378 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001379 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1380 spin_lock(&BTRFS_I(inode)->lock);
1381 BTRFS_I(inode)->outstanding_extents--;
1382 spin_unlock(&BTRFS_I(inode)->lock);
1383 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001384
1385 if (*bits & EXTENT_DO_ACCOUNTING)
1386 btrfs_delalloc_release_metadata(inode, len);
1387
Josef Bacik0cb59c92010-07-02 12:14:14 -04001388 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1389 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001390 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001391
Chris Mason75eff682008-12-15 15:54:40 -05001392 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001393 root->fs_info->delalloc_bytes -= len;
1394 BTRFS_I(inode)->delalloc_bytes -= len;
1395
Josef Bacik0cb59c92010-07-02 12:14:14 -04001396 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001397 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1398 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1399 }
Chris Mason75eff682008-12-15 15:54:40 -05001400 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001401 }
Chris Mason291d6732008-01-29 15:55:23 -05001402}
1403
Chris Masond352ac62008-09-29 15:18:18 -04001404/*
1405 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1406 * we don't create bios that span stripes or chunks
1407 */
Chris Mason239b14b2008-03-24 15:02:07 -04001408int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001409 size_t size, struct bio *bio,
1410 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001411{
1412 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1413 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001414 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001415 u64 length = 0;
1416 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001417 int ret;
1418
Chris Mason771ed682008-11-06 22:02:51 -05001419 if (bio_flags & EXTENT_BIO_COMPRESSED)
1420 return 0;
1421
Chris Masonf2d8d742008-04-21 10:03:05 -04001422 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001423 map_tree = &root->fs_info->mapping_tree;
1424 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001425 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001426 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001427
Chris Masond3977122009-01-05 21:25:51 -05001428 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001429 return 1;
Andi Kleen411fc6b2010-10-29 15:14:31 -04001430 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04001431}
1432
Chris Masond352ac62008-09-29 15:18:18 -04001433/*
1434 * in order to insert checksums into the metadata in large chunks,
1435 * we wait until bio submission time. All the pages in the bio are
1436 * checksummed and sums are attached onto the ordered extent record.
1437 *
1438 * At IO completion time the cums attached on the ordered extent record
1439 * are inserted into the btree
1440 */
Chris Masond3977122009-01-05 21:25:51 -05001441static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1442 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001443 unsigned long bio_flags,
1444 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001445{
Chris Mason065631f2008-02-20 12:07:25 -05001446 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001447 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001448
Chris Masond20f7042008-12-08 16:58:54 -05001449 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001450 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001451 return 0;
1452}
Chris Masone0156402008-04-16 11:15:20 -04001453
Chris Mason4a69a412008-11-06 22:03:00 -05001454/*
1455 * in order to insert checksums into the metadata in large chunks,
1456 * we wait until bio submission time. All the pages in the bio are
1457 * checksummed and sums are attached onto the ordered extent record.
1458 *
1459 * At IO completion time the cums attached on the ordered extent record
1460 * are inserted into the btree
1461 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001462static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001463 int mirror_num, unsigned long bio_flags,
1464 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001465{
1466 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001467 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001468}
1469
Chris Masond352ac62008-09-29 15:18:18 -04001470/*
Chris Masoncad321a2008-12-17 14:51:42 -05001471 * extent_io.c submission hook. This does the right thing for csum calculation
1472 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001473 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001474static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001475 int mirror_num, unsigned long bio_flags,
1476 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001477{
1478 struct btrfs_root *root = BTRFS_I(inode)->root;
1479 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001480 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001481
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001482 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001483
Chris Mason2cf85722011-07-26 15:35:09 -04001484 if (btrfs_is_free_space_inode(root, inode))
Josef Bacik0cb59c92010-07-02 12:14:14 -04001485 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1486 else
1487 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001488 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001489
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001490 if (!(rw & REQ_WRITE)) {
Chris Masond20f7042008-12-08 16:58:54 -05001491 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001492 return btrfs_submit_compressed_read(inode, bio,
1493 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001494 } else if (!skip_sum) {
1495 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1496 if (ret)
1497 return ret;
1498 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001499 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001500 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001501 /* csum items have already been cloned */
1502 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1503 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001504 /* we're doing a write, do the async checksumming */
1505 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001506 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001507 bio_flags, bio_offset,
1508 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001509 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001510 }
1511
Chris Mason0b86a832008-03-24 15:01:56 -04001512mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001513 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001514}
Chris Mason6885f302008-02-20 16:11:05 -05001515
Chris Masond352ac62008-09-29 15:18:18 -04001516/*
1517 * given a list of ordered sums record them in the inode. This happens
1518 * at IO completion time based on sums calculated at bio submission time.
1519 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001520static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001521 struct inode *inode, u64 file_offset,
1522 struct list_head *list)
1523{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001524 struct btrfs_ordered_sum *sum;
1525
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001526 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001527 btrfs_csum_file_blocks(trans,
1528 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001529 }
1530 return 0;
1531}
1532
Josef Bacik2ac55d42010-02-03 19:33:23 +00001533int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1534 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001535{
Chris Masond3977122009-01-05 21:25:51 -05001536 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001537 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001538 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001539 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001540}
1541
Chris Masond352ac62008-09-29 15:18:18 -04001542/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001543struct btrfs_writepage_fixup {
1544 struct page *page;
1545 struct btrfs_work work;
1546};
1547
Christoph Hellwigb2950862008-12-02 09:54:17 -05001548static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001549{
1550 struct btrfs_writepage_fixup *fixup;
1551 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001552 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001553 struct page *page;
1554 struct inode *inode;
1555 u64 page_start;
1556 u64 page_end;
1557
1558 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1559 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001560again:
Chris Mason247e7432008-07-17 12:53:51 -04001561 lock_page(page);
1562 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1563 ClearPageChecked(page);
1564 goto out_page;
1565 }
1566
1567 inode = page->mapping->host;
1568 page_start = page_offset(page);
1569 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1570
Josef Bacik2ac55d42010-02-03 19:33:23 +00001571 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1572 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001573
1574 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001575 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001576 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001577
1578 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1579 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001580 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1581 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001582 unlock_page(page);
1583 btrfs_start_ordered_extent(inode, ordered, 1);
1584 goto again;
1585 }
Chris Mason247e7432008-07-17 12:53:51 -04001586
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001587 BUG();
Josef Bacik2ac55d42010-02-03 19:33:23 +00001588 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001589 ClearPageChecked(page);
1590out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001591 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1592 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001593out_page:
1594 unlock_page(page);
1595 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001596 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001597}
1598
1599/*
1600 * There are a few paths in the higher layers of the kernel that directly
1601 * set the page dirty bit without asking the filesystem if it is a
1602 * good idea. This causes problems because we want to make sure COW
1603 * properly happens and the data=ordered rules are followed.
1604 *
Chris Masonc8b97812008-10-29 14:49:59 -04001605 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001606 * hasn't been properly setup for IO. We kick off an async process
1607 * to fix it up. The async helper will wait for ordered extents, set
1608 * the delalloc bit and make it safe to write the page.
1609 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001610static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001611{
1612 struct inode *inode = page->mapping->host;
1613 struct btrfs_writepage_fixup *fixup;
1614 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001615
Chris Mason8b62b722009-09-02 16:53:46 -04001616 /* this page is properly in the ordered list */
1617 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001618 return 0;
1619
1620 if (PageChecked(page))
1621 return -EAGAIN;
1622
1623 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1624 if (!fixup)
1625 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001626
Chris Mason247e7432008-07-17 12:53:51 -04001627 SetPageChecked(page);
1628 page_cache_get(page);
1629 fixup->work.func = btrfs_writepage_fixup_worker;
1630 fixup->page = page;
1631 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1632 return -EAGAIN;
1633}
1634
Yan Zhengd899e052008-10-30 14:25:28 -04001635static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1636 struct inode *inode, u64 file_pos,
1637 u64 disk_bytenr, u64 disk_num_bytes,
1638 u64 num_bytes, u64 ram_bytes,
1639 u8 compression, u8 encryption,
1640 u16 other_encoding, int extent_type)
1641{
1642 struct btrfs_root *root = BTRFS_I(inode)->root;
1643 struct btrfs_file_extent_item *fi;
1644 struct btrfs_path *path;
1645 struct extent_buffer *leaf;
1646 struct btrfs_key ins;
1647 u64 hint;
1648 int ret;
1649
1650 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001651 if (!path)
1652 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001653
Chris Masonb9473432009-03-13 11:00:37 -04001654 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001655
1656 /*
1657 * we may be replacing one extent in the tree with another.
1658 * The new extent is pinned in the extent map, and we don't want
1659 * to drop it from the cache until it is completely in the btree.
1660 *
1661 * So, tell btrfs_drop_extents to leave this extent in the cache.
1662 * the caller is expected to unpin it and allow it to be merged
1663 * with the others.
1664 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001665 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1666 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001667 BUG_ON(ret);
1668
Li Zefan33345d012011-04-20 10:31:50 +08001669 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001670 ins.offset = file_pos;
1671 ins.type = BTRFS_EXTENT_DATA_KEY;
1672 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1673 BUG_ON(ret);
1674 leaf = path->nodes[0];
1675 fi = btrfs_item_ptr(leaf, path->slots[0],
1676 struct btrfs_file_extent_item);
1677 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1678 btrfs_set_file_extent_type(leaf, fi, extent_type);
1679 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1680 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1681 btrfs_set_file_extent_offset(leaf, fi, 0);
1682 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1683 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1684 btrfs_set_file_extent_compression(leaf, fi, compression);
1685 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1686 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001687
1688 btrfs_unlock_up_safe(path, 1);
1689 btrfs_set_lock_blocking(leaf);
1690
Yan Zhengd899e052008-10-30 14:25:28 -04001691 btrfs_mark_buffer_dirty(leaf);
1692
1693 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001694
1695 ins.objectid = disk_bytenr;
1696 ins.offset = disk_num_bytes;
1697 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001698 ret = btrfs_alloc_reserved_file_extent(trans, root,
1699 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001700 btrfs_ino(inode), file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001701 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001702 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001703
Yan Zhengd899e052008-10-30 14:25:28 -04001704 return 0;
1705}
1706
Chris Mason5d13a982009-03-13 11:41:46 -04001707/*
1708 * helper function for btrfs_finish_ordered_io, this
1709 * just reads in some of the csum leaves to prime them into ram
1710 * before we start the transaction. It limits the amount of btree
1711 * reads required while inside the transaction.
1712 */
Chris Masond352ac62008-09-29 15:18:18 -04001713/* as ordered data IO finishes, this gets called so we can finish
1714 * an ordered extent if the range of bytes in the file it covers are
1715 * fully written.
1716 */
Chris Mason211f90e2008-07-18 11:56:15 -04001717static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001718{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001719 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001720 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001721 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001722 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001723 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001724 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001725 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001726 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001727
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001728 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1729 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001730 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001731 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001732 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001733
Chris Mason2cf85722011-07-26 15:35:09 -04001734 nolock = btrfs_is_free_space_inode(root, inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001735
Yan, Zhengc2167752009-11-12 09:34:21 +00001736 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1737 BUG_ON(!list_empty(&ordered_extent->list));
1738 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1739 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001740 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001741 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001742 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001743 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001744 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001745 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason21151332011-11-10 20:39:08 -05001746 ret = btrfs_update_inode_fallback(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +00001747 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001748 }
1749 goto out;
1750 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001751
Josef Bacik2ac55d42010-02-03 19:33:23 +00001752 lock_extent_bits(io_tree, ordered_extent->file_offset,
1753 ordered_extent->file_offset + ordered_extent->len - 1,
1754 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001755
Josef Bacik0cb59c92010-07-02 12:14:14 -04001756 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001757 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001758 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001759 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001760 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001761 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001762
Chris Masonc8b97812008-10-29 14:49:59 -04001763 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001764 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001765 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001766 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001767 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001768 ordered_extent->file_offset,
1769 ordered_extent->file_offset +
1770 ordered_extent->len);
1771 BUG_ON(ret);
1772 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001773 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001774 ret = insert_reserved_file_extent(trans, inode,
1775 ordered_extent->file_offset,
1776 ordered_extent->start,
1777 ordered_extent->disk_len,
1778 ordered_extent->len,
1779 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001780 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001781 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001782 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1783 ordered_extent->file_offset,
1784 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001785 BUG_ON(ret);
1786 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001787 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1788 ordered_extent->file_offset +
1789 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1790
Chris Masone6dcd2d2008-07-17 12:53:50 -04001791 add_pending_csums(trans, inode, ordered_extent->file_offset,
1792 &ordered_extent->list);
1793
Josef Bacik1ef30be2011-04-05 19:25:36 -04001794 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Miao Xiea39f7522011-09-11 10:52:25 -04001795 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Chris Mason21151332011-11-10 20:39:08 -05001796 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik1ef30be2011-04-05 19:25:36 -04001797 BUG_ON(ret);
1798 }
1799 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00001800out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001801 if (root != root->fs_info->tree_root)
1802 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1803 if (trans) {
1804 if (nolock)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001805 btrfs_end_transaction_nolock(trans, root);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001806 else
Josef Bacik0cb59c92010-07-02 12:14:14 -04001807 btrfs_end_transaction(trans, root);
1808 }
1809
Chris Masone6dcd2d2008-07-17 12:53:50 -04001810 /* once for us */
1811 btrfs_put_ordered_extent(ordered_extent);
1812 /* once for the tree */
1813 btrfs_put_ordered_extent(ordered_extent);
1814
Chris Masone6dcd2d2008-07-17 12:53:50 -04001815 return 0;
1816}
1817
Christoph Hellwigb2950862008-12-02 09:54:17 -05001818static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001819 struct extent_state *state, int uptodate)
1820{
liubo1abe9b82011-03-24 11:18:59 +00001821 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1822
Chris Mason8b62b722009-09-02 16:53:46 -04001823 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001824 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1825}
1826
Chris Masond352ac62008-09-29 15:18:18 -04001827/*
Chris Masond352ac62008-09-29 15:18:18 -04001828 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001829 * if there's a match, we allow the bio to finish. If not, the code in
1830 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04001831 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001832static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001833 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001834{
Chris Mason35ebb932007-10-30 16:56:53 -04001835 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001836 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001837 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001838 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001839 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001840 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001841 struct btrfs_root *root = BTRFS_I(inode)->root;
1842 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001843
Chris Masond20f7042008-12-08 16:58:54 -05001844 if (PageChecked(page)) {
1845 ClearPageChecked(page);
1846 goto good;
1847 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001848
1849 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02001850 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05001851
Yan Zheng17d217f2008-12-12 10:03:38 -05001852 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001853 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001854 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1855 GFP_NOFS);
1856 return 0;
1857 }
1858
Yanc2e639f2008-02-04 08:57:25 -05001859 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001860 private = state->private;
1861 ret = 0;
1862 } else {
1863 ret = get_state_private(io_tree, start, &private);
1864 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001865 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001866 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001867 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001868
Chris Masonff79f812007-10-15 16:22:25 -04001869 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1870 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001871 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001872 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001873
Chris Mason9ab86c82009-01-07 09:48:51 -05001874 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001875good:
Chris Mason07157aa2007-08-30 08:50:51 -04001876 return 0;
1877
1878zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04001879 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08001880 "private %llu\n",
1881 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04001882 (unsigned long long)start, csum,
1883 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04001884 memset(kaddr + offset, 1, end - start + 1);
1885 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001886 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001887 if (private == 0)
1888 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001889 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001890}
Chris Masonb888db22007-08-27 16:49:44 -04001891
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001892struct delayed_iput {
1893 struct list_head list;
1894 struct inode *inode;
1895};
1896
1897void btrfs_add_delayed_iput(struct inode *inode)
1898{
1899 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1900 struct delayed_iput *delayed;
1901
1902 if (atomic_add_unless(&inode->i_count, -1, 1))
1903 return;
1904
1905 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
1906 delayed->inode = inode;
1907
1908 spin_lock(&fs_info->delayed_iput_lock);
1909 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
1910 spin_unlock(&fs_info->delayed_iput_lock);
1911}
1912
1913void btrfs_run_delayed_iputs(struct btrfs_root *root)
1914{
1915 LIST_HEAD(list);
1916 struct btrfs_fs_info *fs_info = root->fs_info;
1917 struct delayed_iput *delayed;
1918 int empty;
1919
1920 spin_lock(&fs_info->delayed_iput_lock);
1921 empty = list_empty(&fs_info->delayed_iputs);
1922 spin_unlock(&fs_info->delayed_iput_lock);
1923 if (empty)
1924 return;
1925
1926 down_read(&root->fs_info->cleanup_work_sem);
1927 spin_lock(&fs_info->delayed_iput_lock);
1928 list_splice_init(&fs_info->delayed_iputs, &list);
1929 spin_unlock(&fs_info->delayed_iput_lock);
1930
1931 while (!list_empty(&list)) {
1932 delayed = list_entry(list.next, struct delayed_iput, list);
1933 list_del(&delayed->list);
1934 iput(delayed->inode);
1935 kfree(delayed);
1936 }
1937 up_read(&root->fs_info->cleanup_work_sem);
1938}
1939
Yan, Zhengd68fc572010-05-16 10:49:58 -04001940enum btrfs_orphan_cleanup_state {
1941 ORPHAN_CLEANUP_STARTED = 1,
1942 ORPHAN_CLEANUP_DONE = 2,
1943};
1944
1945/*
1946 * This is called in transaction commmit time. If there are no orphan
1947 * files in the subvolume, it removes orphan item and frees block_rsv
1948 * structure.
1949 */
1950void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
1951 struct btrfs_root *root)
1952{
1953 int ret;
1954
1955 if (!list_empty(&root->orphan_list) ||
1956 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
1957 return;
1958
1959 if (root->orphan_item_inserted &&
1960 btrfs_root_refs(&root->root_item) > 0) {
1961 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
1962 root->root_key.objectid);
1963 BUG_ON(ret);
1964 root->orphan_item_inserted = 0;
1965 }
1966
1967 if (root->orphan_block_rsv) {
1968 WARN_ON(root->orphan_block_rsv->size > 0);
1969 btrfs_free_block_rsv(root, root->orphan_block_rsv);
1970 root->orphan_block_rsv = NULL;
1971 }
1972}
1973
1974/*
Josef Bacik7b128762008-07-24 12:17:14 -04001975 * This creates an orphan entry for the given inode in case something goes
1976 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04001977 *
1978 * NOTE: caller of this function should reserve 5 units of metadata for
1979 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04001980 */
1981int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1982{
1983 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04001984 struct btrfs_block_rsv *block_rsv = NULL;
1985 int reserve = 0;
1986 int insert = 0;
1987 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04001988
Yan, Zhengd68fc572010-05-16 10:49:58 -04001989 if (!root->orphan_block_rsv) {
1990 block_rsv = btrfs_alloc_block_rsv(root);
Tsutomu Itohb5324022011-07-19 07:27:20 +00001991 if (!block_rsv)
1992 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04001993 }
1994
Yan, Zhengd68fc572010-05-16 10:49:58 -04001995 spin_lock(&root->orphan_lock);
1996 if (!root->orphan_block_rsv) {
1997 root->orphan_block_rsv = block_rsv;
1998 } else if (block_rsv) {
1999 btrfs_free_block_rsv(root, block_rsv);
2000 block_rsv = NULL;
2001 }
Josef Bacik7b128762008-07-24 12:17:14 -04002002
Yan, Zhengd68fc572010-05-16 10:49:58 -04002003 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2004 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2005#if 0
2006 /*
2007 * For proper ENOSPC handling, we should do orphan
2008 * cleanup when mounting. But this introduces backward
2009 * compatibility issue.
2010 */
2011 if (!xchg(&root->orphan_item_inserted, 1))
2012 insert = 2;
2013 else
2014 insert = 1;
2015#endif
2016 insert = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002017 }
Josef Bacik7b128762008-07-24 12:17:14 -04002018
Yan, Zhengd68fc572010-05-16 10:49:58 -04002019 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2020 BTRFS_I(inode)->orphan_meta_reserved = 1;
2021 reserve = 1;
2022 }
2023 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002024
Yan, Zhengd68fc572010-05-16 10:49:58 -04002025 /* grab metadata reservation from transaction handle */
2026 if (reserve) {
2027 ret = btrfs_orphan_reserve_metadata(trans, inode);
2028 BUG_ON(ret);
2029 }
2030
2031 /* insert an orphan item to track this unlinked/truncated file */
2032 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002033 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Yan, Zhengd68fc572010-05-16 10:49:58 -04002034 BUG_ON(ret);
2035 }
2036
2037 /* insert an orphan item to track subvolume contains orphan files */
2038 if (insert >= 2) {
2039 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2040 root->root_key.objectid);
2041 BUG_ON(ret);
2042 }
2043 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002044}
2045
2046/*
2047 * We have done the truncate/delete so we can go ahead and remove the orphan
2048 * item for this particular inode.
2049 */
2050int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2051{
2052 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002053 int delete_item = 0;
2054 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002055 int ret = 0;
2056
Yan, Zhengd68fc572010-05-16 10:49:58 -04002057 spin_lock(&root->orphan_lock);
2058 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2059 list_del_init(&BTRFS_I(inode)->i_orphan);
2060 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002061 }
2062
Yan, Zhengd68fc572010-05-16 10:49:58 -04002063 if (BTRFS_I(inode)->orphan_meta_reserved) {
2064 BTRFS_I(inode)->orphan_meta_reserved = 0;
2065 release_rsv = 1;
2066 }
2067 spin_unlock(&root->orphan_lock);
2068
2069 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002070 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Yan, Zhengd68fc572010-05-16 10:49:58 -04002071 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002072 }
2073
Yan, Zhengd68fc572010-05-16 10:49:58 -04002074 if (release_rsv)
2075 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002076
Yan, Zhengd68fc572010-05-16 10:49:58 -04002077 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002078}
2079
2080/*
2081 * this cleans up any orphans that may be left on the list from the last use
2082 * of this root.
2083 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002084int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002085{
2086 struct btrfs_path *path;
2087 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002088 struct btrfs_key key, found_key;
2089 struct btrfs_trans_handle *trans;
2090 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002091 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002092 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2093
Yan, Zhengd68fc572010-05-16 10:49:58 -04002094 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002095 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002096
2097 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002098 if (!path) {
2099 ret = -ENOMEM;
2100 goto out;
2101 }
Josef Bacik7b128762008-07-24 12:17:14 -04002102 path->reada = -1;
2103
2104 key.objectid = BTRFS_ORPHAN_OBJECTID;
2105 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2106 key.offset = (u64)-1;
2107
Josef Bacik7b128762008-07-24 12:17:14 -04002108 while (1) {
2109 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002110 if (ret < 0)
2111 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002112
2113 /*
2114 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002115 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002116 * find the key and see if we have stuff that matches
2117 */
2118 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002119 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002120 if (path->slots[0] == 0)
2121 break;
2122 path->slots[0]--;
2123 }
2124
2125 /* pull out the item */
2126 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002127 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2128
2129 /* make sure the item matches what we want */
2130 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2131 break;
2132 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2133 break;
2134
2135 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002136 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002137
2138 /*
2139 * this is where we are basically btrfs_lookup, without the
2140 * crossing root thing. we store the inode number in the
2141 * offset of the orphan item.
2142 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002143
2144 if (found_key.offset == last_objectid) {
2145 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2146 "stopping orphan cleanup\n");
2147 ret = -EINVAL;
2148 goto out;
2149 }
2150
2151 last_objectid = found_key.offset;
2152
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002153 found_key.objectid = found_key.offset;
2154 found_key.type = BTRFS_INODE_ITEM_KEY;
2155 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002156 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002157 ret = PTR_RET(inode);
2158 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002159 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002160
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002161 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2162 struct btrfs_root *dead_root;
2163 struct btrfs_fs_info *fs_info = root->fs_info;
2164 int is_dead_root = 0;
2165
2166 /*
2167 * this is an orphan in the tree root. Currently these
2168 * could come from 2 sources:
2169 * a) a snapshot deletion in progress
2170 * b) a free space cache inode
2171 * We need to distinguish those two, as the snapshot
2172 * orphan must not get deleted.
2173 * find_dead_roots already ran before us, so if this
2174 * is a snapshot deletion, we should find the root
2175 * in the dead_roots list
2176 */
2177 spin_lock(&fs_info->trans_lock);
2178 list_for_each_entry(dead_root, &fs_info->dead_roots,
2179 root_list) {
2180 if (dead_root->root_key.objectid ==
2181 found_key.objectid) {
2182 is_dead_root = 1;
2183 break;
2184 }
2185 }
2186 spin_unlock(&fs_info->trans_lock);
2187 if (is_dead_root) {
2188 /* prevent this orphan from being found again */
2189 key.offset = found_key.objectid - 1;
2190 continue;
2191 }
2192 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002193 /*
2194 * Inode is already gone but the orphan item is still there,
2195 * kill the orphan item.
2196 */
2197 if (ret == -ESTALE) {
2198 trans = btrfs_start_transaction(root, 1);
2199 if (IS_ERR(trans)) {
2200 ret = PTR_ERR(trans);
2201 goto out;
2202 }
2203 ret = btrfs_del_orphan_item(trans, root,
2204 found_key.objectid);
2205 BUG_ON(ret);
2206 btrfs_end_transaction(trans, root);
2207 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002208 }
Josef Bacik7b128762008-07-24 12:17:14 -04002209
Josef Bacik7b128762008-07-24 12:17:14 -04002210 /*
2211 * add this inode to the orphan list so btrfs_orphan_del does
2212 * the proper thing when we hit it
2213 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002214 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002215 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002216 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002217
Josef Bacik7b128762008-07-24 12:17:14 -04002218 /* if we have links, this was a truncate, lets do that */
2219 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002220 if (!S_ISREG(inode->i_mode)) {
2221 WARN_ON(1);
2222 iput(inode);
2223 continue;
2224 }
Josef Bacik7b128762008-07-24 12:17:14 -04002225 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002226 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002227 } else {
2228 nr_unlink++;
2229 }
2230
2231 /* this will do delete_inode and everything for us */
2232 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002233 if (ret)
2234 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002235 }
Miao Xie3254c872011-11-10 20:45:05 -05002236 /* release the path since we're done with it */
2237 btrfs_release_path(path);
2238
Yan, Zhengd68fc572010-05-16 10:49:58 -04002239 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2240
2241 if (root->orphan_block_rsv)
2242 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2243 (u64)-1);
2244
2245 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002246 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002247 if (!IS_ERR(trans))
2248 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002249 }
Josef Bacik7b128762008-07-24 12:17:14 -04002250
2251 if (nr_unlink)
2252 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2253 if (nr_truncate)
2254 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002255
2256out:
2257 if (ret)
2258 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2259 btrfs_free_path(path);
2260 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002261}
2262
Chris Masond352ac62008-09-29 15:18:18 -04002263/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002264 * very simple check to peek ahead in the leaf looking for xattrs. If we
2265 * don't find any xattrs, we know there can't be any acls.
2266 *
2267 * slot is the slot the inode is in, objectid is the objectid of the inode
2268 */
2269static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2270 int slot, u64 objectid)
2271{
2272 u32 nritems = btrfs_header_nritems(leaf);
2273 struct btrfs_key found_key;
2274 int scanned = 0;
2275
2276 slot++;
2277 while (slot < nritems) {
2278 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2279
2280 /* we found a different objectid, there must not be acls */
2281 if (found_key.objectid != objectid)
2282 return 0;
2283
2284 /* we found an xattr, assume we've got an acl */
2285 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2286 return 1;
2287
2288 /*
2289 * we found a key greater than an xattr key, there can't
2290 * be any acls later on
2291 */
2292 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2293 return 0;
2294
2295 slot++;
2296 scanned++;
2297
2298 /*
2299 * it goes inode, inode backrefs, xattrs, extents,
2300 * so if there are a ton of hard links to an inode there can
2301 * be a lot of backrefs. Don't waste time searching too hard,
2302 * this is just an optimization
2303 */
2304 if (scanned >= 8)
2305 break;
2306 }
2307 /* we hit the end of the leaf before we found an xattr or
2308 * something larger than an xattr. We have to assume the inode
2309 * has acls
2310 */
2311 return 1;
2312}
2313
2314/*
Chris Masond352ac62008-09-29 15:18:18 -04002315 * read an inode from the btree into the in-memory inode
2316 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002317static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002318{
2319 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002320 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002321 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002322 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002323 struct btrfs_root *root = BTRFS_I(inode)->root;
2324 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002325 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002326 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002327 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002328 bool filled = false;
2329
2330 ret = btrfs_fill_inode(inode, &rdev);
2331 if (!ret)
2332 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002333
2334 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002335 if (!path)
2336 goto make_bad;
2337
Josef Bacikd90c7322011-05-17 09:50:54 -04002338 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002339 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002340
Chris Mason39279cc2007-06-12 06:35:45 -04002341 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002342 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002343 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002344
Chris Mason5f39d392007-10-15 16:14:19 -04002345 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002346
2347 if (filled)
2348 goto cache_acl;
2349
Chris Mason5f39d392007-10-15 16:14:19 -04002350 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2351 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002352 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2353 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2354 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2355 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002356 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002357
2358 tspec = btrfs_inode_atime(inode_item);
2359 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2360 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2361
2362 tspec = btrfs_inode_mtime(inode_item);
2363 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2364 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2365
2366 tspec = btrfs_inode_ctime(inode_item);
2367 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2368 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2369
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002370 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002371 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002372 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002373 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002374 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002375 rdev = btrfs_inode_rdev(leaf, inode_item);
2376
Josef Bacikaec74772008-07-24 12:12:38 -04002377 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002378 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002379cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002380 /*
2381 * try to precache a NULL acl entry for files that don't have
2382 * any xattrs or acls
2383 */
Li Zefan33345d012011-04-20 10:31:50 +08002384 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2385 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002386 if (!maybe_acls)
2387 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002388
Chris Mason39279cc2007-06-12 06:35:45 -04002389 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002390
Chris Mason39279cc2007-06-12 06:35:45 -04002391 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002392 case S_IFREG:
2393 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002394 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002395 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002396 inode->i_fop = &btrfs_file_operations;
2397 inode->i_op = &btrfs_file_inode_operations;
2398 break;
2399 case S_IFDIR:
2400 inode->i_fop = &btrfs_dir_file_operations;
2401 if (root == root->fs_info->tree_root)
2402 inode->i_op = &btrfs_dir_ro_inode_operations;
2403 else
2404 inode->i_op = &btrfs_dir_inode_operations;
2405 break;
2406 case S_IFLNK:
2407 inode->i_op = &btrfs_symlink_inode_operations;
2408 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002409 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002410 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002411 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002412 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002413 init_special_inode(inode, inode->i_mode, rdev);
2414 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002415 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002416
2417 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002418 return;
2419
2420make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002421 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002422 make_bad_inode(inode);
2423}
2424
Chris Masond352ac62008-09-29 15:18:18 -04002425/*
2426 * given a leaf and an inode, copy the inode fields into the leaf
2427 */
Chris Masone02119d2008-09-05 16:13:11 -04002428static void fill_inode_item(struct btrfs_trans_handle *trans,
2429 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002430 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002431 struct inode *inode)
2432{
Chris Mason5f39d392007-10-15 16:14:19 -04002433 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2434 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002435 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002436 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2437 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2438
2439 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2440 inode->i_atime.tv_sec);
2441 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2442 inode->i_atime.tv_nsec);
2443
2444 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2445 inode->i_mtime.tv_sec);
2446 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2447 inode->i_mtime.tv_nsec);
2448
2449 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2450 inode->i_ctime.tv_sec);
2451 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2452 inode->i_ctime.tv_nsec);
2453
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002454 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002455 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002456 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002457 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002458 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002459 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f12011-05-11 15:26:06 -04002460 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002461}
2462
Chris Masond352ac62008-09-29 15:18:18 -04002463/*
2464 * copy everything in the in-memory inode into the btree.
2465 */
Chris Mason21151332011-11-10 20:39:08 -05002466static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002467 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002468{
2469 struct btrfs_inode_item *inode_item;
2470 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002471 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002472 int ret;
2473
2474 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002475 if (!path)
2476 return -ENOMEM;
2477
Chris Masonb9473432009-03-13 11:00:37 -04002478 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002479 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2480 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002481 if (ret) {
2482 if (ret > 0)
2483 ret = -ENOENT;
2484 goto failed;
2485 }
2486
Chris Masonb4ce94d2009-02-04 09:25:08 -05002487 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002488 leaf = path->nodes[0];
2489 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002490 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002491
Chris Masone02119d2008-09-05 16:13:11 -04002492 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002493 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002494 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002495 ret = 0;
2496failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002497 btrfs_free_path(path);
2498 return ret;
2499}
2500
Chris Masond352ac62008-09-29 15:18:18 -04002501/*
Chris Mason21151332011-11-10 20:39:08 -05002502 * copy everything in the in-memory inode into the btree.
2503 */
2504noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2505 struct btrfs_root *root, struct inode *inode)
2506{
2507 int ret;
2508
2509 /*
2510 * If the inode is a free space inode, we can deadlock during commit
2511 * if we put it into the delayed code.
2512 *
2513 * The data relocation inode should also be directly updated
2514 * without delay
2515 */
2516 if (!btrfs_is_free_space_inode(root, inode)
2517 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
2518 ret = btrfs_delayed_update_inode(trans, root, inode);
2519 if (!ret)
2520 btrfs_set_inode_last_trans(trans, inode);
2521 return ret;
2522 }
2523
2524 return btrfs_update_inode_item(trans, root, inode);
2525}
2526
2527static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2528 struct btrfs_root *root, struct inode *inode)
2529{
2530 int ret;
2531
2532 ret = btrfs_update_inode(trans, root, inode);
2533 if (ret == -ENOSPC)
2534 return btrfs_update_inode_item(trans, root, inode);
2535 return ret;
2536}
2537
2538/*
Chris Masond352ac62008-09-29 15:18:18 -04002539 * unlink helper that gets used here in inode.c and in the tree logging
2540 * recovery code. It remove a link in a directory with a given name, and
2541 * also drops the back refs in the inode to the directory
2542 */
Al Viro92986792011-03-04 17:14:37 +00002543static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2544 struct btrfs_root *root,
2545 struct inode *dir, struct inode *inode,
2546 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002547{
2548 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002549 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002550 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002551 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002552 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002553 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002554 u64 ino = btrfs_ino(inode);
2555 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002556
2557 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002558 if (!path) {
2559 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002560 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002561 }
2562
Chris Masonb9473432009-03-13 11:00:37 -04002563 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002564 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002565 name, name_len, -1);
2566 if (IS_ERR(di)) {
2567 ret = PTR_ERR(di);
2568 goto err;
2569 }
2570 if (!di) {
2571 ret = -ENOENT;
2572 goto err;
2573 }
Chris Mason5f39d392007-10-15 16:14:19 -04002574 leaf = path->nodes[0];
2575 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002576 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002577 if (ret)
2578 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002579 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002580
Li Zefan33345d012011-04-20 10:31:50 +08002581 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2582 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002583 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002584 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002585 "inode %llu parent %llu\n", name_len, name,
2586 (unsigned long long)ino, (unsigned long long)dir_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002587 goto err;
2588 }
2589
Miao Xie16cdcec2011-04-22 18:12:22 +08002590 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2591 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002592 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002593
Chris Masone02119d2008-09-05 16:13:11 -04002594 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002595 inode, dir_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002596 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002597
2598 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2599 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002600 if (ret == -ENOENT)
2601 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002602err:
2603 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002604 if (ret)
2605 goto out;
2606
2607 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2608 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2609 btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002610out:
Chris Mason39279cc2007-06-12 06:35:45 -04002611 return ret;
2612}
2613
Al Viro92986792011-03-04 17:14:37 +00002614int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2615 struct btrfs_root *root,
2616 struct inode *dir, struct inode *inode,
2617 const char *name, int name_len)
2618{
2619 int ret;
2620 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2621 if (!ret) {
2622 btrfs_drop_nlink(inode);
2623 ret = btrfs_update_inode(trans, root, inode);
2624 }
2625 return ret;
2626}
2627
2628
Yan, Zhenga22285a2010-05-16 10:48:46 -04002629/* helper to check if there is any shared block in the path */
2630static int check_path_shared(struct btrfs_root *root,
2631 struct btrfs_path *path)
2632{
2633 struct extent_buffer *eb;
2634 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00002635 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002636
2637 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002638 int ret;
2639
Yan, Zhenga22285a2010-05-16 10:48:46 -04002640 if (!path->nodes[level])
2641 break;
2642 eb = path->nodes[level];
2643 if (!btrfs_block_can_be_shared(root, eb))
2644 continue;
2645 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2646 &refs, NULL);
2647 if (refs > 1)
2648 return 1;
2649 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002650 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002651}
2652
2653/*
2654 * helper to start transaction for unlink and rmdir.
2655 *
2656 * unlink and rmdir are special in btrfs, they do not always free space.
2657 * so in enospc case, we should make sure they will free space before
2658 * allowing them to use the global metadata reservation.
2659 */
2660static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2661 struct dentry *dentry)
2662{
2663 struct btrfs_trans_handle *trans;
2664 struct btrfs_root *root = BTRFS_I(dir)->root;
2665 struct btrfs_path *path;
2666 struct btrfs_inode_ref *ref;
2667 struct btrfs_dir_item *di;
2668 struct inode *inode = dentry->d_inode;
2669 u64 index;
2670 int check_link = 1;
2671 int err = -ENOSPC;
2672 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002673 u64 ino = btrfs_ino(inode);
2674 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002675
Josef Bacike70bea52011-10-11 14:18:24 -04002676 /*
2677 * 1 for the possible orphan item
2678 * 1 for the dir item
2679 * 1 for the dir index
2680 * 1 for the inode ref
2681 * 1 for the inode ref in the tree log
2682 * 2 for the dir entries in the log
2683 * 1 for the inode
2684 */
2685 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002686 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2687 return trans;
2688
Li Zefan33345d012011-04-20 10:31:50 +08002689 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002690 return ERR_PTR(-ENOSPC);
2691
2692 /* check if there is someone else holds reference */
2693 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2694 return ERR_PTR(-ENOSPC);
2695
2696 if (atomic_read(&inode->i_count) > 2)
2697 return ERR_PTR(-ENOSPC);
2698
2699 if (xchg(&root->fs_info->enospc_unlink, 1))
2700 return ERR_PTR(-ENOSPC);
2701
2702 path = btrfs_alloc_path();
2703 if (!path) {
2704 root->fs_info->enospc_unlink = 0;
2705 return ERR_PTR(-ENOMEM);
2706 }
2707
Josef Bacik3880a1b2011-10-14 14:46:51 -04002708 /* 1 for the orphan item */
2709 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002710 if (IS_ERR(trans)) {
2711 btrfs_free_path(path);
2712 root->fs_info->enospc_unlink = 0;
2713 return trans;
2714 }
2715
2716 path->skip_locking = 1;
2717 path->search_commit_root = 1;
2718
2719 ret = btrfs_lookup_inode(trans, root, path,
2720 &BTRFS_I(dir)->location, 0);
2721 if (ret < 0) {
2722 err = ret;
2723 goto out;
2724 }
2725 if (ret == 0) {
2726 if (check_path_shared(root, path))
2727 goto out;
2728 } else {
2729 check_link = 0;
2730 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002731 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002732
2733 ret = btrfs_lookup_inode(trans, root, path,
2734 &BTRFS_I(inode)->location, 0);
2735 if (ret < 0) {
2736 err = ret;
2737 goto out;
2738 }
2739 if (ret == 0) {
2740 if (check_path_shared(root, path))
2741 goto out;
2742 } else {
2743 check_link = 0;
2744 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002745 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002746
2747 if (ret == 0 && S_ISREG(inode->i_mode)) {
2748 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002749 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002750 if (ret < 0) {
2751 err = ret;
2752 goto out;
2753 }
2754 BUG_ON(ret == 0);
2755 if (check_path_shared(root, path))
2756 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02002757 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002758 }
2759
2760 if (!check_link) {
2761 err = 0;
2762 goto out;
2763 }
2764
Li Zefan33345d012011-04-20 10:31:50 +08002765 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002766 dentry->d_name.name, dentry->d_name.len, 0);
2767 if (IS_ERR(di)) {
2768 err = PTR_ERR(di);
2769 goto out;
2770 }
2771 if (di) {
2772 if (check_path_shared(root, path))
2773 goto out;
2774 } else {
2775 err = 0;
2776 goto out;
2777 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002778 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002779
2780 ref = btrfs_lookup_inode_ref(trans, root, path,
2781 dentry->d_name.name, dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08002782 ino, dir_ino, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002783 if (IS_ERR(ref)) {
2784 err = PTR_ERR(ref);
2785 goto out;
2786 }
2787 BUG_ON(!ref);
2788 if (check_path_shared(root, path))
2789 goto out;
2790 index = btrfs_inode_ref_index(path->nodes[0], ref);
David Sterbab3b4aa72011-04-21 01:20:15 +02002791 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002792
Miao Xie16cdcec2011-04-22 18:12:22 +08002793 /*
2794 * This is a commit root search, if we can lookup inode item and other
2795 * relative items in the commit root, it means the transaction of
2796 * dir/file creation has been committed, and the dir index item that we
2797 * delay to insert has also been inserted into the commit root. So
2798 * we needn't worry about the delayed insertion of the dir index item
2799 * here.
2800 */
Li Zefan33345d012011-04-20 10:31:50 +08002801 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002802 dentry->d_name.name, dentry->d_name.len, 0);
2803 if (IS_ERR(di)) {
2804 err = PTR_ERR(di);
2805 goto out;
2806 }
2807 BUG_ON(ret == -ENOENT);
2808 if (check_path_shared(root, path))
2809 goto out;
2810
2811 err = 0;
2812out:
2813 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04002814 /* Migrate the orphan reservation over */
2815 if (!err)
2816 err = btrfs_block_rsv_migrate(trans->block_rsv,
2817 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04002818 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04002819
Yan, Zhenga22285a2010-05-16 10:48:46 -04002820 if (err) {
2821 btrfs_end_transaction(trans, root);
2822 root->fs_info->enospc_unlink = 0;
2823 return ERR_PTR(err);
2824 }
2825
2826 trans->block_rsv = &root->fs_info->global_block_rsv;
2827 return trans;
2828}
2829
2830static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2831 struct btrfs_root *root)
2832{
2833 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
Josef Bacik5a77d762011-11-01 14:32:23 -04002834 btrfs_block_rsv_release(root, trans->block_rsv,
2835 trans->bytes_reserved);
2836 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002837 BUG_ON(!root->fs_info->enospc_unlink);
2838 root->fs_info->enospc_unlink = 0;
2839 }
2840 btrfs_end_transaction_throttle(trans, root);
2841}
2842
Chris Mason39279cc2007-06-12 06:35:45 -04002843static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2844{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002845 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002846 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002847 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002848 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002849 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002850
Yan, Zhenga22285a2010-05-16 10:48:46 -04002851 trans = __unlink_start_trans(dir, dentry);
2852 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002853 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002854
Chris Mason12fcfd22009-03-24 10:24:20 -04002855 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2856
Chris Masone02119d2008-09-05 16:13:11 -04002857 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2858 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002859 if (ret)
2860 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002861
Yan, Zhenga22285a2010-05-16 10:48:46 -04002862 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002863 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002864 if (ret)
2865 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002866 }
Josef Bacik7b128762008-07-24 12:17:14 -04002867
Tsutomu Itohb5324022011-07-19 07:27:20 +00002868out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002869 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002870 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002871 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002872 return ret;
2873}
2874
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002875int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2876 struct btrfs_root *root,
2877 struct inode *dir, u64 objectid,
2878 const char *name, int name_len)
2879{
2880 struct btrfs_path *path;
2881 struct extent_buffer *leaf;
2882 struct btrfs_dir_item *di;
2883 struct btrfs_key key;
2884 u64 index;
2885 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002886 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002887
2888 path = btrfs_alloc_path();
2889 if (!path)
2890 return -ENOMEM;
2891
Li Zefan33345d012011-04-20 10:31:50 +08002892 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002893 name, name_len, -1);
David Sterbac7040052011-04-19 18:00:01 +02002894 BUG_ON(IS_ERR_OR_NULL(di));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002895
2896 leaf = path->nodes[0];
2897 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2898 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2899 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2900 BUG_ON(ret);
David Sterbab3b4aa72011-04-21 01:20:15 +02002901 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002902
2903 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2904 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002905 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002906 if (ret < 0) {
2907 BUG_ON(ret != -ENOENT);
Li Zefan33345d012011-04-20 10:31:50 +08002908 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002909 name, name_len);
David Sterbac7040052011-04-19 18:00:01 +02002910 BUG_ON(IS_ERR_OR_NULL(di));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002911
2912 leaf = path->nodes[0];
2913 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002914 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002915 index = key.offset;
2916 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002917 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002918
Miao Xie16cdcec2011-04-22 18:12:22 +08002919 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2920 BUG_ON(ret);
2921
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002922 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2923 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2924 ret = btrfs_update_inode(trans, root, dir);
2925 BUG_ON(ret);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002926
Josef Bacik71d7aed2011-06-14 14:24:32 -04002927 btrfs_free_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002928 return 0;
2929}
2930
Chris Mason39279cc2007-06-12 06:35:45 -04002931static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2932{
2933 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002934 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002935 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002936 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002937 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002938
Chris Mason3394e162008-11-17 20:42:26 -05002939 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Li Zefan33345d012011-04-20 10:31:50 +08002940 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002941 return -ENOTEMPTY;
2942
Yan, Zhenga22285a2010-05-16 10:48:46 -04002943 trans = __unlink_start_trans(dir, dentry);
2944 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002945 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002946
Li Zefan33345d012011-04-20 10:31:50 +08002947 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002948 err = btrfs_unlink_subvol(trans, root, dir,
2949 BTRFS_I(inode)->location.objectid,
2950 dentry->d_name.name,
2951 dentry->d_name.len);
2952 goto out;
2953 }
2954
Josef Bacik7b128762008-07-24 12:17:14 -04002955 err = btrfs_orphan_add(trans, inode);
2956 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002957 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002958
Chris Mason39279cc2007-06-12 06:35:45 -04002959 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002960 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2961 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002962 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002963 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002964out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002965 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002966 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002967 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002968
Chris Mason39279cc2007-06-12 06:35:45 -04002969 return err;
2970}
2971
Chris Mason323ac952008-10-01 19:05:46 -04002972/*
Chris Mason39279cc2007-06-12 06:35:45 -04002973 * this can truncate away extent items, csum items and directory items.
2974 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002975 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002976 *
2977 * csum items that cross the new i_size are truncated to the new size
2978 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002979 *
2980 * min_type is the minimum key type to truncate down to. If set to 0, this
2981 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002982 */
Yan, Zheng80825102009-11-12 09:35:36 +00002983int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2984 struct btrfs_root *root,
2985 struct inode *inode,
2986 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002987{
Chris Mason39279cc2007-06-12 06:35:45 -04002988 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002989 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002990 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00002991 struct btrfs_key key;
2992 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002993 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002994 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002995 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002996 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00002997 u64 mask = root->sectorsize - 1;
2998 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04002999 int found_extent;
3000 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003001 int pending_del_nr = 0;
3002 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003003 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003004 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003005 int ret;
3006 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003007 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003008
3009 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003010
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003011 path = btrfs_alloc_path();
3012 if (!path)
3013 return -ENOMEM;
3014 path->reada = -1;
3015
Josef Bacik0af3d002010-06-21 14:48:16 -04003016 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003017 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003018
Miao Xie16cdcec2011-04-22 18:12:22 +08003019 /*
3020 * This function is also used to drop the items in the log tree before
3021 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3022 * it is used to drop the loged items. So we shouldn't kill the delayed
3023 * items.
3024 */
3025 if (min_type == 0 && root == BTRFS_I(inode)->root)
3026 btrfs_kill_delayed_inode_items(inode);
3027
Li Zefan33345d012011-04-20 10:31:50 +08003028 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003029 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003030 key.type = (u8)-1;
3031
Chris Mason85e21ba2008-01-29 15:11:36 -05003032search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003033 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003034 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003035 if (ret < 0) {
3036 err = ret;
3037 goto out;
3038 }
Chris Masond3977122009-01-05 21:25:51 -05003039
Chris Mason85e21ba2008-01-29 15:11:36 -05003040 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003041 /* there are no items in the tree for us to truncate, we're
3042 * done
3043 */
Yan, Zheng80825102009-11-12 09:35:36 +00003044 if (path->slots[0] == 0)
3045 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003046 path->slots[0]--;
3047 }
3048
Chris Masond3977122009-01-05 21:25:51 -05003049 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003050 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003051 leaf = path->nodes[0];
3052 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3053 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003054 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003055
Li Zefan33345d012011-04-20 10:31:50 +08003056 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003057 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003058
Chris Mason85e21ba2008-01-29 15:11:36 -05003059 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003060 break;
3061
Chris Mason5f39d392007-10-15 16:14:19 -04003062 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003063 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003064 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003065 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003066 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003067 encoding = btrfs_file_extent_compression(leaf, fi);
3068 encoding |= btrfs_file_extent_encryption(leaf, fi);
3069 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3070
Chris Mason179e29e2007-11-01 11:28:41 -04003071 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003072 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003073 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003074 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003075 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003076 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003077 }
Yan008630c2007-11-07 13:31:09 -05003078 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003079 }
Yan, Zheng80825102009-11-12 09:35:36 +00003080 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003081 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003082 } else {
3083 if (item_end < new_size)
3084 break;
3085 if (found_key.offset >= new_size)
3086 del_item = 1;
3087 else
3088 del_item = 0;
3089 }
Chris Mason39279cc2007-06-12 06:35:45 -04003090 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003091 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003092 if (found_type != BTRFS_EXTENT_DATA_KEY)
3093 goto delete;
3094
3095 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003096 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003097 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003098 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003099 u64 orig_num_bytes =
3100 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003101 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003102 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003103 extent_num_bytes = extent_num_bytes &
3104 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003105 btrfs_set_file_extent_num_bytes(leaf, fi,
3106 extent_num_bytes);
3107 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003108 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003109 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003110 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003111 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003112 } else {
Chris Masondb945352007-10-15 16:15:53 -04003113 extent_num_bytes =
3114 btrfs_file_extent_disk_num_bytes(leaf,
3115 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003116 extent_offset = found_key.offset -
3117 btrfs_file_extent_offset(leaf, fi);
3118
Chris Mason39279cc2007-06-12 06:35:45 -04003119 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003120 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003121 if (extent_start != 0) {
3122 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003123 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003124 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003125 }
3126 }
Chris Mason90692182008-02-08 13:49:28 -05003127 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003128 /*
3129 * we can't truncate inline items that have had
3130 * special encodings
3131 */
3132 if (!del_item &&
3133 btrfs_file_extent_compression(leaf, fi) == 0 &&
3134 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3135 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003136 u32 size = new_size - found_key.offset;
3137
3138 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003139 inode_sub_bytes(inode, item_end + 1 -
3140 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003141 }
3142 size =
3143 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003144 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003145 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003146 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003147 inode_sub_bytes(inode, item_end + 1 -
3148 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003149 }
Chris Mason39279cc2007-06-12 06:35:45 -04003150 }
Chris Mason179e29e2007-11-01 11:28:41 -04003151delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003152 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003153 if (!pending_del_nr) {
3154 /* no pending yet, add ourselves */
3155 pending_del_slot = path->slots[0];
3156 pending_del_nr = 1;
3157 } else if (pending_del_nr &&
3158 path->slots[0] + 1 == pending_del_slot) {
3159 /* hop on the pending chunk */
3160 pending_del_nr++;
3161 pending_del_slot = path->slots[0];
3162 } else {
Chris Masond3977122009-01-05 21:25:51 -05003163 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003164 }
Chris Mason39279cc2007-06-12 06:35:45 -04003165 } else {
3166 break;
3167 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003168 if (found_extent && (root->ref_cows ||
3169 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003170 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003171 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003172 extent_num_bytes, 0,
3173 btrfs_header_owner(leaf),
Li Zefan33345d012011-04-20 10:31:50 +08003174 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003175 BUG_ON(ret);
3176 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003177
Yan, Zheng80825102009-11-12 09:35:36 +00003178 if (found_type == BTRFS_INODE_ITEM_KEY)
3179 break;
3180
3181 if (path->slots[0] == 0 ||
3182 path->slots[0] != pending_del_slot) {
Li Zefan82d59022011-04-20 10:33:24 +08003183 if (root->ref_cows &&
3184 BTRFS_I(inode)->location.objectid !=
3185 BTRFS_FREE_INO_OBJECTID) {
Yan, Zheng80825102009-11-12 09:35:36 +00003186 err = -EAGAIN;
3187 goto out;
3188 }
3189 if (pending_del_nr) {
3190 ret = btrfs_del_items(trans, root, path,
3191 pending_del_slot,
3192 pending_del_nr);
3193 BUG_ON(ret);
3194 pending_del_nr = 0;
3195 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003196 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003197 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003198 } else {
3199 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003200 }
Chris Mason39279cc2007-06-12 06:35:45 -04003201 }
Yan, Zheng80825102009-11-12 09:35:36 +00003202out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003203 if (pending_del_nr) {
3204 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3205 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003206 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003207 }
Chris Mason39279cc2007-06-12 06:35:45 -04003208 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003209 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003210}
3211
Chris Masona52d9a82007-08-27 16:49:44 -04003212/*
3213 * taken from block_truncate_page, but does cow as it zeros out
3214 * any bytes left in the last page in the file.
3215 */
3216static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3217{
3218 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003219 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003220 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3221 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003222 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003223 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003224 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003225 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3226 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3227 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003228 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003229 int ret = 0;
3230 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003231 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003232
3233 if ((offset & (blocksize - 1)) == 0)
3234 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003235 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003236 if (ret)
3237 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003238
3239 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003240again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003241 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003242 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003243 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003244 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003245 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003246
3247 page_start = page_offset(page);
3248 page_end = page_start + PAGE_CACHE_SIZE - 1;
3249
Chris Masona52d9a82007-08-27 16:49:44 -04003250 if (!PageUptodate(page)) {
3251 ret = btrfs_readpage(NULL, page);
3252 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003253 if (page->mapping != mapping) {
3254 unlock_page(page);
3255 page_cache_release(page);
3256 goto again;
3257 }
Chris Masona52d9a82007-08-27 16:49:44 -04003258 if (!PageUptodate(page)) {
3259 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003260 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003261 }
3262 }
Chris Mason211c17f2008-05-15 09:13:45 -04003263 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003264
Josef Bacik2ac55d42010-02-03 19:33:23 +00003265 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3266 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003267 set_page_extent_mapped(page);
3268
3269 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3270 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003271 unlock_extent_cached(io_tree, page_start, page_end,
3272 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003273 unlock_page(page);
3274 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003275 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003276 btrfs_put_ordered_extent(ordered);
3277 goto again;
3278 }
3279
Josef Bacik2ac55d42010-02-03 19:33:23 +00003280 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003281 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003282 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003283
Josef Bacik2ac55d42010-02-03 19:33:23 +00003284 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3285 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003286 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003287 unlock_extent_cached(io_tree, page_start, page_end,
3288 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003289 goto out_unlock;
3290 }
3291
Chris Masone6dcd2d2008-07-17 12:53:50 -04003292 ret = 0;
3293 if (offset != PAGE_CACHE_SIZE) {
3294 kaddr = kmap(page);
3295 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3296 flush_dcache_page(page);
3297 kunmap(page);
3298 }
Chris Mason247e7432008-07-17 12:53:51 -04003299 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003300 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003301 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3302 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003303
Chris Mason89642222008-07-24 09:41:53 -04003304out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003305 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003306 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003307 unlock_page(page);
3308 page_cache_release(page);
3309out:
3310 return ret;
3311}
3312
Josef Bacik695a0d02011-03-04 15:46:53 -05003313/*
3314 * This function puts in dummy file extents for the area we're creating a hole
3315 * for. So if we are truncating this file to a larger size we need to insert
3316 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3317 * the range between oldsize and size
3318 */
Josef Bacika41ad392011-01-31 15:30:16 -05003319int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003320{
3321 struct btrfs_trans_handle *trans;
3322 struct btrfs_root *root = BTRFS_I(inode)->root;
3323 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003324 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003325 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003326 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003327 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003328 u64 block_end = (size + mask) & ~mask;
3329 u64 last_byte;
3330 u64 cur_offset;
3331 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003332 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003333
3334 if (size <= hole_start)
3335 return 0;
3336
Yan Zheng9036c102008-10-30 14:19:41 -04003337 while (1) {
3338 struct btrfs_ordered_extent *ordered;
3339 btrfs_wait_ordered_range(inode, hole_start,
3340 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003341 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3342 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003343 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3344 if (!ordered)
3345 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003346 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3347 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003348 btrfs_put_ordered_extent(ordered);
3349 }
3350
Yan Zheng9036c102008-10-30 14:19:41 -04003351 cur_offset = hole_start;
3352 while (1) {
3353 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3354 block_end - cur_offset, 0);
David Sterbac7040052011-04-19 18:00:01 +02003355 BUG_ON(IS_ERR_OR_NULL(em));
Yan Zheng9036c102008-10-30 14:19:41 -04003356 last_byte = min(extent_map_end(em), block_end);
3357 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003358 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003359 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003360 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003361
Miao Xie36423202011-12-14 20:12:02 -05003362 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003363 if (IS_ERR(trans)) {
3364 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003365 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003366 }
Yan, Zheng80825102009-11-12 09:35:36 +00003367
3368 err = btrfs_drop_extents(trans, inode, cur_offset,
3369 cur_offset + hole_size,
3370 &hint_byte, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003371 if (err) {
Miao Xie36423202011-12-14 20:12:02 -05003372 btrfs_update_inode(trans, root, inode);
Miao Xie5b397372011-09-11 10:52:24 -04003373 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003374 break;
Miao Xie5b397372011-09-11 10:52:24 -04003375 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003376
Yan Zheng9036c102008-10-30 14:19:41 -04003377 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003378 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003379 0, hole_size, 0, hole_size,
3380 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003381 if (err) {
Miao Xie36423202011-12-14 20:12:02 -05003382 btrfs_update_inode(trans, root, inode);
Miao Xie5b397372011-09-11 10:52:24 -04003383 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003384 break;
Miao Xie5b397372011-09-11 10:52:24 -04003385 }
Yan, Zheng80825102009-11-12 09:35:36 +00003386
Yan Zheng9036c102008-10-30 14:19:41 -04003387 btrfs_drop_extent_cache(inode, hole_start,
3388 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003389
Miao Xie36423202011-12-14 20:12:02 -05003390 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003391 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003392 }
3393 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003394 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003395 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003396 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003397 break;
3398 }
3399
Yan, Zhenga22285a2010-05-16 10:48:46 -04003400 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003401 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3402 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003403 return err;
3404}
3405
Josef Bacika41ad392011-01-31 15:30:16 -05003406static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003407{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003408 struct btrfs_root *root = BTRFS_I(inode)->root;
3409 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003410 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003411 int ret;
3412
Josef Bacika41ad392011-01-31 15:30:16 -05003413 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003414 return 0;
3415
Josef Bacika41ad392011-01-31 15:30:16 -05003416 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003417 truncate_pagecache(inode, oldsize, newsize);
3418 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003419 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003420 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003421
Miao Xief4a2f4c2011-12-14 20:12:01 -05003422 trans = btrfs_start_transaction(root, 1);
3423 if (IS_ERR(trans))
3424 return PTR_ERR(trans);
3425
3426 i_size_write(inode, newsize);
3427 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3428 ret = btrfs_update_inode(trans, root, inode);
3429
3430 btrfs_end_transaction_throttle(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003431 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003432
Josef Bacika41ad392011-01-31 15:30:16 -05003433 /*
3434 * We're truncating a file that used to have good data down to
3435 * zero. Make sure it gets into the ordered flush list so that
3436 * any new writes get down to disk quickly.
3437 */
3438 if (newsize == 0)
3439 BTRFS_I(inode)->ordered_data_close = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003440
Josef Bacika41ad392011-01-31 15:30:16 -05003441 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3442 truncate_setsize(inode, newsize);
3443 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003444 }
3445
Josef Bacika41ad392011-01-31 15:30:16 -05003446 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003447}
3448
Chris Mason39279cc2007-06-12 06:35:45 -04003449static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3450{
3451 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003452 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003453 int err;
3454
Li Zefanb83cc962010-12-20 16:04:08 +08003455 if (btrfs_root_readonly(root))
3456 return -EROFS;
3457
Chris Mason39279cc2007-06-12 06:35:45 -04003458 err = inode_change_ok(inode, attr);
3459 if (err)
3460 return err;
3461
Chris Mason5a3f23d2009-03-31 13:27:11 -04003462 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003463 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003464 if (err)
3465 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003466 }
Yan Zheng9036c102008-10-30 14:19:41 -04003467
Christoph Hellwig10257742010-06-04 11:30:02 +02003468 if (attr->ia_valid) {
3469 setattr_copy(inode, attr);
3470 mark_inode_dirty(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003471
Christoph Hellwig10257742010-06-04 11:30:02 +02003472 if (attr->ia_valid & ATTR_MODE)
3473 err = btrfs_acl_chmod(inode);
3474 }
3475
Chris Mason39279cc2007-06-12 06:35:45 -04003476 return err;
3477}
Chris Mason61295eb2008-01-14 16:24:38 -05003478
Al Virobd555972010-06-07 11:35:40 -04003479void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003480{
3481 struct btrfs_trans_handle *trans;
3482 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003483 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003484 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003485 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003486 int ret;
3487
liubo1abe9b82011-03-24 11:18:59 +00003488 trace_btrfs_inode_evict(inode);
3489
Chris Mason39279cc2007-06-12 06:35:45 -04003490 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003491 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Chris Mason2cf85722011-07-26 15:35:09 -04003492 btrfs_is_free_space_inode(root, inode)))
Al Virobd555972010-06-07 11:35:40 -04003493 goto no_delete;
3494
Chris Mason39279cc2007-06-12 06:35:45 -04003495 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003496 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003497 goto no_delete;
3498 }
Al Virobd555972010-06-07 11:35:40 -04003499 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003500 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003501
Yan, Zhengc71bf092009-11-12 09:34:40 +00003502 if (root->fs_info->log_root_recovering) {
3503 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3504 goto no_delete;
3505 }
3506
Yan, Zheng76dda932009-09-21 16:00:26 -04003507 if (inode->i_nlink > 0) {
3508 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3509 goto no_delete;
3510 }
3511
Josef Bacik4289a662011-08-05 13:22:24 -04003512 rsv = btrfs_alloc_block_rsv(root);
3513 if (!rsv) {
3514 btrfs_orphan_del(NULL, inode);
3515 goto no_delete;
3516 }
Josef Bacik4a338542011-08-29 11:01:31 -04003517 rsv->size = min_size;
Josef Bacik726c35f2011-09-26 15:46:06 -04003518 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003519
Chris Masondbe674a2008-07-17 12:54:05 -04003520 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003521
Josef Bacik4289a662011-08-05 13:22:24 -04003522 /*
3523 * This is a bit simpler than btrfs_truncate since
3524 *
3525 * 1) We've already reserved our space for our orphan item in the
3526 * unlink.
3527 * 2) We're going to delete the inode item, so we don't need to update
3528 * it at all.
3529 *
3530 * So we just need to reserve some slack space in case we add bytes when
3531 * doing the truncate.
3532 */
Yan, Zheng80825102009-11-12 09:35:36 +00003533 while (1) {
Miao Xieaa38a712011-11-18 17:43:00 +08003534 ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
Josef Bacik726c35f2011-09-26 15:46:06 -04003535
3536 /*
3537 * Try and steal from the global reserve since we will
3538 * likely not use this space anyway, we want to try as
3539 * hard as possible to get this to work.
3540 */
3541 if (ret)
3542 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3543
Yan, Zhengd68fc572010-05-16 10:49:58 -04003544 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003545 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003546 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003547 btrfs_orphan_del(NULL, inode);
3548 btrfs_free_block_rsv(root, rsv);
3549 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003550 }
3551
Josef Bacik4289a662011-08-05 13:22:24 -04003552 trans = btrfs_start_transaction(root, 0);
3553 if (IS_ERR(trans)) {
3554 btrfs_orphan_del(NULL, inode);
3555 btrfs_free_block_rsv(root, rsv);
3556 goto no_delete;
3557 }
3558
3559 trans->block_rsv = rsv;
3560
Yan, Zhengd68fc572010-05-16 10:49:58 -04003561 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003562 if (ret != -EAGAIN)
3563 break;
3564
3565 nr = trans->blocks_used;
3566 btrfs_end_transaction(trans, root);
3567 trans = NULL;
3568 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003569 }
3570
Josef Bacik4289a662011-08-05 13:22:24 -04003571 btrfs_free_block_rsv(root, rsv);
3572
Yan, Zheng80825102009-11-12 09:35:36 +00003573 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003574 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003575 ret = btrfs_orphan_del(trans, inode);
3576 BUG_ON(ret);
3577 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003578
Josef Bacik4289a662011-08-05 13:22:24 -04003579 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003580 if (!(root == root->fs_info->tree_root ||
3581 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003582 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003583
Chris Masond3c2fdc2007-09-17 10:58:06 -04003584 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003585 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003586 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003587no_delete:
Al Virobd555972010-06-07 11:35:40 -04003588 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003589 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003590}
3591
3592/*
3593 * this returns the key found in the dir entry in the location pointer.
3594 * If no dir entries were found, location->objectid is 0.
3595 */
3596static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3597 struct btrfs_key *location)
3598{
3599 const char *name = dentry->d_name.name;
3600 int namelen = dentry->d_name.len;
3601 struct btrfs_dir_item *di;
3602 struct btrfs_path *path;
3603 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003604 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003605
3606 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003607 if (!path)
3608 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003609
Li Zefan33345d012011-04-20 10:31:50 +08003610 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003611 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003612 if (IS_ERR(di))
3613 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003614
David Sterbac7040052011-04-19 18:00:01 +02003615 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003616 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003617
Chris Mason5f39d392007-10-15 16:14:19 -04003618 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003619out:
Chris Mason39279cc2007-06-12 06:35:45 -04003620 btrfs_free_path(path);
3621 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003622out_err:
3623 location->objectid = 0;
3624 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003625}
3626
3627/*
3628 * when we hit a tree root in a directory, the btrfs part of the inode
3629 * needs to be changed to reflect the root directory of the tree root. This
3630 * is kind of like crossing a mount point.
3631 */
3632static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003633 struct inode *dir,
3634 struct dentry *dentry,
3635 struct btrfs_key *location,
3636 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003637{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003638 struct btrfs_path *path;
3639 struct btrfs_root *new_root;
3640 struct btrfs_root_ref *ref;
3641 struct extent_buffer *leaf;
3642 int ret;
3643 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003644
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003645 path = btrfs_alloc_path();
3646 if (!path) {
3647 err = -ENOMEM;
3648 goto out;
3649 }
Chris Mason39279cc2007-06-12 06:35:45 -04003650
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003651 err = -ENOENT;
3652 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3653 BTRFS_I(dir)->root->root_key.objectid,
3654 location->objectid);
3655 if (ret) {
3656 if (ret < 0)
3657 err = ret;
3658 goto out;
3659 }
Chris Mason39279cc2007-06-12 06:35:45 -04003660
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003661 leaf = path->nodes[0];
3662 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003663 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003664 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3665 goto out;
3666
3667 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3668 (unsigned long)(ref + 1),
3669 dentry->d_name.len);
3670 if (ret)
3671 goto out;
3672
David Sterbab3b4aa72011-04-21 01:20:15 +02003673 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003674
3675 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3676 if (IS_ERR(new_root)) {
3677 err = PTR_ERR(new_root);
3678 goto out;
3679 }
3680
3681 if (btrfs_root_refs(&new_root->root_item) == 0) {
3682 err = -ENOENT;
3683 goto out;
3684 }
3685
3686 *sub_root = new_root;
3687 location->objectid = btrfs_root_dirid(&new_root->root_item);
3688 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003689 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003690 err = 0;
3691out:
3692 btrfs_free_path(path);
3693 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003694}
3695
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003696static void inode_tree_add(struct inode *inode)
3697{
3698 struct btrfs_root *root = BTRFS_I(inode)->root;
3699 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003700 struct rb_node **p;
3701 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08003702 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003703again:
3704 p = &root->inode_tree.rb_node;
3705 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003706
Al Viro1d3382c2010-10-23 15:19:20 -04003707 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003708 return;
3709
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003710 spin_lock(&root->inode_lock);
3711 while (*p) {
3712 parent = *p;
3713 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3714
Li Zefan33345d012011-04-20 10:31:50 +08003715 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003716 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003717 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003718 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003719 else {
3720 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003721 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003722 rb_erase(parent, &root->inode_tree);
3723 RB_CLEAR_NODE(parent);
3724 spin_unlock(&root->inode_lock);
3725 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003726 }
3727 }
3728 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3729 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3730 spin_unlock(&root->inode_lock);
3731}
3732
3733static void inode_tree_del(struct inode *inode)
3734{
3735 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003736 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003737
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003738 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003739 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003740 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003741 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003742 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003743 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003744 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003745
Josef Bacik0af3d002010-06-21 14:48:16 -04003746 /*
3747 * Free space cache has inodes in the tree root, but the tree root has a
3748 * root_refs of 0, so this could end up dropping the tree root as a
3749 * snapshot, so we need the extra !root->fs_info->tree_root check to
3750 * make sure we don't drop it.
3751 */
3752 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3753 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003754 synchronize_srcu(&root->fs_info->subvol_srcu);
3755 spin_lock(&root->inode_lock);
3756 empty = RB_EMPTY_ROOT(&root->inode_tree);
3757 spin_unlock(&root->inode_lock);
3758 if (empty)
3759 btrfs_add_dead_root(root);
3760 }
3761}
3762
3763int btrfs_invalidate_inodes(struct btrfs_root *root)
3764{
3765 struct rb_node *node;
3766 struct rb_node *prev;
3767 struct btrfs_inode *entry;
3768 struct inode *inode;
3769 u64 objectid = 0;
3770
3771 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3772
3773 spin_lock(&root->inode_lock);
3774again:
3775 node = root->inode_tree.rb_node;
3776 prev = NULL;
3777 while (node) {
3778 prev = node;
3779 entry = rb_entry(node, struct btrfs_inode, rb_node);
3780
Li Zefan33345d012011-04-20 10:31:50 +08003781 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003782 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003783 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003784 node = node->rb_right;
3785 else
3786 break;
3787 }
3788 if (!node) {
3789 while (prev) {
3790 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08003791 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003792 node = prev;
3793 break;
3794 }
3795 prev = rb_next(prev);
3796 }
3797 }
3798 while (node) {
3799 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08003800 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04003801 inode = igrab(&entry->vfs_inode);
3802 if (inode) {
3803 spin_unlock(&root->inode_lock);
3804 if (atomic_read(&inode->i_count) > 1)
3805 d_prune_aliases(inode);
3806 /*
Al Viro45321ac2010-06-07 13:43:19 -04003807 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04003808 * the inode cache when its usage count
3809 * hits zero.
3810 */
3811 iput(inode);
3812 cond_resched();
3813 spin_lock(&root->inode_lock);
3814 goto again;
3815 }
3816
3817 if (cond_resched_lock(&root->inode_lock))
3818 goto again;
3819
3820 node = rb_next(node);
3821 }
3822 spin_unlock(&root->inode_lock);
3823 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003824}
3825
Chris Masone02119d2008-09-05 16:13:11 -04003826static int btrfs_init_locked_inode(struct inode *inode, void *p)
3827{
3828 struct btrfs_iget_args *args = p;
3829 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04003830 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003831 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003832 return 0;
3833}
3834
3835static int btrfs_find_actor(struct inode *inode, void *opaque)
3836{
3837 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08003838 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05003839 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003840}
3841
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003842static struct inode *btrfs_iget_locked(struct super_block *s,
3843 u64 objectid,
3844 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003845{
3846 struct inode *inode;
3847 struct btrfs_iget_args args;
3848 args.ino = objectid;
3849 args.root = root;
3850
3851 inode = iget5_locked(s, objectid, btrfs_find_actor,
3852 btrfs_init_locked_inode,
3853 (void *)&args);
3854 return inode;
3855}
3856
Balaji Rao1a54ef82008-07-21 02:01:04 +05303857/* Get an inode object given its location and corresponding root.
3858 * Returns in *is_new if the inode was read from disk
3859 */
3860struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00003861 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303862{
3863 struct inode *inode;
3864
3865 inode = btrfs_iget_locked(s, location->objectid, root);
3866 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003867 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303868
3869 if (inode->i_state & I_NEW) {
3870 BTRFS_I(inode)->root = root;
3871 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3872 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07003873 if (!is_bad_inode(inode)) {
3874 inode_tree_add(inode);
3875 unlock_new_inode(inode);
3876 if (new)
3877 *new = 1;
3878 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04003879 unlock_new_inode(inode);
3880 iput(inode);
3881 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07003882 }
3883 }
3884
Balaji Rao1a54ef82008-07-21 02:01:04 +05303885 return inode;
3886}
3887
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003888static struct inode *new_simple_dir(struct super_block *s,
3889 struct btrfs_key *key,
3890 struct btrfs_root *root)
3891{
3892 struct inode *inode = new_inode(s);
3893
3894 if (!inode)
3895 return ERR_PTR(-ENOMEM);
3896
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003897 BTRFS_I(inode)->root = root;
3898 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3899 BTRFS_I(inode)->dummy_inode = 1;
3900
3901 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3902 inode->i_op = &simple_dir_inode_operations;
3903 inode->i_fop = &simple_dir_operations;
3904 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3905 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3906
3907 return inode;
3908}
3909
Chris Mason3de45862008-11-17 21:02:50 -05003910struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003911{
Chris Masond3977122009-01-05 21:25:51 -05003912 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003913 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003914 struct btrfs_root *sub_root = root;
3915 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003916 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04003917 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003918
3919 if (dentry->d_name.len > BTRFS_NAME_LEN)
3920 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003921
Josef Bacikb4aff1f2011-06-28 16:18:59 -04003922 if (unlikely(d_need_lookup(dentry))) {
3923 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
3924 kfree(dentry->d_fsdata);
3925 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04003926 /* This thing is hashed, drop it for now */
3927 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04003928 } else {
3929 ret = btrfs_inode_by_name(dir, dentry, &location);
3930 }
Chris Mason5f39d392007-10-15 16:14:19 -04003931
Chris Mason39279cc2007-06-12 06:35:45 -04003932 if (ret < 0)
3933 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003934
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003935 if (location.objectid == 0)
3936 return NULL;
3937
3938 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00003939 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003940 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003941 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003942
3943 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3944
Yan, Zheng76dda932009-09-21 16:00:26 -04003945 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003946 ret = fixup_tree_root_location(root, dir, dentry,
3947 &location, &sub_root);
3948 if (ret < 0) {
3949 if (ret != -ENOENT)
3950 inode = ERR_PTR(ret);
3951 else
3952 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3953 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00003954 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003955 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003956 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3957
Julia Lawall34d19ba2011-01-24 19:55:19 +00003958 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00003959 down_read(&root->fs_info->cleanup_work_sem);
3960 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003961 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00003962 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003963 if (ret)
3964 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00003965 }
3966
Chris Mason3de45862008-11-17 21:02:50 -05003967 return inode;
3968}
3969
Nick Pigginfe15ce42011-01-07 17:49:23 +11003970static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04003971{
3972 struct btrfs_root *root;
3973
Yan, Zhengefefb142009-10-09 09:25:16 -04003974 if (!dentry->d_inode && !IS_ROOT(dentry))
3975 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04003976
Yan, Zhengefefb142009-10-09 09:25:16 -04003977 if (dentry->d_inode) {
3978 root = BTRFS_I(dentry->d_inode)->root;
3979 if (btrfs_root_refs(&root->root_item) == 0)
3980 return 1;
3981 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003982 return 0;
3983}
3984
Josef Bacikb4aff1f2011-06-28 16:18:59 -04003985static void btrfs_dentry_release(struct dentry *dentry)
3986{
3987 if (dentry->d_fsdata)
3988 kfree(dentry->d_fsdata);
3989}
3990
Chris Mason3de45862008-11-17 21:02:50 -05003991static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3992 struct nameidata *nd)
3993{
Josef Bacika66e7cc2011-09-18 10:34:03 -04003994 struct dentry *ret;
3995
3996 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
3997 if (unlikely(d_need_lookup(dentry))) {
3998 spin_lock(&dentry->d_lock);
3999 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4000 spin_unlock(&dentry->d_lock);
4001 }
4002 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004003}
4004
Miao Xie16cdcec2011-04-22 18:12:22 +08004005unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004006 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4007};
4008
David Woodhousecbdf5a22008-08-06 19:42:33 +01004009static int btrfs_real_readdir(struct file *filp, void *dirent,
4010 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004011{
Chris Mason6da6aba2007-12-18 16:15:09 -05004012 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004013 struct btrfs_root *root = BTRFS_I(inode)->root;
4014 struct btrfs_item *item;
4015 struct btrfs_dir_item *di;
4016 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004017 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004018 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004019 struct list_head ins_list;
4020 struct list_head del_list;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004021 struct qstr q;
Chris Mason39279cc2007-06-12 06:35:45 -04004022 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004023 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004024 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004025 unsigned char d_type;
4026 int over = 0;
4027 u32 di_cur;
4028 u32 di_total;
4029 u32 di_len;
4030 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004031 char tmp_name[32];
4032 char *name_ptr;
4033 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004034 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004035
4036 /* FIXME, use a real flag for deciding about the key type */
4037 if (root->fs_info->tree_root == root)
4038 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004039
Chris Mason39544012007-12-12 14:38:19 -05004040 /* special case for "." */
4041 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004042 over = filldir(dirent, ".", 1,
4043 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004044 if (over)
4045 return 0;
4046 filp->f_pos = 1;
4047 }
Chris Mason39544012007-12-12 14:38:19 -05004048 /* special case for .., just use the back ref */
4049 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004050 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004051 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004052 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004053 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004054 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004055 filp->f_pos = 2;
4056 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004057 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004058 if (!path)
4059 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004060
Josef Bacik026fd312011-05-13 10:32:11 -04004061 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004062
Miao Xie16cdcec2011-04-22 18:12:22 +08004063 if (key_type == BTRFS_DIR_INDEX_KEY) {
4064 INIT_LIST_HEAD(&ins_list);
4065 INIT_LIST_HEAD(&del_list);
4066 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4067 }
4068
Chris Mason39279cc2007-06-12 06:35:45 -04004069 btrfs_set_key_type(&key, key_type);
4070 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004071 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004072
Chris Mason39279cc2007-06-12 06:35:45 -04004073 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4074 if (ret < 0)
4075 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004076
4077 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004078 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004079 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004080 if (slot >= btrfs_header_nritems(leaf)) {
4081 ret = btrfs_next_leaf(root, path);
4082 if (ret < 0)
4083 goto err;
4084 else if (ret > 0)
4085 break;
4086 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004087 }
Chris Mason3de45862008-11-17 21:02:50 -05004088
Chris Mason5f39d392007-10-15 16:14:19 -04004089 item = btrfs_item_nr(leaf, slot);
4090 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4091
4092 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004093 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004094 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004095 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004096 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004097 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004098 if (key_type == BTRFS_DIR_INDEX_KEY &&
4099 btrfs_should_delete_dir_index(&del_list,
4100 found_key.offset))
4101 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004102
4103 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004104 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004105
Chris Mason39279cc2007-06-12 06:35:45 -04004106 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4107 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004108 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004109
4110 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004111 struct btrfs_key location;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004112 struct dentry *tmp;
Chris Mason5f39d392007-10-15 16:14:19 -04004113
Josef Bacik22a94d42011-03-16 16:47:17 -04004114 if (verify_dir_item(root, leaf, di))
4115 break;
4116
Chris Mason5f39d392007-10-15 16:14:19 -04004117 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004118 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004119 name_ptr = tmp_name;
4120 } else {
4121 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004122 if (!name_ptr) {
4123 ret = -ENOMEM;
4124 goto err;
4125 }
Chris Mason5f39d392007-10-15 16:14:19 -04004126 }
4127 read_extent_buffer(leaf, name_ptr,
4128 (unsigned long)(di + 1), name_len);
4129
4130 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4131 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004132
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004133 q.name = name_ptr;
4134 q.len = name_len;
4135 q.hash = full_name_hash(q.name, q.len);
4136 tmp = d_lookup(filp->f_dentry, &q);
4137 if (!tmp) {
4138 struct btrfs_key *newkey;
4139
4140 newkey = kzalloc(sizeof(struct btrfs_key),
4141 GFP_NOFS);
4142 if (!newkey)
4143 goto no_dentry;
4144 tmp = d_alloc(filp->f_dentry, &q);
4145 if (!tmp) {
4146 kfree(newkey);
4147 dput(tmp);
4148 goto no_dentry;
4149 }
4150 memcpy(newkey, &location,
4151 sizeof(struct btrfs_key));
4152 tmp->d_fsdata = newkey;
4153 tmp->d_flags |= DCACHE_NEED_LOOKUP;
4154 d_rehash(tmp);
4155 dput(tmp);
4156 } else {
4157 dput(tmp);
4158 }
4159no_dentry:
Chris Mason3de45862008-11-17 21:02:50 -05004160 /* is this a reference to our own snapshot? If so
4161 * skip it
4162 */
4163 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4164 location.objectid == root->root_key.objectid) {
4165 over = 0;
4166 goto skip;
4167 }
Chris Mason5f39d392007-10-15 16:14:19 -04004168 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004169 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004170 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004171
Chris Mason3de45862008-11-17 21:02:50 -05004172skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004173 if (name_ptr != tmp_name)
4174 kfree(name_ptr);
4175
Chris Mason39279cc2007-06-12 06:35:45 -04004176 if (over)
4177 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004178 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004179 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004180 di_cur += di_len;
4181 di = (struct btrfs_dir_item *)((char *)di + di_len);
4182 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004183next:
4184 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004185 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004186
Miao Xie16cdcec2011-04-22 18:12:22 +08004187 if (key_type == BTRFS_DIR_INDEX_KEY) {
4188 if (is_curr)
4189 filp->f_pos++;
4190 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4191 &ins_list);
4192 if (ret)
4193 goto nopos;
4194 }
4195
David Woodhouse49593bf2008-08-17 17:08:36 +01004196 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004197 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004198 /*
4199 * 32-bit glibc will use getdents64, but then strtol -
4200 * so the last number we can serve is this.
4201 */
4202 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004203 else
4204 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004205nopos:
4206 ret = 0;
4207err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004208 if (key_type == BTRFS_DIR_INDEX_KEY)
4209 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004210 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004211 return ret;
4212}
4213
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004214int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004215{
4216 struct btrfs_root *root = BTRFS_I(inode)->root;
4217 struct btrfs_trans_handle *trans;
4218 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004219 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004220
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004221 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004222 return 0;
4223
Chris Mason2cf85722011-07-26 15:35:09 -04004224 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(root, inode))
Li Zefan82d59022011-04-20 10:33:24 +08004225 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004226
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004227 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004228 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004229 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004230 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004231 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004232 if (IS_ERR(trans))
4233 return PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04004234 if (nolock)
4235 ret = btrfs_end_transaction_nolock(trans, root);
4236 else
4237 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004238 }
4239 return ret;
4240}
4241
4242/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004243 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004244 * inode changes. But, it is most likely to find the inode in cache.
4245 * FIXME, needs more benchmarking...there are no reasons other than performance
4246 * to keep or drop this code.
4247 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04004248void btrfs_dirty_inode(struct inode *inode, int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04004249{
4250 struct btrfs_root *root = BTRFS_I(inode)->root;
4251 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004252 int ret;
4253
4254 if (BTRFS_I(inode)->dummy_inode)
4255 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004256
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004257 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004258 BUG_ON(IS_ERR(trans));
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004259
4260 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004261 if (ret && ret == -ENOSPC) {
4262 /* whoops, lets try again with the full transaction */
4263 btrfs_end_transaction(trans, root);
4264 trans = btrfs_start_transaction(root, 1);
Chris Mason9aeead72010-05-27 10:23:00 -04004265 if (IS_ERR(trans)) {
David Sterba7a36dde2011-05-06 15:33:15 +02004266 printk_ratelimited(KERN_ERR "btrfs: fail to "
Li Zefan33345d012011-04-20 10:31:50 +08004267 "dirty inode %llu error %ld\n",
4268 (unsigned long long)btrfs_ino(inode),
4269 PTR_ERR(trans));
Chris Mason9aeead72010-05-27 10:23:00 -04004270 return;
4271 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004272
Chris Mason94b60442010-05-26 11:02:00 -04004273 ret = btrfs_update_inode(trans, root, inode);
4274 if (ret) {
David Sterba7a36dde2011-05-06 15:33:15 +02004275 printk_ratelimited(KERN_ERR "btrfs: fail to "
Li Zefan33345d012011-04-20 10:31:50 +08004276 "dirty inode %llu error %d\n",
4277 (unsigned long long)btrfs_ino(inode),
4278 ret);
Chris Mason94b60442010-05-26 11:02:00 -04004279 }
4280 }
Chris Mason39279cc2007-06-12 06:35:45 -04004281 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004282 if (BTRFS_I(inode)->delayed_node)
4283 btrfs_balance_delayed_items(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004284}
4285
Chris Masond352ac62008-09-29 15:18:18 -04004286/*
4287 * find the highest existing sequence number in a directory
4288 * and then set the in-memory index_cnt variable to reflect
4289 * free sequence numbers
4290 */
Josef Bacikaec74772008-07-24 12:12:38 -04004291static int btrfs_set_inode_index_count(struct inode *inode)
4292{
4293 struct btrfs_root *root = BTRFS_I(inode)->root;
4294 struct btrfs_key key, found_key;
4295 struct btrfs_path *path;
4296 struct extent_buffer *leaf;
4297 int ret;
4298
Li Zefan33345d012011-04-20 10:31:50 +08004299 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004300 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4301 key.offset = (u64)-1;
4302
4303 path = btrfs_alloc_path();
4304 if (!path)
4305 return -ENOMEM;
4306
4307 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4308 if (ret < 0)
4309 goto out;
4310 /* FIXME: we should be able to handle this */
4311 if (ret == 0)
4312 goto out;
4313 ret = 0;
4314
4315 /*
4316 * MAGIC NUMBER EXPLANATION:
4317 * since we search a directory based on f_pos we have to start at 2
4318 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4319 * else has to start at 2
4320 */
4321 if (path->slots[0] == 0) {
4322 BTRFS_I(inode)->index_cnt = 2;
4323 goto out;
4324 }
4325
4326 path->slots[0]--;
4327
4328 leaf = path->nodes[0];
4329 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4330
Li Zefan33345d012011-04-20 10:31:50 +08004331 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004332 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4333 BTRFS_I(inode)->index_cnt = 2;
4334 goto out;
4335 }
4336
4337 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4338out:
4339 btrfs_free_path(path);
4340 return ret;
4341}
4342
Chris Masond352ac62008-09-29 15:18:18 -04004343/*
4344 * helper to find a free sequence number in a given directory. This current
4345 * code is very simple, later versions will do smarter things in the btree
4346 */
Chris Mason3de45862008-11-17 21:02:50 -05004347int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004348{
4349 int ret = 0;
4350
4351 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004352 ret = btrfs_inode_delayed_dir_index_count(dir);
4353 if (ret) {
4354 ret = btrfs_set_inode_index_count(dir);
4355 if (ret)
4356 return ret;
4357 }
Josef Bacikaec74772008-07-24 12:12:38 -04004358 }
4359
Chris Mason00e4e6b2008-08-05 11:18:09 -04004360 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004361 BTRFS_I(dir)->index_cnt++;
4362
4363 return ret;
4364}
4365
Chris Mason39279cc2007-06-12 06:35:45 -04004366static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4367 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004368 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004369 const char *name, int name_len,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004370 u64 ref_objectid, u64 objectid, int mode,
4371 u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004372{
4373 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004374 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004375 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004376 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004377 struct btrfs_inode_ref *ref;
4378 struct btrfs_key key[2];
4379 u32 sizes[2];
4380 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004381 int ret;
4382 int owner;
4383
Chris Mason5f39d392007-10-15 16:14:19 -04004384 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004385 if (!path)
4386 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004387
Chris Mason39279cc2007-06-12 06:35:45 -04004388 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004389 if (!inode) {
4390 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004391 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004392 }
Chris Mason39279cc2007-06-12 06:35:45 -04004393
Li Zefan581bb052011-04-20 10:06:11 +08004394 /*
4395 * we have to initialize this early, so we can reclaim the inode
4396 * number if we fail afterwards in this function.
4397 */
4398 inode->i_ino = objectid;
4399
Josef Bacikaec74772008-07-24 12:12:38 -04004400 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004401 trace_btrfs_inode_request(dir);
4402
Chris Mason3de45862008-11-17 21:02:50 -05004403 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004404 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004405 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004406 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004407 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004408 }
Josef Bacikaec74772008-07-24 12:12:38 -04004409 }
4410 /*
4411 * index_cnt is ignored for everything but a dir,
4412 * btrfs_get_inode_index_count has an explanation for the magic
4413 * number
4414 */
4415 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004416 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004417 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004418 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004419 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004420
Al Viro569254b2011-07-24 17:08:40 -04004421 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004422 owner = 0;
4423 else
4424 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004425
4426 key[0].objectid = objectid;
4427 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4428 key[0].offset = 0;
4429
4430 key[1].objectid = objectid;
4431 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4432 key[1].offset = ref_objectid;
4433
4434 sizes[0] = sizeof(struct btrfs_inode_item);
4435 sizes[1] = name_len + sizeof(*ref);
4436
Chris Masonb9473432009-03-13 11:00:37 -04004437 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004438 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4439 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004440 goto fail;
4441
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004442 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004443 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004444 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004445 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4446 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004447 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004448
4449 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4450 struct btrfs_inode_ref);
4451 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004452 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004453 ptr = (unsigned long)(ref + 1);
4454 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4455
Chris Mason5f39d392007-10-15 16:14:19 -04004456 btrfs_mark_buffer_dirty(path->nodes[0]);
4457 btrfs_free_path(path);
4458
Chris Mason39279cc2007-06-12 06:35:45 -04004459 location = &BTRFS_I(inode)->location;
4460 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004461 location->offset = 0;
4462 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4463
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004464 btrfs_inherit_iflags(inode, dir);
4465
Al Viro569254b2011-07-24 17:08:40 -04004466 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004467 if (btrfs_test_opt(root, NODATASUM))
4468 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004469 if (btrfs_test_opt(root, NODATACOW) ||
4470 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004471 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4472 }
4473
Chris Mason39279cc2007-06-12 06:35:45 -04004474 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004475 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004476
4477 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004478 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004479
Chris Mason39279cc2007-06-12 06:35:45 -04004480 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004481fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004482 if (dir)
4483 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004484 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004485 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004486 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004487}
4488
4489static inline u8 btrfs_inode_type(struct inode *inode)
4490{
4491 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4492}
4493
Chris Masond352ac62008-09-29 15:18:18 -04004494/*
4495 * utility function to add 'inode' into 'parent_inode' with
4496 * a give name and a given sequence number.
4497 * if 'add_backref' is true, also insert a backref from the
4498 * inode to the parent directory.
4499 */
Chris Masone02119d2008-09-05 16:13:11 -04004500int btrfs_add_link(struct btrfs_trans_handle *trans,
4501 struct inode *parent_inode, struct inode *inode,
4502 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004503{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004504 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004505 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004506 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004507 u64 ino = btrfs_ino(inode);
4508 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004509
Li Zefan33345d012011-04-20 10:31:50 +08004510 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004511 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4512 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004513 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004514 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4515 key.offset = 0;
4516 }
Chris Mason39279cc2007-06-12 06:35:45 -04004517
Li Zefan33345d012011-04-20 10:31:50 +08004518 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004519 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4520 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004521 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004522 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004523 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4524 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004525 }
4526
Chris Mason39279cc2007-06-12 06:35:45 -04004527 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004528 ret = btrfs_insert_dir_item(trans, root, name, name_len,
Miao Xie16cdcec2011-04-22 18:12:22 +08004529 parent_inode, &key,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004530 btrfs_inode_type(inode), index);
4531 BUG_ON(ret);
4532
Chris Masondbe674a2008-07-17 12:54:05 -04004533 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004534 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004535 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004536 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004537 }
4538 return ret;
4539}
4540
4541static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004542 struct inode *dir, struct dentry *dentry,
4543 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004544{
Josef Bacika1b075d2010-11-19 20:36:11 +00004545 int err = btrfs_add_link(trans, dir, inode,
4546 dentry->d_name.name, dentry->d_name.len,
4547 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004548 if (!err) {
4549 d_instantiate(dentry, inode);
4550 return 0;
4551 }
4552 if (err > 0)
4553 err = -EEXIST;
4554 return err;
4555}
4556
Josef Bacik618e21d2007-07-11 10:18:17 -04004557static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4558 int mode, dev_t rdev)
4559{
4560 struct btrfs_trans_handle *trans;
4561 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004562 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004563 int err;
4564 int drop_inode = 0;
4565 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004566 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004567 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004568
4569 if (!new_valid_dev(rdev))
4570 return -EINVAL;
4571
Josef Bacik9ed74f22009-09-11 16:12:44 -04004572 /*
4573 * 2 for inode item and ref
4574 * 2 for dir items
4575 * 1 for xattr if selinux is on
4576 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004577 trans = btrfs_start_transaction(root, 5);
4578 if (IS_ERR(trans))
4579 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004580
Li Zefan581bb052011-04-20 10:06:11 +08004581 err = btrfs_find_free_ino(root, &objectid);
4582 if (err)
4583 goto out_unlock;
4584
Josef Bacikaec74772008-07-24 12:12:38 -04004585 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004586 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004587 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004588 if (IS_ERR(inode)) {
4589 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004590 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004591 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004592
Eric Paris2a7dba32011-02-01 11:05:39 -05004593 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004594 if (err) {
4595 drop_inode = 1;
4596 goto out_unlock;
4597 }
4598
Casey Schauflerad19db72011-12-15 10:09:07 -05004599 /*
4600 * If the active LSM wants to access the inode during
4601 * d_instantiate it needs these. Smack checks to see
4602 * if the filesystem supports xattrs by looking at the
4603 * ops vector.
4604 */
4605
4606 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004607 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004608 if (err)
4609 drop_inode = 1;
4610 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004611 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004612 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004613 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004614out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004615 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004616 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004617 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004618 if (drop_inode) {
4619 inode_dec_link_count(inode);
4620 iput(inode);
4621 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004622 return err;
4623}
4624
Chris Mason39279cc2007-06-12 06:35:45 -04004625static int btrfs_create(struct inode *dir, struct dentry *dentry,
4626 int mode, struct nameidata *nd)
4627{
4628 struct btrfs_trans_handle *trans;
4629 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004630 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004631 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004632 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004633 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004634 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004635 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004636
Josef Bacik9ed74f22009-09-11 16:12:44 -04004637 /*
4638 * 2 for inode item and ref
4639 * 2 for dir items
4640 * 1 for xattr if selinux is on
4641 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004642 trans = btrfs_start_transaction(root, 5);
4643 if (IS_ERR(trans))
4644 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004645
Li Zefan581bb052011-04-20 10:06:11 +08004646 err = btrfs_find_free_ino(root, &objectid);
4647 if (err)
4648 goto out_unlock;
4649
Josef Bacikaec74772008-07-24 12:12:38 -04004650 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004651 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004652 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004653 if (IS_ERR(inode)) {
4654 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004655 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004656 }
Chris Mason39279cc2007-06-12 06:35:45 -04004657
Eric Paris2a7dba32011-02-01 11:05:39 -05004658 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004659 if (err) {
4660 drop_inode = 1;
4661 goto out_unlock;
4662 }
4663
Casey Schauflerad19db72011-12-15 10:09:07 -05004664 /*
4665 * If the active LSM wants to access the inode during
4666 * d_instantiate it needs these. Smack checks to see
4667 * if the filesystem supports xattrs by looking at the
4668 * ops vector.
4669 */
4670 inode->i_fop = &btrfs_file_operations;
4671 inode->i_op = &btrfs_file_inode_operations;
4672
Josef Bacika1b075d2010-11-19 20:36:11 +00004673 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004674 if (err)
4675 drop_inode = 1;
4676 else {
4677 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004678 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05004679 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004680 }
Chris Mason39279cc2007-06-12 06:35:45 -04004681out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004682 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004683 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004684 if (drop_inode) {
4685 inode_dec_link_count(inode);
4686 iput(inode);
4687 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004688 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004689 return err;
4690}
4691
4692static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4693 struct dentry *dentry)
4694{
4695 struct btrfs_trans_handle *trans;
4696 struct btrfs_root *root = BTRFS_I(dir)->root;
4697 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004698 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004699 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004700 int err;
4701 int drop_inode = 0;
4702
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004703 /* do not allow sys_link's with other subvols of the same device */
4704 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00004705 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004706
Al Viroc055e992011-03-04 17:15:18 +00004707 if (inode->i_nlink == ~0U)
4708 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004709
Chris Mason3de45862008-11-17 21:02:50 -05004710 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004711 if (err)
4712 goto fail;
4713
Yan, Zhenga22285a2010-05-16 10:48:46 -04004714 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00004715 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04004716 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00004717 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04004718 */
Miao Xie7e6b6462011-02-18 09:21:17 +00004719 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004720 if (IS_ERR(trans)) {
4721 err = PTR_ERR(trans);
4722 goto fail;
4723 }
Chris Mason5f39d392007-10-15 16:14:19 -04004724
Miao Xie31534952011-04-13 13:19:21 +08004725 btrfs_inc_nlink(inode);
4726 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04004727 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004728
Josef Bacika1b075d2010-11-19 20:36:11 +00004729 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004730
Yan, Zhenga5719522009-09-24 09:17:31 -04004731 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004732 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004733 } else {
Al Viro10d9f302011-07-16 23:09:10 -04004734 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04004735 err = btrfs_update_inode(trans, root, inode);
4736 BUG_ON(err);
Josef Bacik6a912212010-11-20 09:48:00 +00004737 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004738 }
Chris Mason39279cc2007-06-12 06:35:45 -04004739
Chris Masond3c2fdc2007-09-17 10:58:06 -04004740 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004741 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004742fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004743 if (drop_inode) {
4744 inode_dec_link_count(inode);
4745 iput(inode);
4746 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004747 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004748 return err;
4749}
4750
Chris Mason39279cc2007-06-12 06:35:45 -04004751static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4752{
Chris Masonb9d86662008-05-02 16:13:49 -04004753 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004754 struct btrfs_trans_handle *trans;
4755 struct btrfs_root *root = BTRFS_I(dir)->root;
4756 int err = 0;
4757 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004758 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004759 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04004760 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004761
Josef Bacik9ed74f22009-09-11 16:12:44 -04004762 /*
4763 * 2 items for inode and ref
4764 * 2 items for dir items
4765 * 1 for xattr if selinux is on
4766 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004767 trans = btrfs_start_transaction(root, 5);
4768 if (IS_ERR(trans))
4769 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04004770
Li Zefan581bb052011-04-20 10:06:11 +08004771 err = btrfs_find_free_ino(root, &objectid);
4772 if (err)
4773 goto out_fail;
4774
Josef Bacikaec74772008-07-24 12:12:38 -04004775 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004776 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004777 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004778 if (IS_ERR(inode)) {
4779 err = PTR_ERR(inode);
4780 goto out_fail;
4781 }
Chris Mason5f39d392007-10-15 16:14:19 -04004782
Chris Mason39279cc2007-06-12 06:35:45 -04004783 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004784
Eric Paris2a7dba32011-02-01 11:05:39 -05004785 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004786 if (err)
4787 goto out_fail;
4788
Chris Mason39279cc2007-06-12 06:35:45 -04004789 inode->i_op = &btrfs_dir_inode_operations;
4790 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04004791
Chris Masondbe674a2008-07-17 12:54:05 -04004792 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004793 err = btrfs_update_inode(trans, root, inode);
4794 if (err)
4795 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004796
Josef Bacika1b075d2010-11-19 20:36:11 +00004797 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4798 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004799 if (err)
4800 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004801
Chris Mason39279cc2007-06-12 06:35:45 -04004802 d_instantiate(dentry, inode);
4803 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004804
4805out_fail:
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 Mason39279cc2007-06-12 06:35:45 -04004808 if (drop_on_err)
4809 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04004810 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004811 return err;
4812}
4813
Chris Masond352ac62008-09-29 15:18:18 -04004814/* helper for btfs_get_extent. Given an existing extent in the tree,
4815 * and an extent that you want to insert, deal with overlap and insert
4816 * the new extent into the tree.
4817 */
Chris Mason3b951512008-04-17 11:29:12 -04004818static int merge_extent_mapping(struct extent_map_tree *em_tree,
4819 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004820 struct extent_map *em,
4821 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004822{
4823 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004824
Chris Masone6dcd2d2008-07-17 12:53:50 -04004825 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4826 start_diff = map_start - em->start;
4827 em->start = map_start;
4828 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004829 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4830 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004831 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004832 em->block_len -= start_diff;
4833 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004834 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004835}
4836
Chris Masonc8b97812008-10-29 14:49:59 -04004837static noinline int uncompress_inline(struct btrfs_path *path,
4838 struct inode *inode, struct page *page,
4839 size_t pg_offset, u64 extent_offset,
4840 struct btrfs_file_extent_item *item)
4841{
4842 int ret;
4843 struct extent_buffer *leaf = path->nodes[0];
4844 char *tmp;
4845 size_t max_size;
4846 unsigned long inline_size;
4847 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08004848 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04004849
4850 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08004851 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04004852 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4853 inline_size = btrfs_file_extent_inline_item_len(leaf,
4854 btrfs_item_nr(leaf, path->slots[0]));
4855 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04004856 if (!tmp)
4857 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04004858 ptr = btrfs_file_extent_inline_start(item);
4859
4860 read_extent_buffer(leaf, tmp, ptr, inline_size);
4861
Chris Mason5b050f02008-11-11 09:34:41 -05004862 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08004863 ret = btrfs_decompress(compress_type, tmp, page,
4864 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004865 if (ret) {
4866 char *kaddr = kmap_atomic(page, KM_USER0);
4867 unsigned long copy_size = min_t(u64,
4868 PAGE_CACHE_SIZE - pg_offset,
4869 max_size - extent_offset);
4870 memset(kaddr + pg_offset, 0, copy_size);
4871 kunmap_atomic(kaddr, KM_USER0);
4872 }
4873 kfree(tmp);
4874 return 0;
4875}
4876
Chris Masond352ac62008-09-29 15:18:18 -04004877/*
4878 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004879 * the ugly parts come from merging extents from the disk with the in-ram
4880 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004881 * where the in-ram extents might be locked pending data=ordered completion.
4882 *
4883 * This also copies inline extents directly into the page.
4884 */
Chris Masond3977122009-01-05 21:25:51 -05004885
Chris Masona52d9a82007-08-27 16:49:44 -04004886struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004887 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004888 int create)
4889{
4890 int ret;
4891 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004892 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004893 u64 extent_start = 0;
4894 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004895 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04004896 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004897 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004898 struct btrfs_root *root = BTRFS_I(inode)->root;
4899 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004900 struct extent_buffer *leaf;
4901 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004902 struct extent_map *em = NULL;
4903 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004904 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004905 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08004906 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04004907
Chris Masona52d9a82007-08-27 16:49:44 -04004908again:
Chris Mason890871b2009-09-02 16:24:52 -04004909 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004910 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004911 if (em)
4912 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004913 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004914
Chris Masona52d9a82007-08-27 16:49:44 -04004915 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004916 if (em->start > start || em->start + em->len <= start)
4917 free_extent_map(em);
4918 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004919 free_extent_map(em);
4920 else
4921 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004922 }
David Sterba172ddd62011-04-21 00:48:27 +02004923 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04004924 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004925 err = -ENOMEM;
4926 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004927 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004928 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004929 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004930 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004931 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004932 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004933
4934 if (!path) {
4935 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04004936 if (!path) {
4937 err = -ENOMEM;
4938 goto out;
4939 }
4940 /*
4941 * Chances are we'll be called again, so go ahead and do
4942 * readahead
4943 */
4944 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04004945 }
4946
Chris Mason179e29e2007-11-01 11:28:41 -04004947 ret = btrfs_lookup_file_extent(trans, root, path,
4948 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004949 if (ret < 0) {
4950 err = ret;
4951 goto out;
4952 }
4953
4954 if (ret != 0) {
4955 if (path->slots[0] == 0)
4956 goto not_found;
4957 path->slots[0]--;
4958 }
4959
Chris Mason5f39d392007-10-15 16:14:19 -04004960 leaf = path->nodes[0];
4961 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004962 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004963 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004964 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4965 found_type = btrfs_key_type(&found_key);
4966 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004967 found_type != BTRFS_EXTENT_DATA_KEY) {
4968 goto not_found;
4969 }
4970
Chris Mason5f39d392007-10-15 16:14:19 -04004971 found_type = btrfs_file_extent_type(leaf, item);
4972 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08004973 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004974 if (found_type == BTRFS_FILE_EXTENT_REG ||
4975 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004976 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004977 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004978 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4979 size_t size;
4980 size = btrfs_file_extent_inline_len(leaf, item);
4981 extent_end = (extent_start + size + root->sectorsize - 1) &
4982 ~((u64)root->sectorsize - 1);
4983 }
4984
4985 if (start >= extent_end) {
4986 path->slots[0]++;
4987 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4988 ret = btrfs_next_leaf(root, path);
4989 if (ret < 0) {
4990 err = ret;
4991 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004992 }
Yan Zheng9036c102008-10-30 14:19:41 -04004993 if (ret > 0)
4994 goto not_found;
4995 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004996 }
Yan Zheng9036c102008-10-30 14:19:41 -04004997 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4998 if (found_key.objectid != objectid ||
4999 found_key.type != BTRFS_EXTENT_DATA_KEY)
5000 goto not_found;
5001 if (start + len <= found_key.offset)
5002 goto not_found;
5003 em->start = start;
5004 em->len = found_key.offset - start;
5005 goto not_found_em;
5006 }
5007
Yan Zhengd899e052008-10-30 14:25:28 -04005008 if (found_type == BTRFS_FILE_EXTENT_REG ||
5009 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005010 em->start = extent_start;
5011 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005012 em->orig_start = extent_start -
5013 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005014 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5015 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005016 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005017 goto insert;
5018 }
Li Zefan261507a02010-12-17 14:21:50 +08005019 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005020 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005021 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005022 em->block_start = bytenr;
5023 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5024 item);
5025 } else {
5026 bytenr += btrfs_file_extent_offset(leaf, item);
5027 em->block_start = bytenr;
5028 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005029 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5030 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005031 }
Chris Masona52d9a82007-08-27 16:49:44 -04005032 goto insert;
5033 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005034 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005035 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005036 size_t size;
5037 size_t extent_offset;
5038 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005039
Chris Masona52d9a82007-08-27 16:49:44 -04005040 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005041 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005042 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005043 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005044 goto out;
5045 }
5046
Yan Zheng9036c102008-10-30 14:19:41 -04005047 size = btrfs_file_extent_inline_len(leaf, item);
5048 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005049 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005050 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005051 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005052 em->len = (copy_size + root->sectorsize - 1) &
5053 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005054 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005055 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005056 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005057 em->compress_type = compress_type;
5058 }
Yan689f9342007-10-29 11:41:07 -04005059 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005060 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005061 if (btrfs_file_extent_compression(leaf, item) !=
5062 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005063 ret = uncompress_inline(path, inode, page,
5064 pg_offset,
5065 extent_offset, item);
5066 BUG_ON(ret);
5067 } else {
5068 map = kmap(page);
5069 read_extent_buffer(leaf, map + pg_offset, ptr,
5070 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005071 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5072 memset(map + pg_offset + copy_size, 0,
5073 PAGE_CACHE_SIZE - pg_offset -
5074 copy_size);
5075 }
Chris Masonc8b97812008-10-29 14:49:59 -04005076 kunmap(page);
5077 }
Chris Mason179e29e2007-11-01 11:28:41 -04005078 flush_dcache_page(page);
5079 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005080 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005081 if (!trans) {
5082 kunmap(page);
5083 free_extent_map(em);
5084 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005085
David Sterbab3b4aa72011-04-21 01:20:15 +02005086 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005087 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005088
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005089 if (IS_ERR(trans))
5090 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005091 goto again;
5092 }
Chris Masonc8b97812008-10-29 14:49:59 -04005093 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005094 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005095 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005096 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005097 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005098 }
Chris Masond1310b22008-01-24 16:13:08 -05005099 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005100 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005101 goto insert;
5102 } else {
Chris Masond3977122009-01-05 21:25:51 -05005103 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005104 WARN_ON(1);
5105 }
5106not_found:
5107 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005108 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005109not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005110 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005111 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005112insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005113 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005114 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005115 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5116 "[%llu %llu]\n", (unsigned long long)em->start,
5117 (unsigned long long)em->len,
5118 (unsigned long long)start,
5119 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005120 err = -EIO;
5121 goto out;
5122 }
Chris Masond1310b22008-01-24 16:13:08 -05005123
5124 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005125 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005126 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005127 /* it is possible that someone inserted the extent into the tree
5128 * while we had the lock dropped. It is also possible that
5129 * an overlapping map exists in the tree
5130 */
Chris Masona52d9a82007-08-27 16:49:44 -04005131 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005132 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005133
5134 ret = 0;
5135
Chris Mason3b951512008-04-17 11:29:12 -04005136 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005137 if (existing && (existing->start > start ||
5138 existing->start + existing->len <= start)) {
5139 free_extent_map(existing);
5140 existing = NULL;
5141 }
Chris Mason3b951512008-04-17 11:29:12 -04005142 if (!existing) {
5143 existing = lookup_extent_mapping(em_tree, em->start,
5144 em->len);
5145 if (existing) {
5146 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005147 em, start,
5148 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005149 free_extent_map(existing);
5150 if (err) {
5151 free_extent_map(em);
5152 em = NULL;
5153 }
5154 } else {
5155 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005156 free_extent_map(em);
5157 em = NULL;
5158 }
5159 } else {
5160 free_extent_map(em);
5161 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005162 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005163 }
Chris Masona52d9a82007-08-27 16:49:44 -04005164 }
Chris Mason890871b2009-09-02 16:24:52 -04005165 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005166out:
liubo1abe9b82011-03-24 11:18:59 +00005167
5168 trace_btrfs_get_extent(root, em);
5169
Chris Masonf4219502008-07-22 11:18:09 -04005170 if (path)
5171 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005172 if (trans) {
5173 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005174 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005175 err = ret;
5176 }
Chris Masona52d9a82007-08-27 16:49:44 -04005177 if (err) {
5178 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005179 return ERR_PTR(err);
5180 }
5181 return em;
5182}
5183
Chris Masonec29ed52011-02-23 16:23:20 -05005184struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5185 size_t pg_offset, u64 start, u64 len,
5186 int create)
5187{
5188 struct extent_map *em;
5189 struct extent_map *hole_em = NULL;
5190 u64 range_start = start;
5191 u64 end;
5192 u64 found;
5193 u64 found_end;
5194 int err = 0;
5195
5196 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5197 if (IS_ERR(em))
5198 return em;
5199 if (em) {
5200 /*
5201 * if our em maps to a hole, there might
5202 * actually be delalloc bytes behind it
5203 */
5204 if (em->block_start != EXTENT_MAP_HOLE)
5205 return em;
5206 else
5207 hole_em = em;
5208 }
5209
5210 /* check to see if we've wrapped (len == -1 or similar) */
5211 end = start + len;
5212 if (end < start)
5213 end = (u64)-1;
5214 else
5215 end -= 1;
5216
5217 em = NULL;
5218
5219 /* ok, we didn't find anything, lets look for delalloc */
5220 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5221 end, len, EXTENT_DELALLOC, 1);
5222 found_end = range_start + found;
5223 if (found_end < range_start)
5224 found_end = (u64)-1;
5225
5226 /*
5227 * we didn't find anything useful, return
5228 * the original results from get_extent()
5229 */
5230 if (range_start > end || found_end <= start) {
5231 em = hole_em;
5232 hole_em = NULL;
5233 goto out;
5234 }
5235
5236 /* adjust the range_start to make sure it doesn't
5237 * go backwards from the start they passed in
5238 */
5239 range_start = max(start,range_start);
5240 found = found_end - range_start;
5241
5242 if (found > 0) {
5243 u64 hole_start = start;
5244 u64 hole_len = len;
5245
David Sterba172ddd62011-04-21 00:48:27 +02005246 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005247 if (!em) {
5248 err = -ENOMEM;
5249 goto out;
5250 }
5251 /*
5252 * when btrfs_get_extent can't find anything it
5253 * returns one huge hole
5254 *
5255 * make sure what it found really fits our range, and
5256 * adjust to make sure it is based on the start from
5257 * the caller
5258 */
5259 if (hole_em) {
5260 u64 calc_end = extent_map_end(hole_em);
5261
5262 if (calc_end <= start || (hole_em->start > end)) {
5263 free_extent_map(hole_em);
5264 hole_em = NULL;
5265 } else {
5266 hole_start = max(hole_em->start, start);
5267 hole_len = calc_end - hole_start;
5268 }
5269 }
5270 em->bdev = NULL;
5271 if (hole_em && range_start > hole_start) {
5272 /* our hole starts before our delalloc, so we
5273 * have to return just the parts of the hole
5274 * that go until the delalloc starts
5275 */
5276 em->len = min(hole_len,
5277 range_start - hole_start);
5278 em->start = hole_start;
5279 em->orig_start = hole_start;
5280 /*
5281 * don't adjust block start at all,
5282 * it is fixed at EXTENT_MAP_HOLE
5283 */
5284 em->block_start = hole_em->block_start;
5285 em->block_len = hole_len;
5286 } else {
5287 em->start = range_start;
5288 em->len = found;
5289 em->orig_start = range_start;
5290 em->block_start = EXTENT_MAP_DELALLOC;
5291 em->block_len = found;
5292 }
5293 } else if (hole_em) {
5294 return hole_em;
5295 }
5296out:
5297
5298 free_extent_map(hole_em);
5299 if (err) {
5300 free_extent_map(em);
5301 return ERR_PTR(err);
5302 }
5303 return em;
5304}
5305
Josef Bacik4b46fce2010-05-23 11:00:55 -04005306static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005307 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005308 u64 start, u64 len)
5309{
5310 struct btrfs_root *root = BTRFS_I(inode)->root;
5311 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005312 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5313 struct btrfs_key ins;
5314 u64 alloc_hint;
5315 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005316 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005317
Josef Bacik16d299a2011-04-06 14:53:07 -04005318 /*
5319 * Ok if the extent map we looked up is a hole and is for the exact
5320 * range we want, there is no reason to allocate a new one, however if
5321 * it is not right then we need to free this one and drop the cache for
5322 * our range.
5323 */
5324 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5325 em->len != len) {
5326 free_extent_map(em);
5327 em = NULL;
5328 insert = true;
5329 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5330 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005331
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005332 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005333 if (IS_ERR(trans))
5334 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005335
Chris Mason4cb53002011-05-24 15:35:30 -04005336 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5337 btrfs_add_inode_defrag(trans, inode);
5338
Josef Bacik4b46fce2010-05-23 11:00:55 -04005339 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5340
5341 alloc_hint = get_extent_allocation_hint(inode, start, len);
5342 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5343 alloc_hint, (u64)-1, &ins, 1);
5344 if (ret) {
5345 em = ERR_PTR(ret);
5346 goto out;
5347 }
5348
Josef Bacik4b46fce2010-05-23 11:00:55 -04005349 if (!em) {
David Sterba172ddd62011-04-21 00:48:27 +02005350 em = alloc_extent_map();
Josef Bacik16d299a2011-04-06 14:53:07 -04005351 if (!em) {
5352 em = ERR_PTR(-ENOMEM);
5353 goto out;
5354 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005355 }
5356
5357 em->start = start;
5358 em->orig_start = em->start;
5359 em->len = ins.offset;
5360
5361 em->block_start = ins.objectid;
5362 em->block_len = ins.offset;
5363 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005364
5365 /*
5366 * We need to do this because if we're using the original em we searched
5367 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5368 */
5369 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005370 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5371
Josef Bacik16d299a2011-04-06 14:53:07 -04005372 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005373 write_lock(&em_tree->lock);
5374 ret = add_extent_mapping(em_tree, em);
5375 write_unlock(&em_tree->lock);
5376 if (ret != -EEXIST)
5377 break;
5378 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5379 }
5380
5381 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5382 ins.offset, ins.offset, 0);
5383 if (ret) {
5384 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5385 em = ERR_PTR(ret);
5386 }
5387out:
5388 btrfs_end_transaction(trans, root);
5389 return em;
5390}
5391
Chris Mason46bfbb52010-05-26 11:04:10 -04005392/*
5393 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5394 * block must be cow'd
5395 */
5396static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5397 struct inode *inode, u64 offset, u64 len)
5398{
5399 struct btrfs_path *path;
5400 int ret;
5401 struct extent_buffer *leaf;
5402 struct btrfs_root *root = BTRFS_I(inode)->root;
5403 struct btrfs_file_extent_item *fi;
5404 struct btrfs_key key;
5405 u64 disk_bytenr;
5406 u64 backref_offset;
5407 u64 extent_end;
5408 u64 num_bytes;
5409 int slot;
5410 int found_type;
5411
5412 path = btrfs_alloc_path();
5413 if (!path)
5414 return -ENOMEM;
5415
Li Zefan33345d012011-04-20 10:31:50 +08005416 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005417 offset, 0);
5418 if (ret < 0)
5419 goto out;
5420
5421 slot = path->slots[0];
5422 if (ret == 1) {
5423 if (slot == 0) {
5424 /* can't find the item, must cow */
5425 ret = 0;
5426 goto out;
5427 }
5428 slot--;
5429 }
5430 ret = 0;
5431 leaf = path->nodes[0];
5432 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005433 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005434 key.type != BTRFS_EXTENT_DATA_KEY) {
5435 /* not our file or wrong item type, must cow */
5436 goto out;
5437 }
5438
5439 if (key.offset > offset) {
5440 /* Wrong offset, must cow */
5441 goto out;
5442 }
5443
5444 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5445 found_type = btrfs_file_extent_type(leaf, fi);
5446 if (found_type != BTRFS_FILE_EXTENT_REG &&
5447 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5448 /* not a regular extent, must cow */
5449 goto out;
5450 }
5451 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5452 backref_offset = btrfs_file_extent_offset(leaf, fi);
5453
5454 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5455 if (extent_end < offset + len) {
5456 /* extent doesn't include our full range, must cow */
5457 goto out;
5458 }
5459
5460 if (btrfs_extent_readonly(root, disk_bytenr))
5461 goto out;
5462
5463 /*
5464 * look for other files referencing this extent, if we
5465 * find any we must cow
5466 */
Li Zefan33345d012011-04-20 10:31:50 +08005467 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005468 key.offset - backref_offset, disk_bytenr))
5469 goto out;
5470
5471 /*
5472 * adjust disk_bytenr and num_bytes to cover just the bytes
5473 * in this extent we are about to write. If there
5474 * are any csums in that range we have to cow in order
5475 * to keep the csums correct
5476 */
5477 disk_bytenr += backref_offset;
5478 disk_bytenr += offset - key.offset;
5479 num_bytes = min(offset + len, extent_end) - offset;
5480 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5481 goto out;
5482 /*
5483 * all of the above have passed, it is safe to overwrite this extent
5484 * without cow
5485 */
5486 ret = 1;
5487out:
5488 btrfs_free_path(path);
5489 return ret;
5490}
5491
Josef Bacik4b46fce2010-05-23 11:00:55 -04005492static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5493 struct buffer_head *bh_result, int create)
5494{
5495 struct extent_map *em;
5496 struct btrfs_root *root = BTRFS_I(inode)->root;
5497 u64 start = iblock << inode->i_blkbits;
5498 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005499 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005500
5501 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5502 if (IS_ERR(em))
5503 return PTR_ERR(em);
5504
5505 /*
5506 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5507 * io. INLINE is special, and we could probably kludge it in here, but
5508 * it's still buffered so for safety lets just fall back to the generic
5509 * buffered path.
5510 *
5511 * For COMPRESSED we _have_ to read the entire extent in so we can
5512 * decompress it, so there will be buffering required no matter what we
5513 * do, so go ahead and fallback to buffered.
5514 *
5515 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5516 * to buffered IO. Don't blame me, this is the price we pay for using
5517 * the generic code.
5518 */
5519 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5520 em->block_start == EXTENT_MAP_INLINE) {
5521 free_extent_map(em);
5522 return -ENOTBLK;
5523 }
5524
5525 /* Just a good old fashioned hole, return */
5526 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5527 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5528 free_extent_map(em);
5529 /* DIO will do one hole at a time, so just unlock a sector */
5530 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5531 start + root->sectorsize - 1, GFP_NOFS);
5532 return 0;
5533 }
5534
5535 /*
5536 * We don't allocate a new extent in the following cases
5537 *
5538 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5539 * existing extent.
5540 * 2) The extent is marked as PREALLOC. We're good to go here and can
5541 * just use the extent.
5542 *
5543 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005544 if (!create) {
5545 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005546 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005547 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005548
5549 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5550 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5551 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005552 int type;
5553 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005554 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005555
5556 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5557 type = BTRFS_ORDERED_PREALLOC;
5558 else
5559 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005560 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005561 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005562
5563 /*
5564 * we're not going to log anything, but we do need
5565 * to make sure the current transaction stays open
5566 * while we look for nocow cross refs
5567 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005568 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005569 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005570 goto must_cow;
5571
5572 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5573 ret = btrfs_add_ordered_extent_dio(inode, start,
5574 block_start, len, len, type);
5575 btrfs_end_transaction(trans, root);
5576 if (ret) {
5577 free_extent_map(em);
5578 return ret;
5579 }
5580 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005581 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005582 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005583 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005584must_cow:
5585 /*
5586 * this will cow the extent, reset the len in case we changed
5587 * it above
5588 */
5589 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04005590 em = btrfs_new_extent_direct(inode, em, start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04005591 if (IS_ERR(em))
5592 return PTR_ERR(em);
5593 len = min(len, em->len - (start - em->start));
5594unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005595 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5596 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5597 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005598map:
5599 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5600 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005601 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005602 bh_result->b_bdev = em->bdev;
5603 set_buffer_mapped(bh_result);
5604 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5605 set_buffer_new(bh_result);
5606
5607 free_extent_map(em);
5608
5609 return 0;
5610}
5611
5612struct btrfs_dio_private {
5613 struct inode *inode;
5614 u64 logical_offset;
5615 u64 disk_bytenr;
5616 u64 bytes;
5617 u32 *csums;
5618 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005619
5620 /* number of bios pending for this dio */
5621 atomic_t pending_bios;
5622
5623 /* IO errors */
5624 int errors;
5625
5626 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005627};
5628
5629static void btrfs_endio_direct_read(struct bio *bio, int err)
5630{
Miao Xiee65e1532010-11-22 03:04:43 +00005631 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005632 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5633 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005634 struct inode *inode = dip->inode;
5635 struct btrfs_root *root = BTRFS_I(inode)->root;
5636 u64 start;
5637 u32 *private = dip->csums;
5638
5639 start = dip->logical_offset;
5640 do {
5641 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5642 struct page *page = bvec->bv_page;
5643 char *kaddr;
5644 u32 csum = ~(u32)0;
5645 unsigned long flags;
5646
5647 local_irq_save(flags);
5648 kaddr = kmap_atomic(page, KM_IRQ0);
5649 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5650 csum, bvec->bv_len);
5651 btrfs_csum_final(csum, (char *)&csum);
5652 kunmap_atomic(kaddr, KM_IRQ0);
5653 local_irq_restore(flags);
5654
5655 flush_dcache_page(bvec->bv_page);
5656 if (csum != *private) {
Li Zefan33345d012011-04-20 10:31:50 +08005657 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04005658 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08005659 (unsigned long long)btrfs_ino(inode),
5660 (unsigned long long)start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005661 csum, *private);
5662 err = -EIO;
5663 }
5664 }
5665
5666 start += bvec->bv_len;
5667 private++;
5668 bvec++;
5669 } while (bvec <= bvec_end);
5670
5671 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5672 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5673 bio->bi_private = dip->private;
5674
5675 kfree(dip->csums);
5676 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005677
5678 /* If we had a csum failure make sure to clear the uptodate flag */
5679 if (err)
5680 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005681 dio_end_io(bio, err);
5682}
5683
5684static void btrfs_endio_direct_write(struct bio *bio, int err)
5685{
5686 struct btrfs_dio_private *dip = bio->bi_private;
5687 struct inode *inode = dip->inode;
5688 struct btrfs_root *root = BTRFS_I(inode)->root;
5689 struct btrfs_trans_handle *trans;
5690 struct btrfs_ordered_extent *ordered = NULL;
5691 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005692 u64 ordered_offset = dip->logical_offset;
5693 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005694 int ret;
5695
5696 if (err)
5697 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005698again:
5699 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5700 &ordered_offset,
5701 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005702 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005703 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005704
5705 BUG_ON(!ordered);
5706
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005707 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005708 if (IS_ERR(trans)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005709 err = -ENOMEM;
5710 goto out;
5711 }
5712 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5713
5714 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5715 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5716 if (!ret)
Chris Mason21151332011-11-10 20:39:08 -05005717 err = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005718 goto out;
5719 }
5720
5721 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5722 ordered->file_offset + ordered->len - 1, 0,
5723 &cached_state, GFP_NOFS);
5724
5725 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5726 ret = btrfs_mark_extent_written(trans, inode,
5727 ordered->file_offset,
5728 ordered->file_offset +
5729 ordered->len);
5730 if (ret) {
5731 err = ret;
5732 goto out_unlock;
5733 }
5734 } else {
5735 ret = insert_reserved_file_extent(trans, inode,
5736 ordered->file_offset,
5737 ordered->start,
5738 ordered->disk_len,
5739 ordered->len,
5740 ordered->len,
5741 0, 0, 0,
5742 BTRFS_FILE_EXTENT_REG);
5743 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5744 ordered->file_offset, ordered->len);
5745 if (ret) {
5746 err = ret;
5747 WARN_ON(1);
5748 goto out_unlock;
5749 }
5750 }
5751
5752 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005753 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
Miao Xiea39f7522011-09-11 10:52:25 -04005754 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags))
Chris Mason21151332011-11-10 20:39:08 -05005755 btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005756 ret = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005757out_unlock:
5758 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5759 ordered->file_offset + ordered->len - 1,
5760 &cached_state, GFP_NOFS);
5761out:
5762 btrfs_delalloc_release_metadata(inode, ordered->len);
5763 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005764 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005765 btrfs_put_ordered_extent(ordered);
5766 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05005767
5768out_test:
5769 /*
5770 * our bio might span multiple ordered extents. If we haven't
5771 * completed the accounting for the whole dio, go back and try again
5772 */
5773 if (ordered_offset < dip->logical_offset + dip->bytes) {
5774 ordered_bytes = dip->logical_offset + dip->bytes -
5775 ordered_offset;
5776 goto again;
5777 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005778out_done:
5779 bio->bi_private = dip->private;
5780
5781 kfree(dip->csums);
5782 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005783
5784 /* If we had an error make sure to clear the uptodate flag */
5785 if (err)
5786 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005787 dio_end_io(bio, err);
5788}
5789
Chris Masoneaf25d92010-05-25 09:48:28 -04005790static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5791 struct bio *bio, int mirror_num,
5792 unsigned long bio_flags, u64 offset)
5793{
5794 int ret;
5795 struct btrfs_root *root = BTRFS_I(inode)->root;
5796 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5797 BUG_ON(ret);
5798 return 0;
5799}
5800
Miao Xiee65e1532010-11-22 03:04:43 +00005801static void btrfs_end_dio_bio(struct bio *bio, int err)
5802{
5803 struct btrfs_dio_private *dip = bio->bi_private;
5804
5805 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08005806 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00005807 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08005808 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00005809 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00005810 dip->errors = 1;
5811
5812 /*
5813 * before atomic variable goto zero, we must make sure
5814 * dip->errors is perceived to be set.
5815 */
5816 smp_mb__before_atomic_dec();
5817 }
5818
5819 /* if there are more bios still pending for this dio, just exit */
5820 if (!atomic_dec_and_test(&dip->pending_bios))
5821 goto out;
5822
5823 if (dip->errors)
5824 bio_io_error(dip->orig_bio);
5825 else {
5826 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
5827 bio_endio(dip->orig_bio, 0);
5828 }
5829out:
5830 bio_put(bio);
5831}
5832
5833static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
5834 u64 first_sector, gfp_t gfp_flags)
5835{
5836 int nr_vecs = bio_get_nr_vecs(bdev);
5837 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
5838}
5839
5840static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
5841 int rw, u64 file_offset, int skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04005842 u32 *csums, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00005843{
5844 int write = rw & REQ_WRITE;
5845 struct btrfs_root *root = BTRFS_I(inode)->root;
5846 int ret;
5847
5848 bio_get(bio);
5849 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
5850 if (ret)
5851 goto err;
5852
Josef Bacik1ae39932011-04-06 14:41:34 -04005853 if (skip_sum)
5854 goto map;
5855
5856 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00005857 ret = btrfs_wq_submit_bio(root->fs_info,
5858 inode, rw, bio, 0, 0,
5859 file_offset,
5860 __btrfs_submit_bio_start_direct_io,
5861 __btrfs_submit_bio_done);
5862 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04005863 } else if (write) {
5864 /*
5865 * If we aren't doing async submit, calculate the csum of the
5866 * bio now.
5867 */
5868 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
5869 if (ret)
5870 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00005871 } else if (!skip_sum) {
5872 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
Miao Xiee65e1532010-11-22 03:04:43 +00005873 file_offset, csums);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00005874 if (ret)
5875 goto err;
5876 }
Miao Xiee65e1532010-11-22 03:04:43 +00005877
Josef Bacik1ae39932011-04-06 14:41:34 -04005878map:
5879 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00005880err:
5881 bio_put(bio);
5882 return ret;
5883}
5884
5885static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
5886 int skip_sum)
5887{
5888 struct inode *inode = dip->inode;
5889 struct btrfs_root *root = BTRFS_I(inode)->root;
5890 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5891 struct bio *bio;
5892 struct bio *orig_bio = dip->orig_bio;
5893 struct bio_vec *bvec = orig_bio->bi_io_vec;
5894 u64 start_sector = orig_bio->bi_sector;
5895 u64 file_offset = dip->logical_offset;
5896 u64 submit_len = 0;
5897 u64 map_length;
5898 int nr_pages = 0;
5899 u32 *csums = dip->csums;
5900 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04005901 int async_submit = 0;
Josef Bacik98bc3142011-03-22 11:00:46 -04005902 int write = rw & REQ_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00005903
Miao Xiee65e1532010-11-22 03:04:43 +00005904 map_length = orig_bio->bi_size;
5905 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5906 &map_length, NULL, 0);
5907 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04005908 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00005909 return -EIO;
5910 }
5911
Josef Bacik02f57c72011-04-06 14:25:44 -04005912 if (map_length >= orig_bio->bi_size) {
5913 bio = orig_bio;
5914 goto submit;
5915 }
5916
Josef Bacik1ae39932011-04-06 14:41:34 -04005917 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04005918 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
5919 if (!bio)
5920 return -ENOMEM;
5921 bio->bi_private = dip;
5922 bio->bi_end_io = btrfs_end_dio_bio;
5923 atomic_inc(&dip->pending_bios);
5924
Miao Xiee65e1532010-11-22 03:04:43 +00005925 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
5926 if (unlikely(map_length < submit_len + bvec->bv_len ||
5927 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5928 bvec->bv_offset) < bvec->bv_len)) {
5929 /*
5930 * inc the count before we submit the bio so
5931 * we know the end IO handler won't happen before
5932 * we inc the count. Otherwise, the dip might get freed
5933 * before we're done setting it up
5934 */
5935 atomic_inc(&dip->pending_bios);
5936 ret = __btrfs_submit_dio_bio(bio, inode, rw,
5937 file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04005938 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00005939 if (ret) {
5940 bio_put(bio);
5941 atomic_dec(&dip->pending_bios);
5942 goto out_err;
5943 }
5944
Josef Bacik98bc3142011-03-22 11:00:46 -04005945 /* Write's use the ordered csums */
5946 if (!write && !skip_sum)
Miao Xiee65e1532010-11-22 03:04:43 +00005947 csums = csums + nr_pages;
5948 start_sector += submit_len >> 9;
5949 file_offset += submit_len;
5950
5951 submit_len = 0;
5952 nr_pages = 0;
5953
5954 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
5955 start_sector, GFP_NOFS);
5956 if (!bio)
5957 goto out_err;
5958 bio->bi_private = dip;
5959 bio->bi_end_io = btrfs_end_dio_bio;
5960
5961 map_length = orig_bio->bi_size;
5962 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5963 &map_length, NULL, 0);
5964 if (ret) {
5965 bio_put(bio);
5966 goto out_err;
5967 }
5968 } else {
5969 submit_len += bvec->bv_len;
5970 nr_pages ++;
5971 bvec++;
5972 }
5973 }
5974
Josef Bacik02f57c72011-04-06 14:25:44 -04005975submit:
Miao Xiee65e1532010-11-22 03:04:43 +00005976 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04005977 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00005978 if (!ret)
5979 return 0;
5980
5981 bio_put(bio);
5982out_err:
5983 dip->errors = 1;
5984 /*
5985 * before atomic variable goto zero, we must
5986 * make sure dip->errors is perceived to be set.
5987 */
5988 smp_mb__before_atomic_dec();
5989 if (atomic_dec_and_test(&dip->pending_bios))
5990 bio_io_error(dip->orig_bio);
5991
5992 /* bio_end_io() will handle error, so we needn't return it */
5993 return 0;
5994}
5995
Josef Bacik4b46fce2010-05-23 11:00:55 -04005996static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
5997 loff_t file_offset)
5998{
5999 struct btrfs_root *root = BTRFS_I(inode)->root;
6000 struct btrfs_dio_private *dip;
6001 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006002 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006003 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006004 int ret = 0;
6005
6006 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6007
6008 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6009 if (!dip) {
6010 ret = -ENOMEM;
6011 goto free_ordered;
6012 }
6013 dip->csums = NULL;
6014
Josef Bacik98bc3142011-03-22 11:00:46 -04006015 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6016 if (!write && !skip_sum) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006017 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6018 if (!dip->csums) {
Daniel J Bluemanb4966b72011-03-09 16:46:42 +00006019 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006020 ret = -ENOMEM;
6021 goto free_ordered;
6022 }
6023 }
6024
6025 dip->private = bio->bi_private;
6026 dip->inode = inode;
6027 dip->logical_offset = file_offset;
6028
Josef Bacik4b46fce2010-05-23 11:00:55 -04006029 dip->bytes = 0;
6030 do {
6031 dip->bytes += bvec->bv_len;
6032 bvec++;
6033 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6034
Chris Mason46bfbb52010-05-26 11:04:10 -04006035 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006036 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006037 dip->errors = 0;
6038 dip->orig_bio = bio;
6039 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006040
6041 if (write)
6042 bio->bi_end_io = btrfs_endio_direct_write;
6043 else
6044 bio->bi_end_io = btrfs_endio_direct_read;
6045
Miao Xiee65e1532010-11-22 03:04:43 +00006046 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6047 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006048 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006049free_ordered:
6050 /*
6051 * If this is a write, we need to clean up the reserved space and kill
6052 * the ordered extent.
6053 */
6054 if (write) {
6055 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006056 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006057 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6058 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6059 btrfs_free_reserved_extent(root, ordered->start,
6060 ordered->disk_len);
6061 btrfs_put_ordered_extent(ordered);
6062 btrfs_put_ordered_extent(ordered);
6063 }
6064 bio_endio(bio, ret);
6065}
6066
Chris Mason5a5f79b2010-05-26 21:33:37 -04006067static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6068 const struct iovec *iov, loff_t offset,
6069 unsigned long nr_segs)
6070{
6071 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006072 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006073 size_t size;
6074 unsigned long addr;
6075 unsigned blocksize_mask = root->sectorsize - 1;
6076 ssize_t retval = -EINVAL;
6077 loff_t end = offset;
6078
6079 if (offset & blocksize_mask)
6080 goto out;
6081
6082 /* Check the memory alignment. Blocks cannot straddle pages */
6083 for (seg = 0; seg < nr_segs; seg++) {
6084 addr = (unsigned long)iov[seg].iov_base;
6085 size = iov[seg].iov_len;
6086 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006087 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006088 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006089
6090 /* If this is a write we don't need to check anymore */
6091 if (rw & WRITE)
6092 continue;
6093
6094 /*
6095 * Check to make sure we don't have duplicate iov_base's in this
6096 * iovec, if so return EINVAL, otherwise we'll get csum errors
6097 * when reading back.
6098 */
6099 for (i = seg + 1; i < nr_segs; i++) {
6100 if (iov[seg].iov_base == iov[i].iov_base)
6101 goto out;
6102 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006103 }
6104 retval = 0;
6105out:
6106 return retval;
6107}
Chris Mason16432982008-04-10 10:23:21 -04006108static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6109 const struct iovec *iov, loff_t offset,
6110 unsigned long nr_segs)
6111{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006112 struct file *file = iocb->ki_filp;
6113 struct inode *inode = file->f_mapping->host;
6114 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006115 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006116 u64 lockstart, lockend;
6117 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006118 int writing = rw & WRITE;
6119 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006120 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006121
Chris Mason5a5f79b2010-05-26 21:33:37 -04006122 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6123 offset, nr_segs)) {
6124 return 0;
6125 }
6126
Josef Bacik4b46fce2010-05-23 11:00:55 -04006127 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006128 lockend = offset + count - 1;
6129
6130 if (writing) {
6131 ret = btrfs_delalloc_reserve_space(inode, count);
6132 if (ret)
6133 goto out;
6134 }
Chris Mason4845e442010-05-25 20:56:50 -04006135
Josef Bacik4b46fce2010-05-23 11:00:55 -04006136 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006137 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6138 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006139 /*
6140 * We're concerned with the entire range that we're going to be
6141 * doing DIO to, so we need to make sure theres no ordered
6142 * extents in this range.
6143 */
6144 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6145 lockend - lockstart + 1);
6146 if (!ordered)
6147 break;
Chris Mason4845e442010-05-25 20:56:50 -04006148 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6149 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006150 btrfs_start_ordered_extent(inode, ordered, 1);
6151 btrfs_put_ordered_extent(ordered);
6152 cond_resched();
6153 }
6154
Chris Mason4845e442010-05-25 20:56:50 -04006155 /*
6156 * we don't use btrfs_set_extent_delalloc because we don't want
6157 * the dirty or uptodate bits
6158 */
6159 if (writing) {
6160 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6161 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6162 EXTENT_DELALLOC, 0, NULL, &cached_state,
6163 GFP_NOFS);
6164 if (ret) {
6165 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6166 lockend, EXTENT_LOCKED | write_bits,
6167 1, 0, &cached_state, GFP_NOFS);
6168 goto out;
6169 }
6170 }
6171
6172 free_extent_state(cached_state);
6173 cached_state = NULL;
6174
Chris Mason5a5f79b2010-05-26 21:33:37 -04006175 ret = __blockdev_direct_IO(rw, iocb, inode,
6176 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6177 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6178 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006179
6180 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006181 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6182 offset + iov_length(iov, nr_segs) - 1,
6183 EXTENT_LOCKED | write_bits, 1, 0,
6184 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006185 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6186 /*
6187 * We're falling back to buffered, unlock the section we didn't
6188 * do IO on.
6189 */
Chris Mason4845e442010-05-25 20:56:50 -04006190 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6191 offset + iov_length(iov, nr_segs) - 1,
6192 EXTENT_LOCKED | write_bits, 1, 0,
6193 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006194 }
Chris Mason4845e442010-05-25 20:56:50 -04006195out:
6196 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006197 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006198}
6199
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006200static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6201 __u64 start, __u64 len)
6202{
Chris Masonec29ed52011-02-23 16:23:20 -05006203 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006204}
6205
Chris Mason9ebefb182007-06-15 13:50:00 -04006206int btrfs_readpage(struct file *file, struct page *page)
6207{
Chris Masond1310b22008-01-24 16:13:08 -05006208 struct extent_io_tree *tree;
6209 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006210 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006211}
Chris Mason1832a6d2007-12-21 16:27:21 -05006212
Chris Mason39279cc2007-06-12 06:35:45 -04006213static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6214{
Chris Masond1310b22008-01-24 16:13:08 -05006215 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006216
6217
6218 if (current->flags & PF_MEMALLOC) {
6219 redirty_page_for_writepage(wbc, page);
6220 unlock_page(page);
6221 return 0;
6222 }
Chris Masond1310b22008-01-24 16:13:08 -05006223 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006224 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6225}
Chris Mason39279cc2007-06-12 06:35:45 -04006226
Chris Masonf4219502008-07-22 11:18:09 -04006227int btrfs_writepages(struct address_space *mapping,
6228 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006229{
Chris Masond1310b22008-01-24 16:13:08 -05006230 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006231
Chris Masond1310b22008-01-24 16:13:08 -05006232 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006233 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6234}
6235
Chris Mason3ab2fb52007-11-08 10:59:22 -05006236static int
6237btrfs_readpages(struct file *file, struct address_space *mapping,
6238 struct list_head *pages, unsigned nr_pages)
6239{
Chris Masond1310b22008-01-24 16:13:08 -05006240 struct extent_io_tree *tree;
6241 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006242 return extent_readpages(tree, mapping, pages, nr_pages,
6243 btrfs_get_extent);
6244}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006245static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006246{
Chris Masond1310b22008-01-24 16:13:08 -05006247 struct extent_io_tree *tree;
6248 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006249 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006250
Chris Masond1310b22008-01-24 16:13:08 -05006251 tree = &BTRFS_I(page->mapping->host)->io_tree;
6252 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006253 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006254 if (ret == 1) {
6255 ClearPagePrivate(page);
6256 set_page_private(page, 0);
6257 page_cache_release(page);
6258 }
6259 return ret;
6260}
Chris Mason39279cc2007-06-12 06:35:45 -04006261
Chris Masone6dcd2d2008-07-17 12:53:50 -04006262static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6263{
Chris Mason98509cf2008-09-11 15:51:43 -04006264 if (PageWriteback(page) || PageDirty(page))
6265 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006266 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006267}
6268
Chris Masona52d9a82007-08-27 16:49:44 -04006269static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6270{
Chris Masond1310b22008-01-24 16:13:08 -05006271 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006272 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006273 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006274 u64 page_start = page_offset(page);
6275 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006276
Chris Mason8b62b722009-09-02 16:53:46 -04006277
6278 /*
6279 * we have the page locked, so new writeback can't start,
6280 * and the dirty bit won't be cleared while we are here.
6281 *
6282 * Wait for IO on this page so that we can safely clear
6283 * the PagePrivate2 bit and do ordered accounting
6284 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006285 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006286
Chris Masond1310b22008-01-24 16:13:08 -05006287 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006288 if (offset) {
6289 btrfs_releasepage(page, GFP_NOFS);
6290 return;
6291 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006292 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6293 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006294 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6295 page_offset(page));
6296 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006297 /*
6298 * IO on this page will never be started, so we need
6299 * to account for any ordered extents now
6300 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006301 clear_extent_bit(tree, page_start, page_end,
6302 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006303 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006304 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006305 /*
6306 * whoever cleared the private bit is responsible
6307 * for the finish_ordered_io
6308 */
6309 if (TestClearPagePrivate2(page)) {
6310 btrfs_finish_ordered_io(page->mapping->host,
6311 page_start, page_end);
6312 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006313 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006314 cached_state = NULL;
6315 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6316 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006317 }
6318 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006319 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006320 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006321 __btrfs_releasepage(page, GFP_NOFS);
6322
Chris Mason4a096752008-07-21 10:29:44 -04006323 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006324 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006325 ClearPagePrivate(page);
6326 set_page_private(page, 0);
6327 page_cache_release(page);
6328 }
Chris Mason39279cc2007-06-12 06:35:45 -04006329}
6330
Chris Mason9ebefb182007-06-15 13:50:00 -04006331/*
6332 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6333 * called from a page fault handler when a page is first dirtied. Hence we must
6334 * be careful to check for EOF conditions here. We set the page up correctly
6335 * for a written page which means we get ENOSPC checking when writing into
6336 * holes and correct delalloc and unwritten extent mapping on filesystems that
6337 * support these features.
6338 *
6339 * We are not allowed to take the i_mutex here so we have to play games to
6340 * protect against truncate races as the page could now be beyond EOF. Because
6341 * vmtruncate() writes the inode size before removing pages, once we have the
6342 * page lock we can determine safely if the page is beyond EOF. If it is not
6343 * beyond EOF, then the page is guaranteed safe against truncation until we
6344 * unlock the page.
6345 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006346int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006347{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006348 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006349 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006350 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006351 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6352 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006353 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006354 char *kaddr;
6355 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006356 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006357 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04006358 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006359 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006360
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006361 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07006362 if (ret) {
6363 if (ret == -ENOMEM)
6364 ret = VM_FAULT_OOM;
6365 else /* -ENOSPC, -EIO, etc */
6366 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05006367 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07006368 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006369
Nick Piggin56a76f82009-03-31 15:23:23 -07006370 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006371again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006372 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006373 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006374 page_start = page_offset(page);
6375 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006376
Chris Mason9ebefb182007-06-15 13:50:00 -04006377 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006378 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006379 /* page got truncated out from underneath us */
6380 goto out_unlock;
6381 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006382 wait_on_page_writeback(page);
6383
Josef Bacik2ac55d42010-02-03 19:33:23 +00006384 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6385 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006386 set_page_extent_mapped(page);
6387
Chris Masoneb84ae02008-07-17 13:53:27 -04006388 /*
6389 * we can't set the delalloc bits if there are pending ordered
6390 * extents. Drop our locks and wait for them to finish
6391 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006392 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6393 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006394 unlock_extent_cached(io_tree, page_start, page_end,
6395 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006396 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006397 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006398 btrfs_put_ordered_extent(ordered);
6399 goto again;
6400 }
6401
Josef Bacikfbf19082009-10-01 17:10:23 -04006402 /*
6403 * XXX - page_mkwrite gets called every time the page is dirtied, even
6404 * if it was already dirty, so for space accounting reasons we need to
6405 * clear any delalloc bits for the range we are fixing to save. There
6406 * is probably a better way to do this, but for now keep consistent with
6407 * prepare_pages in the normal write path.
6408 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006409 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006410 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006411 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006412
Josef Bacik2ac55d42010-02-03 19:33:23 +00006413 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6414 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006415 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006416 unlock_extent_cached(io_tree, page_start, page_end,
6417 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006418 ret = VM_FAULT_SIGBUS;
6419 goto out_unlock;
6420 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006421 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006422
6423 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006424 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006425 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006426 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006427 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006428
Chris Masone6dcd2d2008-07-17 12:53:50 -04006429 if (zero_start != PAGE_CACHE_SIZE) {
6430 kaddr = kmap(page);
6431 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6432 flush_dcache_page(page);
6433 kunmap(page);
6434 }
Chris Mason247e7432008-07-17 12:53:51 -04006435 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006436 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006437 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006438
Chris Mason257c62e2009-10-13 13:21:08 -04006439 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6440 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6441
Josef Bacik2ac55d42010-02-03 19:33:23 +00006442 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006443
6444out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006445 if (!ret)
6446 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006447 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006448 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05006449out:
Chris Mason9ebefb182007-06-15 13:50:00 -04006450 return ret;
6451}
6452
Josef Bacika41ad392011-01-31 15:30:16 -05006453static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006454{
6455 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006456 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006457 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006458 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006459 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04006460 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006461 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006462 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006463
Josef Bacik5d5e1032009-10-13 16:46:49 -04006464 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6465 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006466 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006467
Chris Mason4a096752008-07-21 10:29:44 -04006468 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006469 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006470
Josef Bacikfcb80c22011-05-03 10:40:22 -04006471 /*
6472 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6473 * 3 things going on here
6474 *
6475 * 1) We need to reserve space for our orphan item and the space to
6476 * delete our orphan item. Lord knows we don't want to have a dangling
6477 * orphan item because we didn't reserve space to remove it.
6478 *
6479 * 2) We need to reserve space to update our inode.
6480 *
6481 * 3) We need to have something to cache all the space that is going to
6482 * be free'd up by the truncate operation, but also have some slack
6483 * space reserved in case it uses space during the truncate (thank you
6484 * very much snapshotting).
6485 *
6486 * And we need these to all be seperate. The fact is we can use alot of
6487 * space doing the truncate, and we have no earthly idea how much space
6488 * we will use, so we need the truncate reservation to be seperate so it
6489 * doesn't end up using space reserved for updating the inode or
6490 * removing the orphan item. We also need to be able to stop the
6491 * transaction and start a new one, which means we need to be able to
6492 * update the inode several times, and we have no idea of knowing how
6493 * many times that will be, so we can't just reserve 1 item for the
6494 * entirety of the opration, so that has to be done seperately as well.
6495 * Then there is the orphan item, which does indeed need to be held on
6496 * to for the whole operation, and we need nobody to touch this reserved
6497 * space except the orphan code.
6498 *
6499 * So that leaves us with
6500 *
6501 * 1) root->orphan_block_rsv - for the orphan deletion.
6502 * 2) rsv - for the truncate reservation, which we will steal from the
6503 * transaction reservation.
6504 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6505 * updating the inode.
6506 */
6507 rsv = btrfs_alloc_block_rsv(root);
6508 if (!rsv)
6509 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006510 rsv->size = min_size;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006511
Josef Bacik907cbce2011-08-08 13:46:15 -04006512 /*
Josef Bacik07127182011-08-19 10:29:59 -04006513 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006514 * 1 for the orphan item we're going to add
6515 * 1 for the orphan item deletion
6516 * 1 for updating the inode.
6517 */
Josef Bacik07127182011-08-19 10:29:59 -04006518 trans = btrfs_start_transaction(root, 4);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006519 if (IS_ERR(trans)) {
6520 err = PTR_ERR(trans);
6521 goto out;
6522 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006523
Josef Bacik907cbce2011-08-08 13:46:15 -04006524 /* Migrate the slack space for the truncate to our reserve */
6525 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6526 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006527 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006528
6529 ret = btrfs_orphan_add(trans, inode);
6530 if (ret) {
6531 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006532 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006533 }
6534
Chris Mason5a3f23d2009-03-31 13:27:11 -04006535 /*
6536 * setattr is responsible for setting the ordered_data_close flag,
6537 * but that is only tested during the last file release. That
6538 * could happen well after the next commit, leaving a great big
6539 * window where new writes may get lost if someone chooses to write
6540 * to this file after truncating to zero
6541 *
6542 * The inode doesn't have any dirty data here, and so if we commit
6543 * this is a noop. If someone immediately starts writing to the inode
6544 * it is very likely we'll catch some of their writes in this
6545 * transaction, and the commit will find this file on the ordered
6546 * data list with good things to send down.
6547 *
6548 * This is a best effort solution, there is still a window where
6549 * using truncate to replace the contents of the file will
6550 * end up with a zero length file after a crash.
6551 */
6552 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6553 btrfs_add_ordered_operation(trans, root, inode);
6554
Yan, Zheng80825102009-11-12 09:35:36 +00006555 while (1) {
Josef Bacik36ba0222011-10-18 12:15:48 -04006556 ret = btrfs_block_rsv_refill(root, rsv, min_size);
Josef Bacik907cbce2011-08-08 13:46:15 -04006557 if (ret) {
6558 /*
6559 * This can only happen with the original transaction we
6560 * started above, every other time we shouldn't have a
6561 * transaction started yet.
6562 */
6563 if (ret == -EAGAIN)
6564 goto end_trans;
6565 err = ret;
6566 break;
6567 }
6568
Yan, Zhengd68fc572010-05-16 10:49:58 -04006569 if (!trans) {
Josef Bacik907cbce2011-08-08 13:46:15 -04006570 /* Just need the 1 for updating the inode */
6571 trans = btrfs_start_transaction(root, 1);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006572 if (IS_ERR(trans)) {
6573 err = PTR_ERR(trans);
6574 goto out;
6575 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006576 }
6577
Josef Bacik907cbce2011-08-08 13:46:15 -04006578 trans->block_rsv = rsv;
6579
Yan, Zheng80825102009-11-12 09:35:36 +00006580 ret = btrfs_truncate_inode_items(trans, root, inode,
6581 inode->i_size,
6582 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006583 if (ret != -EAGAIN) {
6584 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006585 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006586 }
Chris Mason39279cc2007-06-12 06:35:45 -04006587
Josef Bacikfcb80c22011-05-03 10:40:22 -04006588 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006589 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006590 if (ret) {
6591 err = ret;
6592 break;
6593 }
Josef Bacik907cbce2011-08-08 13:46:15 -04006594end_trans:
Yan, Zheng80825102009-11-12 09:35:36 +00006595 nr = trans->blocks_used;
6596 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006597 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006598 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006599 }
6600
6601 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04006602 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006603 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006604 if (ret)
6605 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006606 } else if (ret && inode->i_nlink > 0) {
6607 /*
6608 * Failed to do the truncate, remove us from the in memory
6609 * orphan list.
6610 */
6611 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006612 }
6613
Chris Mason917c16b2011-11-08 14:49:59 -05006614 if (trans) {
6615 trans->block_rsv = &root->fs_info->trans_block_rsv;
6616 ret = btrfs_update_inode(trans, root, inode);
6617 if (ret && !err)
6618 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006619
Chris Mason917c16b2011-11-08 14:49:59 -05006620 nr = trans->blocks_used;
6621 ret = btrfs_end_transaction_throttle(trans, root);
6622 btrfs_btree_balance_dirty(root, nr);
6623 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04006624
6625out:
6626 btrfs_free_block_rsv(root, rsv);
6627
Josef Bacik3893e332011-01-31 16:03:11 -05006628 if (ret && !err)
6629 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05006630
Josef Bacik3893e332011-01-31 16:03:11 -05006631 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006632}
6633
Sven Wegener3b963622008-06-09 21:57:42 -04006634/*
Chris Masond352ac62008-09-29 15:18:18 -04006635 * create a new subvolume directory/inode (helper for the ioctl).
6636 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006637int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006638 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04006639{
Chris Mason39279cc2007-06-12 06:35:45 -04006640 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006641 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006642 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006643
Josef Bacikaec74772008-07-24 12:12:38 -04006644 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006645 new_dirid, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006646 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006647 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006648 inode->i_op = &btrfs_dir_inode_operations;
6649 inode->i_fop = &btrfs_dir_file_operations;
6650
Chris Mason39279cc2007-06-12 06:35:45 -04006651 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006652 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006653
Yan, Zheng76dda932009-09-21 16:00:26 -04006654 err = btrfs_update_inode(trans, new_root, inode);
6655 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006656
Yan, Zheng76dda932009-09-21 16:00:26 -04006657 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006658 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006659}
6660
Chris Mason39279cc2007-06-12 06:35:45 -04006661struct inode *btrfs_alloc_inode(struct super_block *sb)
6662{
6663 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006664 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006665
6666 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6667 if (!ei)
6668 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006669
6670 ei->root = NULL;
6671 ei->space_info = NULL;
6672 ei->generation = 0;
6673 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006674 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006675 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006676 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006677 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006678 ei->disk_i_size = 0;
6679 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04006680 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006681 ei->index_cnt = (u64)-1;
6682 ei->last_unlink_trans = 0;
6683
Josef Bacik9e0baf62011-07-15 15:16:44 +00006684 spin_lock_init(&ei->lock);
6685 ei->outstanding_extents = 0;
6686 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006687
6688 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006689 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006690 ei->dummy_inode = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04006691 ei->in_defrag = 0;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05006692 ei->delalloc_meta_reserved = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006693 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006694
Miao Xie16cdcec2011-04-22 18:12:22 +08006695 ei->delayed_node = NULL;
6696
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006697 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02006698 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02006699 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6700 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006701 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006702 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006703 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006704 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006705 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006706 RB_CLEAR_NODE(&ei->rb_node);
6707
6708 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006709}
6710
Nick Pigginfa0d7e32011-01-07 17:49:49 +11006711static void btrfs_i_callback(struct rcu_head *head)
6712{
6713 struct inode *inode = container_of(head, struct inode, i_rcu);
6714 INIT_LIST_HEAD(&inode->i_dentry);
6715 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6716}
6717
Chris Mason39279cc2007-06-12 06:35:45 -04006718void btrfs_destroy_inode(struct inode *inode)
6719{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006720 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006721 struct btrfs_root *root = BTRFS_I(inode)->root;
6722
Chris Mason39279cc2007-06-12 06:35:45 -04006723 WARN_ON(!list_empty(&inode->i_dentry));
6724 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006725 WARN_ON(BTRFS_I(inode)->outstanding_extents);
6726 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04006727 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
6728 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04006729
Chris Mason5a3f23d2009-03-31 13:27:11 -04006730 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006731 * This can happen where we create an inode, but somebody else also
6732 * created the same inode and we need to destroy the one we already
6733 * created.
6734 */
6735 if (!root)
6736 goto free;
6737
6738 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006739 * Make sure we're properly removed from the ordered operation
6740 * lists.
6741 */
6742 smp_mb();
6743 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6744 spin_lock(&root->fs_info->ordered_extent_lock);
6745 list_del_init(&BTRFS_I(inode)->ordered_operations);
6746 spin_unlock(&root->fs_info->ordered_extent_lock);
6747 }
6748
Yan, Zhengd68fc572010-05-16 10:49:58 -04006749 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006750 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Li Zefan33345d012011-04-20 10:31:50 +08006751 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6752 (unsigned long long)btrfs_ino(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00006753 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006754 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006755 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006756
Chris Masond3977122009-01-05 21:25:51 -05006757 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006758 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6759 if (!ordered)
6760 break;
6761 else {
Chris Masond3977122009-01-05 21:25:51 -05006762 printk(KERN_ERR "btrfs found ordered "
6763 "extent %llu %llu on inode cleanup\n",
6764 (unsigned long long)ordered->file_offset,
6765 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006766 btrfs_remove_ordered_extent(inode, ordered);
6767 btrfs_put_ordered_extent(ordered);
6768 btrfs_put_ordered_extent(ordered);
6769 }
6770 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006771 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006772 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006773free:
Miao Xie16cdcec2011-04-22 18:12:22 +08006774 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e32011-01-07 17:49:49 +11006775 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04006776}
6777
Al Viro45321ac2010-06-07 13:43:19 -04006778int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006779{
6780 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006781
Josef Bacik0af3d002010-06-21 14:48:16 -04006782 if (btrfs_root_refs(&root->root_item) == 0 &&
Chris Mason2cf85722011-07-26 15:35:09 -04006783 !btrfs_is_free_space_inode(root, inode))
Al Viro45321ac2010-06-07 13:43:19 -04006784 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006785 else
Al Viro45321ac2010-06-07 13:43:19 -04006786 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006787}
6788
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006789static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006790{
6791 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6792
6793 inode_init_once(&ei->vfs_inode);
6794}
6795
6796void btrfs_destroy_cachep(void)
6797{
6798 if (btrfs_inode_cachep)
6799 kmem_cache_destroy(btrfs_inode_cachep);
6800 if (btrfs_trans_handle_cachep)
6801 kmem_cache_destroy(btrfs_trans_handle_cachep);
6802 if (btrfs_transaction_cachep)
6803 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006804 if (btrfs_path_cachep)
6805 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05006806 if (btrfs_free_space_cachep)
6807 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006808}
6809
6810int btrfs_init_cachep(void)
6811{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006812 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6813 sizeof(struct btrfs_inode), 0,
6814 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006815 if (!btrfs_inode_cachep)
6816 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006817
6818 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6819 sizeof(struct btrfs_trans_handle), 0,
6820 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006821 if (!btrfs_trans_handle_cachep)
6822 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006823
6824 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6825 sizeof(struct btrfs_transaction), 0,
6826 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006827 if (!btrfs_transaction_cachep)
6828 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006829
6830 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6831 sizeof(struct btrfs_path), 0,
6832 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006833 if (!btrfs_path_cachep)
6834 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006835
Josef Bacikdc89e982011-01-28 17:05:48 -05006836 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
6837 sizeof(struct btrfs_free_space), 0,
6838 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
6839 if (!btrfs_free_space_cachep)
6840 goto fail;
6841
Chris Mason39279cc2007-06-12 06:35:45 -04006842 return 0;
6843fail:
6844 btrfs_destroy_cachep();
6845 return -ENOMEM;
6846}
6847
6848static int btrfs_getattr(struct vfsmount *mnt,
6849 struct dentry *dentry, struct kstat *stat)
6850{
6851 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05006852 u32 blocksize = inode->i_sb->s_blocksize;
6853
Chris Mason39279cc2007-06-12 06:35:45 -04006854 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04006855 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05006856 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05006857 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
6858 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04006859 return 0;
6860}
6861
Liu Bo75e7cb72011-03-22 10:12:20 +00006862/*
6863 * If a file is moved, it will inherit the cow and compression flags of the new
6864 * directory.
6865 */
6866static void fixup_inode_flags(struct inode *dir, struct inode *inode)
6867{
6868 struct btrfs_inode *b_dir = BTRFS_I(dir);
6869 struct btrfs_inode *b_inode = BTRFS_I(inode);
6870
6871 if (b_dir->flags & BTRFS_INODE_NODATACOW)
6872 b_inode->flags |= BTRFS_INODE_NODATACOW;
6873 else
6874 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
6875
6876 if (b_dir->flags & BTRFS_INODE_COMPRESS)
6877 b_inode->flags |= BTRFS_INODE_COMPRESS;
6878 else
6879 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
6880}
6881
Chris Masond3977122009-01-05 21:25:51 -05006882static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6883 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04006884{
6885 struct btrfs_trans_handle *trans;
6886 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006887 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04006888 struct inode *new_inode = new_dentry->d_inode;
6889 struct inode *old_inode = old_dentry->d_inode;
6890 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006891 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006892 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04006893 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08006894 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006895
Li Zefan33345d012011-04-20 10:31:50 +08006896 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04006897 return -EPERM;
6898
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006899 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08006900 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05006901 return -EXDEV;
6902
Li Zefan33345d012011-04-20 10:31:50 +08006903 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
6904 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006905 return -ENOTEMPTY;
6906
Chris Mason39279cc2007-06-12 06:35:45 -04006907 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006908 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04006909 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006910 /*
6911 * we're using rename to replace one file with another.
6912 * and the replacement file is large. Start IO on it now so
6913 * we don't add too much work to the end of the transaction
6914 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04006915 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04006916 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
6917 filemap_flush(old_inode->i_mapping);
6918
Yan, Zheng76dda932009-09-21 16:00:26 -04006919 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08006920 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04006921 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006922 /*
6923 * We want to reserve the absolute worst case amount of items. So if
6924 * both inodes are subvols and we need to unlink them then that would
6925 * require 4 item modifications, but if they are both normal inodes it
6926 * would require 5 item modifications, so we'll assume their normal
6927 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6928 * should cover the worst case number of items we'll modify.
6929 */
6930 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00006931 if (IS_ERR(trans)) {
6932 ret = PTR_ERR(trans);
6933 goto out_notrans;
6934 }
Chris Mason5f39d392007-10-15 16:14:19 -04006935
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006936 if (dest != root)
6937 btrfs_record_root_in_trans(trans, dest);
6938
Yan, Zhenga5719522009-09-24 09:17:31 -04006939 ret = btrfs_set_inode_index(new_dir, &index);
6940 if (ret)
6941 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006942
Li Zefan33345d012011-04-20 10:31:50 +08006943 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006944 /* force full log commit if subvolume involved. */
6945 root->fs_info->last_trans_log_full_commit = trans->transid;
6946 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04006947 ret = btrfs_insert_inode_ref(trans, dest,
6948 new_dentry->d_name.name,
6949 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08006950 old_ino,
6951 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04006952 if (ret)
6953 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006954 /*
6955 * this is an ugly little race, but the rename is required
6956 * to make sure that if we crash, the inode is either at the
6957 * old name or the new one. pinning the log transaction lets
6958 * us make sure we don't allow a log commit to come in after
6959 * we unlink the name but before we add the new name back in.
6960 */
6961 btrfs_pin_log_trans(root);
6962 }
Chris Mason12fcfd22009-03-24 10:24:20 -04006963 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006964 * make sure the inode gets flushed if it is replacing
6965 * something.
6966 */
Li Zefan33345d012011-04-20 10:31:50 +08006967 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04006968 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006969
Chris Mason39279cc2007-06-12 06:35:45 -04006970 old_dir->i_ctime = old_dir->i_mtime = ctime;
6971 new_dir->i_ctime = new_dir->i_mtime = ctime;
6972 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04006973
Chris Mason12fcfd22009-03-24 10:24:20 -04006974 if (old_dentry->d_parent != new_dentry->d_parent)
6975 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
6976
Li Zefan33345d012011-04-20 10:31:50 +08006977 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006978 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
6979 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
6980 old_dentry->d_name.name,
6981 old_dentry->d_name.len);
6982 } else {
Al Viro92986792011-03-04 17:14:37 +00006983 ret = __btrfs_unlink_inode(trans, root, old_dir,
6984 old_dentry->d_inode,
6985 old_dentry->d_name.name,
6986 old_dentry->d_name.len);
6987 if (!ret)
6988 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006989 }
6990 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006991
6992 if (new_inode) {
6993 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08006994 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006995 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
6996 root_objectid = BTRFS_I(new_inode)->location.objectid;
6997 ret = btrfs_unlink_subvol(trans, dest, new_dir,
6998 root_objectid,
6999 new_dentry->d_name.name,
7000 new_dentry->d_name.len);
7001 BUG_ON(new_inode->i_nlink == 0);
7002 } else {
7003 ret = btrfs_unlink_inode(trans, dest, new_dir,
7004 new_dentry->d_inode,
7005 new_dentry->d_name.name,
7006 new_dentry->d_name.len);
7007 }
7008 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007009 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007010 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007011 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007012 }
Chris Mason39279cc2007-06-12 06:35:45 -04007013 }
Josef Bacikaec74772008-07-24 12:12:38 -04007014
Liu Bo75e7cb72011-03-22 10:12:20 +00007015 fixup_inode_flags(new_dir, old_inode);
7016
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007017 ret = btrfs_add_link(trans, new_dir, old_inode,
7018 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007019 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007020 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04007021
Li Zefan33345d012011-04-20 10:31:50 +08007022 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007023 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007024 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007025 btrfs_end_log_trans(root);
7026 }
Chris Mason39279cc2007-06-12 06:35:45 -04007027out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04007028 btrfs_end_transaction_throttle(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007029out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007030 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007031 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007032
Chris Mason39279cc2007-06-12 06:35:45 -04007033 return ret;
7034}
7035
Chris Masond352ac62008-09-29 15:18:18 -04007036/*
7037 * some fairly slow code that needs optimization. This walks the list
7038 * of all the inodes with pending delalloc and forces them to disk.
7039 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007040int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007041{
7042 struct list_head *head = &root->fs_info->delalloc_inodes;
7043 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007044 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007045
Yan Zhengc146afa2008-11-12 14:34:12 -05007046 if (root->fs_info->sb->s_flags & MS_RDONLY)
7047 return -EROFS;
7048
Chris Mason75eff682008-12-15 15:54:40 -05007049 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007050 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007051 binode = list_entry(head->next, struct btrfs_inode,
7052 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007053 inode = igrab(&binode->vfs_inode);
7054 if (!inode)
7055 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007056 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007057 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007058 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007059 if (delay_iput)
7060 btrfs_add_delayed_iput(inode);
7061 else
7062 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007063 }
7064 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007065 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007066 }
Chris Mason75eff682008-12-15 15:54:40 -05007067 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007068
7069 /* the filemap_flush will queue IO into the worker threads, but
7070 * we have to make sure the IO is actually started and that
7071 * ordered extents get created before we return
7072 */
7073 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007074 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007075 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007076 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007077 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7078 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007079 }
7080 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007081 return 0;
7082}
7083
Chris Mason39279cc2007-06-12 06:35:45 -04007084static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7085 const char *symname)
7086{
7087 struct btrfs_trans_handle *trans;
7088 struct btrfs_root *root = BTRFS_I(dir)->root;
7089 struct btrfs_path *path;
7090 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007091 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007092 int err;
7093 int drop_inode = 0;
7094 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007095 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007096 int name_len;
7097 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007098 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007099 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007100 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007101 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007102
7103 name_len = strlen(symname) + 1;
7104 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7105 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007106
Josef Bacik9ed74f22009-09-11 16:12:44 -04007107 /*
7108 * 2 items for inode item and ref
7109 * 2 items for dir items
7110 * 1 item for xattr if selinux is on
7111 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007112 trans = btrfs_start_transaction(root, 5);
7113 if (IS_ERR(trans))
7114 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007115
Li Zefan581bb052011-04-20 10:06:11 +08007116 err = btrfs_find_free_ino(root, &objectid);
7117 if (err)
7118 goto out_unlock;
7119
Josef Bacikaec74772008-07-24 12:12:38 -04007120 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007121 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007122 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007123 if (IS_ERR(inode)) {
7124 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007125 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007126 }
Chris Mason39279cc2007-06-12 06:35:45 -04007127
Eric Paris2a7dba32011-02-01 11:05:39 -05007128 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007129 if (err) {
7130 drop_inode = 1;
7131 goto out_unlock;
7132 }
7133
Casey Schauflerad19db72011-12-15 10:09:07 -05007134 /*
7135 * If the active LSM wants to access the inode during
7136 * d_instantiate it needs these. Smack checks to see
7137 * if the filesystem supports xattrs by looking at the
7138 * ops vector.
7139 */
7140 inode->i_fop = &btrfs_file_operations;
7141 inode->i_op = &btrfs_file_inode_operations;
7142
Josef Bacika1b075d2010-11-19 20:36:11 +00007143 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007144 if (err)
7145 drop_inode = 1;
7146 else {
7147 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007148 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007149 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007150 }
Chris Mason39279cc2007-06-12 06:35:45 -04007151 if (drop_inode)
7152 goto out_unlock;
7153
7154 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007155 if (!path) {
7156 err = -ENOMEM;
7157 drop_inode = 1;
7158 goto out_unlock;
7159 }
Li Zefan33345d012011-04-20 10:31:50 +08007160 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007161 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007162 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7163 datasize = btrfs_file_extent_calc_inline_size(name_len);
7164 err = btrfs_insert_empty_item(trans, root, path, &key,
7165 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007166 if (err) {
7167 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007168 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007169 goto out_unlock;
7170 }
Chris Mason5f39d392007-10-15 16:14:19 -04007171 leaf = path->nodes[0];
7172 ei = btrfs_item_ptr(leaf, path->slots[0],
7173 struct btrfs_file_extent_item);
7174 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7175 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007176 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007177 btrfs_set_file_extent_encryption(leaf, ei, 0);
7178 btrfs_set_file_extent_compression(leaf, ei, 0);
7179 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7180 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7181
Chris Mason39279cc2007-06-12 06:35:45 -04007182 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007183 write_extent_buffer(leaf, symname, ptr, name_len);
7184 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007185 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007186
Chris Mason39279cc2007-06-12 06:35:45 -04007187 inode->i_op = &btrfs_symlink_inode_operations;
7188 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007189 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007190 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007191 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007192 err = btrfs_update_inode(trans, root, inode);
7193 if (err)
7194 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007195
7196out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04007197 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04007198 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007199 if (drop_inode) {
7200 inode_dec_link_count(inode);
7201 iput(inode);
7202 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04007203 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007204 return err;
7205}
Chris Mason16432982008-04-10 10:23:21 -04007206
Josef Bacik0af3d002010-06-21 14:48:16 -04007207static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7208 u64 start, u64 num_bytes, u64 min_size,
7209 loff_t actual_len, u64 *alloc_hint,
7210 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007211{
Yan Zhengd899e052008-10-30 14:25:28 -04007212 struct btrfs_root *root = BTRFS_I(inode)->root;
7213 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007214 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007215 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007216 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007217 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007218
Josef Bacik0af3d002010-06-21 14:48:16 -04007219 if (trans)
7220 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007221 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007222 if (own_trans) {
7223 trans = btrfs_start_transaction(root, 3);
7224 if (IS_ERR(trans)) {
7225 ret = PTR_ERR(trans);
7226 break;
7227 }
Yan Zhengd899e052008-10-30 14:25:28 -04007228 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007229
Yan, Zhengefa56462010-05-16 10:49:59 -04007230 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7231 0, *alloc_hint, (u64)-1, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007232 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007233 if (own_trans)
7234 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007235 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007236 }
7237
Yan Zhengd899e052008-10-30 14:25:28 -04007238 ret = insert_reserved_file_extent(trans, inode,
7239 cur_offset, ins.objectid,
7240 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007241 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007242 BTRFS_FILE_EXTENT_PREALLOC);
7243 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04007244 btrfs_drop_extent_cache(inode, cur_offset,
7245 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007246
Yan Zhengd899e052008-10-30 14:25:28 -04007247 num_bytes -= ins.offset;
7248 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007249 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007250
Yan Zhengd899e052008-10-30 14:25:28 -04007251 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007252 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007253 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007254 (actual_len > inode->i_size) &&
7255 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007256 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007257 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007258 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007259 i_size = cur_offset;
7260 i_size_write(inode, i_size);
7261 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007262 }
7263
Yan Zhengd899e052008-10-30 14:25:28 -04007264 ret = btrfs_update_inode(trans, root, inode);
7265 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04007266
Josef Bacik0af3d002010-06-21 14:48:16 -04007267 if (own_trans)
7268 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007269 }
Yan Zhengd899e052008-10-30 14:25:28 -04007270 return ret;
7271}
7272
Josef Bacik0af3d002010-06-21 14:48:16 -04007273int btrfs_prealloc_file_range(struct inode *inode, int mode,
7274 u64 start, u64 num_bytes, u64 min_size,
7275 loff_t actual_len, u64 *alloc_hint)
7276{
7277 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7278 min_size, actual_len, alloc_hint,
7279 NULL);
7280}
7281
7282int btrfs_prealloc_file_range_trans(struct inode *inode,
7283 struct btrfs_trans_handle *trans, int mode,
7284 u64 start, u64 num_bytes, u64 min_size,
7285 loff_t actual_len, u64 *alloc_hint)
7286{
7287 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7288 min_size, actual_len, alloc_hint, trans);
7289}
7290
Chris Masone6dcd2d2008-07-17 12:53:50 -04007291static int btrfs_set_page_dirty(struct page *page)
7292{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007293 return __set_page_dirty_nobuffers(page);
7294}
7295
Al Viro10556cb2011-06-20 19:28:19 -04007296static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007297{
Li Zefanb83cc962010-12-20 16:04:08 +08007298 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007299 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007300
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007301 if (mask & MAY_WRITE &&
7302 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7303 if (btrfs_root_readonly(root))
7304 return -EROFS;
7305 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7306 return -EACCES;
7307 }
Al Viro2830ba72011-06-20 19:16:29 -04007308 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007309}
Chris Mason39279cc2007-06-12 06:35:45 -04007310
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007311static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007312 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007313 .lookup = btrfs_lookup,
7314 .create = btrfs_create,
7315 .unlink = btrfs_unlink,
7316 .link = btrfs_link,
7317 .mkdir = btrfs_mkdir,
7318 .rmdir = btrfs_rmdir,
7319 .rename = btrfs_rename,
7320 .symlink = btrfs_symlink,
7321 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007322 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007323 .setxattr = btrfs_setxattr,
7324 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007325 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007326 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007327 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007328 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007329};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007330static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007331 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007332 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007333 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007334};
Yan, Zheng76dda932009-09-21 16:00:26 -04007335
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007336static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007337 .llseek = generic_file_llseek,
7338 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007339 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007340 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007341#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007342 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007343#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007344 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007345 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007346};
7347
Chris Masond1310b22008-01-24 16:13:08 -05007348static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007349 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007350 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007351 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007352 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007353 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007354 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007355 .set_bit_hook = btrfs_set_bit_hook,
7356 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007357 .merge_extent_hook = btrfs_merge_extent_hook,
7358 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007359};
7360
Chris Mason35054392009-01-21 13:11:13 -05007361/*
7362 * btrfs doesn't support the bmap operation because swapfiles
7363 * use bmap to make a mapping of extents in the file. They assume
7364 * these extents won't change over the life of the file and they
7365 * use the bmap result to do IO directly to the drive.
7366 *
7367 * the btrfs bmap call would return logical addresses that aren't
7368 * suitable for IO and they also will change frequently as COW
7369 * operations happen. So, swapfile + btrfs == corruption.
7370 *
7371 * For now we're avoiding this by dropping bmap.
7372 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007373static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007374 .readpage = btrfs_readpage,
7375 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007376 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007377 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007378 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007379 .invalidatepage = btrfs_invalidatepage,
7380 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007381 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007382 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007383};
7384
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007385static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007386 .readpage = btrfs_readpage,
7387 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007388 .invalidatepage = btrfs_invalidatepage,
7389 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007390};
7391
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007392static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007393 .getattr = btrfs_getattr,
7394 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007395 .setxattr = btrfs_setxattr,
7396 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007397 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007398 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007399 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007400 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007401 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007402};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007403static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007404 .getattr = btrfs_getattr,
7405 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007406 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007407 .setxattr = btrfs_setxattr,
7408 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007409 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007410 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007411 .get_acl = btrfs_get_acl,
Josef Bacik618e21d2007-07-11 10:18:17 -04007412};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007413static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007414 .readlink = generic_readlink,
7415 .follow_link = page_follow_link_light,
7416 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007417 .getattr = btrfs_getattr,
Yanfdebe2b2008-01-14 13:26:08 -05007418 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007419 .setxattr = btrfs_setxattr,
7420 .getxattr = btrfs_getxattr,
7421 .listxattr = btrfs_listxattr,
7422 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007423 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007424};
Yan, Zheng76dda932009-09-21 16:00:26 -04007425
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007426const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007427 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007428 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04007429};