blob: 3c1723a9ae6f558456bb6e4ee2baba93413c6691 [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>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050042#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040043#include "ctree.h"
44#include "disk-io.h"
45#include "transaction.h"
46#include "btrfs_inode.h"
47#include "ioctl.h"
48#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040049#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040050#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040051#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020052#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040053#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050054#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050055#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080056#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040057
58struct btrfs_iget_args {
59 u64 ino;
60 struct btrfs_root *root;
61};
62
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070063static const struct inode_operations btrfs_dir_inode_operations;
64static const struct inode_operations btrfs_symlink_inode_operations;
65static const struct inode_operations btrfs_dir_ro_inode_operations;
66static const struct inode_operations btrfs_special_inode_operations;
67static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070068static const struct address_space_operations btrfs_aops;
69static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070070static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050071static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
74struct kmem_cache *btrfs_trans_handle_cachep;
75struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078
79#define S_SHIFT 12
80static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
81 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
82 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
83 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
84 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
85 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
86 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
87 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
88};
89
Josef Bacika41ad392011-01-31 15:30:16 -050090static int btrfs_setsize(struct inode *inode, loff_t newsize);
91static int btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040092static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050093static noinline int cow_file_range(struct inode *inode,
94 struct page *locked_page,
95 u64 start, u64 end, int *page_started,
96 unsigned long *nr_written, int unlock);
Chris Mason21151332011-11-10 20:39:08 -050097static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root, struct inode *inode);
Josef Bacik7b128762008-07-24 12:17:14 -040099
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000100static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500101 struct inode *inode, struct inode *dir,
102 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500103{
104 int err;
105
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000106 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500107 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500108 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500109 return err;
110}
111
Chris Masond352ac62008-09-29 15:18:18 -0400112/*
Chris Masonc8b97812008-10-29 14:49:59 -0400113 * this does all the hard work for inserting an inline extent into
114 * the btree. The caller should have done a btrfs_drop_extents so that
115 * no overlapping inline items exist in the btree
116 */
Chris Masond3977122009-01-05 21:25:51 -0500117static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400118 struct btrfs_root *root, struct inode *inode,
119 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000120 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400121 struct page **compressed_pages)
122{
123 struct btrfs_key key;
124 struct btrfs_path *path;
125 struct extent_buffer *leaf;
126 struct page *page = NULL;
127 char *kaddr;
128 unsigned long ptr;
129 struct btrfs_file_extent_item *ei;
130 int err = 0;
131 int ret;
132 size_t cur_size = size;
133 size_t datasize;
134 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400135
Li Zefanfe3f5662011-03-28 08:30:38 +0000136 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400137 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400138
Chris Masond3977122009-01-05 21:25:51 -0500139 path = btrfs_alloc_path();
140 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400141 return -ENOMEM;
142
Chris Masonb9473432009-03-13 11:00:37 -0400143 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefan33345d012011-04-20 10:31:50 +0800145 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400146 key.offset = start;
147 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400148 datasize = btrfs_file_extent_calc_inline_size(cur_size);
149
150 inode_add_bytes(inode, size);
151 ret = btrfs_insert_empty_item(trans, root, path, &key,
152 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400153 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
Cong Wang7ac687d2011-11-25 23:14:28 +0800175 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400176 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800177 kunmap_atomic(kaddr);
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);
Cong Wang7ac687d2011-11-25 23:14:28 +0800189 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400190 offset = start & (PAGE_CACHE_SIZE - 1);
191 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800192 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 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;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100208 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000209
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100210 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400211fail:
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);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100252 if (ret)
253 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400254
255 if (isize > actual_end)
256 inline_len = min_t(u64, isize, actual_end);
257 ret = insert_inline_extent(trans, root, inode, start,
258 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000259 compress_type, compressed_pages);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100260 if (ret) {
261 btrfs_abort_transaction(trans, root, ret);
262 return ret;
263 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400264 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400265 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400266 return 0;
267}
268
Chris Mason771ed682008-11-06 22:02:51 -0500269struct async_extent {
270 u64 start;
271 u64 ram_size;
272 u64 compressed_size;
273 struct page **pages;
274 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800275 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500276 struct list_head list;
277};
278
279struct async_cow {
280 struct inode *inode;
281 struct btrfs_root *root;
282 struct page *locked_page;
283 u64 start;
284 u64 end;
285 struct list_head extents;
286 struct btrfs_work work;
287};
288
289static noinline int add_async_extent(struct async_cow *cow,
290 u64 start, u64 ram_size,
291 u64 compressed_size,
292 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800293 unsigned long nr_pages,
294 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500295{
296 struct async_extent *async_extent;
297
298 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100299 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500300 async_extent->start = start;
301 async_extent->ram_size = ram_size;
302 async_extent->compressed_size = compressed_size;
303 async_extent->pages = pages;
304 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800305 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500306 list_add_tail(&async_extent->list, &cow->extents);
307 return 0;
308}
309
Chris Masonc8b97812008-10-29 14:49:59 -0400310/*
Chris Mason771ed682008-11-06 22:02:51 -0500311 * we create compressed extents in two phases. The first
312 * phase compresses a range of pages that have already been
313 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400314 *
Chris Mason771ed682008-11-06 22:02:51 -0500315 * This is done inside an ordered work queue, and the compression
316 * is spread across many cpus. The actual IO submission is step
317 * two, and the ordered work queue takes care of making sure that
318 * happens in the same order things were put onto the queue by
319 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400320 *
Chris Mason771ed682008-11-06 22:02:51 -0500321 * If this code finds it can't get good compression, it puts an
322 * entry onto the work queue to write the uncompressed bytes. This
323 * makes sure that both compressed inodes and uncompressed inodes
324 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400325 */
Chris Mason771ed682008-11-06 22:02:51 -0500326static noinline int compress_file_range(struct inode *inode,
327 struct page *locked_page,
328 u64 start, u64 end,
329 struct async_cow *async_cow,
330 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400331{
332 struct btrfs_root *root = BTRFS_I(inode)->root;
333 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400334 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400335 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400336 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500337 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400338 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400339 struct page **pages = NULL;
340 unsigned long nr_pages;
341 unsigned long nr_pages_ret = 0;
342 unsigned long total_compressed = 0;
343 unsigned long total_in = 0;
344 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500345 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400346 int i;
347 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800348 int compress_type = root->fs_info->compress_type;
Chris Masonb888db2b2007-08-27 16:49:44 -0400349
Liu Bo4cb13e52012-03-29 09:57:45 -0400350 /* if this is a small write inside eof, kick off a defrag */
351 if ((end - start + 1) < 16 * 1024 &&
352 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400353 btrfs_add_inode_defrag(NULL, inode);
354
Chris Mason42dc7ba2008-12-15 11:44:56 -0500355 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400356again:
357 will_compress = 0;
358 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
359 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
360
Chris Masonf03d9301f2009-02-04 09:31:06 -0500361 /*
362 * we don't want to send crud past the end of i_size through
363 * compression, that's just a waste of CPU time. So, if the
364 * end of the file is before the start of our current
365 * requested range of bytes, we bail out to the uncompressed
366 * cleanup code that can deal with all of this.
367 *
368 * It isn't really the fastest way to fix things, but this is a
369 * very uncommon corner.
370 */
371 if (actual_end <= start)
372 goto cleanup_and_bail_uncompressed;
373
Chris Masonc8b97812008-10-29 14:49:59 -0400374 total_compressed = actual_end - start;
375
376 /* we want to make sure that amount of ram required to uncompress
377 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500378 * of a compressed extent to 128k. This is a crucial number
379 * because it also controls how easily we can spread reads across
380 * cpus for decompression.
381 *
382 * We also want to make sure the amount of IO required to do
383 * a random read is reasonably small, so we limit the size of
384 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400385 */
386 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400387 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500388 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400389 total_in = 0;
390 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400391
Chris Mason771ed682008-11-06 22:02:51 -0500392 /*
393 * we do compression for mount -o compress and when the
394 * inode has not been flagged as nocompress. This flag can
395 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400396 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200397 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500398 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000399 (BTRFS_I(inode)->force_compress) ||
400 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400401 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400402 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800403 if (!pages) {
404 /* just bail out to the uncompressed code */
405 goto cont;
406 }
Chris Mason179e29e2007-11-01 11:28:41 -0400407
Li Zefan261507a02010-12-17 14:21:50 +0800408 if (BTRFS_I(inode)->force_compress)
409 compress_type = BTRFS_I(inode)->force_compress;
410
411 ret = btrfs_compress_pages(compress_type,
412 inode->i_mapping, start,
413 total_compressed, pages,
414 nr_pages, &nr_pages_ret,
415 &total_in,
416 &total_compressed,
417 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400418
419 if (!ret) {
420 unsigned long offset = total_compressed &
421 (PAGE_CACHE_SIZE - 1);
422 struct page *page = pages[nr_pages_ret - 1];
423 char *kaddr;
424
425 /* zero the tail end of the last page, we might be
426 * sending it down to disk
427 */
428 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800429 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400430 memset(kaddr + offset, 0,
431 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800432 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400433 }
434 will_compress = 1;
435 }
436 }
Li Zefan560f7d72011-09-08 10:22:01 +0800437cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400438 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400439 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100440 if (IS_ERR(trans)) {
441 ret = PTR_ERR(trans);
442 trans = NULL;
443 goto cleanup_and_out;
444 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400445 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500446
Chris Masonc8b97812008-10-29 14:49:59 -0400447 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500448 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400449 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500450 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400451 */
452 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000453 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400454 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500455 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400456 ret = cow_file_range_inline(trans, root, inode,
457 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000458 total_compressed,
459 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400460 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100461 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500462 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100463 * inline extent creation worked or returned error,
464 * we don't need to create any more async work items.
465 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500466 */
Chris Masonc8b97812008-10-29 14:49:59 -0400467 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400468 &BTRFS_I(inode)->io_tree,
469 start, end, NULL,
470 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400471 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400472 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000473
474 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400475 goto free_pages_out;
476 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000477 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400478 }
479
480 if (will_compress) {
481 /*
482 * we aren't doing an inline extent round the compressed size
483 * up to a block size boundary so the allocator does sane
484 * things
485 */
486 total_compressed = (total_compressed + blocksize - 1) &
487 ~(blocksize - 1);
488
489 /*
490 * one last check to make sure the compression is really a
491 * win, compare the page count read with the blocks on disk
492 */
493 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
494 ~(PAGE_CACHE_SIZE - 1);
495 if (total_compressed >= total_in) {
496 will_compress = 0;
497 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400498 num_bytes = total_in;
499 }
500 }
501 if (!will_compress && pages) {
502 /*
503 * the compression code ran but failed to make things smaller,
504 * free any pages it allocated and our page pointer array
505 */
506 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400507 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400508 page_cache_release(pages[i]);
509 }
510 kfree(pages);
511 pages = NULL;
512 total_compressed = 0;
513 nr_pages_ret = 0;
514
515 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500516 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
517 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500518 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500519 }
Chris Masonc8b97812008-10-29 14:49:59 -0400520 }
Chris Mason771ed682008-11-06 22:02:51 -0500521 if (will_compress) {
522 *num_added += 1;
523
524 /* the async work queues will take care of doing actual
525 * allocation on disk for these compressed pages,
526 * and will submit them to the elevator.
527 */
528 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800529 total_compressed, pages, nr_pages_ret,
530 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500531
Yan, Zheng24ae6362010-12-06 07:02:36 +0000532 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500533 start += num_bytes;
534 pages = NULL;
535 cond_resched();
536 goto again;
537 }
538 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500539cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500540 /*
541 * No compression, but we still need to write the pages in
542 * the file we've been given so far. redirty the locked
543 * page if it corresponds to our extent and set things up
544 * for the async work queue to run cow_file_range to do
545 * the normal delalloc dance
546 */
547 if (page_offset(locked_page) >= start &&
548 page_offset(locked_page) <= end) {
549 __set_page_dirty_nobuffers(locked_page);
550 /* unlocked later on in the async handlers */
551 }
Li Zefan261507a02010-12-17 14:21:50 +0800552 add_async_extent(async_cow, start, end - start + 1,
553 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500554 *num_added += 1;
555 }
556
557out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100558 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500559
560free_pages_out:
561 for (i = 0; i < nr_pages_ret; i++) {
562 WARN_ON(pages[i]->mapping);
563 page_cache_release(pages[i]);
564 }
Chris Masond3977122009-01-05 21:25:51 -0500565 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500566
567 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100568
569cleanup_and_out:
570 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
571 start, end, NULL,
572 EXTENT_CLEAR_UNLOCK_PAGE |
573 EXTENT_CLEAR_DIRTY |
574 EXTENT_CLEAR_DELALLOC |
575 EXTENT_SET_WRITEBACK |
576 EXTENT_END_WRITEBACK);
577 if (!trans || IS_ERR(trans))
578 btrfs_error(root->fs_info, ret, "Failed to join transaction");
579 else
580 btrfs_abort_transaction(trans, root, ret);
581 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500582}
583
584/*
585 * phase two of compressed writeback. This is the ordered portion
586 * of the code, which only gets called in the order the work was
587 * queued. We walk all the async extents created by compress_file_range
588 * and send them down to the disk.
589 */
590static noinline int submit_compressed_extents(struct inode *inode,
591 struct async_cow *async_cow)
592{
593 struct async_extent *async_extent;
594 u64 alloc_hint = 0;
595 struct btrfs_trans_handle *trans;
596 struct btrfs_key ins;
597 struct extent_map *em;
598 struct btrfs_root *root = BTRFS_I(inode)->root;
599 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
600 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500601 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500602
603 if (list_empty(&async_cow->extents))
604 return 0;
605
Chris Mason771ed682008-11-06 22:02:51 -0500606
Chris Masond3977122009-01-05 21:25:51 -0500607 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500608 async_extent = list_entry(async_cow->extents.next,
609 struct async_extent, list);
610 list_del(&async_extent->list);
611
612 io_tree = &BTRFS_I(inode)->io_tree;
613
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500614retry:
Chris Mason771ed682008-11-06 22:02:51 -0500615 /* did the compression code fall back to uncompressed IO? */
616 if (!async_extent->pages) {
617 int page_started = 0;
618 unsigned long nr_written = 0;
619
620 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000621 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100622 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500623
624 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500625 ret = cow_file_range(inode, async_cow->locked_page,
626 async_extent->start,
627 async_extent->start +
628 async_extent->ram_size - 1,
629 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500630
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100631 /* JDM XXX */
632
Chris Mason771ed682008-11-06 22:02:51 -0500633 /*
634 * if page_started, cow_file_range inserted an
635 * inline extent and took care of all the unlocking
636 * and IO for us. Otherwise, we need to submit
637 * all those pages down to the drive.
638 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500639 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500640 extent_write_locked_range(io_tree,
641 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500642 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500643 async_extent->ram_size - 1,
644 btrfs_get_extent,
645 WB_SYNC_ALL);
646 kfree(async_extent);
647 cond_resched();
648 continue;
649 }
650
651 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100652 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500653
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400654 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100655 if (IS_ERR(trans)) {
656 ret = PTR_ERR(trans);
657 } else {
658 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
659 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500660 async_extent->compressed_size,
661 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500662 0, alloc_hint, &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100663 if (ret)
664 btrfs_abort_transaction(trans, root, ret);
665 btrfs_end_transaction(trans, root);
666 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000667
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500668 if (ret) {
669 int i;
670 for (i = 0; i < async_extent->nr_pages; i++) {
671 WARN_ON(async_extent->pages[i]->mapping);
672 page_cache_release(async_extent->pages[i]);
673 }
674 kfree(async_extent->pages);
675 async_extent->nr_pages = 0;
676 async_extent->pages = NULL;
677 unlock_extent(io_tree, async_extent->start,
678 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100679 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100680 if (ret == -ENOSPC)
681 goto retry;
682 goto out_free; /* JDM: Requeue? */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500683 }
684
Yan, Zhengc2167752009-11-12 09:34:21 +0000685 /*
686 * here we're doing allocation and writeback of the
687 * compressed pages
688 */
689 btrfs_drop_extent_cache(inode, async_extent->start,
690 async_extent->start +
691 async_extent->ram_size - 1, 0);
692
David Sterba172ddd62011-04-21 00:48:27 +0200693 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100694 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500695 em->start = async_extent->start;
696 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500697 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500698
699 em->block_start = ins.objectid;
700 em->block_len = ins.offset;
701 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800702 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500703 set_bit(EXTENT_FLAG_PINNED, &em->flags);
704 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
705
Chris Masond3977122009-01-05 21:25:51 -0500706 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400707 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500708 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400709 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500710 if (ret != -EEXIST) {
711 free_extent_map(em);
712 break;
713 }
714 btrfs_drop_extent_cache(inode, async_extent->start,
715 async_extent->start +
716 async_extent->ram_size - 1, 0);
717 }
718
Li Zefan261507a02010-12-17 14:21:50 +0800719 ret = btrfs_add_ordered_extent_compress(inode,
720 async_extent->start,
721 ins.objectid,
722 async_extent->ram_size,
723 ins.offset,
724 BTRFS_ORDERED_COMPRESSED,
725 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100726 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500727
Chris Mason771ed682008-11-06 22:02:51 -0500728 /*
729 * clear dirty, set writeback and unlock the pages.
730 */
731 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400732 &BTRFS_I(inode)->io_tree,
733 async_extent->start,
734 async_extent->start +
735 async_extent->ram_size - 1,
736 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
737 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400738 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400739 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500740
741 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500742 async_extent->start,
743 async_extent->ram_size,
744 ins.objectid,
745 ins.offset, async_extent->pages,
746 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500747
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100748 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500749 alloc_hint = ins.objectid + ins.offset;
750 kfree(async_extent);
751 cond_resched();
752 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100753 ret = 0;
754out:
755 return ret;
756out_free:
757 kfree(async_extent);
758 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500759}
760
Josef Bacik4b46fce2010-05-23 11:00:55 -0400761static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
762 u64 num_bytes)
763{
764 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
765 struct extent_map *em;
766 u64 alloc_hint = 0;
767
768 read_lock(&em_tree->lock);
769 em = search_extent_mapping(em_tree, start, num_bytes);
770 if (em) {
771 /*
772 * if block start isn't an actual block number then find the
773 * first block in this inode and use that as a hint. If that
774 * block is also bogus then just don't worry about it.
775 */
776 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
777 free_extent_map(em);
778 em = search_extent_mapping(em_tree, 0, 0);
779 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
780 alloc_hint = em->block_start;
781 if (em)
782 free_extent_map(em);
783 } else {
784 alloc_hint = em->block_start;
785 free_extent_map(em);
786 }
787 }
788 read_unlock(&em_tree->lock);
789
790 return alloc_hint;
791}
792
Chris Mason771ed682008-11-06 22:02:51 -0500793/*
794 * when extent_io.c finds a delayed allocation range in the file,
795 * the call backs end up in this code. The basic idea is to
796 * allocate extents on disk for the range, and create ordered data structs
797 * in ram to track those extents.
798 *
799 * locked_page is the page that writepage had locked already. We use
800 * it to make sure we don't do extra locks or unlocks.
801 *
802 * *page_started is set to one if we unlock locked_page and do everything
803 * required to start IO on it. It may be clean and already done with
804 * IO when we return.
805 */
806static noinline int cow_file_range(struct inode *inode,
807 struct page *locked_page,
808 u64 start, u64 end, int *page_started,
809 unsigned long *nr_written,
810 int unlock)
811{
812 struct btrfs_root *root = BTRFS_I(inode)->root;
813 struct btrfs_trans_handle *trans;
814 u64 alloc_hint = 0;
815 u64 num_bytes;
816 unsigned long ram_size;
817 u64 disk_num_bytes;
818 u64 cur_alloc_size;
819 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500820 struct btrfs_key ins;
821 struct extent_map *em;
822 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
823 int ret = 0;
824
Chris Mason2cf85722011-07-26 15:35:09 -0400825 BUG_ON(btrfs_is_free_space_inode(root, inode));
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400826 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100827 if (IS_ERR(trans)) {
828 extent_clear_unlock_delalloc(inode,
829 &BTRFS_I(inode)->io_tree,
830 start, end, NULL,
831 EXTENT_CLEAR_UNLOCK_PAGE |
832 EXTENT_CLEAR_UNLOCK |
833 EXTENT_CLEAR_DELALLOC |
834 EXTENT_CLEAR_DIRTY |
835 EXTENT_SET_WRITEBACK |
836 EXTENT_END_WRITEBACK);
837 return PTR_ERR(trans);
838 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400839 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500840
Chris Mason771ed682008-11-06 22:02:51 -0500841 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
842 num_bytes = max(blocksize, num_bytes);
843 disk_num_bytes = num_bytes;
844 ret = 0;
845
Chris Mason4cb53002011-05-24 15:35:30 -0400846 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400847 if (num_bytes < 64 * 1024 &&
848 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400849 btrfs_add_inode_defrag(trans, inode);
850
Chris Mason771ed682008-11-06 22:02:51 -0500851 if (start == 0) {
852 /* lets try to make an inline extent */
853 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000854 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500855 if (ret == 0) {
856 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400857 &BTRFS_I(inode)->io_tree,
858 start, end, NULL,
859 EXTENT_CLEAR_UNLOCK_PAGE |
860 EXTENT_CLEAR_UNLOCK |
861 EXTENT_CLEAR_DELALLOC |
862 EXTENT_CLEAR_DIRTY |
863 EXTENT_SET_WRITEBACK |
864 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000865
Chris Mason771ed682008-11-06 22:02:51 -0500866 *nr_written = *nr_written +
867 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
868 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500869 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100870 } else if (ret < 0) {
871 btrfs_abort_transaction(trans, root, ret);
872 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500873 }
874 }
Chris Masonc8b97812008-10-29 14:49:59 -0400875
876 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200877 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400878
Josef Bacik4b46fce2010-05-23 11:00:55 -0400879 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400880 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400881
Chris Masond3977122009-01-05 21:25:51 -0500882 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400883 unsigned long op;
884
Josef Bacik287a0ab2010-03-19 18:07:23 +0000885 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400886 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500887 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500888 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100889 if (ret < 0) {
890 btrfs_abort_transaction(trans, root, ret);
891 goto out_unlock;
892 }
Chris Masond3977122009-01-05 21:25:51 -0500893
David Sterba172ddd62011-04-21 00:48:27 +0200894 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100895 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400896 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500897 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500898 ram_size = ins.offset;
899 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400900
Chris Masone6dcd2d2008-07-17 12:53:50 -0400901 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400902 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400903 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400904 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400905
Chris Masond3977122009-01-05 21:25:51 -0500906 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400907 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400908 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400909 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400910 if (ret != -EEXIST) {
911 free_extent_map(em);
912 break;
913 }
914 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400915 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400916 }
917
Chris Mason98d20f62008-04-14 09:46:10 -0400918 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400919 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500920 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100921 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400922
Yan Zheng17d217f2008-12-12 10:03:38 -0500923 if (root->root_key.objectid ==
924 BTRFS_DATA_RELOC_TREE_OBJECTID) {
925 ret = btrfs_reloc_clone_csums(inode, start,
926 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100927 if (ret) {
928 btrfs_abort_transaction(trans, root, ret);
929 goto out_unlock;
930 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500931 }
932
Chris Masond3977122009-01-05 21:25:51 -0500933 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400934 break;
Chris Masond3977122009-01-05 21:25:51 -0500935
Chris Masonc8b97812008-10-29 14:49:59 -0400936 /* we're not doing compressed IO, don't unlock the first
937 * page (which the caller expects to stay locked), don't
938 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400939 *
940 * Do set the Private2 bit so we know this page was properly
941 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400942 */
Chris Masona791e352009-10-08 11:27:10 -0400943 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
944 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
945 EXTENT_SET_PRIVATE2;
946
Chris Masonc8b97812008-10-29 14:49:59 -0400947 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
948 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400949 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400950 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500951 num_bytes -= cur_alloc_size;
952 alloc_hint = ins.objectid + ins.offset;
953 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400954 }
Chris Mason771ed682008-11-06 22:02:51 -0500955 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100956out:
Chris Masonb888db2b2007-08-27 16:49:44 -0400957 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400958
Chris Masonbe20aa92007-12-17 20:14:01 -0500959 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100960out_unlock:
961 extent_clear_unlock_delalloc(inode,
962 &BTRFS_I(inode)->io_tree,
963 start, end, NULL,
964 EXTENT_CLEAR_UNLOCK_PAGE |
965 EXTENT_CLEAR_UNLOCK |
966 EXTENT_CLEAR_DELALLOC |
967 EXTENT_CLEAR_DIRTY |
968 EXTENT_SET_WRITEBACK |
969 EXTENT_END_WRITEBACK);
970
971 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500972}
Chris Masonc8b97812008-10-29 14:49:59 -0400973
Chris Mason771ed682008-11-06 22:02:51 -0500974/*
975 * work queue call back to started compression on a file and pages
976 */
977static noinline void async_cow_start(struct btrfs_work *work)
978{
979 struct async_cow *async_cow;
980 int num_added = 0;
981 async_cow = container_of(work, struct async_cow, work);
982
983 compress_file_range(async_cow->inode, async_cow->locked_page,
984 async_cow->start, async_cow->end, async_cow,
985 &num_added);
986 if (num_added == 0)
987 async_cow->inode = NULL;
988}
989
990/*
991 * work queue call back to submit previously compressed pages
992 */
993static noinline void async_cow_submit(struct btrfs_work *work)
994{
995 struct async_cow *async_cow;
996 struct btrfs_root *root;
997 unsigned long nr_pages;
998
999 async_cow = container_of(work, struct async_cow, work);
1000
1001 root = async_cow->root;
1002 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1003 PAGE_CACHE_SHIFT;
1004
1005 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
1006
1007 if (atomic_read(&root->fs_info->async_delalloc_pages) <
1008 5 * 1042 * 1024 &&
1009 waitqueue_active(&root->fs_info->async_submit_wait))
1010 wake_up(&root->fs_info->async_submit_wait);
1011
Chris Masond3977122009-01-05 21:25:51 -05001012 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001013 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001014}
Chris Masonc8b97812008-10-29 14:49:59 -04001015
Chris Mason771ed682008-11-06 22:02:51 -05001016static noinline void async_cow_free(struct btrfs_work *work)
1017{
1018 struct async_cow *async_cow;
1019 async_cow = container_of(work, struct async_cow, work);
1020 kfree(async_cow);
1021}
1022
1023static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1024 u64 start, u64 end, int *page_started,
1025 unsigned long *nr_written)
1026{
1027 struct async_cow *async_cow;
1028 struct btrfs_root *root = BTRFS_I(inode)->root;
1029 unsigned long nr_pages;
1030 u64 cur_end;
1031 int limit = 10 * 1024 * 1042;
1032
Chris Masona3429ab2009-10-08 12:30:20 -04001033 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1034 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001035 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001036 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001037 BUG_ON(!async_cow); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001038 async_cow->inode = inode;
1039 async_cow->root = root;
1040 async_cow->locked_page = locked_page;
1041 async_cow->start = start;
1042
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001043 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001044 cur_end = end;
1045 else
1046 cur_end = min(end, start + 512 * 1024 - 1);
1047
1048 async_cow->end = cur_end;
1049 INIT_LIST_HEAD(&async_cow->extents);
1050
1051 async_cow->work.func = async_cow_start;
1052 async_cow->work.ordered_func = async_cow_submit;
1053 async_cow->work.ordered_free = async_cow_free;
1054 async_cow->work.flags = 0;
1055
Chris Mason771ed682008-11-06 22:02:51 -05001056 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1057 PAGE_CACHE_SHIFT;
1058 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1059
1060 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1061 &async_cow->work);
1062
1063 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1064 wait_event(root->fs_info->async_submit_wait,
1065 (atomic_read(&root->fs_info->async_delalloc_pages) <
1066 limit));
1067 }
1068
Chris Masond3977122009-01-05 21:25:51 -05001069 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001070 atomic_read(&root->fs_info->async_delalloc_pages)) {
1071 wait_event(root->fs_info->async_submit_wait,
1072 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1073 0));
1074 }
1075
1076 *nr_written += nr_pages;
1077 start = cur_end + 1;
1078 }
1079 *page_started = 1;
1080 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001081}
1082
Chris Masond3977122009-01-05 21:25:51 -05001083static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001084 u64 bytenr, u64 num_bytes)
1085{
1086 int ret;
1087 struct btrfs_ordered_sum *sums;
1088 LIST_HEAD(list);
1089
Yan Zheng07d400a2009-01-06 11:42:00 -05001090 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001091 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001092 if (ret == 0 && list_empty(&list))
1093 return 0;
1094
1095 while (!list_empty(&list)) {
1096 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1097 list_del(&sums->list);
1098 kfree(sums);
1099 }
1100 return 1;
1101}
1102
Chris Masond352ac62008-09-29 15:18:18 -04001103/*
1104 * when nowcow writeback call back. This checks for snapshots or COW copies
1105 * of the extents that exist in the file, and COWs the file as required.
1106 *
1107 * If no cow copies or snapshots exist, we write directly to the existing
1108 * blocks on disk
1109 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001110static noinline int run_delalloc_nocow(struct inode *inode,
1111 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001112 u64 start, u64 end, int *page_started, int force,
1113 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001114{
Chris Masonbe20aa92007-12-17 20:14:01 -05001115 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001116 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001117 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001118 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001119 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001120 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001121 u64 cow_start;
1122 u64 cur_offset;
1123 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001124 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001125 u64 disk_bytenr;
1126 u64 num_bytes;
1127 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001128 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001129 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001130 int nocow;
1131 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001132 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001133 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001134
1135 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001136 if (!path)
1137 return -ENOMEM;
Li Zefan82d59022011-04-20 10:33:24 +08001138
Chris Mason2cf85722011-07-26 15:35:09 -04001139 nolock = btrfs_is_free_space_inode(root, inode);
Li Zefan82d59022011-04-20 10:33:24 +08001140
1141 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001142 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001143 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001144 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001145
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001146 if (IS_ERR(trans)) {
1147 btrfs_free_path(path);
1148 return PTR_ERR(trans);
1149 }
1150
Josef Bacik74b21072011-04-13 12:02:53 -04001151 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001152
Yan Zheng80ff3852008-10-30 14:20:02 -04001153 cow_start = (u64)-1;
1154 cur_offset = start;
1155 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001156 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001157 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001158 if (ret < 0) {
1159 btrfs_abort_transaction(trans, root, ret);
1160 goto error;
1161 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001162 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1163 leaf = path->nodes[0];
1164 btrfs_item_key_to_cpu(leaf, &found_key,
1165 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001166 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001167 found_key.type == BTRFS_EXTENT_DATA_KEY)
1168 path->slots[0]--;
1169 }
1170 check_prev = 0;
1171next_slot:
1172 leaf = path->nodes[0];
1173 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1174 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001175 if (ret < 0) {
1176 btrfs_abort_transaction(trans, root, ret);
1177 goto error;
1178 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001179 if (ret > 0)
1180 break;
1181 leaf = path->nodes[0];
1182 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001183
Yan Zheng80ff3852008-10-30 14:20:02 -04001184 nocow = 0;
1185 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001186 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001187 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001188
Li Zefan33345d012011-04-20 10:31:50 +08001189 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001190 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1191 found_key.offset > end)
1192 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001193
Yan Zheng80ff3852008-10-30 14:20:02 -04001194 if (found_key.offset > cur_offset) {
1195 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001196 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001197 goto out_check;
1198 }
Chris Masonc31f8832008-01-08 15:46:31 -05001199
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 fi = btrfs_item_ptr(leaf, path->slots[0],
1201 struct btrfs_file_extent_item);
1202 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001203
Yan Zhengd899e052008-10-30 14:25:28 -04001204 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1205 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001206 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001207 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 extent_end = found_key.offset +
1209 btrfs_file_extent_num_bytes(leaf, fi);
1210 if (extent_end <= start) {
1211 path->slots[0]++;
1212 goto next_slot;
1213 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001214 if (disk_bytenr == 0)
1215 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 if (btrfs_file_extent_compression(leaf, fi) ||
1217 btrfs_file_extent_encryption(leaf, fi) ||
1218 btrfs_file_extent_other_encoding(leaf, fi))
1219 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001220 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1221 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001222 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001223 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001224 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001225 found_key.offset -
1226 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001227 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001228 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001229 disk_bytenr += cur_offset - found_key.offset;
1230 num_bytes = min(end + 1, extent_end) - cur_offset;
1231 /*
1232 * force cow if csum exists in the range.
1233 * this ensure that csum for a given extent are
1234 * either valid or do not exist.
1235 */
1236 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1237 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 nocow = 1;
1239 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1240 extent_end = found_key.offset +
1241 btrfs_file_extent_inline_len(leaf, fi);
1242 extent_end = ALIGN(extent_end, root->sectorsize);
1243 } else {
1244 BUG_ON(1);
1245 }
1246out_check:
1247 if (extent_end <= start) {
1248 path->slots[0]++;
1249 goto next_slot;
1250 }
1251 if (!nocow) {
1252 if (cow_start == (u64)-1)
1253 cow_start = cur_offset;
1254 cur_offset = extent_end;
1255 if (cur_offset > end)
1256 break;
1257 path->slots[0]++;
1258 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001259 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001260
David Sterbab3b4aa72011-04-21 01:20:15 +02001261 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 if (cow_start != (u64)-1) {
1263 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001264 found_key.offset - 1, page_started,
1265 nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001266 if (ret) {
1267 btrfs_abort_transaction(trans, root, ret);
1268 goto error;
1269 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001271 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001272
Yan Zhengd899e052008-10-30 14:25:28 -04001273 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1274 struct extent_map *em;
1275 struct extent_map_tree *em_tree;
1276 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001277 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001278 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001279 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001280 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001281 em->len = num_bytes;
1282 em->block_len = num_bytes;
1283 em->block_start = disk_bytenr;
1284 em->bdev = root->fs_info->fs_devices->latest_bdev;
1285 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1286 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001287 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001288 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001289 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001290 if (ret != -EEXIST) {
1291 free_extent_map(em);
1292 break;
1293 }
1294 btrfs_drop_extent_cache(inode, em->start,
1295 em->start + em->len - 1, 0);
1296 }
1297 type = BTRFS_ORDERED_PREALLOC;
1298 } else {
1299 type = BTRFS_ORDERED_NOCOW;
1300 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001301
1302 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001303 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001304 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001305
Yan, Zhengefa56462010-05-16 10:49:59 -04001306 if (root->root_key.objectid ==
1307 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1308 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1309 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001310 if (ret) {
1311 btrfs_abort_transaction(trans, root, ret);
1312 goto error;
1313 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001314 }
1315
Yan Zhengd899e052008-10-30 14:25:28 -04001316 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001317 cur_offset, cur_offset + num_bytes - 1,
1318 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1319 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1320 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 cur_offset = extent_end;
1322 if (cur_offset > end)
1323 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001324 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001325 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001326
1327 if (cur_offset <= end && cow_start == (u64)-1)
1328 cow_start = cur_offset;
1329 if (cow_start != (u64)-1) {
1330 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001331 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001332 if (ret) {
1333 btrfs_abort_transaction(trans, root, ret);
1334 goto error;
1335 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 }
1337
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001338error:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001339 if (nolock) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001340 err = btrfs_end_transaction_nolock(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001341 } else {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001342 err = btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001343 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001344 if (!ret)
1345 ret = err;
1346
Yan Zheng7ea394f2008-08-05 13:05:02 -04001347 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001348 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001349}
1350
Chris Masond352ac62008-09-29 15:18:18 -04001351/*
1352 * extent_io.c call back to do delayed allocation processing
1353 */
Chris Masonc8b97812008-10-29 14:49:59 -04001354static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001355 u64 start, u64 end, int *page_started,
1356 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001357{
Chris Masonbe20aa92007-12-17 20:14:01 -05001358 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001359 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001360
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001361 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001362 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001363 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001364 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001365 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001366 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001367 else if (!btrfs_test_opt(root, COMPRESS) &&
Liu Bo75e7cb72011-03-22 10:12:20 +00001368 !(BTRFS_I(inode)->force_compress) &&
1369 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
Chris Mason7f366cf2009-03-12 20:12:45 -04001370 ret = cow_file_range(inode, locked_page, start, end,
1371 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001372 else
Chris Mason771ed682008-11-06 22:02:51 -05001373 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001374 page_started, nr_written);
Chris Masonb888db2b2007-08-27 16:49:44 -04001375 return ret;
1376}
1377
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001378static void btrfs_split_extent_hook(struct inode *inode,
1379 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001380{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001381 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001382 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001383 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001384
Josef Bacik9e0baf62011-07-15 15:16:44 +00001385 spin_lock(&BTRFS_I(inode)->lock);
1386 BTRFS_I(inode)->outstanding_extents++;
1387 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001388}
1389
1390/*
1391 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1392 * extents so we can keep track of new extents that are just merged onto old
1393 * extents, such as when we are doing sequential writes, so we can properly
1394 * account for the metadata space we'll need.
1395 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001396static void btrfs_merge_extent_hook(struct inode *inode,
1397 struct extent_state *new,
1398 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001399{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001400 /* not delalloc, ignore it */
1401 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001402 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001403
Josef Bacik9e0baf62011-07-15 15:16:44 +00001404 spin_lock(&BTRFS_I(inode)->lock);
1405 BTRFS_I(inode)->outstanding_extents--;
1406 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001407}
1408
Chris Masond352ac62008-09-29 15:18:18 -04001409/*
1410 * extent_io.c set_bit_hook, used to track delayed allocation
1411 * bytes in this file, and to maintain the list of inodes that
1412 * have pending delalloc work to be done.
1413 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001414static void btrfs_set_bit_hook(struct inode *inode,
1415 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001416{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001417
Chris Mason75eff682008-12-15 15:54:40 -05001418 /*
1419 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001420 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001421 * bit, which is only set or cleared with irqs on
1422 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001423 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001424 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001425 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001426 bool do_list = !btrfs_is_free_space_inode(root, inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001427
Josef Bacik9e0baf62011-07-15 15:16:44 +00001428 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001429 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001430 } else {
1431 spin_lock(&BTRFS_I(inode)->lock);
1432 BTRFS_I(inode)->outstanding_extents++;
1433 spin_unlock(&BTRFS_I(inode)->lock);
1434 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001435
Chris Mason75eff682008-12-15 15:54:40 -05001436 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001437 BTRFS_I(inode)->delalloc_bytes += len;
1438 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001439 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001440 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1441 &root->fs_info->delalloc_inodes);
1442 }
Chris Mason75eff682008-12-15 15:54:40 -05001443 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001444 }
Chris Mason291d6732008-01-29 15:55:23 -05001445}
1446
Chris Masond352ac62008-09-29 15:18:18 -04001447/*
1448 * extent_io.c clear_bit_hook, see set_bit_hook for why
1449 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001450static void btrfs_clear_bit_hook(struct inode *inode,
1451 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001452{
Chris Mason75eff682008-12-15 15:54:40 -05001453 /*
1454 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001455 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001456 * bit, which is only set or cleared with irqs on
1457 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001458 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001459 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001460 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001461 bool do_list = !btrfs_is_free_space_inode(root, inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001462
Josef Bacik9e0baf62011-07-15 15:16:44 +00001463 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001464 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001465 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1466 spin_lock(&BTRFS_I(inode)->lock);
1467 BTRFS_I(inode)->outstanding_extents--;
1468 spin_unlock(&BTRFS_I(inode)->lock);
1469 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001470
1471 if (*bits & EXTENT_DO_ACCOUNTING)
1472 btrfs_delalloc_release_metadata(inode, len);
1473
Josef Bacik0cb59c92010-07-02 12:14:14 -04001474 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1475 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001476 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001477
Chris Mason75eff682008-12-15 15:54:40 -05001478 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001479 root->fs_info->delalloc_bytes -= len;
1480 BTRFS_I(inode)->delalloc_bytes -= len;
1481
Josef Bacik0cb59c92010-07-02 12:14:14 -04001482 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001483 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1484 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1485 }
Chris Mason75eff682008-12-15 15:54:40 -05001486 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001487 }
Chris Mason291d6732008-01-29 15:55:23 -05001488}
1489
Chris Masond352ac62008-09-29 15:18:18 -04001490/*
1491 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1492 * we don't create bios that span stripes or chunks
1493 */
Chris Mason239b14b2008-03-24 15:02:07 -04001494int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001495 size_t size, struct bio *bio,
1496 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001497{
1498 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1499 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001500 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001501 u64 length = 0;
1502 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001503 int ret;
1504
Chris Mason771ed682008-11-06 22:02:51 -05001505 if (bio_flags & EXTENT_BIO_COMPRESSED)
1506 return 0;
1507
Chris Masonf2d8d742008-04-21 10:03:05 -04001508 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001509 map_tree = &root->fs_info->mapping_tree;
1510 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001511 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001512 &map_length, NULL, 0);
Jeff Mahoney3444a972011-10-03 23:23:13 -04001513 /* Will always return 0 or 1 with map_multi == NULL */
1514 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001515 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001516 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001517 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001518}
1519
Chris Masond352ac62008-09-29 15:18:18 -04001520/*
1521 * in order to insert checksums into the metadata in large chunks,
1522 * we wait until bio submission time. All the pages in the bio are
1523 * checksummed and sums are attached onto the ordered extent record.
1524 *
1525 * At IO completion time the cums attached on the ordered extent record
1526 * are inserted into the btree
1527 */
Chris Masond3977122009-01-05 21:25:51 -05001528static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1529 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001530 unsigned long bio_flags,
1531 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001532{
Chris Mason065631f2008-02-20 12:07:25 -05001533 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001534 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001535
Chris Masond20f7042008-12-08 16:58:54 -05001536 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001537 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001538 return 0;
1539}
Chris Masone0156402008-04-16 11:15:20 -04001540
Chris Mason4a69a412008-11-06 22:03:00 -05001541/*
1542 * in order to insert checksums into the metadata in large chunks,
1543 * we wait until bio submission time. All the pages in the bio are
1544 * checksummed and sums are attached onto the ordered extent record.
1545 *
1546 * At IO completion time the cums attached on the ordered extent record
1547 * are inserted into the btree
1548 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001549static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001550 int mirror_num, unsigned long bio_flags,
1551 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001552{
1553 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001554 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001555}
1556
Chris Masond352ac62008-09-29 15:18:18 -04001557/*
Chris Masoncad321a2008-12-17 14:51:42 -05001558 * extent_io.c submission hook. This does the right thing for csum calculation
1559 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001560 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001561static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001562 int mirror_num, unsigned long bio_flags,
1563 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001564{
1565 struct btrfs_root *root = BTRFS_I(inode)->root;
1566 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001567 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001568 int metadata = 0;
Chris Mason44b8bd72008-04-16 11:14:51 -04001569
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001570 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001571
Chris Mason2cf85722011-07-26 15:35:09 -04001572 if (btrfs_is_free_space_inode(root, inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001573 metadata = 2;
1574
1575 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
Jeff Mahoney355808c2011-10-03 23:23:14 -04001576 if (ret)
1577 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001578
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001579 if (!(rw & REQ_WRITE)) {
Chris Masond20f7042008-12-08 16:58:54 -05001580 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001581 return btrfs_submit_compressed_read(inode, bio,
1582 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001583 } else if (!skip_sum) {
1584 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1585 if (ret)
1586 return ret;
1587 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001588 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001589 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001590 /* csum items have already been cloned */
1591 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1592 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001593 /* we're doing a write, do the async checksumming */
1594 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001595 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001596 bio_flags, bio_offset,
1597 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001598 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001599 }
1600
Chris Mason0b86a832008-03-24 15:01:56 -04001601mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001602 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001603}
Chris Mason6885f302008-02-20 16:11:05 -05001604
Chris Masond352ac62008-09-29 15:18:18 -04001605/*
1606 * given a list of ordered sums record them in the inode. This happens
1607 * at IO completion time based on sums calculated at bio submission time.
1608 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001609static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001610 struct inode *inode, u64 file_offset,
1611 struct list_head *list)
1612{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001613 struct btrfs_ordered_sum *sum;
1614
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001615 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001616 btrfs_csum_file_blocks(trans,
1617 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001618 }
1619 return 0;
1620}
1621
Josef Bacik2ac55d42010-02-03 19:33:23 +00001622int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1623 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001624{
Chris Masond3977122009-01-05 21:25:51 -05001625 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001626 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001627 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001628 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001629}
1630
Chris Masond352ac62008-09-29 15:18:18 -04001631/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001632struct btrfs_writepage_fixup {
1633 struct page *page;
1634 struct btrfs_work work;
1635};
1636
Christoph Hellwigb2950862008-12-02 09:54:17 -05001637static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001638{
1639 struct btrfs_writepage_fixup *fixup;
1640 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001641 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001642 struct page *page;
1643 struct inode *inode;
1644 u64 page_start;
1645 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001646 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001647
1648 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1649 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001650again:
Chris Mason247e7432008-07-17 12:53:51 -04001651 lock_page(page);
1652 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1653 ClearPageChecked(page);
1654 goto out_page;
1655 }
1656
1657 inode = page->mapping->host;
1658 page_start = page_offset(page);
1659 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1660
Josef Bacik2ac55d42010-02-03 19:33:23 +00001661 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001662 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001663
1664 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001665 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001666 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001667
1668 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1669 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001670 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1671 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001672 unlock_page(page);
1673 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001674 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001675 goto again;
1676 }
Chris Mason247e7432008-07-17 12:53:51 -04001677
Jeff Mahoney87826df2012-02-15 16:23:57 +01001678 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1679 if (ret) {
1680 mapping_set_error(page->mapping, ret);
1681 end_extent_writepage(page, ret, page_start, page_end);
1682 ClearPageChecked(page);
1683 goto out;
1684 }
1685
Josef Bacik2ac55d42010-02-03 19:33:23 +00001686 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001687 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001688 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001689out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001690 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1691 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001692out_page:
1693 unlock_page(page);
1694 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001695 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001696}
1697
1698/*
1699 * There are a few paths in the higher layers of the kernel that directly
1700 * set the page dirty bit without asking the filesystem if it is a
1701 * good idea. This causes problems because we want to make sure COW
1702 * properly happens and the data=ordered rules are followed.
1703 *
Chris Masonc8b97812008-10-29 14:49:59 -04001704 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001705 * hasn't been properly setup for IO. We kick off an async process
1706 * to fix it up. The async helper will wait for ordered extents, set
1707 * the delalloc bit and make it safe to write the page.
1708 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001709static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001710{
1711 struct inode *inode = page->mapping->host;
1712 struct btrfs_writepage_fixup *fixup;
1713 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001714
Chris Mason8b62b722009-09-02 16:53:46 -04001715 /* this page is properly in the ordered list */
1716 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001717 return 0;
1718
1719 if (PageChecked(page))
1720 return -EAGAIN;
1721
1722 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1723 if (!fixup)
1724 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001725
Chris Mason247e7432008-07-17 12:53:51 -04001726 SetPageChecked(page);
1727 page_cache_get(page);
1728 fixup->work.func = btrfs_writepage_fixup_worker;
1729 fixup->page = page;
1730 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001731 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001732}
1733
Yan Zhengd899e052008-10-30 14:25:28 -04001734static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1735 struct inode *inode, u64 file_pos,
1736 u64 disk_bytenr, u64 disk_num_bytes,
1737 u64 num_bytes, u64 ram_bytes,
1738 u8 compression, u8 encryption,
1739 u16 other_encoding, int extent_type)
1740{
1741 struct btrfs_root *root = BTRFS_I(inode)->root;
1742 struct btrfs_file_extent_item *fi;
1743 struct btrfs_path *path;
1744 struct extent_buffer *leaf;
1745 struct btrfs_key ins;
1746 u64 hint;
1747 int ret;
1748
1749 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001750 if (!path)
1751 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001752
Chris Masonb9473432009-03-13 11:00:37 -04001753 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001754
1755 /*
1756 * we may be replacing one extent in the tree with another.
1757 * The new extent is pinned in the extent map, and we don't want
1758 * to drop it from the cache until it is completely in the btree.
1759 *
1760 * So, tell btrfs_drop_extents to leave this extent in the cache.
1761 * the caller is expected to unpin it and allow it to be merged
1762 * with the others.
1763 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001764 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1765 &hint, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001766 if (ret)
1767 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001768
Li Zefan33345d012011-04-20 10:31:50 +08001769 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001770 ins.offset = file_pos;
1771 ins.type = BTRFS_EXTENT_DATA_KEY;
1772 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001773 if (ret)
1774 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001775 leaf = path->nodes[0];
1776 fi = btrfs_item_ptr(leaf, path->slots[0],
1777 struct btrfs_file_extent_item);
1778 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1779 btrfs_set_file_extent_type(leaf, fi, extent_type);
1780 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1781 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1782 btrfs_set_file_extent_offset(leaf, fi, 0);
1783 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1784 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1785 btrfs_set_file_extent_compression(leaf, fi, compression);
1786 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1787 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001788
1789 btrfs_unlock_up_safe(path, 1);
1790 btrfs_set_lock_blocking(leaf);
1791
Yan Zhengd899e052008-10-30 14:25:28 -04001792 btrfs_mark_buffer_dirty(leaf);
1793
1794 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001795
1796 ins.objectid = disk_bytenr;
1797 ins.offset = disk_num_bytes;
1798 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001799 ret = btrfs_alloc_reserved_file_extent(trans, root,
1800 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001801 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001802out:
Yan Zhengd899e052008-10-30 14:25:28 -04001803 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001804
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001805 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001806}
1807
Chris Mason5d13a982009-03-13 11:41:46 -04001808/*
1809 * helper function for btrfs_finish_ordered_io, this
1810 * just reads in some of the csum leaves to prime them into ram
1811 * before we start the transaction. It limits the amount of btree
1812 * reads required while inside the transaction.
1813 */
Chris Masond352ac62008-09-29 15:18:18 -04001814/* as ordered data IO finishes, this gets called so we can finish
1815 * an ordered extent if the range of bytes in the file it covers are
1816 * fully written.
1817 */
Chris Mason211f90e2008-07-18 11:56:15 -04001818static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001819{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001820 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001821 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001822 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001823 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001824 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001825 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001826 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001827 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001828
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001829 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1830 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001831 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001832 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001833 BUG_ON(!ordered_extent); /* Logic error */
Josef Bacikefd049f2010-02-02 20:50:10 +00001834
Chris Mason2cf85722011-07-26 15:35:09 -04001835 nolock = btrfs_is_free_space_inode(root, inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001836
Yan, Zhengc2167752009-11-12 09:34:21 +00001837 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001838 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Yan, Zhengc2167752009-11-12 09:34:21 +00001839 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1840 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001841 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001842 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001843 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001844 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001845 if (IS_ERR(trans))
1846 return PTR_ERR(trans);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001847 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason21151332011-11-10 20:39:08 -05001848 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001849 if (ret) /* -ENOMEM or corruption */
1850 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001851 }
1852 goto out;
1853 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001854
Josef Bacik2ac55d42010-02-03 19:33:23 +00001855 lock_extent_bits(io_tree, ordered_extent->file_offset,
1856 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001857 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001858
Josef Bacik0cb59c92010-07-02 12:14:14 -04001859 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001860 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001861 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001862 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001863 if (IS_ERR(trans)) {
1864 ret = PTR_ERR(trans);
1865 trans = NULL;
1866 goto out_unlock;
1867 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001868 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001869
Chris Masonc8b97812008-10-29 14:49:59 -04001870 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001871 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001872 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001873 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001874 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001875 ordered_extent->file_offset,
1876 ordered_extent->file_offset +
1877 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001878 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001879 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001880 ret = insert_reserved_file_extent(trans, inode,
1881 ordered_extent->file_offset,
1882 ordered_extent->start,
1883 ordered_extent->disk_len,
1884 ordered_extent->len,
1885 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001886 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001887 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001888 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1889 ordered_extent->file_offset,
1890 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001891 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001892 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1893 ordered_extent->file_offset +
1894 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001895 if (ret < 0) {
1896 btrfs_abort_transaction(trans, root, ret);
1897 goto out;
1898 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001899
Chris Masone6dcd2d2008-07-17 12:53:50 -04001900 add_pending_csums(trans, inode, ordered_extent->file_offset,
1901 &ordered_extent->list);
1902
Josef Bacik1ef30be2011-04-05 19:25:36 -04001903 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Miao Xiea39f7522011-09-11 10:52:25 -04001904 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Chris Mason21151332011-11-10 20:39:08 -05001905 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001906 if (ret) { /* -ENOMEM or corruption */
1907 btrfs_abort_transaction(trans, root, ret);
1908 goto out;
1909 }
Josef Bacik1ef30be2011-04-05 19:25:36 -04001910 }
1911 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00001912out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001913 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001914 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001915 if (trans) {
1916 if (nolock)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001917 btrfs_end_transaction_nolock(trans, root);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001918 else
Josef Bacik0cb59c92010-07-02 12:14:14 -04001919 btrfs_end_transaction(trans, root);
1920 }
1921
Chris Masone6dcd2d2008-07-17 12:53:50 -04001922 /* once for us */
1923 btrfs_put_ordered_extent(ordered_extent);
1924 /* once for the tree */
1925 btrfs_put_ordered_extent(ordered_extent);
1926
Chris Masone6dcd2d2008-07-17 12:53:50 -04001927 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928out_unlock:
1929 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1930 ordered_extent->file_offset +
1931 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1932 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001933}
1934
Christoph Hellwigb2950862008-12-02 09:54:17 -05001935static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001936 struct extent_state *state, int uptodate)
1937{
liubo1abe9b82011-03-24 11:18:59 +00001938 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1939
Chris Mason8b62b722009-09-02 16:53:46 -04001940 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001941 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1942}
1943
Chris Masond352ac62008-09-29 15:18:18 -04001944/*
Chris Masond352ac62008-09-29 15:18:18 -04001945 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001946 * if there's a match, we allow the bio to finish. If not, the code in
1947 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04001948 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001949static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04001950 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04001951{
Chris Mason35ebb932007-10-30 16:56:53 -04001952 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001953 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001954 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001955 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001956 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001957 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001958 struct btrfs_root *root = BTRFS_I(inode)->root;
1959 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001960
Chris Masond20f7042008-12-08 16:58:54 -05001961 if (PageChecked(page)) {
1962 ClearPageChecked(page);
1963 goto good;
1964 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001965
1966 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02001967 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05001968
Yan Zheng17d217f2008-12-12 10:03:38 -05001969 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001970 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001971 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1972 GFP_NOFS);
1973 return 0;
1974 }
1975
Yanc2e639f2008-02-04 08:57:25 -05001976 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001977 private = state->private;
1978 ret = 0;
1979 } else {
1980 ret = get_state_private(io_tree, start, &private);
1981 }
Cong Wang7ac687d2011-11-25 23:14:28 +08001982 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05001983 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001984 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001985
Chris Masonff79f812007-10-15 16:22:25 -04001986 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1987 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001988 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001989 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001990
Cong Wang7ac687d2011-11-25 23:14:28 +08001991 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05001992good:
Chris Mason07157aa2007-08-30 08:50:51 -04001993 return 0;
1994
1995zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04001996 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08001997 "private %llu\n",
1998 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04001999 (unsigned long long)start, csum,
2000 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002001 memset(kaddr + offset, 1, end - start + 1);
2002 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002003 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002004 if (private == 0)
2005 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002006 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002007}
Chris Masonb888db2b2007-08-27 16:49:44 -04002008
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002009struct delayed_iput {
2010 struct list_head list;
2011 struct inode *inode;
2012};
2013
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002014/* JDM: If this is fs-wide, why can't we add a pointer to
2015 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002016void btrfs_add_delayed_iput(struct inode *inode)
2017{
2018 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2019 struct delayed_iput *delayed;
2020
2021 if (atomic_add_unless(&inode->i_count, -1, 1))
2022 return;
2023
2024 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2025 delayed->inode = inode;
2026
2027 spin_lock(&fs_info->delayed_iput_lock);
2028 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2029 spin_unlock(&fs_info->delayed_iput_lock);
2030}
2031
2032void btrfs_run_delayed_iputs(struct btrfs_root *root)
2033{
2034 LIST_HEAD(list);
2035 struct btrfs_fs_info *fs_info = root->fs_info;
2036 struct delayed_iput *delayed;
2037 int empty;
2038
2039 spin_lock(&fs_info->delayed_iput_lock);
2040 empty = list_empty(&fs_info->delayed_iputs);
2041 spin_unlock(&fs_info->delayed_iput_lock);
2042 if (empty)
2043 return;
2044
2045 down_read(&root->fs_info->cleanup_work_sem);
2046 spin_lock(&fs_info->delayed_iput_lock);
2047 list_splice_init(&fs_info->delayed_iputs, &list);
2048 spin_unlock(&fs_info->delayed_iput_lock);
2049
2050 while (!list_empty(&list)) {
2051 delayed = list_entry(list.next, struct delayed_iput, list);
2052 list_del(&delayed->list);
2053 iput(delayed->inode);
2054 kfree(delayed);
2055 }
2056 up_read(&root->fs_info->cleanup_work_sem);
2057}
2058
Yan, Zhengd68fc572010-05-16 10:49:58 -04002059enum btrfs_orphan_cleanup_state {
2060 ORPHAN_CLEANUP_STARTED = 1,
2061 ORPHAN_CLEANUP_DONE = 2,
2062};
2063
2064/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002065 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002066 * files in the subvolume, it removes orphan item and frees block_rsv
2067 * structure.
2068 */
2069void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2070 struct btrfs_root *root)
2071{
Josef Bacik90290e12011-12-02 15:44:12 -05002072 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002073 int ret;
2074
2075 if (!list_empty(&root->orphan_list) ||
2076 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2077 return;
2078
Josef Bacik90290e12011-12-02 15:44:12 -05002079 spin_lock(&root->orphan_lock);
2080 if (!list_empty(&root->orphan_list)) {
2081 spin_unlock(&root->orphan_lock);
2082 return;
2083 }
2084
2085 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2086 spin_unlock(&root->orphan_lock);
2087 return;
2088 }
2089
2090 block_rsv = root->orphan_block_rsv;
2091 root->orphan_block_rsv = NULL;
2092 spin_unlock(&root->orphan_lock);
2093
Yan, Zhengd68fc572010-05-16 10:49:58 -04002094 if (root->orphan_item_inserted &&
2095 btrfs_root_refs(&root->root_item) > 0) {
2096 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2097 root->root_key.objectid);
2098 BUG_ON(ret);
2099 root->orphan_item_inserted = 0;
2100 }
2101
Josef Bacik90290e12011-12-02 15:44:12 -05002102 if (block_rsv) {
2103 WARN_ON(block_rsv->size > 0);
2104 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002105 }
2106}
2107
2108/*
Josef Bacik7b128762008-07-24 12:17:14 -04002109 * This creates an orphan entry for the given inode in case something goes
2110 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002111 *
2112 * NOTE: caller of this function should reserve 5 units of metadata for
2113 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002114 */
2115int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2116{
2117 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002118 struct btrfs_block_rsv *block_rsv = NULL;
2119 int reserve = 0;
2120 int insert = 0;
2121 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002122
Yan, Zhengd68fc572010-05-16 10:49:58 -04002123 if (!root->orphan_block_rsv) {
2124 block_rsv = btrfs_alloc_block_rsv(root);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002125 if (!block_rsv)
2126 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002127 }
2128
Yan, Zhengd68fc572010-05-16 10:49:58 -04002129 spin_lock(&root->orphan_lock);
2130 if (!root->orphan_block_rsv) {
2131 root->orphan_block_rsv = block_rsv;
2132 } else if (block_rsv) {
2133 btrfs_free_block_rsv(root, block_rsv);
2134 block_rsv = NULL;
2135 }
Josef Bacik7b128762008-07-24 12:17:14 -04002136
Yan, Zhengd68fc572010-05-16 10:49:58 -04002137 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2138 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2139#if 0
2140 /*
2141 * For proper ENOSPC handling, we should do orphan
2142 * cleanup when mounting. But this introduces backward
2143 * compatibility issue.
2144 */
2145 if (!xchg(&root->orphan_item_inserted, 1))
2146 insert = 2;
2147 else
2148 insert = 1;
2149#endif
2150 insert = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002151 }
Josef Bacik7b128762008-07-24 12:17:14 -04002152
Yan, Zhengd68fc572010-05-16 10:49:58 -04002153 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2154 BTRFS_I(inode)->orphan_meta_reserved = 1;
2155 reserve = 1;
2156 }
2157 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002158
Yan, Zhengd68fc572010-05-16 10:49:58 -04002159 /* grab metadata reservation from transaction handle */
2160 if (reserve) {
2161 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002162 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002163 }
2164
2165 /* insert an orphan item to track this unlinked/truncated file */
2166 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002167 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002168 if (ret && ret != -EEXIST) {
2169 btrfs_abort_transaction(trans, root, ret);
2170 return ret;
2171 }
2172 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002173 }
2174
2175 /* insert an orphan item to track subvolume contains orphan files */
2176 if (insert >= 2) {
2177 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2178 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002179 if (ret && ret != -EEXIST) {
2180 btrfs_abort_transaction(trans, root, ret);
2181 return ret;
2182 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002183 }
2184 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002185}
2186
2187/*
2188 * We have done the truncate/delete so we can go ahead and remove the orphan
2189 * item for this particular inode.
2190 */
2191int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2192{
2193 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002194 int delete_item = 0;
2195 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002196 int ret = 0;
2197
Yan, Zhengd68fc572010-05-16 10:49:58 -04002198 spin_lock(&root->orphan_lock);
2199 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2200 list_del_init(&BTRFS_I(inode)->i_orphan);
2201 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002202 }
2203
Yan, Zhengd68fc572010-05-16 10:49:58 -04002204 if (BTRFS_I(inode)->orphan_meta_reserved) {
2205 BTRFS_I(inode)->orphan_meta_reserved = 0;
2206 release_rsv = 1;
2207 }
2208 spin_unlock(&root->orphan_lock);
2209
2210 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002211 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002212 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002213 }
2214
Yan, Zhengd68fc572010-05-16 10:49:58 -04002215 if (release_rsv)
2216 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002217
Yan, Zhengd68fc572010-05-16 10:49:58 -04002218 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002219}
2220
2221/*
2222 * this cleans up any orphans that may be left on the list from the last use
2223 * of this root.
2224 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002225int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002226{
2227 struct btrfs_path *path;
2228 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002229 struct btrfs_key key, found_key;
2230 struct btrfs_trans_handle *trans;
2231 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002232 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002233 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2234
Yan, Zhengd68fc572010-05-16 10:49:58 -04002235 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002236 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002237
2238 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002239 if (!path) {
2240 ret = -ENOMEM;
2241 goto out;
2242 }
Josef Bacik7b128762008-07-24 12:17:14 -04002243 path->reada = -1;
2244
2245 key.objectid = BTRFS_ORPHAN_OBJECTID;
2246 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2247 key.offset = (u64)-1;
2248
Josef Bacik7b128762008-07-24 12:17:14 -04002249 while (1) {
2250 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002251 if (ret < 0)
2252 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002253
2254 /*
2255 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002256 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002257 * find the key and see if we have stuff that matches
2258 */
2259 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002260 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002261 if (path->slots[0] == 0)
2262 break;
2263 path->slots[0]--;
2264 }
2265
2266 /* pull out the item */
2267 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002268 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2269
2270 /* make sure the item matches what we want */
2271 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2272 break;
2273 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2274 break;
2275
2276 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002277 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002278
2279 /*
2280 * this is where we are basically btrfs_lookup, without the
2281 * crossing root thing. we store the inode number in the
2282 * offset of the orphan item.
2283 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002284
2285 if (found_key.offset == last_objectid) {
2286 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2287 "stopping orphan cleanup\n");
2288 ret = -EINVAL;
2289 goto out;
2290 }
2291
2292 last_objectid = found_key.offset;
2293
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002294 found_key.objectid = found_key.offset;
2295 found_key.type = BTRFS_INODE_ITEM_KEY;
2296 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002297 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002298 ret = PTR_RET(inode);
2299 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002300 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002301
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002302 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2303 struct btrfs_root *dead_root;
2304 struct btrfs_fs_info *fs_info = root->fs_info;
2305 int is_dead_root = 0;
2306
2307 /*
2308 * this is an orphan in the tree root. Currently these
2309 * could come from 2 sources:
2310 * a) a snapshot deletion in progress
2311 * b) a free space cache inode
2312 * We need to distinguish those two, as the snapshot
2313 * orphan must not get deleted.
2314 * find_dead_roots already ran before us, so if this
2315 * is a snapshot deletion, we should find the root
2316 * in the dead_roots list
2317 */
2318 spin_lock(&fs_info->trans_lock);
2319 list_for_each_entry(dead_root, &fs_info->dead_roots,
2320 root_list) {
2321 if (dead_root->root_key.objectid ==
2322 found_key.objectid) {
2323 is_dead_root = 1;
2324 break;
2325 }
2326 }
2327 spin_unlock(&fs_info->trans_lock);
2328 if (is_dead_root) {
2329 /* prevent this orphan from being found again */
2330 key.offset = found_key.objectid - 1;
2331 continue;
2332 }
2333 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002334 /*
2335 * Inode is already gone but the orphan item is still there,
2336 * kill the orphan item.
2337 */
2338 if (ret == -ESTALE) {
2339 trans = btrfs_start_transaction(root, 1);
2340 if (IS_ERR(trans)) {
2341 ret = PTR_ERR(trans);
2342 goto out;
2343 }
2344 ret = btrfs_del_orphan_item(trans, root,
2345 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002346 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002347 btrfs_end_transaction(trans, root);
2348 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002349 }
Josef Bacik7b128762008-07-24 12:17:14 -04002350
Josef Bacik7b128762008-07-24 12:17:14 -04002351 /*
2352 * add this inode to the orphan list so btrfs_orphan_del does
2353 * the proper thing when we hit it
2354 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002355 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002356 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002357 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002358
Josef Bacik7b128762008-07-24 12:17:14 -04002359 /* if we have links, this was a truncate, lets do that */
2360 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002361 if (!S_ISREG(inode->i_mode)) {
2362 WARN_ON(1);
2363 iput(inode);
2364 continue;
2365 }
Josef Bacik7b128762008-07-24 12:17:14 -04002366 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002367 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002368 } else {
2369 nr_unlink++;
2370 }
2371
2372 /* this will do delete_inode and everything for us */
2373 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002374 if (ret)
2375 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002376 }
Miao Xie3254c872011-11-10 20:45:05 -05002377 /* release the path since we're done with it */
2378 btrfs_release_path(path);
2379
Yan, Zhengd68fc572010-05-16 10:49:58 -04002380 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2381
2382 if (root->orphan_block_rsv)
2383 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2384 (u64)-1);
2385
2386 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002387 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002388 if (!IS_ERR(trans))
2389 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002390 }
Josef Bacik7b128762008-07-24 12:17:14 -04002391
2392 if (nr_unlink)
2393 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2394 if (nr_truncate)
2395 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002396
2397out:
2398 if (ret)
2399 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2400 btrfs_free_path(path);
2401 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002402}
2403
Chris Masond352ac62008-09-29 15:18:18 -04002404/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002405 * very simple check to peek ahead in the leaf looking for xattrs. If we
2406 * don't find any xattrs, we know there can't be any acls.
2407 *
2408 * slot is the slot the inode is in, objectid is the objectid of the inode
2409 */
2410static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2411 int slot, u64 objectid)
2412{
2413 u32 nritems = btrfs_header_nritems(leaf);
2414 struct btrfs_key found_key;
2415 int scanned = 0;
2416
2417 slot++;
2418 while (slot < nritems) {
2419 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2420
2421 /* we found a different objectid, there must not be acls */
2422 if (found_key.objectid != objectid)
2423 return 0;
2424
2425 /* we found an xattr, assume we've got an acl */
2426 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2427 return 1;
2428
2429 /*
2430 * we found a key greater than an xattr key, there can't
2431 * be any acls later on
2432 */
2433 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2434 return 0;
2435
2436 slot++;
2437 scanned++;
2438
2439 /*
2440 * it goes inode, inode backrefs, xattrs, extents,
2441 * so if there are a ton of hard links to an inode there can
2442 * be a lot of backrefs. Don't waste time searching too hard,
2443 * this is just an optimization
2444 */
2445 if (scanned >= 8)
2446 break;
2447 }
2448 /* we hit the end of the leaf before we found an xattr or
2449 * something larger than an xattr. We have to assume the inode
2450 * has acls
2451 */
2452 return 1;
2453}
2454
2455/*
Chris Masond352ac62008-09-29 15:18:18 -04002456 * read an inode from the btree into the in-memory inode
2457 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002458static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002459{
2460 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002461 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002462 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002463 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002464 struct btrfs_root *root = BTRFS_I(inode)->root;
2465 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002466 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002467 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002468 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002469 bool filled = false;
2470
2471 ret = btrfs_fill_inode(inode, &rdev);
2472 if (!ret)
2473 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002474
2475 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002476 if (!path)
2477 goto make_bad;
2478
Josef Bacikd90c7322011-05-17 09:50:54 -04002479 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002480 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002481
Chris Mason39279cc2007-06-12 06:35:45 -04002482 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002483 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002484 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002485
Chris Mason5f39d392007-10-15 16:14:19 -04002486 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002487
2488 if (filled)
2489 goto cache_acl;
2490
Chris Mason5f39d392007-10-15 16:14:19 -04002491 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2492 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002493 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002494 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002495 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2496 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002497 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002498
2499 tspec = btrfs_inode_atime(inode_item);
2500 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2501 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2502
2503 tspec = btrfs_inode_mtime(inode_item);
2504 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2505 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2506
2507 tspec = btrfs_inode_ctime(inode_item);
2508 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2509 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2510
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002511 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002512 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002513 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002514 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002515 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002516 rdev = btrfs_inode_rdev(leaf, inode_item);
2517
Josef Bacikaec74772008-07-24 12:12:38 -04002518 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002519 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002520cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002521 /*
2522 * try to precache a NULL acl entry for files that don't have
2523 * any xattrs or acls
2524 */
Li Zefan33345d012011-04-20 10:31:50 +08002525 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2526 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002527 if (!maybe_acls)
2528 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002529
Chris Mason39279cc2007-06-12 06:35:45 -04002530 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002531
Chris Mason39279cc2007-06-12 06:35:45 -04002532 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002533 case S_IFREG:
2534 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002535 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002536 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002537 inode->i_fop = &btrfs_file_operations;
2538 inode->i_op = &btrfs_file_inode_operations;
2539 break;
2540 case S_IFDIR:
2541 inode->i_fop = &btrfs_dir_file_operations;
2542 if (root == root->fs_info->tree_root)
2543 inode->i_op = &btrfs_dir_ro_inode_operations;
2544 else
2545 inode->i_op = &btrfs_dir_inode_operations;
2546 break;
2547 case S_IFLNK:
2548 inode->i_op = &btrfs_symlink_inode_operations;
2549 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002550 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002551 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002552 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002553 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002554 init_special_inode(inode, inode->i_mode, rdev);
2555 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002556 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002557
2558 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002559 return;
2560
2561make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002562 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002563 make_bad_inode(inode);
2564}
2565
Chris Masond352ac62008-09-29 15:18:18 -04002566/*
2567 * given a leaf and an inode, copy the inode fields into the leaf
2568 */
Chris Masone02119d2008-09-05 16:13:11 -04002569static void fill_inode_item(struct btrfs_trans_handle *trans,
2570 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002571 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002572 struct inode *inode)
2573{
Chris Mason5f39d392007-10-15 16:14:19 -04002574 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2575 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002576 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002577 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2578 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2579
2580 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2581 inode->i_atime.tv_sec);
2582 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2583 inode->i_atime.tv_nsec);
2584
2585 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2586 inode->i_mtime.tv_sec);
2587 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2588 inode->i_mtime.tv_nsec);
2589
2590 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2591 inode->i_ctime.tv_sec);
2592 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2593 inode->i_ctime.tv_nsec);
2594
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002595 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002596 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002597 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002598 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002599 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002600 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04002601 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002602}
2603
Chris Masond352ac62008-09-29 15:18:18 -04002604/*
2605 * copy everything in the in-memory inode into the btree.
2606 */
Chris Mason21151332011-11-10 20:39:08 -05002607static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002608 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002609{
2610 struct btrfs_inode_item *inode_item;
2611 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002612 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002613 int ret;
2614
2615 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002616 if (!path)
2617 return -ENOMEM;
2618
Chris Masonb9473432009-03-13 11:00:37 -04002619 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002620 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2621 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002622 if (ret) {
2623 if (ret > 0)
2624 ret = -ENOENT;
2625 goto failed;
2626 }
2627
Chris Masonb4ce94d2009-02-04 09:25:08 -05002628 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002629 leaf = path->nodes[0];
2630 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002631 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002632
Chris Masone02119d2008-09-05 16:13:11 -04002633 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002634 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002635 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002636 ret = 0;
2637failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002638 btrfs_free_path(path);
2639 return ret;
2640}
2641
Chris Masond352ac62008-09-29 15:18:18 -04002642/*
Chris Mason21151332011-11-10 20:39:08 -05002643 * copy everything in the in-memory inode into the btree.
2644 */
2645noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2646 struct btrfs_root *root, struct inode *inode)
2647{
2648 int ret;
2649
2650 /*
2651 * If the inode is a free space inode, we can deadlock during commit
2652 * if we put it into the delayed code.
2653 *
2654 * The data relocation inode should also be directly updated
2655 * without delay
2656 */
2657 if (!btrfs_is_free_space_inode(root, inode)
2658 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
2659 ret = btrfs_delayed_update_inode(trans, root, inode);
2660 if (!ret)
2661 btrfs_set_inode_last_trans(trans, inode);
2662 return ret;
2663 }
2664
2665 return btrfs_update_inode_item(trans, root, inode);
2666}
2667
2668static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2669 struct btrfs_root *root, struct inode *inode)
2670{
2671 int ret;
2672
2673 ret = btrfs_update_inode(trans, root, inode);
2674 if (ret == -ENOSPC)
2675 return btrfs_update_inode_item(trans, root, inode);
2676 return ret;
2677}
2678
2679/*
Chris Masond352ac62008-09-29 15:18:18 -04002680 * unlink helper that gets used here in inode.c and in the tree logging
2681 * recovery code. It remove a link in a directory with a given name, and
2682 * also drops the back refs in the inode to the directory
2683 */
Al Viro92986792011-03-04 17:14:37 +00002684static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2685 struct btrfs_root *root,
2686 struct inode *dir, struct inode *inode,
2687 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002688{
2689 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002690 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002691 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002692 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002693 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002694 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002695 u64 ino = btrfs_ino(inode);
2696 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002697
2698 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002699 if (!path) {
2700 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002701 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002702 }
2703
Chris Masonb9473432009-03-13 11:00:37 -04002704 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002705 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002706 name, name_len, -1);
2707 if (IS_ERR(di)) {
2708 ret = PTR_ERR(di);
2709 goto err;
2710 }
2711 if (!di) {
2712 ret = -ENOENT;
2713 goto err;
2714 }
Chris Mason5f39d392007-10-15 16:14:19 -04002715 leaf = path->nodes[0];
2716 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002717 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002718 if (ret)
2719 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002720 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002721
Li Zefan33345d012011-04-20 10:31:50 +08002722 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2723 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002724 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002725 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002726 "inode %llu parent %llu\n", name_len, name,
2727 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002728 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002729 goto err;
2730 }
2731
Miao Xie16cdcec2011-04-22 18:12:22 +08002732 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002733 if (ret) {
2734 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002735 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002736 }
Chris Mason39279cc2007-06-12 06:35:45 -04002737
Chris Masone02119d2008-09-05 16:13:11 -04002738 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002739 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002740 if (ret != 0 && ret != -ENOENT) {
2741 btrfs_abort_transaction(trans, root, ret);
2742 goto err;
2743 }
Chris Masone02119d2008-09-05 16:13:11 -04002744
2745 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2746 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002747 if (ret == -ENOENT)
2748 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002749err:
2750 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002751 if (ret)
2752 goto out;
2753
2754 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2755 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2756 btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002757out:
Chris Mason39279cc2007-06-12 06:35:45 -04002758 return ret;
2759}
2760
Al Viro92986792011-03-04 17:14:37 +00002761int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2762 struct btrfs_root *root,
2763 struct inode *dir, struct inode *inode,
2764 const char *name, int name_len)
2765{
2766 int ret;
2767 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2768 if (!ret) {
2769 btrfs_drop_nlink(inode);
2770 ret = btrfs_update_inode(trans, root, inode);
2771 }
2772 return ret;
2773}
2774
2775
Yan, Zhenga22285a2010-05-16 10:48:46 -04002776/* helper to check if there is any shared block in the path */
2777static int check_path_shared(struct btrfs_root *root,
2778 struct btrfs_path *path)
2779{
2780 struct extent_buffer *eb;
2781 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002782 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002783
2784 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002785 int ret;
2786
Yan, Zhenga22285a2010-05-16 10:48:46 -04002787 if (!path->nodes[level])
2788 break;
2789 eb = path->nodes[level];
2790 if (!btrfs_block_can_be_shared(root, eb))
2791 continue;
2792 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2793 &refs, NULL);
2794 if (refs > 1)
2795 return 1;
2796 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002797 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002798}
2799
2800/*
2801 * helper to start transaction for unlink and rmdir.
2802 *
2803 * unlink and rmdir are special in btrfs, they do not always free space.
2804 * so in enospc case, we should make sure they will free space before
2805 * allowing them to use the global metadata reservation.
2806 */
2807static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2808 struct dentry *dentry)
2809{
2810 struct btrfs_trans_handle *trans;
2811 struct btrfs_root *root = BTRFS_I(dir)->root;
2812 struct btrfs_path *path;
2813 struct btrfs_inode_ref *ref;
2814 struct btrfs_dir_item *di;
2815 struct inode *inode = dentry->d_inode;
2816 u64 index;
2817 int check_link = 1;
2818 int err = -ENOSPC;
2819 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002820 u64 ino = btrfs_ino(inode);
2821 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002822
Josef Bacike70bea52011-10-11 14:18:24 -04002823 /*
2824 * 1 for the possible orphan item
2825 * 1 for the dir item
2826 * 1 for the dir index
2827 * 1 for the inode ref
2828 * 1 for the inode ref in the tree log
2829 * 2 for the dir entries in the log
2830 * 1 for the inode
2831 */
2832 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002833 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2834 return trans;
2835
Li Zefan33345d012011-04-20 10:31:50 +08002836 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002837 return ERR_PTR(-ENOSPC);
2838
2839 /* check if there is someone else holds reference */
2840 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2841 return ERR_PTR(-ENOSPC);
2842
2843 if (atomic_read(&inode->i_count) > 2)
2844 return ERR_PTR(-ENOSPC);
2845
2846 if (xchg(&root->fs_info->enospc_unlink, 1))
2847 return ERR_PTR(-ENOSPC);
2848
2849 path = btrfs_alloc_path();
2850 if (!path) {
2851 root->fs_info->enospc_unlink = 0;
2852 return ERR_PTR(-ENOMEM);
2853 }
2854
Josef Bacik3880a1b2011-10-14 14:46:51 -04002855 /* 1 for the orphan item */
2856 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002857 if (IS_ERR(trans)) {
2858 btrfs_free_path(path);
2859 root->fs_info->enospc_unlink = 0;
2860 return trans;
2861 }
2862
2863 path->skip_locking = 1;
2864 path->search_commit_root = 1;
2865
2866 ret = btrfs_lookup_inode(trans, root, path,
2867 &BTRFS_I(dir)->location, 0);
2868 if (ret < 0) {
2869 err = ret;
2870 goto out;
2871 }
2872 if (ret == 0) {
2873 if (check_path_shared(root, path))
2874 goto out;
2875 } else {
2876 check_link = 0;
2877 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002878 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002879
2880 ret = btrfs_lookup_inode(trans, root, path,
2881 &BTRFS_I(inode)->location, 0);
2882 if (ret < 0) {
2883 err = ret;
2884 goto out;
2885 }
2886 if (ret == 0) {
2887 if (check_path_shared(root, path))
2888 goto out;
2889 } else {
2890 check_link = 0;
2891 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002892 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002893
2894 if (ret == 0 && S_ISREG(inode->i_mode)) {
2895 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002896 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002897 if (ret < 0) {
2898 err = ret;
2899 goto out;
2900 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002901 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002902 if (check_path_shared(root, path))
2903 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02002904 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002905 }
2906
2907 if (!check_link) {
2908 err = 0;
2909 goto out;
2910 }
2911
Li Zefan33345d012011-04-20 10:31:50 +08002912 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002913 dentry->d_name.name, dentry->d_name.len, 0);
2914 if (IS_ERR(di)) {
2915 err = PTR_ERR(di);
2916 goto out;
2917 }
2918 if (di) {
2919 if (check_path_shared(root, path))
2920 goto out;
2921 } else {
2922 err = 0;
2923 goto out;
2924 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002925 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002926
2927 ref = btrfs_lookup_inode_ref(trans, root, path,
2928 dentry->d_name.name, dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08002929 ino, dir_ino, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002930 if (IS_ERR(ref)) {
2931 err = PTR_ERR(ref);
2932 goto out;
2933 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002934 BUG_ON(!ref); /* Logic error */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002935 if (check_path_shared(root, path))
2936 goto out;
2937 index = btrfs_inode_ref_index(path->nodes[0], ref);
David Sterbab3b4aa72011-04-21 01:20:15 +02002938 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002939
Miao Xie16cdcec2011-04-22 18:12:22 +08002940 /*
2941 * This is a commit root search, if we can lookup inode item and other
2942 * relative items in the commit root, it means the transaction of
2943 * dir/file creation has been committed, and the dir index item that we
2944 * delay to insert has also been inserted into the commit root. So
2945 * we needn't worry about the delayed insertion of the dir index item
2946 * here.
2947 */
Li Zefan33345d012011-04-20 10:31:50 +08002948 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002949 dentry->d_name.name, dentry->d_name.len, 0);
2950 if (IS_ERR(di)) {
2951 err = PTR_ERR(di);
2952 goto out;
2953 }
2954 BUG_ON(ret == -ENOENT);
2955 if (check_path_shared(root, path))
2956 goto out;
2957
2958 err = 0;
2959out:
2960 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04002961 /* Migrate the orphan reservation over */
2962 if (!err)
2963 err = btrfs_block_rsv_migrate(trans->block_rsv,
2964 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04002965 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04002966
Yan, Zhenga22285a2010-05-16 10:48:46 -04002967 if (err) {
2968 btrfs_end_transaction(trans, root);
2969 root->fs_info->enospc_unlink = 0;
2970 return ERR_PTR(err);
2971 }
2972
2973 trans->block_rsv = &root->fs_info->global_block_rsv;
2974 return trans;
2975}
2976
2977static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2978 struct btrfs_root *root)
2979{
2980 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
Josef Bacik5a77d762011-11-01 14:32:23 -04002981 btrfs_block_rsv_release(root, trans->block_rsv,
2982 trans->bytes_reserved);
2983 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002984 BUG_ON(!root->fs_info->enospc_unlink);
2985 root->fs_info->enospc_unlink = 0;
2986 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05002987 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002988}
2989
Chris Mason39279cc2007-06-12 06:35:45 -04002990static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2991{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002992 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002993 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002994 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002995 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002996 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002997
Yan, Zhenga22285a2010-05-16 10:48:46 -04002998 trans = __unlink_start_trans(dir, dentry);
2999 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003000 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003001
Chris Mason12fcfd22009-03-24 10:24:20 -04003002 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3003
Chris Masone02119d2008-09-05 16:13:11 -04003004 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3005 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003006 if (ret)
3007 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003008
Yan, Zhenga22285a2010-05-16 10:48:46 -04003009 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003010 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003011 if (ret)
3012 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003013 }
Josef Bacik7b128762008-07-24 12:17:14 -04003014
Tsutomu Itohb5324022011-07-19 07:27:20 +00003015out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003016 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003017 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003018 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003019 return ret;
3020}
3021
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003022int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3023 struct btrfs_root *root,
3024 struct inode *dir, u64 objectid,
3025 const char *name, int name_len)
3026{
3027 struct btrfs_path *path;
3028 struct extent_buffer *leaf;
3029 struct btrfs_dir_item *di;
3030 struct btrfs_key key;
3031 u64 index;
3032 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003033 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003034
3035 path = btrfs_alloc_path();
3036 if (!path)
3037 return -ENOMEM;
3038
Li Zefan33345d012011-04-20 10:31:50 +08003039 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003040 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003041 if (IS_ERR_OR_NULL(di)) {
3042 if (!di)
3043 ret = -ENOENT;
3044 else
3045 ret = PTR_ERR(di);
3046 goto out;
3047 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003048
3049 leaf = path->nodes[0];
3050 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3051 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3052 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003053 if (ret) {
3054 btrfs_abort_transaction(trans, root, ret);
3055 goto out;
3056 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003057 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003058
3059 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3060 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003061 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003062 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003063 if (ret != -ENOENT) {
3064 btrfs_abort_transaction(trans, root, ret);
3065 goto out;
3066 }
Li Zefan33345d012011-04-20 10:31:50 +08003067 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003068 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003069 if (IS_ERR_OR_NULL(di)) {
3070 if (!di)
3071 ret = -ENOENT;
3072 else
3073 ret = PTR_ERR(di);
3074 btrfs_abort_transaction(trans, root, ret);
3075 goto out;
3076 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003077
3078 leaf = path->nodes[0];
3079 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003080 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003081 index = key.offset;
3082 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003083 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003084
Miao Xie16cdcec2011-04-22 18:12:22 +08003085 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003086 if (ret) {
3087 btrfs_abort_transaction(trans, root, ret);
3088 goto out;
3089 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003090
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003091 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3092 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3093 ret = btrfs_update_inode(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003094 if (ret)
3095 btrfs_abort_transaction(trans, root, ret);
3096out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003097 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003098 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003099}
3100
Chris Mason39279cc2007-06-12 06:35:45 -04003101static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3102{
3103 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003104 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003105 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003106 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003107 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003108
Chris Mason3394e162008-11-17 20:42:26 -05003109 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Li Zefan33345d012011-04-20 10:31:50 +08003110 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003111 return -ENOTEMPTY;
3112
Yan, Zhenga22285a2010-05-16 10:48:46 -04003113 trans = __unlink_start_trans(dir, dentry);
3114 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003115 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003116
Li Zefan33345d012011-04-20 10:31:50 +08003117 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003118 err = btrfs_unlink_subvol(trans, root, dir,
3119 BTRFS_I(inode)->location.objectid,
3120 dentry->d_name.name,
3121 dentry->d_name.len);
3122 goto out;
3123 }
3124
Josef Bacik7b128762008-07-24 12:17:14 -04003125 err = btrfs_orphan_add(trans, inode);
3126 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003127 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003128
Chris Mason39279cc2007-06-12 06:35:45 -04003129 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003130 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3131 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003132 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003133 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003134out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003135 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003136 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003137 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003138
Chris Mason39279cc2007-06-12 06:35:45 -04003139 return err;
3140}
3141
Chris Mason323ac952008-10-01 19:05:46 -04003142/*
Chris Mason39279cc2007-06-12 06:35:45 -04003143 * this can truncate away extent items, csum items and directory items.
3144 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003145 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003146 *
3147 * csum items that cross the new i_size are truncated to the new size
3148 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003149 *
3150 * min_type is the minimum key type to truncate down to. If set to 0, this
3151 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003152 */
Yan, Zheng80825102009-11-12 09:35:36 +00003153int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3154 struct btrfs_root *root,
3155 struct inode *inode,
3156 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003157{
Chris Mason39279cc2007-06-12 06:35:45 -04003158 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003159 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003160 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003161 struct btrfs_key key;
3162 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003163 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003164 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003165 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003166 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003167 u64 mask = root->sectorsize - 1;
3168 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003169 int found_extent;
3170 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003171 int pending_del_nr = 0;
3172 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003173 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003174 int ret;
3175 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003176 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003177
3178 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003179
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003180 path = btrfs_alloc_path();
3181 if (!path)
3182 return -ENOMEM;
3183 path->reada = -1;
3184
Josef Bacik0af3d002010-06-21 14:48:16 -04003185 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003186 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003187
Miao Xie16cdcec2011-04-22 18:12:22 +08003188 /*
3189 * This function is also used to drop the items in the log tree before
3190 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3191 * it is used to drop the loged items. So we shouldn't kill the delayed
3192 * items.
3193 */
3194 if (min_type == 0 && root == BTRFS_I(inode)->root)
3195 btrfs_kill_delayed_inode_items(inode);
3196
Li Zefan33345d012011-04-20 10:31:50 +08003197 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003198 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003199 key.type = (u8)-1;
3200
Chris Mason85e21ba2008-01-29 15:11:36 -05003201search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003202 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003203 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003204 if (ret < 0) {
3205 err = ret;
3206 goto out;
3207 }
Chris Masond3977122009-01-05 21:25:51 -05003208
Chris Mason85e21ba2008-01-29 15:11:36 -05003209 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003210 /* there are no items in the tree for us to truncate, we're
3211 * done
3212 */
Yan, Zheng80825102009-11-12 09:35:36 +00003213 if (path->slots[0] == 0)
3214 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003215 path->slots[0]--;
3216 }
3217
Chris Masond3977122009-01-05 21:25:51 -05003218 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003219 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003220 leaf = path->nodes[0];
3221 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3222 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003223
Li Zefan33345d012011-04-20 10:31:50 +08003224 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003225 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003226
Chris Mason85e21ba2008-01-29 15:11:36 -05003227 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003228 break;
3229
Chris Mason5f39d392007-10-15 16:14:19 -04003230 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003231 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003232 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003233 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003234 extent_type = btrfs_file_extent_type(leaf, fi);
3235 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003236 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003237 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003238 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003239 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003240 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003241 }
Yan008630c2007-11-07 13:31:09 -05003242 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003243 }
Yan, Zheng80825102009-11-12 09:35:36 +00003244 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003245 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003246 } else {
3247 if (item_end < new_size)
3248 break;
3249 if (found_key.offset >= new_size)
3250 del_item = 1;
3251 else
3252 del_item = 0;
3253 }
Chris Mason39279cc2007-06-12 06:35:45 -04003254 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003255 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003256 if (found_type != BTRFS_EXTENT_DATA_KEY)
3257 goto delete;
3258
3259 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003260 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003261 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003262 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003263 u64 orig_num_bytes =
3264 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003265 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003266 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003267 extent_num_bytes = extent_num_bytes &
3268 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003269 btrfs_set_file_extent_num_bytes(leaf, fi,
3270 extent_num_bytes);
3271 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003272 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003273 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003274 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003275 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003276 } else {
Chris Masondb945352007-10-15 16:15:53 -04003277 extent_num_bytes =
3278 btrfs_file_extent_disk_num_bytes(leaf,
3279 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003280 extent_offset = found_key.offset -
3281 btrfs_file_extent_offset(leaf, fi);
3282
Chris Mason39279cc2007-06-12 06:35:45 -04003283 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003284 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003285 if (extent_start != 0) {
3286 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003287 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003288 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003289 }
3290 }
Chris Mason90692182008-02-08 13:49:28 -05003291 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003292 /*
3293 * we can't truncate inline items that have had
3294 * special encodings
3295 */
3296 if (!del_item &&
3297 btrfs_file_extent_compression(leaf, fi) == 0 &&
3298 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3299 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003300 u32 size = new_size - found_key.offset;
3301
3302 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003303 inode_sub_bytes(inode, item_end + 1 -
3304 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003305 }
3306 size =
3307 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003308 btrfs_truncate_item(trans, root, path,
3309 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003310 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003311 inode_sub_bytes(inode, item_end + 1 -
3312 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003313 }
Chris Mason39279cc2007-06-12 06:35:45 -04003314 }
Chris Mason179e29e2007-11-01 11:28:41 -04003315delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003316 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003317 if (!pending_del_nr) {
3318 /* no pending yet, add ourselves */
3319 pending_del_slot = path->slots[0];
3320 pending_del_nr = 1;
3321 } else if (pending_del_nr &&
3322 path->slots[0] + 1 == pending_del_slot) {
3323 /* hop on the pending chunk */
3324 pending_del_nr++;
3325 pending_del_slot = path->slots[0];
3326 } else {
Chris Masond3977122009-01-05 21:25:51 -05003327 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003328 }
Chris Mason39279cc2007-06-12 06:35:45 -04003329 } else {
3330 break;
3331 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003332 if (found_extent && (root->ref_cows ||
3333 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003334 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003335 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003336 extent_num_bytes, 0,
3337 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003338 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003339 BUG_ON(ret);
3340 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003341
Yan, Zheng80825102009-11-12 09:35:36 +00003342 if (found_type == BTRFS_INODE_ITEM_KEY)
3343 break;
3344
3345 if (path->slots[0] == 0 ||
3346 path->slots[0] != pending_del_slot) {
Li Zefan82d59022011-04-20 10:33:24 +08003347 if (root->ref_cows &&
3348 BTRFS_I(inode)->location.objectid !=
3349 BTRFS_FREE_INO_OBJECTID) {
Yan, Zheng80825102009-11-12 09:35:36 +00003350 err = -EAGAIN;
3351 goto out;
3352 }
3353 if (pending_del_nr) {
3354 ret = btrfs_del_items(trans, root, path,
3355 pending_del_slot,
3356 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003357 if (ret) {
3358 btrfs_abort_transaction(trans,
3359 root, ret);
3360 goto error;
3361 }
Yan, Zheng80825102009-11-12 09:35:36 +00003362 pending_del_nr = 0;
3363 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003364 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003365 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003366 } else {
3367 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003368 }
Chris Mason39279cc2007-06-12 06:35:45 -04003369 }
Yan, Zheng80825102009-11-12 09:35:36 +00003370out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003371 if (pending_del_nr) {
3372 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3373 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003374 if (ret)
3375 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003376 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003377error:
Chris Mason39279cc2007-06-12 06:35:45 -04003378 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003379 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003380}
3381
Chris Masona52d9a82007-08-27 16:49:44 -04003382/*
3383 * taken from block_truncate_page, but does cow as it zeros out
3384 * any bytes left in the last page in the file.
3385 */
3386static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3387{
3388 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003389 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003390 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3391 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003392 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003393 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003394 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003395 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3396 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3397 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003398 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003399 int ret = 0;
3400 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003401 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003402
3403 if ((offset & (blocksize - 1)) == 0)
3404 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003405 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003406 if (ret)
3407 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003408
3409 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003410again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003411 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003412 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003413 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003414 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003415 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003416
3417 page_start = page_offset(page);
3418 page_end = page_start + PAGE_CACHE_SIZE - 1;
3419
Chris Masona52d9a82007-08-27 16:49:44 -04003420 if (!PageUptodate(page)) {
3421 ret = btrfs_readpage(NULL, page);
3422 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003423 if (page->mapping != mapping) {
3424 unlock_page(page);
3425 page_cache_release(page);
3426 goto again;
3427 }
Chris Masona52d9a82007-08-27 16:49:44 -04003428 if (!PageUptodate(page)) {
3429 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003430 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003431 }
3432 }
Chris Mason211c17f2008-05-15 09:13:45 -04003433 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003434
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003435 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003436 set_page_extent_mapped(page);
3437
3438 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3439 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003440 unlock_extent_cached(io_tree, page_start, page_end,
3441 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003442 unlock_page(page);
3443 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003444 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003445 btrfs_put_ordered_extent(ordered);
3446 goto again;
3447 }
3448
Josef Bacik2ac55d42010-02-03 19:33:23 +00003449 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003450 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003451 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003452
Josef Bacik2ac55d42010-02-03 19:33:23 +00003453 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3454 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003455 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003456 unlock_extent_cached(io_tree, page_start, page_end,
3457 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003458 goto out_unlock;
3459 }
3460
Chris Masone6dcd2d2008-07-17 12:53:50 -04003461 ret = 0;
3462 if (offset != PAGE_CACHE_SIZE) {
3463 kaddr = kmap(page);
3464 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3465 flush_dcache_page(page);
3466 kunmap(page);
3467 }
Chris Mason247e7432008-07-17 12:53:51 -04003468 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003469 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003470 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3471 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003472
Chris Mason89642222008-07-24 09:41:53 -04003473out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003474 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003475 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003476 unlock_page(page);
3477 page_cache_release(page);
3478out:
3479 return ret;
3480}
3481
Josef Bacik695a0d02011-03-04 15:46:53 -05003482/*
3483 * This function puts in dummy file extents for the area we're creating a hole
3484 * for. So if we are truncating this file to a larger size we need to insert
3485 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3486 * the range between oldsize and size
3487 */
Josef Bacika41ad392011-01-31 15:30:16 -05003488int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003489{
3490 struct btrfs_trans_handle *trans;
3491 struct btrfs_root *root = BTRFS_I(inode)->root;
3492 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003493 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003494 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003495 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003496 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003497 u64 block_end = (size + mask) & ~mask;
3498 u64 last_byte;
3499 u64 cur_offset;
3500 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003501 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003502
3503 if (size <= hole_start)
3504 return 0;
3505
Yan Zheng9036c102008-10-30 14:19:41 -04003506 while (1) {
3507 struct btrfs_ordered_extent *ordered;
3508 btrfs_wait_ordered_range(inode, hole_start,
3509 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003510 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003511 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003512 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3513 if (!ordered)
3514 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003515 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3516 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003517 btrfs_put_ordered_extent(ordered);
3518 }
3519
Yan Zheng9036c102008-10-30 14:19:41 -04003520 cur_offset = hole_start;
3521 while (1) {
3522 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3523 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003524 if (IS_ERR(em)) {
3525 err = PTR_ERR(em);
3526 break;
3527 }
Yan Zheng9036c102008-10-30 14:19:41 -04003528 last_byte = min(extent_map_end(em), block_end);
3529 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003530 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003531 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003532 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003533
Miao Xie36423202011-12-14 20:12:02 -05003534 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003535 if (IS_ERR(trans)) {
3536 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003537 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003538 }
Yan, Zheng80825102009-11-12 09:35:36 +00003539
3540 err = btrfs_drop_extents(trans, inode, cur_offset,
3541 cur_offset + hole_size,
3542 &hint_byte, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003543 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003544 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003545 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003546 break;
Miao Xie5b397372011-09-11 10:52:24 -04003547 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003548
Yan Zheng9036c102008-10-30 14:19:41 -04003549 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003550 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003551 0, hole_size, 0, hole_size,
3552 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003553 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003554 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003555 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003556 break;
Miao Xie5b397372011-09-11 10:52:24 -04003557 }
Yan, Zheng80825102009-11-12 09:35:36 +00003558
Yan Zheng9036c102008-10-30 14:19:41 -04003559 btrfs_drop_extent_cache(inode, hole_start,
3560 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003561
Miao Xie36423202011-12-14 20:12:02 -05003562 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003563 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003564 }
3565 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003566 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003567 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003568 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003569 break;
3570 }
3571
Yan, Zhenga22285a2010-05-16 10:48:46 -04003572 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003573 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3574 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003575 return err;
3576}
3577
Josef Bacika41ad392011-01-31 15:30:16 -05003578static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003579{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003580 struct btrfs_root *root = BTRFS_I(inode)->root;
3581 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003582 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003583 int ret;
3584
Josef Bacika41ad392011-01-31 15:30:16 -05003585 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003586 return 0;
3587
Josef Bacika41ad392011-01-31 15:30:16 -05003588 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003589 truncate_pagecache(inode, oldsize, newsize);
3590 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003591 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003592 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003593
Miao Xief4a2f4c2011-12-14 20:12:01 -05003594 trans = btrfs_start_transaction(root, 1);
3595 if (IS_ERR(trans))
3596 return PTR_ERR(trans);
3597
3598 i_size_write(inode, newsize);
3599 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3600 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003601 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003602 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003603
Josef Bacika41ad392011-01-31 15:30:16 -05003604 /*
3605 * We're truncating a file that used to have good data down to
3606 * zero. Make sure it gets into the ordered flush list so that
3607 * any new writes get down to disk quickly.
3608 */
3609 if (newsize == 0)
3610 BTRFS_I(inode)->ordered_data_close = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003611
Josef Bacika41ad392011-01-31 15:30:16 -05003612 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3613 truncate_setsize(inode, newsize);
3614 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003615 }
3616
Josef Bacika41ad392011-01-31 15:30:16 -05003617 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003618}
3619
Chris Mason39279cc2007-06-12 06:35:45 -04003620static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3621{
3622 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003623 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003624 int err;
3625
Li Zefanb83cc962010-12-20 16:04:08 +08003626 if (btrfs_root_readonly(root))
3627 return -EROFS;
3628
Chris Mason39279cc2007-06-12 06:35:45 -04003629 err = inode_change_ok(inode, attr);
3630 if (err)
3631 return err;
3632
Chris Mason5a3f23d2009-03-31 13:27:11 -04003633 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003634 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003635 if (err)
3636 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003637 }
Yan Zheng9036c102008-10-30 14:19:41 -04003638
Christoph Hellwig10257742010-06-04 11:30:02 +02003639 if (attr->ia_valid) {
3640 setattr_copy(inode, attr);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003641 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003642
Josef Bacik22c44fe2011-11-30 10:45:38 -05003643 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003644 err = btrfs_acl_chmod(inode);
3645 }
3646
Chris Mason39279cc2007-06-12 06:35:45 -04003647 return err;
3648}
Chris Mason61295eb2008-01-14 16:24:38 -05003649
Al Virobd555972010-06-07 11:35:40 -04003650void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003651{
3652 struct btrfs_trans_handle *trans;
3653 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003654 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003655 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003656 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003657 int ret;
3658
liubo1abe9b82011-03-24 11:18:59 +00003659 trace_btrfs_inode_evict(inode);
3660
Chris Mason39279cc2007-06-12 06:35:45 -04003661 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003662 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Chris Mason2cf85722011-07-26 15:35:09 -04003663 btrfs_is_free_space_inode(root, inode)))
Al Virobd555972010-06-07 11:35:40 -04003664 goto no_delete;
3665
Chris Mason39279cc2007-06-12 06:35:45 -04003666 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003667 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003668 goto no_delete;
3669 }
Al Virobd555972010-06-07 11:35:40 -04003670 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003671 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003672
Yan, Zhengc71bf092009-11-12 09:34:40 +00003673 if (root->fs_info->log_root_recovering) {
3674 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3675 goto no_delete;
3676 }
3677
Yan, Zheng76dda932009-09-21 16:00:26 -04003678 if (inode->i_nlink > 0) {
3679 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3680 goto no_delete;
3681 }
3682
Josef Bacik4289a662011-08-05 13:22:24 -04003683 rsv = btrfs_alloc_block_rsv(root);
3684 if (!rsv) {
3685 btrfs_orphan_del(NULL, inode);
3686 goto no_delete;
3687 }
Josef Bacik4a338542011-08-29 11:01:31 -04003688 rsv->size = min_size;
Josef Bacik726c35f2011-09-26 15:46:06 -04003689 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003690
Chris Masondbe674a2008-07-17 12:54:05 -04003691 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003692
Josef Bacik4289a662011-08-05 13:22:24 -04003693 /*
3694 * This is a bit simpler than btrfs_truncate since
3695 *
3696 * 1) We've already reserved our space for our orphan item in the
3697 * unlink.
3698 * 2) We're going to delete the inode item, so we don't need to update
3699 * it at all.
3700 *
3701 * So we just need to reserve some slack space in case we add bytes when
3702 * doing the truncate.
3703 */
Yan, Zheng80825102009-11-12 09:35:36 +00003704 while (1) {
Miao Xieaa38a712011-11-18 17:43:00 +08003705 ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003706
Josef Bacik726c35f2011-09-26 15:46:06 -04003707 /*
3708 * Try and steal from the global reserve since we will
3709 * likely not use this space anyway, we want to try as
3710 * hard as possible to get this to work.
3711 */
3712 if (ret)
3713 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3714
Yan, Zhengd68fc572010-05-16 10:49:58 -04003715 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003716 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003717 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003718 btrfs_orphan_del(NULL, inode);
3719 btrfs_free_block_rsv(root, rsv);
3720 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003721 }
3722
Josef Bacik4289a662011-08-05 13:22:24 -04003723 trans = btrfs_start_transaction(root, 0);
3724 if (IS_ERR(trans)) {
3725 btrfs_orphan_del(NULL, inode);
3726 btrfs_free_block_rsv(root, rsv);
3727 goto no_delete;
3728 }
3729
3730 trans->block_rsv = rsv;
3731
Yan, Zhengd68fc572010-05-16 10:49:58 -04003732 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003733 if (ret != -EAGAIN)
3734 break;
3735
3736 nr = trans->blocks_used;
3737 btrfs_end_transaction(trans, root);
3738 trans = NULL;
3739 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003740 }
3741
Josef Bacik4289a662011-08-05 13:22:24 -04003742 btrfs_free_block_rsv(root, rsv);
3743
Yan, Zheng80825102009-11-12 09:35:36 +00003744 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003745 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003746 ret = btrfs_orphan_del(trans, inode);
3747 BUG_ON(ret);
3748 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003749
Josef Bacik4289a662011-08-05 13:22:24 -04003750 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003751 if (!(root == root->fs_info->tree_root ||
3752 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003753 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003754
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003755 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003756 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003757 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003758no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02003759 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003760 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003761}
3762
3763/*
3764 * this returns the key found in the dir entry in the location pointer.
3765 * If no dir entries were found, location->objectid is 0.
3766 */
3767static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3768 struct btrfs_key *location)
3769{
3770 const char *name = dentry->d_name.name;
3771 int namelen = dentry->d_name.len;
3772 struct btrfs_dir_item *di;
3773 struct btrfs_path *path;
3774 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003775 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003776
3777 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003778 if (!path)
3779 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003780
Li Zefan33345d012011-04-20 10:31:50 +08003781 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003782 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003783 if (IS_ERR(di))
3784 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003785
David Sterbac7040052011-04-19 18:00:01 +02003786 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003787 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003788
Chris Mason5f39d392007-10-15 16:14:19 -04003789 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003790out:
Chris Mason39279cc2007-06-12 06:35:45 -04003791 btrfs_free_path(path);
3792 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003793out_err:
3794 location->objectid = 0;
3795 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003796}
3797
3798/*
3799 * when we hit a tree root in a directory, the btrfs part of the inode
3800 * needs to be changed to reflect the root directory of the tree root. This
3801 * is kind of like crossing a mount point.
3802 */
3803static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003804 struct inode *dir,
3805 struct dentry *dentry,
3806 struct btrfs_key *location,
3807 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003808{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003809 struct btrfs_path *path;
3810 struct btrfs_root *new_root;
3811 struct btrfs_root_ref *ref;
3812 struct extent_buffer *leaf;
3813 int ret;
3814 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003815
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003816 path = btrfs_alloc_path();
3817 if (!path) {
3818 err = -ENOMEM;
3819 goto out;
3820 }
Chris Mason39279cc2007-06-12 06:35:45 -04003821
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003822 err = -ENOENT;
3823 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3824 BTRFS_I(dir)->root->root_key.objectid,
3825 location->objectid);
3826 if (ret) {
3827 if (ret < 0)
3828 err = ret;
3829 goto out;
3830 }
Chris Mason39279cc2007-06-12 06:35:45 -04003831
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003832 leaf = path->nodes[0];
3833 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003834 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003835 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3836 goto out;
3837
3838 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3839 (unsigned long)(ref + 1),
3840 dentry->d_name.len);
3841 if (ret)
3842 goto out;
3843
David Sterbab3b4aa72011-04-21 01:20:15 +02003844 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003845
3846 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3847 if (IS_ERR(new_root)) {
3848 err = PTR_ERR(new_root);
3849 goto out;
3850 }
3851
3852 if (btrfs_root_refs(&new_root->root_item) == 0) {
3853 err = -ENOENT;
3854 goto out;
3855 }
3856
3857 *sub_root = new_root;
3858 location->objectid = btrfs_root_dirid(&new_root->root_item);
3859 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003860 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003861 err = 0;
3862out:
3863 btrfs_free_path(path);
3864 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003865}
3866
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003867static void inode_tree_add(struct inode *inode)
3868{
3869 struct btrfs_root *root = BTRFS_I(inode)->root;
3870 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003871 struct rb_node **p;
3872 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08003873 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003874again:
3875 p = &root->inode_tree.rb_node;
3876 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003877
Al Viro1d3382cb2010-10-23 15:19:20 -04003878 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003879 return;
3880
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003881 spin_lock(&root->inode_lock);
3882 while (*p) {
3883 parent = *p;
3884 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3885
Li Zefan33345d012011-04-20 10:31:50 +08003886 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003887 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003888 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003889 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003890 else {
3891 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003892 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003893 rb_erase(parent, &root->inode_tree);
3894 RB_CLEAR_NODE(parent);
3895 spin_unlock(&root->inode_lock);
3896 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003897 }
3898 }
3899 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3900 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3901 spin_unlock(&root->inode_lock);
3902}
3903
3904static void inode_tree_del(struct inode *inode)
3905{
3906 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003907 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003908
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003909 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003910 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003911 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003912 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003913 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003914 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003915 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003916
Josef Bacik0af3d002010-06-21 14:48:16 -04003917 /*
3918 * Free space cache has inodes in the tree root, but the tree root has a
3919 * root_refs of 0, so this could end up dropping the tree root as a
3920 * snapshot, so we need the extra !root->fs_info->tree_root check to
3921 * make sure we don't drop it.
3922 */
3923 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3924 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003925 synchronize_srcu(&root->fs_info->subvol_srcu);
3926 spin_lock(&root->inode_lock);
3927 empty = RB_EMPTY_ROOT(&root->inode_tree);
3928 spin_unlock(&root->inode_lock);
3929 if (empty)
3930 btrfs_add_dead_root(root);
3931 }
3932}
3933
Jeff Mahoney143bede2012-03-01 14:56:26 +01003934void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04003935{
3936 struct rb_node *node;
3937 struct rb_node *prev;
3938 struct btrfs_inode *entry;
3939 struct inode *inode;
3940 u64 objectid = 0;
3941
3942 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3943
3944 spin_lock(&root->inode_lock);
3945again:
3946 node = root->inode_tree.rb_node;
3947 prev = NULL;
3948 while (node) {
3949 prev = node;
3950 entry = rb_entry(node, struct btrfs_inode, rb_node);
3951
Li Zefan33345d012011-04-20 10:31:50 +08003952 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003953 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003954 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003955 node = node->rb_right;
3956 else
3957 break;
3958 }
3959 if (!node) {
3960 while (prev) {
3961 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08003962 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003963 node = prev;
3964 break;
3965 }
3966 prev = rb_next(prev);
3967 }
3968 }
3969 while (node) {
3970 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08003971 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04003972 inode = igrab(&entry->vfs_inode);
3973 if (inode) {
3974 spin_unlock(&root->inode_lock);
3975 if (atomic_read(&inode->i_count) > 1)
3976 d_prune_aliases(inode);
3977 /*
Al Viro45321ac2010-06-07 13:43:19 -04003978 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04003979 * the inode cache when its usage count
3980 * hits zero.
3981 */
3982 iput(inode);
3983 cond_resched();
3984 spin_lock(&root->inode_lock);
3985 goto again;
3986 }
3987
3988 if (cond_resched_lock(&root->inode_lock))
3989 goto again;
3990
3991 node = rb_next(node);
3992 }
3993 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003994}
3995
Chris Masone02119d2008-09-05 16:13:11 -04003996static int btrfs_init_locked_inode(struct inode *inode, void *p)
3997{
3998 struct btrfs_iget_args *args = p;
3999 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004000 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004001 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004002 return 0;
4003}
4004
4005static int btrfs_find_actor(struct inode *inode, void *opaque)
4006{
4007 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004008 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004009 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004010}
4011
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004012static struct inode *btrfs_iget_locked(struct super_block *s,
4013 u64 objectid,
4014 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004015{
4016 struct inode *inode;
4017 struct btrfs_iget_args args;
4018 args.ino = objectid;
4019 args.root = root;
4020
4021 inode = iget5_locked(s, objectid, btrfs_find_actor,
4022 btrfs_init_locked_inode,
4023 (void *)&args);
4024 return inode;
4025}
4026
Balaji Rao1a54ef82008-07-21 02:01:04 +05304027/* Get an inode object given its location and corresponding root.
4028 * Returns in *is_new if the inode was read from disk
4029 */
4030struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004031 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304032{
4033 struct inode *inode;
4034
4035 inode = btrfs_iget_locked(s, location->objectid, root);
4036 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004037 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304038
4039 if (inode->i_state & I_NEW) {
4040 BTRFS_I(inode)->root = root;
4041 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4042 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004043 if (!is_bad_inode(inode)) {
4044 inode_tree_add(inode);
4045 unlock_new_inode(inode);
4046 if (new)
4047 *new = 1;
4048 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004049 unlock_new_inode(inode);
4050 iput(inode);
4051 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004052 }
4053 }
4054
Balaji Rao1a54ef82008-07-21 02:01:04 +05304055 return inode;
4056}
4057
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004058static struct inode *new_simple_dir(struct super_block *s,
4059 struct btrfs_key *key,
4060 struct btrfs_root *root)
4061{
4062 struct inode *inode = new_inode(s);
4063
4064 if (!inode)
4065 return ERR_PTR(-ENOMEM);
4066
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004067 BTRFS_I(inode)->root = root;
4068 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4069 BTRFS_I(inode)->dummy_inode = 1;
4070
4071 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004072 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004073 inode->i_fop = &simple_dir_operations;
4074 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4075 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4076
4077 return inode;
4078}
4079
Chris Mason3de45862008-11-17 21:02:50 -05004080struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004081{
Chris Masond3977122009-01-05 21:25:51 -05004082 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004083 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004084 struct btrfs_root *sub_root = root;
4085 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004086 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004087 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004088
4089 if (dentry->d_name.len > BTRFS_NAME_LEN)
4090 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004091
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004092 if (unlikely(d_need_lookup(dentry))) {
4093 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4094 kfree(dentry->d_fsdata);
4095 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004096 /* This thing is hashed, drop it for now */
4097 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004098 } else {
4099 ret = btrfs_inode_by_name(dir, dentry, &location);
4100 }
Chris Mason5f39d392007-10-15 16:14:19 -04004101
Chris Mason39279cc2007-06-12 06:35:45 -04004102 if (ret < 0)
4103 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004104
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004105 if (location.objectid == 0)
4106 return NULL;
4107
4108 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004109 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004110 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004111 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004112
4113 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4114
Yan, Zheng76dda932009-09-21 16:00:26 -04004115 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004116 ret = fixup_tree_root_location(root, dir, dentry,
4117 &location, &sub_root);
4118 if (ret < 0) {
4119 if (ret != -ENOENT)
4120 inode = ERR_PTR(ret);
4121 else
4122 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4123 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004124 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004125 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004126 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4127
Julia Lawall34d19ba2011-01-24 19:55:19 +00004128 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004129 down_read(&root->fs_info->cleanup_work_sem);
4130 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004131 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004132 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004133 if (ret)
4134 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004135 }
4136
Chris Mason3de45862008-11-17 21:02:50 -05004137 return inode;
4138}
4139
Nick Pigginfe15ce42011-01-07 17:49:23 +11004140static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004141{
4142 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004143 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004144
Li Zefan848cce02012-02-21 17:04:28 +08004145 if (!inode && !IS_ROOT(dentry))
4146 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004147
Li Zefan848cce02012-02-21 17:04:28 +08004148 if (inode) {
4149 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004150 if (btrfs_root_refs(&root->root_item) == 0)
4151 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004152
4153 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4154 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004155 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004156 return 0;
4157}
4158
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004159static void btrfs_dentry_release(struct dentry *dentry)
4160{
4161 if (dentry->d_fsdata)
4162 kfree(dentry->d_fsdata);
4163}
4164
Chris Mason3de45862008-11-17 21:02:50 -05004165static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4166 struct nameidata *nd)
4167{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004168 struct dentry *ret;
4169
4170 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4171 if (unlikely(d_need_lookup(dentry))) {
4172 spin_lock(&dentry->d_lock);
4173 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4174 spin_unlock(&dentry->d_lock);
4175 }
4176 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004177}
4178
Miao Xie16cdcec2011-04-22 18:12:22 +08004179unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004180 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4181};
4182
David Woodhousecbdf5a22008-08-06 19:42:33 +01004183static int btrfs_real_readdir(struct file *filp, void *dirent,
4184 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004185{
Chris Mason6da6aba2007-12-18 16:15:09 -05004186 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004187 struct btrfs_root *root = BTRFS_I(inode)->root;
4188 struct btrfs_item *item;
4189 struct btrfs_dir_item *di;
4190 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004191 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004192 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004193 struct list_head ins_list;
4194 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004195 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004196 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004197 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004198 unsigned char d_type;
4199 int over = 0;
4200 u32 di_cur;
4201 u32 di_total;
4202 u32 di_len;
4203 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004204 char tmp_name[32];
4205 char *name_ptr;
4206 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004207 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004208
4209 /* FIXME, use a real flag for deciding about the key type */
4210 if (root->fs_info->tree_root == root)
4211 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004212
Chris Mason39544012007-12-12 14:38:19 -05004213 /* special case for "." */
4214 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004215 over = filldir(dirent, ".", 1,
4216 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004217 if (over)
4218 return 0;
4219 filp->f_pos = 1;
4220 }
Chris Mason39544012007-12-12 14:38:19 -05004221 /* special case for .., just use the back ref */
4222 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004223 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004224 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004225 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004226 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004227 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004228 filp->f_pos = 2;
4229 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004230 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004231 if (!path)
4232 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004233
Josef Bacik026fd312011-05-13 10:32:11 -04004234 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004235
Miao Xie16cdcec2011-04-22 18:12:22 +08004236 if (key_type == BTRFS_DIR_INDEX_KEY) {
4237 INIT_LIST_HEAD(&ins_list);
4238 INIT_LIST_HEAD(&del_list);
4239 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4240 }
4241
Chris Mason39279cc2007-06-12 06:35:45 -04004242 btrfs_set_key_type(&key, key_type);
4243 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004244 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004245
Chris Mason39279cc2007-06-12 06:35:45 -04004246 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4247 if (ret < 0)
4248 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004249
4250 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004251 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004252 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004253 if (slot >= btrfs_header_nritems(leaf)) {
4254 ret = btrfs_next_leaf(root, path);
4255 if (ret < 0)
4256 goto err;
4257 else if (ret > 0)
4258 break;
4259 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004260 }
Chris Mason3de45862008-11-17 21:02:50 -05004261
Chris Mason5f39d392007-10-15 16:14:19 -04004262 item = btrfs_item_nr(leaf, slot);
4263 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4264
4265 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004266 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004267 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004268 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004269 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004270 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004271 if (key_type == BTRFS_DIR_INDEX_KEY &&
4272 btrfs_should_delete_dir_index(&del_list,
4273 found_key.offset))
4274 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004275
4276 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004277 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004278
Chris Mason39279cc2007-06-12 06:35:45 -04004279 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4280 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004281 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004282
4283 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004284 struct btrfs_key location;
4285
Josef Bacik22a94d42011-03-16 16:47:17 -04004286 if (verify_dir_item(root, leaf, di))
4287 break;
4288
Chris Mason5f39d392007-10-15 16:14:19 -04004289 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004290 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004291 name_ptr = tmp_name;
4292 } else {
4293 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004294 if (!name_ptr) {
4295 ret = -ENOMEM;
4296 goto err;
4297 }
Chris Mason5f39d392007-10-15 16:14:19 -04004298 }
4299 read_extent_buffer(leaf, name_ptr,
4300 (unsigned long)(di + 1), name_len);
4301
4302 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4303 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004304
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004305
Chris Mason3de45862008-11-17 21:02:50 -05004306 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004307 * skip it.
4308 *
4309 * In contrast to old kernels, we insert the snapshot's
4310 * dir item and dir index after it has been created, so
4311 * we won't find a reference to our own snapshot. We
4312 * still keep the following code for backward
4313 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004314 */
4315 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4316 location.objectid == root->root_key.objectid) {
4317 over = 0;
4318 goto skip;
4319 }
Chris Mason5f39d392007-10-15 16:14:19 -04004320 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004321 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004322 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004323
Chris Mason3de45862008-11-17 21:02:50 -05004324skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004325 if (name_ptr != tmp_name)
4326 kfree(name_ptr);
4327
Chris Mason39279cc2007-06-12 06:35:45 -04004328 if (over)
4329 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004330 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004331 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004332 di_cur += di_len;
4333 di = (struct btrfs_dir_item *)((char *)di + di_len);
4334 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004335next:
4336 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004337 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004338
Miao Xie16cdcec2011-04-22 18:12:22 +08004339 if (key_type == BTRFS_DIR_INDEX_KEY) {
4340 if (is_curr)
4341 filp->f_pos++;
4342 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4343 &ins_list);
4344 if (ret)
4345 goto nopos;
4346 }
4347
David Woodhouse49593bf2008-08-17 17:08:36 +01004348 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004349 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004350 /*
4351 * 32-bit glibc will use getdents64, but then strtol -
4352 * so the last number we can serve is this.
4353 */
4354 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004355 else
4356 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004357nopos:
4358 ret = 0;
4359err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004360 if (key_type == BTRFS_DIR_INDEX_KEY)
4361 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004362 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004363 return ret;
4364}
4365
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004366int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004367{
4368 struct btrfs_root *root = BTRFS_I(inode)->root;
4369 struct btrfs_trans_handle *trans;
4370 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004371 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004372
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004373 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004374 return 0;
4375
Chris Mason2cf85722011-07-26 15:35:09 -04004376 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(root, inode))
Li Zefan82d59022011-04-20 10:33:24 +08004377 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004378
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004379 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004380 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004381 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004382 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004383 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004384 if (IS_ERR(trans))
4385 return PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04004386 if (nolock)
4387 ret = btrfs_end_transaction_nolock(trans, root);
4388 else
4389 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004390 }
4391 return ret;
4392}
4393
4394/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004395 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004396 * inode changes. But, it is most likely to find the inode in cache.
4397 * FIXME, needs more benchmarking...there are no reasons other than performance
4398 * to keep or drop this code.
4399 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004400int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004401{
4402 struct btrfs_root *root = BTRFS_I(inode)->root;
4403 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004404 int ret;
4405
4406 if (BTRFS_I(inode)->dummy_inode)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004407 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004408
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004409 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004410 if (IS_ERR(trans))
4411 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004412
4413 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004414 if (ret && ret == -ENOSPC) {
4415 /* whoops, lets try again with the full transaction */
4416 btrfs_end_transaction(trans, root);
4417 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004418 if (IS_ERR(trans))
4419 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004420
Chris Mason94b60442010-05-26 11:02:00 -04004421 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004422 }
Chris Mason39279cc2007-06-12 06:35:45 -04004423 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004424 if (BTRFS_I(inode)->delayed_node)
4425 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004426
4427 return ret;
4428}
4429
4430/*
4431 * This is a copy of file_update_time. We need this so we can return error on
4432 * ENOSPC for updating the inode in the case of file write and mmap writes.
4433 */
Josef Bacike41f9412012-03-26 09:46:47 -04004434static int btrfs_update_time(struct inode *inode, struct timespec *now,
4435 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004436{
Josef Bacike41f9412012-03-26 09:46:47 -04004437 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004438 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004439 if (flags & S_CTIME)
4440 inode->i_ctime = *now;
4441 if (flags & S_MTIME)
4442 inode->i_mtime = *now;
4443 if (flags & S_ATIME)
4444 inode->i_atime = *now;
4445 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004446}
4447
Chris Masond352ac62008-09-29 15:18:18 -04004448/*
4449 * find the highest existing sequence number in a directory
4450 * and then set the in-memory index_cnt variable to reflect
4451 * free sequence numbers
4452 */
Josef Bacikaec74772008-07-24 12:12:38 -04004453static int btrfs_set_inode_index_count(struct inode *inode)
4454{
4455 struct btrfs_root *root = BTRFS_I(inode)->root;
4456 struct btrfs_key key, found_key;
4457 struct btrfs_path *path;
4458 struct extent_buffer *leaf;
4459 int ret;
4460
Li Zefan33345d012011-04-20 10:31:50 +08004461 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004462 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4463 key.offset = (u64)-1;
4464
4465 path = btrfs_alloc_path();
4466 if (!path)
4467 return -ENOMEM;
4468
4469 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4470 if (ret < 0)
4471 goto out;
4472 /* FIXME: we should be able to handle this */
4473 if (ret == 0)
4474 goto out;
4475 ret = 0;
4476
4477 /*
4478 * MAGIC NUMBER EXPLANATION:
4479 * since we search a directory based on f_pos we have to start at 2
4480 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4481 * else has to start at 2
4482 */
4483 if (path->slots[0] == 0) {
4484 BTRFS_I(inode)->index_cnt = 2;
4485 goto out;
4486 }
4487
4488 path->slots[0]--;
4489
4490 leaf = path->nodes[0];
4491 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4492
Li Zefan33345d012011-04-20 10:31:50 +08004493 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004494 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4495 BTRFS_I(inode)->index_cnt = 2;
4496 goto out;
4497 }
4498
4499 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4500out:
4501 btrfs_free_path(path);
4502 return ret;
4503}
4504
Chris Masond352ac62008-09-29 15:18:18 -04004505/*
4506 * helper to find a free sequence number in a given directory. This current
4507 * code is very simple, later versions will do smarter things in the btree
4508 */
Chris Mason3de45862008-11-17 21:02:50 -05004509int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004510{
4511 int ret = 0;
4512
4513 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004514 ret = btrfs_inode_delayed_dir_index_count(dir);
4515 if (ret) {
4516 ret = btrfs_set_inode_index_count(dir);
4517 if (ret)
4518 return ret;
4519 }
Josef Bacikaec74772008-07-24 12:12:38 -04004520 }
4521
Chris Mason00e4e6b2008-08-05 11:18:09 -04004522 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004523 BTRFS_I(dir)->index_cnt++;
4524
4525 return ret;
4526}
4527
Chris Mason39279cc2007-06-12 06:35:45 -04004528static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4529 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004530 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004531 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004532 u64 ref_objectid, u64 objectid,
4533 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004534{
4535 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004536 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004537 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004538 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004539 struct btrfs_inode_ref *ref;
4540 struct btrfs_key key[2];
4541 u32 sizes[2];
4542 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004543 int ret;
4544 int owner;
4545
Chris Mason5f39d392007-10-15 16:14:19 -04004546 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004547 if (!path)
4548 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004549
Chris Mason39279cc2007-06-12 06:35:45 -04004550 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004551 if (!inode) {
4552 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004553 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004554 }
Chris Mason39279cc2007-06-12 06:35:45 -04004555
Li Zefan581bb052011-04-20 10:06:11 +08004556 /*
4557 * we have to initialize this early, so we can reclaim the inode
4558 * number if we fail afterwards in this function.
4559 */
4560 inode->i_ino = objectid;
4561
Josef Bacikaec74772008-07-24 12:12:38 -04004562 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004563 trace_btrfs_inode_request(dir);
4564
Chris Mason3de45862008-11-17 21:02:50 -05004565 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004566 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004567 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004568 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004569 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004570 }
Josef Bacikaec74772008-07-24 12:12:38 -04004571 }
4572 /*
4573 * index_cnt is ignored for everything but a dir,
4574 * btrfs_get_inode_index_count has an explanation for the magic
4575 * number
4576 */
4577 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004578 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004579 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004580 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004581 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db2b2007-08-27 16:49:44 -04004582
Al Viro569254b2011-07-24 17:08:40 -04004583 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004584 owner = 0;
4585 else
4586 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004587
4588 key[0].objectid = objectid;
4589 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4590 key[0].offset = 0;
4591
4592 key[1].objectid = objectid;
4593 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4594 key[1].offset = ref_objectid;
4595
4596 sizes[0] = sizeof(struct btrfs_inode_item);
4597 sizes[1] = name_len + sizeof(*ref);
4598
Chris Masonb9473432009-03-13 11:00:37 -04004599 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004600 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4601 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004602 goto fail;
4603
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004604 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004605 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004606 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004607 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4608 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004609 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004610
4611 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4612 struct btrfs_inode_ref);
4613 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004614 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004615 ptr = (unsigned long)(ref + 1);
4616 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4617
Chris Mason5f39d392007-10-15 16:14:19 -04004618 btrfs_mark_buffer_dirty(path->nodes[0]);
4619 btrfs_free_path(path);
4620
Chris Mason39279cc2007-06-12 06:35:45 -04004621 location = &BTRFS_I(inode)->location;
4622 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004623 location->offset = 0;
4624 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4625
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004626 btrfs_inherit_iflags(inode, dir);
4627
Al Viro569254b2011-07-24 17:08:40 -04004628 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004629 if (btrfs_test_opt(root, NODATASUM))
4630 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004631 if (btrfs_test_opt(root, NODATACOW) ||
4632 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004633 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4634 }
4635
Chris Mason39279cc2007-06-12 06:35:45 -04004636 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004637 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004638
4639 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004640 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004641
Chris Mason39279cc2007-06-12 06:35:45 -04004642 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004643fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004644 if (dir)
4645 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004646 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004647 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004648 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004649}
4650
4651static inline u8 btrfs_inode_type(struct inode *inode)
4652{
4653 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4654}
4655
Chris Masond352ac62008-09-29 15:18:18 -04004656/*
4657 * utility function to add 'inode' into 'parent_inode' with
4658 * a give name and a given sequence number.
4659 * if 'add_backref' is true, also insert a backref from the
4660 * inode to the parent directory.
4661 */
Chris Masone02119d2008-09-05 16:13:11 -04004662int btrfs_add_link(struct btrfs_trans_handle *trans,
4663 struct inode *parent_inode, struct inode *inode,
4664 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004665{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004666 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004667 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004668 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004669 u64 ino = btrfs_ino(inode);
4670 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004671
Li Zefan33345d012011-04-20 10:31:50 +08004672 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004673 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4674 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004675 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004676 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4677 key.offset = 0;
4678 }
Chris Mason39279cc2007-06-12 06:35:45 -04004679
Li Zefan33345d012011-04-20 10:31:50 +08004680 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004681 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4682 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004683 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004684 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004685 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4686 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004687 }
4688
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004689 /* Nothing to clean up yet */
4690 if (ret)
4691 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004692
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004693 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4694 parent_inode, &key,
4695 btrfs_inode_type(inode), index);
4696 if (ret == -EEXIST)
4697 goto fail_dir_item;
4698 else if (ret) {
4699 btrfs_abort_transaction(trans, root, ret);
4700 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004701 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004702
4703 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4704 name_len * 2);
4705 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4706 ret = btrfs_update_inode(trans, root, parent_inode);
4707 if (ret)
4708 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004709 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004710
4711fail_dir_item:
4712 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4713 u64 local_index;
4714 int err;
4715 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4716 key.objectid, root->root_key.objectid,
4717 parent_ino, &local_index, name, name_len);
4718
4719 } else if (add_backref) {
4720 u64 local_index;
4721 int err;
4722
4723 err = btrfs_del_inode_ref(trans, root, name, name_len,
4724 ino, parent_ino, &local_index);
4725 }
4726 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004727}
4728
4729static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004730 struct inode *dir, struct dentry *dentry,
4731 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004732{
Josef Bacika1b075d2010-11-19 20:36:11 +00004733 int err = btrfs_add_link(trans, dir, inode,
4734 dentry->d_name.name, dentry->d_name.len,
4735 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004736 if (err > 0)
4737 err = -EEXIST;
4738 return err;
4739}
4740
Josef Bacik618e21d2007-07-11 10:18:17 -04004741static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004742 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004743{
4744 struct btrfs_trans_handle *trans;
4745 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004746 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004747 int err;
4748 int drop_inode = 0;
4749 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004750 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004751 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004752
4753 if (!new_valid_dev(rdev))
4754 return -EINVAL;
4755
Josef Bacik9ed74f22009-09-11 16:12:44 -04004756 /*
4757 * 2 for inode item and ref
4758 * 2 for dir items
4759 * 1 for xattr if selinux is on
4760 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004761 trans = btrfs_start_transaction(root, 5);
4762 if (IS_ERR(trans))
4763 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004764
Li Zefan581bb052011-04-20 10:06:11 +08004765 err = btrfs_find_free_ino(root, &objectid);
4766 if (err)
4767 goto out_unlock;
4768
Josef Bacikaec74772008-07-24 12:12:38 -04004769 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004770 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04004771 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004772 if (IS_ERR(inode)) {
4773 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004774 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004775 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004776
Eric Paris2a7dba32011-02-01 11:05:39 -05004777 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004778 if (err) {
4779 drop_inode = 1;
4780 goto out_unlock;
4781 }
4782
Casey Schauflerad19db72011-12-15 10:09:07 -05004783 /*
4784 * If the active LSM wants to access the inode during
4785 * d_instantiate it needs these. Smack checks to see
4786 * if the filesystem supports xattrs by looking at the
4787 * ops vector.
4788 */
4789
4790 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004791 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004792 if (err)
4793 drop_inode = 1;
4794 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004795 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004796 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05004797 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004798 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004799out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004800 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004801 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004802 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004803 if (drop_inode) {
4804 inode_dec_link_count(inode);
4805 iput(inode);
4806 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004807 return err;
4808}
4809
Chris Mason39279cc2007-06-12 06:35:45 -04004810static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viro4acdaf22011-07-26 01:42:34 -04004811 umode_t mode, struct nameidata *nd)
Chris Mason39279cc2007-06-12 06:35:45 -04004812{
4813 struct btrfs_trans_handle *trans;
4814 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004815 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004816 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004817 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004818 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004819 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004820 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004821
Josef Bacik9ed74f22009-09-11 16:12:44 -04004822 /*
4823 * 2 for inode item and ref
4824 * 2 for dir items
4825 * 1 for xattr if selinux is on
4826 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004827 trans = btrfs_start_transaction(root, 5);
4828 if (IS_ERR(trans))
4829 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004830
Li Zefan581bb052011-04-20 10:06:11 +08004831 err = btrfs_find_free_ino(root, &objectid);
4832 if (err)
4833 goto out_unlock;
4834
Josef Bacikaec74772008-07-24 12:12:38 -04004835 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004836 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04004837 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004838 if (IS_ERR(inode)) {
4839 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004840 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004841 }
Chris Mason39279cc2007-06-12 06:35:45 -04004842
Eric Paris2a7dba32011-02-01 11:05:39 -05004843 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004844 if (err) {
4845 drop_inode = 1;
4846 goto out_unlock;
4847 }
4848
Casey Schauflerad19db72011-12-15 10:09:07 -05004849 /*
4850 * If the active LSM wants to access the inode during
4851 * d_instantiate it needs these. Smack checks to see
4852 * if the filesystem supports xattrs by looking at the
4853 * ops vector.
4854 */
4855 inode->i_fop = &btrfs_file_operations;
4856 inode->i_op = &btrfs_file_inode_operations;
4857
Josef Bacika1b075d2010-11-19 20:36:11 +00004858 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004859 if (err)
4860 drop_inode = 1;
4861 else {
4862 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004863 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05004864 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro08c422c2011-12-23 07:58:13 -05004865 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004866 }
Chris Mason39279cc2007-06-12 06:35:45 -04004867out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004868 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004869 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004870 if (drop_inode) {
4871 inode_dec_link_count(inode);
4872 iput(inode);
4873 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004874 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004875 return err;
4876}
4877
4878static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4879 struct dentry *dentry)
4880{
4881 struct btrfs_trans_handle *trans;
4882 struct btrfs_root *root = BTRFS_I(dir)->root;
4883 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004884 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004885 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004886 int err;
4887 int drop_inode = 0;
4888
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004889 /* do not allow sys_link's with other subvols of the same device */
4890 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00004891 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004892
Al Viroc055e992011-03-04 17:15:18 +00004893 if (inode->i_nlink == ~0U)
4894 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004895
Chris Mason3de45862008-11-17 21:02:50 -05004896 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004897 if (err)
4898 goto fail;
4899
Yan, Zhenga22285a2010-05-16 10:48:46 -04004900 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00004901 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04004902 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00004903 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04004904 */
Miao Xie7e6b6462011-02-18 09:21:17 +00004905 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004906 if (IS_ERR(trans)) {
4907 err = PTR_ERR(trans);
4908 goto fail;
4909 }
Chris Mason5f39d392007-10-15 16:14:19 -04004910
Miao Xie31534952011-04-13 13:19:21 +08004911 btrfs_inc_nlink(inode);
4912 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04004913 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004914
Josef Bacika1b075d2010-11-19 20:36:11 +00004915 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004916
Yan, Zhenga5719522009-09-24 09:17:31 -04004917 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004918 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004919 } else {
Al Viro10d9f302011-07-16 23:09:10 -04004920 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04004921 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004922 if (err)
4923 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05004924 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00004925 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004926 }
Chris Mason39279cc2007-06-12 06:35:45 -04004927
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004928 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004929 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004930fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004931 if (drop_inode) {
4932 inode_dec_link_count(inode);
4933 iput(inode);
4934 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004935 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004936 return err;
4937}
4938
Al Viro18bb1db2011-07-26 01:41:39 -04004939static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04004940{
Chris Masonb9d86662008-05-02 16:13:49 -04004941 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004942 struct btrfs_trans_handle *trans;
4943 struct btrfs_root *root = BTRFS_I(dir)->root;
4944 int err = 0;
4945 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004946 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004947 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004948 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004949
Josef Bacik9ed74f22009-09-11 16:12:44 -04004950 /*
4951 * 2 items for inode and ref
4952 * 2 items for dir items
4953 * 1 for xattr if selinux is on
4954 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004955 trans = btrfs_start_transaction(root, 5);
4956 if (IS_ERR(trans))
4957 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04004958
Li Zefan581bb052011-04-20 10:06:11 +08004959 err = btrfs_find_free_ino(root, &objectid);
4960 if (err)
4961 goto out_fail;
4962
Josef Bacikaec74772008-07-24 12:12:38 -04004963 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004964 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04004965 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004966 if (IS_ERR(inode)) {
4967 err = PTR_ERR(inode);
4968 goto out_fail;
4969 }
Chris Mason5f39d392007-10-15 16:14:19 -04004970
Chris Mason39279cc2007-06-12 06:35:45 -04004971 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004972
Eric Paris2a7dba32011-02-01 11:05:39 -05004973 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004974 if (err)
4975 goto out_fail;
4976
Chris Mason39279cc2007-06-12 06:35:45 -04004977 inode->i_op = &btrfs_dir_inode_operations;
4978 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04004979
Chris Masondbe674a2008-07-17 12:54:05 -04004980 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004981 err = btrfs_update_inode(trans, root, inode);
4982 if (err)
4983 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004984
Josef Bacika1b075d2010-11-19 20:36:11 +00004985 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4986 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004987 if (err)
4988 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004989
Chris Mason39279cc2007-06-12 06:35:45 -04004990 d_instantiate(dentry, inode);
4991 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004992
4993out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004994 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004995 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004996 if (drop_on_err)
4997 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004998 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004999 return err;
5000}
5001
Chris Masond352ac62008-09-29 15:18:18 -04005002/* helper for btfs_get_extent. Given an existing extent in the tree,
5003 * and an extent that you want to insert, deal with overlap and insert
5004 * the new extent into the tree.
5005 */
Chris Mason3b951512008-04-17 11:29:12 -04005006static int merge_extent_mapping(struct extent_map_tree *em_tree,
5007 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005008 struct extent_map *em,
5009 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005010{
5011 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005012
Chris Masone6dcd2d2008-07-17 12:53:50 -04005013 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5014 start_diff = map_start - em->start;
5015 em->start = map_start;
5016 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005017 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5018 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005019 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005020 em->block_len -= start_diff;
5021 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005022 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005023}
5024
Chris Masonc8b97812008-10-29 14:49:59 -04005025static noinline int uncompress_inline(struct btrfs_path *path,
5026 struct inode *inode, struct page *page,
5027 size_t pg_offset, u64 extent_offset,
5028 struct btrfs_file_extent_item *item)
5029{
5030 int ret;
5031 struct extent_buffer *leaf = path->nodes[0];
5032 char *tmp;
5033 size_t max_size;
5034 unsigned long inline_size;
5035 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005036 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005037
5038 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005039 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005040 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5041 inline_size = btrfs_file_extent_inline_item_len(leaf,
5042 btrfs_item_nr(leaf, path->slots[0]));
5043 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005044 if (!tmp)
5045 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005046 ptr = btrfs_file_extent_inline_start(item);
5047
5048 read_extent_buffer(leaf, tmp, ptr, inline_size);
5049
Chris Mason5b050f02008-11-11 09:34:41 -05005050 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005051 ret = btrfs_decompress(compress_type, tmp, page,
5052 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005053 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005054 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005055 unsigned long copy_size = min_t(u64,
5056 PAGE_CACHE_SIZE - pg_offset,
5057 max_size - extent_offset);
5058 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005059 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005060 }
5061 kfree(tmp);
5062 return 0;
5063}
5064
Chris Masond352ac62008-09-29 15:18:18 -04005065/*
5066 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005067 * the ugly parts come from merging extents from the disk with the in-ram
5068 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005069 * where the in-ram extents might be locked pending data=ordered completion.
5070 *
5071 * This also copies inline extents directly into the page.
5072 */
Chris Masond3977122009-01-05 21:25:51 -05005073
Chris Masona52d9a82007-08-27 16:49:44 -04005074struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005075 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005076 int create)
5077{
5078 int ret;
5079 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005080 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005081 u64 extent_start = 0;
5082 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005083 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005084 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005085 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005086 struct btrfs_root *root = BTRFS_I(inode)->root;
5087 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005088 struct extent_buffer *leaf;
5089 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005090 struct extent_map *em = NULL;
5091 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005092 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005093 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005094 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005095
Chris Masona52d9a82007-08-27 16:49:44 -04005096again:
Chris Mason890871b2009-09-02 16:24:52 -04005097 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005098 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005099 if (em)
5100 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005101 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005102
Chris Masona52d9a82007-08-27 16:49:44 -04005103 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005104 if (em->start > start || em->start + em->len <= start)
5105 free_extent_map(em);
5106 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005107 free_extent_map(em);
5108 else
5109 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005110 }
David Sterba172ddd62011-04-21 00:48:27 +02005111 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005112 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005113 err = -ENOMEM;
5114 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005115 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005116 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005117 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005118 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005119 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005120 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005121
5122 if (!path) {
5123 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005124 if (!path) {
5125 err = -ENOMEM;
5126 goto out;
5127 }
5128 /*
5129 * Chances are we'll be called again, so go ahead and do
5130 * readahead
5131 */
5132 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005133 }
5134
Chris Mason179e29e2007-11-01 11:28:41 -04005135 ret = btrfs_lookup_file_extent(trans, root, path,
5136 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005137 if (ret < 0) {
5138 err = ret;
5139 goto out;
5140 }
5141
5142 if (ret != 0) {
5143 if (path->slots[0] == 0)
5144 goto not_found;
5145 path->slots[0]--;
5146 }
5147
Chris Mason5f39d392007-10-15 16:14:19 -04005148 leaf = path->nodes[0];
5149 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005150 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005151 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005152 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5153 found_type = btrfs_key_type(&found_key);
5154 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005155 found_type != BTRFS_EXTENT_DATA_KEY) {
5156 goto not_found;
5157 }
5158
Chris Mason5f39d392007-10-15 16:14:19 -04005159 found_type = btrfs_file_extent_type(leaf, item);
5160 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005161 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005162 if (found_type == BTRFS_FILE_EXTENT_REG ||
5163 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005164 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005165 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005166 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5167 size_t size;
5168 size = btrfs_file_extent_inline_len(leaf, item);
5169 extent_end = (extent_start + size + root->sectorsize - 1) &
5170 ~((u64)root->sectorsize - 1);
5171 }
5172
5173 if (start >= extent_end) {
5174 path->slots[0]++;
5175 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5176 ret = btrfs_next_leaf(root, path);
5177 if (ret < 0) {
5178 err = ret;
5179 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005180 }
Yan Zheng9036c102008-10-30 14:19:41 -04005181 if (ret > 0)
5182 goto not_found;
5183 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005184 }
Yan Zheng9036c102008-10-30 14:19:41 -04005185 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5186 if (found_key.objectid != objectid ||
5187 found_key.type != BTRFS_EXTENT_DATA_KEY)
5188 goto not_found;
5189 if (start + len <= found_key.offset)
5190 goto not_found;
5191 em->start = start;
5192 em->len = found_key.offset - start;
5193 goto not_found_em;
5194 }
5195
Yan Zhengd899e052008-10-30 14:25:28 -04005196 if (found_type == BTRFS_FILE_EXTENT_REG ||
5197 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005198 em->start = extent_start;
5199 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005200 em->orig_start = extent_start -
5201 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005202 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5203 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005204 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005205 goto insert;
5206 }
Li Zefan261507a02010-12-17 14:21:50 +08005207 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005208 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005209 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005210 em->block_start = bytenr;
5211 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5212 item);
5213 } else {
5214 bytenr += btrfs_file_extent_offset(leaf, item);
5215 em->block_start = bytenr;
5216 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005217 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5218 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005219 }
Chris Masona52d9a82007-08-27 16:49:44 -04005220 goto insert;
5221 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005222 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005223 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005224 size_t size;
5225 size_t extent_offset;
5226 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005227
Chris Masona52d9a82007-08-27 16:49:44 -04005228 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005229 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005230 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005231 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005232 goto out;
5233 }
5234
Yan Zheng9036c102008-10-30 14:19:41 -04005235 size = btrfs_file_extent_inline_len(leaf, item);
5236 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005237 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005238 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005239 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005240 em->len = (copy_size + root->sectorsize - 1) &
5241 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005242 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005243 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005244 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005245 em->compress_type = compress_type;
5246 }
Yan689f9342007-10-29 11:41:07 -04005247 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005248 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005249 if (btrfs_file_extent_compression(leaf, item) !=
5250 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005251 ret = uncompress_inline(path, inode, page,
5252 pg_offset,
5253 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005254 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005255 } else {
5256 map = kmap(page);
5257 read_extent_buffer(leaf, map + pg_offset, ptr,
5258 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005259 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5260 memset(map + pg_offset + copy_size, 0,
5261 PAGE_CACHE_SIZE - pg_offset -
5262 copy_size);
5263 }
Chris Masonc8b97812008-10-29 14:49:59 -04005264 kunmap(page);
5265 }
Chris Mason179e29e2007-11-01 11:28:41 -04005266 flush_dcache_page(page);
5267 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005268 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005269 if (!trans) {
5270 kunmap(page);
5271 free_extent_map(em);
5272 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005273
David Sterbab3b4aa72011-04-21 01:20:15 +02005274 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005275 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005276
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005277 if (IS_ERR(trans))
5278 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005279 goto again;
5280 }
Chris Masonc8b97812008-10-29 14:49:59 -04005281 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005282 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005283 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005284 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005285 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005286 }
Chris Masond1310b22008-01-24 16:13:08 -05005287 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005288 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005289 goto insert;
5290 } else {
Chris Masond3977122009-01-05 21:25:51 -05005291 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005292 WARN_ON(1);
5293 }
5294not_found:
5295 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005296 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005297not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005298 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005299 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005300insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005301 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005302 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005303 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5304 "[%llu %llu]\n", (unsigned long long)em->start,
5305 (unsigned long long)em->len,
5306 (unsigned long long)start,
5307 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005308 err = -EIO;
5309 goto out;
5310 }
Chris Masond1310b22008-01-24 16:13:08 -05005311
5312 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005313 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005314 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005315 /* it is possible that someone inserted the extent into the tree
5316 * while we had the lock dropped. It is also possible that
5317 * an overlapping map exists in the tree
5318 */
Chris Masona52d9a82007-08-27 16:49:44 -04005319 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005320 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005321
5322 ret = 0;
5323
Chris Mason3b951512008-04-17 11:29:12 -04005324 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005325 if (existing && (existing->start > start ||
5326 existing->start + existing->len <= start)) {
5327 free_extent_map(existing);
5328 existing = NULL;
5329 }
Chris Mason3b951512008-04-17 11:29:12 -04005330 if (!existing) {
5331 existing = lookup_extent_mapping(em_tree, em->start,
5332 em->len);
5333 if (existing) {
5334 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005335 em, start,
5336 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005337 free_extent_map(existing);
5338 if (err) {
5339 free_extent_map(em);
5340 em = NULL;
5341 }
5342 } else {
5343 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005344 free_extent_map(em);
5345 em = NULL;
5346 }
5347 } else {
5348 free_extent_map(em);
5349 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005350 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005351 }
Chris Masona52d9a82007-08-27 16:49:44 -04005352 }
Chris Mason890871b2009-09-02 16:24:52 -04005353 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005354out:
liubo1abe9b82011-03-24 11:18:59 +00005355
5356 trace_btrfs_get_extent(root, em);
5357
Chris Masonf4219502008-07-22 11:18:09 -04005358 if (path)
5359 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005360 if (trans) {
5361 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005362 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005363 err = ret;
5364 }
Chris Masona52d9a82007-08-27 16:49:44 -04005365 if (err) {
5366 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005367 return ERR_PTR(err);
5368 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005369 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005370 return em;
5371}
5372
Chris Masonec29ed52011-02-23 16:23:20 -05005373struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5374 size_t pg_offset, u64 start, u64 len,
5375 int create)
5376{
5377 struct extent_map *em;
5378 struct extent_map *hole_em = NULL;
5379 u64 range_start = start;
5380 u64 end;
5381 u64 found;
5382 u64 found_end;
5383 int err = 0;
5384
5385 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5386 if (IS_ERR(em))
5387 return em;
5388 if (em) {
5389 /*
5390 * if our em maps to a hole, there might
5391 * actually be delalloc bytes behind it
5392 */
5393 if (em->block_start != EXTENT_MAP_HOLE)
5394 return em;
5395 else
5396 hole_em = em;
5397 }
5398
5399 /* check to see if we've wrapped (len == -1 or similar) */
5400 end = start + len;
5401 if (end < start)
5402 end = (u64)-1;
5403 else
5404 end -= 1;
5405
5406 em = NULL;
5407
5408 /* ok, we didn't find anything, lets look for delalloc */
5409 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5410 end, len, EXTENT_DELALLOC, 1);
5411 found_end = range_start + found;
5412 if (found_end < range_start)
5413 found_end = (u64)-1;
5414
5415 /*
5416 * we didn't find anything useful, return
5417 * the original results from get_extent()
5418 */
5419 if (range_start > end || found_end <= start) {
5420 em = hole_em;
5421 hole_em = NULL;
5422 goto out;
5423 }
5424
5425 /* adjust the range_start to make sure it doesn't
5426 * go backwards from the start they passed in
5427 */
5428 range_start = max(start,range_start);
5429 found = found_end - range_start;
5430
5431 if (found > 0) {
5432 u64 hole_start = start;
5433 u64 hole_len = len;
5434
David Sterba172ddd62011-04-21 00:48:27 +02005435 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005436 if (!em) {
5437 err = -ENOMEM;
5438 goto out;
5439 }
5440 /*
5441 * when btrfs_get_extent can't find anything it
5442 * returns one huge hole
5443 *
5444 * make sure what it found really fits our range, and
5445 * adjust to make sure it is based on the start from
5446 * the caller
5447 */
5448 if (hole_em) {
5449 u64 calc_end = extent_map_end(hole_em);
5450
5451 if (calc_end <= start || (hole_em->start > end)) {
5452 free_extent_map(hole_em);
5453 hole_em = NULL;
5454 } else {
5455 hole_start = max(hole_em->start, start);
5456 hole_len = calc_end - hole_start;
5457 }
5458 }
5459 em->bdev = NULL;
5460 if (hole_em && range_start > hole_start) {
5461 /* our hole starts before our delalloc, so we
5462 * have to return just the parts of the hole
5463 * that go until the delalloc starts
5464 */
5465 em->len = min(hole_len,
5466 range_start - hole_start);
5467 em->start = hole_start;
5468 em->orig_start = hole_start;
5469 /*
5470 * don't adjust block start at all,
5471 * it is fixed at EXTENT_MAP_HOLE
5472 */
5473 em->block_start = hole_em->block_start;
5474 em->block_len = hole_len;
5475 } else {
5476 em->start = range_start;
5477 em->len = found;
5478 em->orig_start = range_start;
5479 em->block_start = EXTENT_MAP_DELALLOC;
5480 em->block_len = found;
5481 }
5482 } else if (hole_em) {
5483 return hole_em;
5484 }
5485out:
5486
5487 free_extent_map(hole_em);
5488 if (err) {
5489 free_extent_map(em);
5490 return ERR_PTR(err);
5491 }
5492 return em;
5493}
5494
Josef Bacik4b46fce2010-05-23 11:00:55 -04005495static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005496 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005497 u64 start, u64 len)
5498{
5499 struct btrfs_root *root = BTRFS_I(inode)->root;
5500 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005501 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5502 struct btrfs_key ins;
5503 u64 alloc_hint;
5504 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005505 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005506
Josef Bacik16d299a2011-04-06 14:53:07 -04005507 /*
5508 * Ok if the extent map we looked up is a hole and is for the exact
5509 * range we want, there is no reason to allocate a new one, however if
5510 * it is not right then we need to free this one and drop the cache for
5511 * our range.
5512 */
5513 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5514 em->len != len) {
5515 free_extent_map(em);
5516 em = NULL;
5517 insert = true;
5518 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5519 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005520
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005521 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005522 if (IS_ERR(trans))
5523 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005524
Chris Mason4cb53002011-05-24 15:35:30 -04005525 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5526 btrfs_add_inode_defrag(trans, inode);
5527
Josef Bacik4b46fce2010-05-23 11:00:55 -04005528 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5529
5530 alloc_hint = get_extent_allocation_hint(inode, start, len);
5531 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005532 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005533 if (ret) {
5534 em = ERR_PTR(ret);
5535 goto out;
5536 }
5537
Josef Bacik4b46fce2010-05-23 11:00:55 -04005538 if (!em) {
David Sterba172ddd62011-04-21 00:48:27 +02005539 em = alloc_extent_map();
Josef Bacik16d299a2011-04-06 14:53:07 -04005540 if (!em) {
5541 em = ERR_PTR(-ENOMEM);
5542 goto out;
5543 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005544 }
5545
5546 em->start = start;
5547 em->orig_start = em->start;
5548 em->len = ins.offset;
5549
5550 em->block_start = ins.objectid;
5551 em->block_len = ins.offset;
5552 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005553
5554 /*
5555 * We need to do this because if we're using the original em we searched
5556 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5557 */
5558 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005559 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5560
Josef Bacik16d299a2011-04-06 14:53:07 -04005561 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005562 write_lock(&em_tree->lock);
5563 ret = add_extent_mapping(em_tree, em);
5564 write_unlock(&em_tree->lock);
5565 if (ret != -EEXIST)
5566 break;
5567 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5568 }
5569
5570 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5571 ins.offset, ins.offset, 0);
5572 if (ret) {
5573 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5574 em = ERR_PTR(ret);
5575 }
5576out:
5577 btrfs_end_transaction(trans, root);
5578 return em;
5579}
5580
Chris Mason46bfbb52010-05-26 11:04:10 -04005581/*
5582 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5583 * block must be cow'd
5584 */
5585static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5586 struct inode *inode, u64 offset, u64 len)
5587{
5588 struct btrfs_path *path;
5589 int ret;
5590 struct extent_buffer *leaf;
5591 struct btrfs_root *root = BTRFS_I(inode)->root;
5592 struct btrfs_file_extent_item *fi;
5593 struct btrfs_key key;
5594 u64 disk_bytenr;
5595 u64 backref_offset;
5596 u64 extent_end;
5597 u64 num_bytes;
5598 int slot;
5599 int found_type;
5600
5601 path = btrfs_alloc_path();
5602 if (!path)
5603 return -ENOMEM;
5604
Li Zefan33345d012011-04-20 10:31:50 +08005605 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005606 offset, 0);
5607 if (ret < 0)
5608 goto out;
5609
5610 slot = path->slots[0];
5611 if (ret == 1) {
5612 if (slot == 0) {
5613 /* can't find the item, must cow */
5614 ret = 0;
5615 goto out;
5616 }
5617 slot--;
5618 }
5619 ret = 0;
5620 leaf = path->nodes[0];
5621 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005622 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005623 key.type != BTRFS_EXTENT_DATA_KEY) {
5624 /* not our file or wrong item type, must cow */
5625 goto out;
5626 }
5627
5628 if (key.offset > offset) {
5629 /* Wrong offset, must cow */
5630 goto out;
5631 }
5632
5633 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5634 found_type = btrfs_file_extent_type(leaf, fi);
5635 if (found_type != BTRFS_FILE_EXTENT_REG &&
5636 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5637 /* not a regular extent, must cow */
5638 goto out;
5639 }
5640 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5641 backref_offset = btrfs_file_extent_offset(leaf, fi);
5642
5643 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5644 if (extent_end < offset + len) {
5645 /* extent doesn't include our full range, must cow */
5646 goto out;
5647 }
5648
5649 if (btrfs_extent_readonly(root, disk_bytenr))
5650 goto out;
5651
5652 /*
5653 * look for other files referencing this extent, if we
5654 * find any we must cow
5655 */
Li Zefan33345d012011-04-20 10:31:50 +08005656 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005657 key.offset - backref_offset, disk_bytenr))
5658 goto out;
5659
5660 /*
5661 * adjust disk_bytenr and num_bytes to cover just the bytes
5662 * in this extent we are about to write. If there
5663 * are any csums in that range we have to cow in order
5664 * to keep the csums correct
5665 */
5666 disk_bytenr += backref_offset;
5667 disk_bytenr += offset - key.offset;
5668 num_bytes = min(offset + len, extent_end) - offset;
5669 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5670 goto out;
5671 /*
5672 * all of the above have passed, it is safe to overwrite this extent
5673 * without cow
5674 */
5675 ret = 1;
5676out:
5677 btrfs_free_path(path);
5678 return ret;
5679}
5680
Josef Bacik4b46fce2010-05-23 11:00:55 -04005681static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5682 struct buffer_head *bh_result, int create)
5683{
5684 struct extent_map *em;
5685 struct btrfs_root *root = BTRFS_I(inode)->root;
5686 u64 start = iblock << inode->i_blkbits;
5687 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005688 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005689
5690 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5691 if (IS_ERR(em))
5692 return PTR_ERR(em);
5693
5694 /*
5695 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5696 * io. INLINE is special, and we could probably kludge it in here, but
5697 * it's still buffered so for safety lets just fall back to the generic
5698 * buffered path.
5699 *
5700 * For COMPRESSED we _have_ to read the entire extent in so we can
5701 * decompress it, so there will be buffering required no matter what we
5702 * do, so go ahead and fallback to buffered.
5703 *
5704 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5705 * to buffered IO. Don't blame me, this is the price we pay for using
5706 * the generic code.
5707 */
5708 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5709 em->block_start == EXTENT_MAP_INLINE) {
5710 free_extent_map(em);
5711 return -ENOTBLK;
5712 }
5713
5714 /* Just a good old fashioned hole, return */
5715 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5716 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5717 free_extent_map(em);
5718 /* DIO will do one hole at a time, so just unlock a sector */
5719 unlock_extent(&BTRFS_I(inode)->io_tree, start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005720 start + root->sectorsize - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005721 return 0;
5722 }
5723
5724 /*
5725 * We don't allocate a new extent in the following cases
5726 *
5727 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5728 * existing extent.
5729 * 2) The extent is marked as PREALLOC. We're good to go here and can
5730 * just use the extent.
5731 *
5732 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005733 if (!create) {
5734 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005735 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005736 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005737
5738 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5739 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5740 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005741 int type;
5742 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005743 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005744
5745 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5746 type = BTRFS_ORDERED_PREALLOC;
5747 else
5748 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005749 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005750 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005751
5752 /*
5753 * we're not going to log anything, but we do need
5754 * to make sure the current transaction stays open
5755 * while we look for nocow cross refs
5756 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005757 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005758 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005759 goto must_cow;
5760
5761 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5762 ret = btrfs_add_ordered_extent_dio(inode, start,
5763 block_start, len, len, type);
5764 btrfs_end_transaction(trans, root);
5765 if (ret) {
5766 free_extent_map(em);
5767 return ret;
5768 }
5769 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005770 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005771 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005772 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005773must_cow:
5774 /*
5775 * this will cow the extent, reset the len in case we changed
5776 * it above
5777 */
5778 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04005779 em = btrfs_new_extent_direct(inode, em, start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04005780 if (IS_ERR(em))
5781 return PTR_ERR(em);
5782 len = min(len, em->len - (start - em->start));
5783unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005784 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5785 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5786 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005787map:
5788 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5789 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005790 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005791 bh_result->b_bdev = em->bdev;
5792 set_buffer_mapped(bh_result);
5793 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5794 set_buffer_new(bh_result);
5795
5796 free_extent_map(em);
5797
5798 return 0;
5799}
5800
5801struct btrfs_dio_private {
5802 struct inode *inode;
5803 u64 logical_offset;
5804 u64 disk_bytenr;
5805 u64 bytes;
5806 u32 *csums;
5807 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005808
5809 /* number of bios pending for this dio */
5810 atomic_t pending_bios;
5811
5812 /* IO errors */
5813 int errors;
5814
5815 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005816};
5817
5818static void btrfs_endio_direct_read(struct bio *bio, int err)
5819{
Miao Xiee65e1532010-11-22 03:04:43 +00005820 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005821 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5822 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005823 struct inode *inode = dip->inode;
5824 struct btrfs_root *root = BTRFS_I(inode)->root;
5825 u64 start;
5826 u32 *private = dip->csums;
5827
5828 start = dip->logical_offset;
5829 do {
5830 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5831 struct page *page = bvec->bv_page;
5832 char *kaddr;
5833 u32 csum = ~(u32)0;
5834 unsigned long flags;
5835
5836 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08005837 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005838 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5839 csum, bvec->bv_len);
5840 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08005841 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005842 local_irq_restore(flags);
5843
5844 flush_dcache_page(bvec->bv_page);
5845 if (csum != *private) {
Li Zefan33345d012011-04-20 10:31:50 +08005846 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04005847 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08005848 (unsigned long long)btrfs_ino(inode),
5849 (unsigned long long)start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005850 csum, *private);
5851 err = -EIO;
5852 }
5853 }
5854
5855 start += bvec->bv_len;
5856 private++;
5857 bvec++;
5858 } while (bvec <= bvec_end);
5859
5860 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005861 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005862 bio->bi_private = dip->private;
5863
5864 kfree(dip->csums);
5865 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005866
5867 /* If we had a csum failure make sure to clear the uptodate flag */
5868 if (err)
5869 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005870 dio_end_io(bio, err);
5871}
5872
5873static void btrfs_endio_direct_write(struct bio *bio, int err)
5874{
5875 struct btrfs_dio_private *dip = bio->bi_private;
5876 struct inode *inode = dip->inode;
5877 struct btrfs_root *root = BTRFS_I(inode)->root;
5878 struct btrfs_trans_handle *trans;
5879 struct btrfs_ordered_extent *ordered = NULL;
5880 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005881 u64 ordered_offset = dip->logical_offset;
5882 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005883 int ret;
5884
5885 if (err)
5886 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005887again:
5888 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5889 &ordered_offset,
5890 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005891 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005892 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005893
5894 BUG_ON(!ordered);
5895
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005896 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005897 if (IS_ERR(trans)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005898 err = -ENOMEM;
5899 goto out;
5900 }
5901 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5902
5903 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5904 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5905 if (!ret)
Chris Mason21151332011-11-10 20:39:08 -05005906 err = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005907 goto out;
5908 }
5909
5910 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5911 ordered->file_offset + ordered->len - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005912 &cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005913
5914 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5915 ret = btrfs_mark_extent_written(trans, inode,
5916 ordered->file_offset,
5917 ordered->file_offset +
5918 ordered->len);
5919 if (ret) {
5920 err = ret;
5921 goto out_unlock;
5922 }
5923 } else {
5924 ret = insert_reserved_file_extent(trans, inode,
5925 ordered->file_offset,
5926 ordered->start,
5927 ordered->disk_len,
5928 ordered->len,
5929 ordered->len,
5930 0, 0, 0,
5931 BTRFS_FILE_EXTENT_REG);
5932 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5933 ordered->file_offset, ordered->len);
5934 if (ret) {
5935 err = ret;
5936 WARN_ON(1);
5937 goto out_unlock;
5938 }
5939 }
5940
5941 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005942 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
Miao Xiea39f7522011-09-11 10:52:25 -04005943 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags))
Chris Mason21151332011-11-10 20:39:08 -05005944 btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005945 ret = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005946out_unlock:
5947 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5948 ordered->file_offset + ordered->len - 1,
5949 &cached_state, GFP_NOFS);
5950out:
5951 btrfs_delalloc_release_metadata(inode, ordered->len);
5952 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005953 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005954 btrfs_put_ordered_extent(ordered);
5955 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05005956
5957out_test:
5958 /*
5959 * our bio might span multiple ordered extents. If we haven't
5960 * completed the accounting for the whole dio, go back and try again
5961 */
5962 if (ordered_offset < dip->logical_offset + dip->bytes) {
5963 ordered_bytes = dip->logical_offset + dip->bytes -
5964 ordered_offset;
5965 goto again;
5966 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005967out_done:
5968 bio->bi_private = dip->private;
5969
5970 kfree(dip->csums);
5971 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005972
5973 /* If we had an error make sure to clear the uptodate flag */
5974 if (err)
5975 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005976 dio_end_io(bio, err);
5977}
5978
Chris Masoneaf25d92010-05-25 09:48:28 -04005979static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5980 struct bio *bio, int mirror_num,
5981 unsigned long bio_flags, u64 offset)
5982{
5983 int ret;
5984 struct btrfs_root *root = BTRFS_I(inode)->root;
5985 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005986 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04005987 return 0;
5988}
5989
Miao Xiee65e1532010-11-22 03:04:43 +00005990static void btrfs_end_dio_bio(struct bio *bio, int err)
5991{
5992 struct btrfs_dio_private *dip = bio->bi_private;
5993
5994 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08005995 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00005996 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08005997 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00005998 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00005999 dip->errors = 1;
6000
6001 /*
6002 * before atomic variable goto zero, we must make sure
6003 * dip->errors is perceived to be set.
6004 */
6005 smp_mb__before_atomic_dec();
6006 }
6007
6008 /* if there are more bios still pending for this dio, just exit */
6009 if (!atomic_dec_and_test(&dip->pending_bios))
6010 goto out;
6011
6012 if (dip->errors)
6013 bio_io_error(dip->orig_bio);
6014 else {
6015 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6016 bio_endio(dip->orig_bio, 0);
6017 }
6018out:
6019 bio_put(bio);
6020}
6021
6022static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6023 u64 first_sector, gfp_t gfp_flags)
6024{
6025 int nr_vecs = bio_get_nr_vecs(bdev);
6026 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6027}
6028
6029static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6030 int rw, u64 file_offset, int skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006031 u32 *csums, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006032{
6033 int write = rw & REQ_WRITE;
6034 struct btrfs_root *root = BTRFS_I(inode)->root;
6035 int ret;
6036
6037 bio_get(bio);
6038 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6039 if (ret)
6040 goto err;
6041
Josef Bacik1ae39932011-04-06 14:41:34 -04006042 if (skip_sum)
6043 goto map;
6044
6045 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006046 ret = btrfs_wq_submit_bio(root->fs_info,
6047 inode, rw, bio, 0, 0,
6048 file_offset,
6049 __btrfs_submit_bio_start_direct_io,
6050 __btrfs_submit_bio_done);
6051 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006052 } else if (write) {
6053 /*
6054 * If we aren't doing async submit, calculate the csum of the
6055 * bio now.
6056 */
6057 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6058 if (ret)
6059 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006060 } else if (!skip_sum) {
6061 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
Miao Xiee65e1532010-11-22 03:04:43 +00006062 file_offset, csums);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006063 if (ret)
6064 goto err;
6065 }
Miao Xiee65e1532010-11-22 03:04:43 +00006066
Josef Bacik1ae39932011-04-06 14:41:34 -04006067map:
6068 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006069err:
6070 bio_put(bio);
6071 return ret;
6072}
6073
6074static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6075 int skip_sum)
6076{
6077 struct inode *inode = dip->inode;
6078 struct btrfs_root *root = BTRFS_I(inode)->root;
6079 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6080 struct bio *bio;
6081 struct bio *orig_bio = dip->orig_bio;
6082 struct bio_vec *bvec = orig_bio->bi_io_vec;
6083 u64 start_sector = orig_bio->bi_sector;
6084 u64 file_offset = dip->logical_offset;
6085 u64 submit_len = 0;
6086 u64 map_length;
6087 int nr_pages = 0;
6088 u32 *csums = dip->csums;
6089 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006090 int async_submit = 0;
Josef Bacik98bc3142011-03-22 11:00:46 -04006091 int write = rw & REQ_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00006092
Miao Xiee65e1532010-11-22 03:04:43 +00006093 map_length = orig_bio->bi_size;
6094 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6095 &map_length, NULL, 0);
6096 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006097 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006098 return -EIO;
6099 }
6100
Josef Bacik02f57c72011-04-06 14:25:44 -04006101 if (map_length >= orig_bio->bi_size) {
6102 bio = orig_bio;
6103 goto submit;
6104 }
6105
Josef Bacik1ae39932011-04-06 14:41:34 -04006106 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006107 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6108 if (!bio)
6109 return -ENOMEM;
6110 bio->bi_private = dip;
6111 bio->bi_end_io = btrfs_end_dio_bio;
6112 atomic_inc(&dip->pending_bios);
6113
Miao Xiee65e1532010-11-22 03:04:43 +00006114 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6115 if (unlikely(map_length < submit_len + bvec->bv_len ||
6116 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6117 bvec->bv_offset) < bvec->bv_len)) {
6118 /*
6119 * inc the count before we submit the bio so
6120 * we know the end IO handler won't happen before
6121 * we inc the count. Otherwise, the dip might get freed
6122 * before we're done setting it up
6123 */
6124 atomic_inc(&dip->pending_bios);
6125 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6126 file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006127 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006128 if (ret) {
6129 bio_put(bio);
6130 atomic_dec(&dip->pending_bios);
6131 goto out_err;
6132 }
6133
Josef Bacik98bc3142011-03-22 11:00:46 -04006134 /* Write's use the ordered csums */
6135 if (!write && !skip_sum)
Miao Xiee65e1532010-11-22 03:04:43 +00006136 csums = csums + nr_pages;
6137 start_sector += submit_len >> 9;
6138 file_offset += submit_len;
6139
6140 submit_len = 0;
6141 nr_pages = 0;
6142
6143 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6144 start_sector, GFP_NOFS);
6145 if (!bio)
6146 goto out_err;
6147 bio->bi_private = dip;
6148 bio->bi_end_io = btrfs_end_dio_bio;
6149
6150 map_length = orig_bio->bi_size;
6151 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6152 &map_length, NULL, 0);
6153 if (ret) {
6154 bio_put(bio);
6155 goto out_err;
6156 }
6157 } else {
6158 submit_len += bvec->bv_len;
6159 nr_pages ++;
6160 bvec++;
6161 }
6162 }
6163
Josef Bacik02f57c72011-04-06 14:25:44 -04006164submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006165 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006166 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006167 if (!ret)
6168 return 0;
6169
6170 bio_put(bio);
6171out_err:
6172 dip->errors = 1;
6173 /*
6174 * before atomic variable goto zero, we must
6175 * make sure dip->errors is perceived to be set.
6176 */
6177 smp_mb__before_atomic_dec();
6178 if (atomic_dec_and_test(&dip->pending_bios))
6179 bio_io_error(dip->orig_bio);
6180
6181 /* bio_end_io() will handle error, so we needn't return it */
6182 return 0;
6183}
6184
Josef Bacik4b46fce2010-05-23 11:00:55 -04006185static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6186 loff_t file_offset)
6187{
6188 struct btrfs_root *root = BTRFS_I(inode)->root;
6189 struct btrfs_dio_private *dip;
6190 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006191 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006192 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006193 int ret = 0;
6194
6195 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6196
6197 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6198 if (!dip) {
6199 ret = -ENOMEM;
6200 goto free_ordered;
6201 }
6202 dip->csums = NULL;
6203
Josef Bacik98bc3142011-03-22 11:00:46 -04006204 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6205 if (!write && !skip_sum) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006206 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6207 if (!dip->csums) {
Daniel J Bluemanb4966b72011-03-09 16:46:42 +00006208 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006209 ret = -ENOMEM;
6210 goto free_ordered;
6211 }
6212 }
6213
6214 dip->private = bio->bi_private;
6215 dip->inode = inode;
6216 dip->logical_offset = file_offset;
6217
Josef Bacik4b46fce2010-05-23 11:00:55 -04006218 dip->bytes = 0;
6219 do {
6220 dip->bytes += bvec->bv_len;
6221 bvec++;
6222 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6223
Chris Mason46bfbb52010-05-26 11:04:10 -04006224 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006225 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006226 dip->errors = 0;
6227 dip->orig_bio = bio;
6228 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006229
6230 if (write)
6231 bio->bi_end_io = btrfs_endio_direct_write;
6232 else
6233 bio->bi_end_io = btrfs_endio_direct_read;
6234
Miao Xiee65e1532010-11-22 03:04:43 +00006235 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6236 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006237 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006238free_ordered:
6239 /*
6240 * If this is a write, we need to clean up the reserved space and kill
6241 * the ordered extent.
6242 */
6243 if (write) {
6244 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006245 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006246 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6247 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6248 btrfs_free_reserved_extent(root, ordered->start,
6249 ordered->disk_len);
6250 btrfs_put_ordered_extent(ordered);
6251 btrfs_put_ordered_extent(ordered);
6252 }
6253 bio_endio(bio, ret);
6254}
6255
Chris Mason5a5f79b2010-05-26 21:33:37 -04006256static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6257 const struct iovec *iov, loff_t offset,
6258 unsigned long nr_segs)
6259{
6260 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006261 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006262 size_t size;
6263 unsigned long addr;
6264 unsigned blocksize_mask = root->sectorsize - 1;
6265 ssize_t retval = -EINVAL;
6266 loff_t end = offset;
6267
6268 if (offset & blocksize_mask)
6269 goto out;
6270
6271 /* Check the memory alignment. Blocks cannot straddle pages */
6272 for (seg = 0; seg < nr_segs; seg++) {
6273 addr = (unsigned long)iov[seg].iov_base;
6274 size = iov[seg].iov_len;
6275 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006276 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006277 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006278
6279 /* If this is a write we don't need to check anymore */
6280 if (rw & WRITE)
6281 continue;
6282
6283 /*
6284 * Check to make sure we don't have duplicate iov_base's in this
6285 * iovec, if so return EINVAL, otherwise we'll get csum errors
6286 * when reading back.
6287 */
6288 for (i = seg + 1; i < nr_segs; i++) {
6289 if (iov[seg].iov_base == iov[i].iov_base)
6290 goto out;
6291 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006292 }
6293 retval = 0;
6294out:
6295 return retval;
6296}
Chris Mason16432982008-04-10 10:23:21 -04006297static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6298 const struct iovec *iov, loff_t offset,
6299 unsigned long nr_segs)
6300{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006301 struct file *file = iocb->ki_filp;
6302 struct inode *inode = file->f_mapping->host;
6303 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006304 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006305 u64 lockstart, lockend;
6306 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006307 int writing = rw & WRITE;
6308 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006309 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006310
Chris Mason5a5f79b2010-05-26 21:33:37 -04006311 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6312 offset, nr_segs)) {
6313 return 0;
6314 }
6315
Josef Bacik4b46fce2010-05-23 11:00:55 -04006316 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006317 lockend = offset + count - 1;
6318
6319 if (writing) {
6320 ret = btrfs_delalloc_reserve_space(inode, count);
6321 if (ret)
6322 goto out;
6323 }
Chris Mason4845e442010-05-25 20:56:50 -04006324
Josef Bacik4b46fce2010-05-23 11:00:55 -04006325 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006326 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006327 0, &cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006328 /*
6329 * We're concerned with the entire range that we're going to be
6330 * doing DIO to, so we need to make sure theres no ordered
6331 * extents in this range.
6332 */
6333 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6334 lockend - lockstart + 1);
6335 if (!ordered)
6336 break;
Chris Mason4845e442010-05-25 20:56:50 -04006337 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6338 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006339 btrfs_start_ordered_extent(inode, ordered, 1);
6340 btrfs_put_ordered_extent(ordered);
6341 cond_resched();
6342 }
6343
Chris Mason4845e442010-05-25 20:56:50 -04006344 /*
6345 * we don't use btrfs_set_extent_delalloc because we don't want
6346 * the dirty or uptodate bits
6347 */
6348 if (writing) {
6349 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6350 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01006351 EXTENT_DELALLOC, NULL, &cached_state,
Chris Mason4845e442010-05-25 20:56:50 -04006352 GFP_NOFS);
6353 if (ret) {
6354 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6355 lockend, EXTENT_LOCKED | write_bits,
6356 1, 0, &cached_state, GFP_NOFS);
6357 goto out;
6358 }
6359 }
6360
6361 free_extent_state(cached_state);
6362 cached_state = NULL;
6363
Chris Mason5a5f79b2010-05-26 21:33:37 -04006364 ret = __blockdev_direct_IO(rw, iocb, inode,
6365 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6366 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6367 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006368
6369 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006370 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6371 offset + iov_length(iov, nr_segs) - 1,
6372 EXTENT_LOCKED | write_bits, 1, 0,
6373 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006374 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6375 /*
6376 * We're falling back to buffered, unlock the section we didn't
6377 * do IO on.
6378 */
Chris Mason4845e442010-05-25 20:56:50 -04006379 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6380 offset + iov_length(iov, nr_segs) - 1,
6381 EXTENT_LOCKED | write_bits, 1, 0,
6382 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006383 }
Chris Mason4845e442010-05-25 20:56:50 -04006384out:
6385 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006386 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006387}
6388
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006389static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6390 __u64 start, __u64 len)
6391{
Chris Masonec29ed52011-02-23 16:23:20 -05006392 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006393}
6394
Chris Mason9ebefb182007-06-15 13:50:00 -04006395int btrfs_readpage(struct file *file, struct page *page)
6396{
Chris Masond1310b22008-01-24 16:13:08 -05006397 struct extent_io_tree *tree;
6398 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006399 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006400}
Chris Mason1832a6d2007-12-21 16:27:21 -05006401
Chris Mason39279cc2007-06-12 06:35:45 -04006402static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6403{
Chris Masond1310b22008-01-24 16:13:08 -05006404 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04006405
6406
6407 if (current->flags & PF_MEMALLOC) {
6408 redirty_page_for_writepage(wbc, page);
6409 unlock_page(page);
6410 return 0;
6411 }
Chris Masond1310b22008-01-24 16:13:08 -05006412 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006413 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6414}
Chris Mason39279cc2007-06-12 06:35:45 -04006415
Chris Masonf4219502008-07-22 11:18:09 -04006416int btrfs_writepages(struct address_space *mapping,
6417 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04006418{
Chris Masond1310b22008-01-24 16:13:08 -05006419 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006420
Chris Masond1310b22008-01-24 16:13:08 -05006421 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04006422 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6423}
6424
Chris Mason3ab2fb52007-11-08 10:59:22 -05006425static int
6426btrfs_readpages(struct file *file, struct address_space *mapping,
6427 struct list_head *pages, unsigned nr_pages)
6428{
Chris Masond1310b22008-01-24 16:13:08 -05006429 struct extent_io_tree *tree;
6430 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006431 return extent_readpages(tree, mapping, pages, nr_pages,
6432 btrfs_get_extent);
6433}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006434static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006435{
Chris Masond1310b22008-01-24 16:13:08 -05006436 struct extent_io_tree *tree;
6437 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006438 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006439
Chris Masond1310b22008-01-24 16:13:08 -05006440 tree = &BTRFS_I(page->mapping->host)->io_tree;
6441 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006442 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006443 if (ret == 1) {
6444 ClearPagePrivate(page);
6445 set_page_private(page, 0);
6446 page_cache_release(page);
6447 }
6448 return ret;
6449}
Chris Mason39279cc2007-06-12 06:35:45 -04006450
Chris Masone6dcd2d2008-07-17 12:53:50 -04006451static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6452{
Chris Mason98509cf2008-09-11 15:51:43 -04006453 if (PageWriteback(page) || PageDirty(page))
6454 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006455 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006456}
6457
Chris Masona52d9a82007-08-27 16:49:44 -04006458static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6459{
Chris Masond1310b22008-01-24 16:13:08 -05006460 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006461 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006462 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006463 u64 page_start = page_offset(page);
6464 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006465
Chris Mason8b62b722009-09-02 16:53:46 -04006466
6467 /*
6468 * we have the page locked, so new writeback can't start,
6469 * and the dirty bit won't be cleared while we are here.
6470 *
6471 * Wait for IO on this page so that we can safely clear
6472 * the PagePrivate2 bit and do ordered accounting
6473 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006474 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006475
Chris Masond1310b22008-01-24 16:13:08 -05006476 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006477 if (offset) {
6478 btrfs_releasepage(page, GFP_NOFS);
6479 return;
6480 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006481 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006482 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6483 page_offset(page));
6484 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006485 /*
6486 * IO on this page will never be started, so we need
6487 * to account for any ordered extents now
6488 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006489 clear_extent_bit(tree, page_start, page_end,
6490 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006491 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006492 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006493 /*
6494 * whoever cleared the private bit is responsible
6495 * for the finish_ordered_io
6496 */
6497 if (TestClearPagePrivate2(page)) {
6498 btrfs_finish_ordered_io(page->mapping->host,
6499 page_start, page_end);
6500 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006501 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006502 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006503 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006504 }
6505 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006506 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006507 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006508 __btrfs_releasepage(page, GFP_NOFS);
6509
Chris Mason4a096752008-07-21 10:29:44 -04006510 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006511 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006512 ClearPagePrivate(page);
6513 set_page_private(page, 0);
6514 page_cache_release(page);
6515 }
Chris Mason39279cc2007-06-12 06:35:45 -04006516}
6517
Chris Mason9ebefb182007-06-15 13:50:00 -04006518/*
6519 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6520 * called from a page fault handler when a page is first dirtied. Hence we must
6521 * be careful to check for EOF conditions here. We set the page up correctly
6522 * for a written page which means we get ENOSPC checking when writing into
6523 * holes and correct delalloc and unwritten extent mapping on filesystems that
6524 * support these features.
6525 *
6526 * We are not allowed to take the i_mutex here so we have to play games to
6527 * protect against truncate races as the page could now be beyond EOF. Because
6528 * vmtruncate() writes the inode size before removing pages, once we have the
6529 * page lock we can determine safely if the page is beyond EOF. If it is not
6530 * beyond EOF, then the page is guaranteed safe against truncation until we
6531 * unlock the page.
6532 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006533int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006534{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006535 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006536 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006537 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006538 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6539 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006540 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006541 char *kaddr;
6542 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006543 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006544 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006545 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006546 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006547 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006548
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006549 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006550 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006551 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006552 reserved = 1;
6553 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006554 if (ret) {
6555 if (ret == -ENOMEM)
6556 ret = VM_FAULT_OOM;
6557 else /* -ENOSPC, -EIO, etc */
6558 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006559 if (reserved)
6560 goto out;
6561 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006562 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006563
Nick Piggin56a76f82009-03-31 15:23:23 -07006564 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006565again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006566 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006567 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006568 page_start = page_offset(page);
6569 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006570
Chris Mason9ebefb182007-06-15 13:50:00 -04006571 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006572 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006573 /* page got truncated out from underneath us */
6574 goto out_unlock;
6575 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006576 wait_on_page_writeback(page);
6577
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006578 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006579 set_page_extent_mapped(page);
6580
Chris Masoneb84ae02008-07-17 13:53:27 -04006581 /*
6582 * we can't set the delalloc bits if there are pending ordered
6583 * extents. Drop our locks and wait for them to finish
6584 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006585 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6586 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006587 unlock_extent_cached(io_tree, page_start, page_end,
6588 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006589 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006590 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006591 btrfs_put_ordered_extent(ordered);
6592 goto again;
6593 }
6594
Josef Bacikfbf19082009-10-01 17:10:23 -04006595 /*
6596 * XXX - page_mkwrite gets called every time the page is dirtied, even
6597 * if it was already dirty, so for space accounting reasons we need to
6598 * clear any delalloc bits for the range we are fixing to save. There
6599 * is probably a better way to do this, but for now keep consistent with
6600 * prepare_pages in the normal write path.
6601 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006602 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006603 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006604 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006605
Josef Bacik2ac55d42010-02-03 19:33:23 +00006606 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6607 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006608 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006609 unlock_extent_cached(io_tree, page_start, page_end,
6610 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006611 ret = VM_FAULT_SIGBUS;
6612 goto out_unlock;
6613 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006614 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006615
6616 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006617 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006618 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006619 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006620 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006621
Chris Masone6dcd2d2008-07-17 12:53:50 -04006622 if (zero_start != PAGE_CACHE_SIZE) {
6623 kaddr = kmap(page);
6624 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6625 flush_dcache_page(page);
6626 kunmap(page);
6627 }
Chris Mason247e7432008-07-17 12:53:51 -04006628 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006629 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006630 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006631
Chris Mason257c62e2009-10-13 13:21:08 -04006632 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6633 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6634
Josef Bacik2ac55d42010-02-03 19:33:23 +00006635 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006636
6637out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006638 if (!ret)
6639 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006640 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006641out:
Josef Bacikec39e182012-01-12 19:10:12 -05006642 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006643out_noreserve:
Chris Mason9ebefb182007-06-15 13:50:00 -04006644 return ret;
6645}
6646
Josef Bacika41ad392011-01-31 15:30:16 -05006647static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006648{
6649 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006650 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006651 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006652 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006653 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006654 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006655 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006656 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006657
Josef Bacik5d5e1032009-10-13 16:46:49 -04006658 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6659 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006660 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006661
Chris Mason4a096752008-07-21 10:29:44 -04006662 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006663 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006664
Josef Bacikfcb80c22011-05-03 10:40:22 -04006665 /*
6666 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6667 * 3 things going on here
6668 *
6669 * 1) We need to reserve space for our orphan item and the space to
6670 * delete our orphan item. Lord knows we don't want to have a dangling
6671 * orphan item because we didn't reserve space to remove it.
6672 *
6673 * 2) We need to reserve space to update our inode.
6674 *
6675 * 3) We need to have something to cache all the space that is going to
6676 * be free'd up by the truncate operation, but also have some slack
6677 * space reserved in case it uses space during the truncate (thank you
6678 * very much snapshotting).
6679 *
6680 * And we need these to all be seperate. The fact is we can use alot of
6681 * space doing the truncate, and we have no earthly idea how much space
6682 * we will use, so we need the truncate reservation to be seperate so it
6683 * doesn't end up using space reserved for updating the inode or
6684 * removing the orphan item. We also need to be able to stop the
6685 * transaction and start a new one, which means we need to be able to
6686 * update the inode several times, and we have no idea of knowing how
6687 * many times that will be, so we can't just reserve 1 item for the
6688 * entirety of the opration, so that has to be done seperately as well.
6689 * Then there is the orphan item, which does indeed need to be held on
6690 * to for the whole operation, and we need nobody to touch this reserved
6691 * space except the orphan code.
6692 *
6693 * So that leaves us with
6694 *
6695 * 1) root->orphan_block_rsv - for the orphan deletion.
6696 * 2) rsv - for the truncate reservation, which we will steal from the
6697 * transaction reservation.
6698 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6699 * updating the inode.
6700 */
6701 rsv = btrfs_alloc_block_rsv(root);
6702 if (!rsv)
6703 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006704 rsv->size = min_size;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006705
Josef Bacik907cbce2011-08-08 13:46:15 -04006706 /*
Josef Bacik07127182011-08-19 10:29:59 -04006707 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006708 * 1 for the orphan item we're going to add
6709 * 1 for the orphan item deletion
6710 * 1 for updating the inode.
6711 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04006712 trans = btrfs_start_transaction(root, 4);
6713 if (IS_ERR(trans)) {
6714 err = PTR_ERR(trans);
6715 goto out;
6716 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006717
Josef Bacik907cbce2011-08-08 13:46:15 -04006718 /* Migrate the slack space for the truncate to our reserve */
6719 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6720 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006721 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006722
6723 ret = btrfs_orphan_add(trans, inode);
6724 if (ret) {
6725 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006726 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006727 }
6728
Chris Mason5a3f23d2009-03-31 13:27:11 -04006729 /*
6730 * setattr is responsible for setting the ordered_data_close flag,
6731 * but that is only tested during the last file release. That
6732 * could happen well after the next commit, leaving a great big
6733 * window where new writes may get lost if someone chooses to write
6734 * to this file after truncating to zero
6735 *
6736 * The inode doesn't have any dirty data here, and so if we commit
6737 * this is a noop. If someone immediately starts writing to the inode
6738 * it is very likely we'll catch some of their writes in this
6739 * transaction, and the commit will find this file on the ordered
6740 * data list with good things to send down.
6741 *
6742 * This is a best effort solution, there is still a window where
6743 * using truncate to replace the contents of the file will
6744 * end up with a zero length file after a crash.
6745 */
6746 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6747 btrfs_add_ordered_operation(trans, root, inode);
6748
Yan, Zheng80825102009-11-12 09:35:36 +00006749 while (1) {
Josef Bacik36ba0222011-10-18 12:15:48 -04006750 ret = btrfs_block_rsv_refill(root, rsv, min_size);
Josef Bacik907cbce2011-08-08 13:46:15 -04006751 if (ret) {
6752 /*
6753 * This can only happen with the original transaction we
6754 * started above, every other time we shouldn't have a
6755 * transaction started yet.
6756 */
6757 if (ret == -EAGAIN)
6758 goto end_trans;
6759 err = ret;
6760 break;
6761 }
6762
Yan, Zhengd68fc572010-05-16 10:49:58 -04006763 if (!trans) {
Josef Bacik907cbce2011-08-08 13:46:15 -04006764 /* Just need the 1 for updating the inode */
6765 trans = btrfs_start_transaction(root, 1);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006766 if (IS_ERR(trans)) {
Josef Bacik7041ee92011-12-09 13:26:22 -05006767 ret = err = PTR_ERR(trans);
6768 trans = NULL;
6769 break;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006770 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006771 }
6772
Josef Bacik907cbce2011-08-08 13:46:15 -04006773 trans->block_rsv = rsv;
6774
Yan, Zheng80825102009-11-12 09:35:36 +00006775 ret = btrfs_truncate_inode_items(trans, root, inode,
6776 inode->i_size,
6777 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006778 if (ret != -EAGAIN) {
6779 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006780 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006781 }
Chris Mason39279cc2007-06-12 06:35:45 -04006782
Josef Bacikfcb80c22011-05-03 10:40:22 -04006783 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006784 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006785 if (ret) {
6786 err = ret;
6787 break;
6788 }
Josef Bacik907cbce2011-08-08 13:46:15 -04006789end_trans:
Yan, Zheng80825102009-11-12 09:35:36 +00006790 nr = trans->blocks_used;
6791 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006792 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006793 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006794 }
6795
6796 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04006797 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006798 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006799 if (ret)
6800 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006801 } else if (ret && inode->i_nlink > 0) {
6802 /*
6803 * Failed to do the truncate, remove us from the in memory
6804 * orphan list.
6805 */
6806 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006807 }
6808
Chris Mason917c16b2011-11-08 14:49:59 -05006809 if (trans) {
6810 trans->block_rsv = &root->fs_info->trans_block_rsv;
6811 ret = btrfs_update_inode(trans, root, inode);
6812 if (ret && !err)
6813 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006814
Chris Mason917c16b2011-11-08 14:49:59 -05006815 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006816 ret = btrfs_end_transaction(trans, root);
Chris Mason917c16b2011-11-08 14:49:59 -05006817 btrfs_btree_balance_dirty(root, nr);
6818 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04006819
6820out:
6821 btrfs_free_block_rsv(root, rsv);
6822
Josef Bacik3893e332011-01-31 16:03:11 -05006823 if (ret && !err)
6824 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05006825
Josef Bacik3893e332011-01-31 16:03:11 -05006826 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006827}
6828
Sven Wegener3b963622008-06-09 21:57:42 -04006829/*
Chris Masond352ac62008-09-29 15:18:18 -04006830 * create a new subvolume directory/inode (helper for the ioctl).
6831 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006832int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006833 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04006834{
Chris Mason39279cc2007-06-12 06:35:45 -04006835 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006836 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006837 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006838
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01006839 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
6840 new_dirid, new_dirid,
6841 S_IFDIR | (~current_umask() & S_IRWXUGO),
6842 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006843 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006844 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006845 inode->i_op = &btrfs_dir_inode_operations;
6846 inode->i_fop = &btrfs_dir_file_operations;
6847
Miklos Szeredibfe86842011-10-28 14:13:29 +02006848 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04006849 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006850
Yan, Zheng76dda932009-09-21 16:00:26 -04006851 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006852
Yan, Zheng76dda932009-09-21 16:00:26 -04006853 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07006854 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006855}
6856
Chris Mason39279cc2007-06-12 06:35:45 -04006857struct inode *btrfs_alloc_inode(struct super_block *sb)
6858{
6859 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006860 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006861
6862 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6863 if (!ei)
6864 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006865
6866 ei->root = NULL;
6867 ei->space_info = NULL;
6868 ei->generation = 0;
6869 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006870 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006871 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006872 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006873 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006874 ei->disk_i_size = 0;
6875 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04006876 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006877 ei->index_cnt = (u64)-1;
6878 ei->last_unlink_trans = 0;
6879
Josef Bacik9e0baf62011-07-15 15:16:44 +00006880 spin_lock_init(&ei->lock);
6881 ei->outstanding_extents = 0;
6882 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006883
6884 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006885 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006886 ei->dummy_inode = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04006887 ei->in_defrag = 0;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05006888 ei->delalloc_meta_reserved = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006889 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006890
Miao Xie16cdcec2011-04-22 18:12:22 +08006891 ei->delayed_node = NULL;
6892
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006893 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02006894 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02006895 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6896 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006897 ei->io_tree.track_uptodate = 1;
6898 ei->io_failure_tree.track_uptodate = 1;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006899 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05006900 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006901 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006902 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006903 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006904 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006905 RB_CLEAR_NODE(&ei->rb_node);
6906
6907 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006908}
6909
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006910static void btrfs_i_callback(struct rcu_head *head)
6911{
6912 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006913 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6914}
6915
Chris Mason39279cc2007-06-12 06:35:45 -04006916void btrfs_destroy_inode(struct inode *inode)
6917{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006918 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006919 struct btrfs_root *root = BTRFS_I(inode)->root;
6920
Chris Mason39279cc2007-06-12 06:35:45 -04006921 WARN_ON(!list_empty(&inode->i_dentry));
6922 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006923 WARN_ON(BTRFS_I(inode)->outstanding_extents);
6924 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04006925 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
6926 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04006927
Chris Mason5a3f23d2009-03-31 13:27:11 -04006928 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006929 * This can happen where we create an inode, but somebody else also
6930 * created the same inode and we need to destroy the one we already
6931 * created.
6932 */
6933 if (!root)
6934 goto free;
6935
6936 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006937 * Make sure we're properly removed from the ordered operation
6938 * lists.
6939 */
6940 smp_mb();
6941 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6942 spin_lock(&root->fs_info->ordered_extent_lock);
6943 list_del_init(&BTRFS_I(inode)->ordered_operations);
6944 spin_unlock(&root->fs_info->ordered_extent_lock);
6945 }
6946
Yan, Zhengd68fc572010-05-16 10:49:58 -04006947 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006948 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Li Zefan33345d012011-04-20 10:31:50 +08006949 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6950 (unsigned long long)btrfs_ino(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00006951 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006952 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006953 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006954
Chris Masond3977122009-01-05 21:25:51 -05006955 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006956 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6957 if (!ordered)
6958 break;
6959 else {
Chris Masond3977122009-01-05 21:25:51 -05006960 printk(KERN_ERR "btrfs found ordered "
6961 "extent %llu %llu on inode cleanup\n",
6962 (unsigned long long)ordered->file_offset,
6963 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006964 btrfs_remove_ordered_extent(inode, ordered);
6965 btrfs_put_ordered_extent(ordered);
6966 btrfs_put_ordered_extent(ordered);
6967 }
6968 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006969 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006970 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006971free:
Miao Xie16cdcec2011-04-22 18:12:22 +08006972 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006973 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04006974}
6975
Al Viro45321ac2010-06-07 13:43:19 -04006976int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006977{
6978 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006979
Josef Bacik0af3d002010-06-21 14:48:16 -04006980 if (btrfs_root_refs(&root->root_item) == 0 &&
Chris Mason2cf85722011-07-26 15:35:09 -04006981 !btrfs_is_free_space_inode(root, inode))
Al Viro45321ac2010-06-07 13:43:19 -04006982 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006983 else
Al Viro45321ac2010-06-07 13:43:19 -04006984 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006985}
6986
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006987static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006988{
6989 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6990
6991 inode_init_once(&ei->vfs_inode);
6992}
6993
6994void btrfs_destroy_cachep(void)
6995{
6996 if (btrfs_inode_cachep)
6997 kmem_cache_destroy(btrfs_inode_cachep);
6998 if (btrfs_trans_handle_cachep)
6999 kmem_cache_destroy(btrfs_trans_handle_cachep);
7000 if (btrfs_transaction_cachep)
7001 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007002 if (btrfs_path_cachep)
7003 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007004 if (btrfs_free_space_cachep)
7005 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007006}
7007
7008int btrfs_init_cachep(void)
7009{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007010 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
7011 sizeof(struct btrfs_inode), 0,
7012 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007013 if (!btrfs_inode_cachep)
7014 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007015
7016 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
7017 sizeof(struct btrfs_trans_handle), 0,
7018 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007019 if (!btrfs_trans_handle_cachep)
7020 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007021
7022 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
7023 sizeof(struct btrfs_transaction), 0,
7024 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007025 if (!btrfs_transaction_cachep)
7026 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007027
7028 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
7029 sizeof(struct btrfs_path), 0,
7030 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007031 if (!btrfs_path_cachep)
7032 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007033
Josef Bacikdc89e982011-01-28 17:05:48 -05007034 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
7035 sizeof(struct btrfs_free_space), 0,
7036 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7037 if (!btrfs_free_space_cachep)
7038 goto fail;
7039
Chris Mason39279cc2007-06-12 06:35:45 -04007040 return 0;
7041fail:
7042 btrfs_destroy_cachep();
7043 return -ENOMEM;
7044}
7045
7046static int btrfs_getattr(struct vfsmount *mnt,
7047 struct dentry *dentry, struct kstat *stat)
7048{
7049 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007050 u32 blocksize = inode->i_sb->s_blocksize;
7051
Chris Mason39279cc2007-06-12 06:35:45 -04007052 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007053 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007054 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007055 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7056 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007057 return 0;
7058}
7059
Liu Bo75e7cb72011-03-22 10:12:20 +00007060/*
7061 * If a file is moved, it will inherit the cow and compression flags of the new
7062 * directory.
7063 */
7064static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7065{
7066 struct btrfs_inode *b_dir = BTRFS_I(dir);
7067 struct btrfs_inode *b_inode = BTRFS_I(inode);
7068
7069 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7070 b_inode->flags |= BTRFS_INODE_NODATACOW;
7071 else
7072 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7073
7074 if (b_dir->flags & BTRFS_INODE_COMPRESS)
7075 b_inode->flags |= BTRFS_INODE_COMPRESS;
7076 else
7077 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7078}
7079
Chris Masond3977122009-01-05 21:25:51 -05007080static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7081 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007082{
7083 struct btrfs_trans_handle *trans;
7084 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007085 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007086 struct inode *new_inode = new_dentry->d_inode;
7087 struct inode *old_inode = old_dentry->d_inode;
7088 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007089 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007090 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007091 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007092 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007093
Li Zefan33345d012011-04-20 10:31:50 +08007094 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007095 return -EPERM;
7096
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007097 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007098 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007099 return -EXDEV;
7100
Li Zefan33345d012011-04-20 10:31:50 +08007101 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7102 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007103 return -ENOTEMPTY;
7104
Chris Mason39279cc2007-06-12 06:35:45 -04007105 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007106 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007107 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007108 /*
7109 * we're using rename to replace one file with another.
7110 * and the replacement file is large. Start IO on it now so
7111 * we don't add too much work to the end of the transaction
7112 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007113 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007114 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7115 filemap_flush(old_inode->i_mapping);
7116
Yan, Zheng76dda932009-09-21 16:00:26 -04007117 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007118 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007119 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007120 /*
7121 * We want to reserve the absolute worst case amount of items. So if
7122 * both inodes are subvols and we need to unlink them then that would
7123 * require 4 item modifications, but if they are both normal inodes it
7124 * would require 5 item modifications, so we'll assume their normal
7125 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7126 * should cover the worst case number of items we'll modify.
7127 */
7128 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007129 if (IS_ERR(trans)) {
7130 ret = PTR_ERR(trans);
7131 goto out_notrans;
7132 }
Chris Mason5f39d392007-10-15 16:14:19 -04007133
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007134 if (dest != root)
7135 btrfs_record_root_in_trans(trans, dest);
7136
Yan, Zhenga5719522009-09-24 09:17:31 -04007137 ret = btrfs_set_inode_index(new_dir, &index);
7138 if (ret)
7139 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007140
Li Zefan33345d012011-04-20 10:31:50 +08007141 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007142 /* force full log commit if subvolume involved. */
7143 root->fs_info->last_trans_log_full_commit = trans->transid;
7144 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007145 ret = btrfs_insert_inode_ref(trans, dest,
7146 new_dentry->d_name.name,
7147 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007148 old_ino,
7149 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007150 if (ret)
7151 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007152 /*
7153 * this is an ugly little race, but the rename is required
7154 * to make sure that if we crash, the inode is either at the
7155 * old name or the new one. pinning the log transaction lets
7156 * us make sure we don't allow a log commit to come in after
7157 * we unlink the name but before we add the new name back in.
7158 */
7159 btrfs_pin_log_trans(root);
7160 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007161 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007162 * make sure the inode gets flushed if it is replacing
7163 * something.
7164 */
Li Zefan33345d012011-04-20 10:31:50 +08007165 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007166 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007167
Chris Mason39279cc2007-06-12 06:35:45 -04007168 old_dir->i_ctime = old_dir->i_mtime = ctime;
7169 new_dir->i_ctime = new_dir->i_mtime = ctime;
7170 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007171
Chris Mason12fcfd22009-03-24 10:24:20 -04007172 if (old_dentry->d_parent != new_dentry->d_parent)
7173 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7174
Li Zefan33345d012011-04-20 10:31:50 +08007175 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007176 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7177 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7178 old_dentry->d_name.name,
7179 old_dentry->d_name.len);
7180 } else {
Al Viro92986792011-03-04 17:14:37 +00007181 ret = __btrfs_unlink_inode(trans, root, old_dir,
7182 old_dentry->d_inode,
7183 old_dentry->d_name.name,
7184 old_dentry->d_name.len);
7185 if (!ret)
7186 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007187 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007188 if (ret) {
7189 btrfs_abort_transaction(trans, root, ret);
7190 goto out_fail;
7191 }
Chris Mason39279cc2007-06-12 06:35:45 -04007192
7193 if (new_inode) {
7194 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007195 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007196 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7197 root_objectid = BTRFS_I(new_inode)->location.objectid;
7198 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7199 root_objectid,
7200 new_dentry->d_name.name,
7201 new_dentry->d_name.len);
7202 BUG_ON(new_inode->i_nlink == 0);
7203 } else {
7204 ret = btrfs_unlink_inode(trans, dest, new_dir,
7205 new_dentry->d_inode,
7206 new_dentry->d_name.name,
7207 new_dentry->d_name.len);
7208 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007209 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007210 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007211 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007212 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007213 if (ret) {
7214 btrfs_abort_transaction(trans, root, ret);
7215 goto out_fail;
7216 }
Chris Mason39279cc2007-06-12 06:35:45 -04007217 }
Josef Bacikaec74772008-07-24 12:12:38 -04007218
Liu Bo75e7cb72011-03-22 10:12:20 +00007219 fixup_inode_flags(new_dir, old_inode);
7220
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007221 ret = btrfs_add_link(trans, new_dir, old_inode,
7222 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007223 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007224 if (ret) {
7225 btrfs_abort_transaction(trans, root, ret);
7226 goto out_fail;
7227 }
Chris Mason39279cc2007-06-12 06:35:45 -04007228
Li Zefan33345d012011-04-20 10:31:50 +08007229 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007230 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007231 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007232 btrfs_end_log_trans(root);
7233 }
Chris Mason39279cc2007-06-12 06:35:45 -04007234out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007235 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007236out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007237 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007238 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007239
Chris Mason39279cc2007-06-12 06:35:45 -04007240 return ret;
7241}
7242
Chris Masond352ac62008-09-29 15:18:18 -04007243/*
7244 * some fairly slow code that needs optimization. This walks the list
7245 * of all the inodes with pending delalloc and forces them to disk.
7246 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007247int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007248{
7249 struct list_head *head = &root->fs_info->delalloc_inodes;
7250 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007251 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007252
Yan Zhengc146afa2008-11-12 14:34:12 -05007253 if (root->fs_info->sb->s_flags & MS_RDONLY)
7254 return -EROFS;
7255
Chris Mason75eff682008-12-15 15:54:40 -05007256 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007257 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007258 binode = list_entry(head->next, struct btrfs_inode,
7259 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007260 inode = igrab(&binode->vfs_inode);
7261 if (!inode)
7262 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007263 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007264 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007265 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007266 if (delay_iput)
7267 btrfs_add_delayed_iput(inode);
7268 else
7269 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007270 }
7271 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007272 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007273 }
Chris Mason75eff682008-12-15 15:54:40 -05007274 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007275
7276 /* the filemap_flush will queue IO into the worker threads, but
7277 * we have to make sure the IO is actually started and that
7278 * ordered extents get created before we return
7279 */
7280 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007281 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007282 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007283 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007284 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7285 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007286 }
7287 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007288 return 0;
7289}
7290
Chris Mason39279cc2007-06-12 06:35:45 -04007291static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7292 const char *symname)
7293{
7294 struct btrfs_trans_handle *trans;
7295 struct btrfs_root *root = BTRFS_I(dir)->root;
7296 struct btrfs_path *path;
7297 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007298 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007299 int err;
7300 int drop_inode = 0;
7301 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007302 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007303 int name_len;
7304 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007305 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007306 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007307 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007308 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007309
7310 name_len = strlen(symname) + 1;
7311 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7312 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007313
Josef Bacik9ed74f22009-09-11 16:12:44 -04007314 /*
7315 * 2 items for inode item and ref
7316 * 2 items for dir items
7317 * 1 item for xattr if selinux is on
7318 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007319 trans = btrfs_start_transaction(root, 5);
7320 if (IS_ERR(trans))
7321 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007322
Li Zefan581bb052011-04-20 10:06:11 +08007323 err = btrfs_find_free_ino(root, &objectid);
7324 if (err)
7325 goto out_unlock;
7326
Josef Bacikaec74772008-07-24 12:12:38 -04007327 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007328 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007329 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007330 if (IS_ERR(inode)) {
7331 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007332 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007333 }
Chris Mason39279cc2007-06-12 06:35:45 -04007334
Eric Paris2a7dba32011-02-01 11:05:39 -05007335 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007336 if (err) {
7337 drop_inode = 1;
7338 goto out_unlock;
7339 }
7340
Casey Schauflerad19db72011-12-15 10:09:07 -05007341 /*
7342 * If the active LSM wants to access the inode during
7343 * d_instantiate it needs these. Smack checks to see
7344 * if the filesystem supports xattrs by looking at the
7345 * ops vector.
7346 */
7347 inode->i_fop = &btrfs_file_operations;
7348 inode->i_op = &btrfs_file_inode_operations;
7349
Josef Bacika1b075d2010-11-19 20:36:11 +00007350 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007351 if (err)
7352 drop_inode = 1;
7353 else {
7354 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007355 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007356 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007357 }
Chris Mason39279cc2007-06-12 06:35:45 -04007358 if (drop_inode)
7359 goto out_unlock;
7360
7361 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007362 if (!path) {
7363 err = -ENOMEM;
7364 drop_inode = 1;
7365 goto out_unlock;
7366 }
Li Zefan33345d012011-04-20 10:31:50 +08007367 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007368 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007369 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7370 datasize = btrfs_file_extent_calc_inline_size(name_len);
7371 err = btrfs_insert_empty_item(trans, root, path, &key,
7372 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007373 if (err) {
7374 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007375 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007376 goto out_unlock;
7377 }
Chris Mason5f39d392007-10-15 16:14:19 -04007378 leaf = path->nodes[0];
7379 ei = btrfs_item_ptr(leaf, path->slots[0],
7380 struct btrfs_file_extent_item);
7381 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7382 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007383 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007384 btrfs_set_file_extent_encryption(leaf, ei, 0);
7385 btrfs_set_file_extent_compression(leaf, ei, 0);
7386 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7387 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7388
Chris Mason39279cc2007-06-12 06:35:45 -04007389 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007390 write_extent_buffer(leaf, symname, ptr, name_len);
7391 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007392 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007393
Chris Mason39279cc2007-06-12 06:35:45 -04007394 inode->i_op = &btrfs_symlink_inode_operations;
7395 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007396 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007397 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007398 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007399 err = btrfs_update_inode(trans, root, inode);
7400 if (err)
7401 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007402
7403out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007404 if (!err)
7405 d_instantiate(dentry, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007406 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007407 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007408 if (drop_inode) {
7409 inode_dec_link_count(inode);
7410 iput(inode);
7411 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007412 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007413 return err;
7414}
Chris Mason16432982008-04-10 10:23:21 -04007415
Josef Bacik0af3d002010-06-21 14:48:16 -04007416static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7417 u64 start, u64 num_bytes, u64 min_size,
7418 loff_t actual_len, u64 *alloc_hint,
7419 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007420{
Yan Zhengd899e052008-10-30 14:25:28 -04007421 struct btrfs_root *root = BTRFS_I(inode)->root;
7422 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007423 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007424 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007425 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007426 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007427
Josef Bacik0af3d002010-06-21 14:48:16 -04007428 if (trans)
7429 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007430 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007431 if (own_trans) {
7432 trans = btrfs_start_transaction(root, 3);
7433 if (IS_ERR(trans)) {
7434 ret = PTR_ERR(trans);
7435 break;
7436 }
Yan Zhengd899e052008-10-30 14:25:28 -04007437 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007438
Yan, Zhengefa56462010-05-16 10:49:59 -04007439 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007440 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007441 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007442 if (own_trans)
7443 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007444 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007445 }
7446
Yan Zhengd899e052008-10-30 14:25:28 -04007447 ret = insert_reserved_file_extent(trans, inode,
7448 cur_offset, ins.objectid,
7449 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007450 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007451 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007452 if (ret) {
7453 btrfs_abort_transaction(trans, root, ret);
7454 if (own_trans)
7455 btrfs_end_transaction(trans, root);
7456 break;
7457 }
Chris Masona1ed8352009-09-11 12:27:37 -04007458 btrfs_drop_extent_cache(inode, cur_offset,
7459 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007460
Yan Zhengd899e052008-10-30 14:25:28 -04007461 num_bytes -= ins.offset;
7462 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007463 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007464
Yan Zhengd899e052008-10-30 14:25:28 -04007465 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007466 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007467 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007468 (actual_len > inode->i_size) &&
7469 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007470 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007471 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007472 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007473 i_size = cur_offset;
7474 i_size_write(inode, i_size);
7475 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007476 }
7477
Yan Zhengd899e052008-10-30 14:25:28 -04007478 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007479
7480 if (ret) {
7481 btrfs_abort_transaction(trans, root, ret);
7482 if (own_trans)
7483 btrfs_end_transaction(trans, root);
7484 break;
7485 }
Yan Zhengd899e052008-10-30 14:25:28 -04007486
Josef Bacik0af3d002010-06-21 14:48:16 -04007487 if (own_trans)
7488 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007489 }
Yan Zhengd899e052008-10-30 14:25:28 -04007490 return ret;
7491}
7492
Josef Bacik0af3d002010-06-21 14:48:16 -04007493int btrfs_prealloc_file_range(struct inode *inode, int mode,
7494 u64 start, u64 num_bytes, u64 min_size,
7495 loff_t actual_len, u64 *alloc_hint)
7496{
7497 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7498 min_size, actual_len, alloc_hint,
7499 NULL);
7500}
7501
7502int btrfs_prealloc_file_range_trans(struct inode *inode,
7503 struct btrfs_trans_handle *trans, int mode,
7504 u64 start, u64 num_bytes, u64 min_size,
7505 loff_t actual_len, u64 *alloc_hint)
7506{
7507 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7508 min_size, actual_len, alloc_hint, trans);
7509}
7510
Chris Masone6dcd2d2008-07-17 12:53:50 -04007511static int btrfs_set_page_dirty(struct page *page)
7512{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007513 return __set_page_dirty_nobuffers(page);
7514}
7515
Al Viro10556cb2011-06-20 19:28:19 -04007516static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007517{
Li Zefanb83cc962010-12-20 16:04:08 +08007518 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007519 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007520
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007521 if (mask & MAY_WRITE &&
7522 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7523 if (btrfs_root_readonly(root))
7524 return -EROFS;
7525 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7526 return -EACCES;
7527 }
Al Viro2830ba72011-06-20 19:16:29 -04007528 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007529}
Chris Mason39279cc2007-06-12 06:35:45 -04007530
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007531static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007532 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007533 .lookup = btrfs_lookup,
7534 .create = btrfs_create,
7535 .unlink = btrfs_unlink,
7536 .link = btrfs_link,
7537 .mkdir = btrfs_mkdir,
7538 .rmdir = btrfs_rmdir,
7539 .rename = btrfs_rename,
7540 .symlink = btrfs_symlink,
7541 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007542 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007543 .setxattr = btrfs_setxattr,
7544 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007545 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007546 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007547 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007548 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007549};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007550static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007551 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007552 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007553 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007554};
Yan, Zheng76dda932009-09-21 16:00:26 -04007555
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007556static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007557 .llseek = generic_file_llseek,
7558 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007559 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007560 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007561#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007562 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007563#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007564 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007565 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007566};
7567
Chris Masond1310b22008-01-24 16:13:08 -05007568static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007569 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007570 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007571 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007572 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007573 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007574 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007575 .set_bit_hook = btrfs_set_bit_hook,
7576 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007577 .merge_extent_hook = btrfs_merge_extent_hook,
7578 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007579};
7580
Chris Mason35054392009-01-21 13:11:13 -05007581/*
7582 * btrfs doesn't support the bmap operation because swapfiles
7583 * use bmap to make a mapping of extents in the file. They assume
7584 * these extents won't change over the life of the file and they
7585 * use the bmap result to do IO directly to the drive.
7586 *
7587 * the btrfs bmap call would return logical addresses that aren't
7588 * suitable for IO and they also will change frequently as COW
7589 * operations happen. So, swapfile + btrfs == corruption.
7590 *
7591 * For now we're avoiding this by dropping bmap.
7592 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007593static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007594 .readpage = btrfs_readpage,
7595 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04007596 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007597 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007598 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007599 .invalidatepage = btrfs_invalidatepage,
7600 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007601 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007602 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007603};
7604
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007605static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007606 .readpage = btrfs_readpage,
7607 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007608 .invalidatepage = btrfs_invalidatepage,
7609 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007610};
7611
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007612static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007613 .getattr = btrfs_getattr,
7614 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007615 .setxattr = btrfs_setxattr,
7616 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007617 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007618 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007619 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007620 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007621 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007622 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007623};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007624static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007625 .getattr = btrfs_getattr,
7626 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007627 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007628 .setxattr = btrfs_setxattr,
7629 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007630 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007631 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007632 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007633 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04007634};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007635static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007636 .readlink = generic_readlink,
7637 .follow_link = page_follow_link_light,
7638 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007639 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05007640 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007641 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007642 .setxattr = btrfs_setxattr,
7643 .getxattr = btrfs_getxattr,
7644 .listxattr = btrfs_listxattr,
7645 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007646 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007647 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007648};
Yan, Zheng76dda932009-09-21 16:00:26 -04007649
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007650const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007651 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007652 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04007653};