blob: eb6aec7bbacb5de71966d27e10809ad917ed80ff [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
Chris Masonb9473432009-03-13 11:00:37 -0400175 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400176 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400177 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400178
179 i++;
180 ptr += cur_size;
181 compressed_size -= cur_size;
182 }
183 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800184 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400185 } else {
186 page = find_get_page(inode->i_mapping,
187 start >> PAGE_CACHE_SHIFT);
188 btrfs_set_file_extent_compression(leaf, ei, 0);
189 kaddr = kmap_atomic(page, KM_USER0);
190 offset = start & (PAGE_CACHE_SIZE - 1);
191 write_extent_buffer(leaf, kaddr + offset, ptr, size);
192 kunmap_atomic(kaddr, KM_USER0);
193 page_cache_release(page);
194 }
195 btrfs_mark_buffer_dirty(leaf);
196 btrfs_free_path(path);
197
Yan, Zhengc2167752009-11-12 09:34:21 +0000198 /*
199 * we're an inline extent, so nobody can
200 * extend the file past i_size without locking
201 * a page we already have locked.
202 *
203 * We must do any isize and inode updates
204 * before we unlock the pages. Otherwise we
205 * could end up racing with unlink.
206 */
Chris Masonc8b97812008-10-29 14:49:59 -0400207 BTRFS_I(inode)->disk_i_size = inode->i_size;
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 Masonb888db22007-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 Masonb888db22007-08-27 16:49:44 -0400349
Chris Mason4cb53002011-05-24 15:35:30 -0400350 /* if this is a small write inside eof, kick off a defragbot */
351 if (end <= BTRFS_I(inode)->disk_i_size && (end - start + 1) < 16 * 1024)
352 btrfs_add_inode_defrag(NULL, inode);
353
Chris Mason42dc7ba2008-12-15 11:44:56 -0500354 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400355again:
356 will_compress = 0;
357 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
358 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
359
Chris Masonf03d9302009-02-04 09:31:06 -0500360 /*
361 * we don't want to send crud past the end of i_size through
362 * compression, that's just a waste of CPU time. So, if the
363 * end of the file is before the start of our current
364 * requested range of bytes, we bail out to the uncompressed
365 * cleanup code that can deal with all of this.
366 *
367 * It isn't really the fastest way to fix things, but this is a
368 * very uncommon corner.
369 */
370 if (actual_end <= start)
371 goto cleanup_and_bail_uncompressed;
372
Chris Masonc8b97812008-10-29 14:49:59 -0400373 total_compressed = actual_end - start;
374
375 /* we want to make sure that amount of ram required to uncompress
376 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500377 * of a compressed extent to 128k. This is a crucial number
378 * because it also controls how easily we can spread reads across
379 * cpus for decompression.
380 *
381 * We also want to make sure the amount of IO required to do
382 * a random read is reasonably small, so we limit the size of
383 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400384 */
385 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400386 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500387 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400388 total_in = 0;
389 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400390
Chris Mason771ed682008-11-06 22:02:51 -0500391 /*
392 * we do compression for mount -o compress and when the
393 * inode has not been flagged as nocompress. This flag can
394 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400395 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200396 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500397 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000398 (BTRFS_I(inode)->force_compress) ||
399 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400400 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400401 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800402 if (!pages) {
403 /* just bail out to the uncompressed code */
404 goto cont;
405 }
Chris Mason179e29e2007-11-01 11:28:41 -0400406
Li Zefan261507a02010-12-17 14:21:50 +0800407 if (BTRFS_I(inode)->force_compress)
408 compress_type = BTRFS_I(inode)->force_compress;
409
410 ret = btrfs_compress_pages(compress_type,
411 inode->i_mapping, start,
412 total_compressed, pages,
413 nr_pages, &nr_pages_ret,
414 &total_in,
415 &total_compressed,
416 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400417
418 if (!ret) {
419 unsigned long offset = total_compressed &
420 (PAGE_CACHE_SIZE - 1);
421 struct page *page = pages[nr_pages_ret - 1];
422 char *kaddr;
423
424 /* zero the tail end of the last page, we might be
425 * sending it down to disk
426 */
427 if (offset) {
428 kaddr = kmap_atomic(page, KM_USER0);
429 memset(kaddr + offset, 0,
430 PAGE_CACHE_SIZE - offset);
431 kunmap_atomic(kaddr, KM_USER0);
432 }
433 will_compress = 1;
434 }
435 }
Li Zefan560f7d72011-09-08 10:22:01 +0800436cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400437 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400438 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100439 if (IS_ERR(trans)) {
440 ret = PTR_ERR(trans);
441 trans = NULL;
442 goto cleanup_and_out;
443 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400444 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500445
Chris Masonc8b97812008-10-29 14:49:59 -0400446 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500447 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400448 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500449 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400450 */
451 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000452 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400453 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500454 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400455 ret = cow_file_range_inline(trans, root, inode,
456 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000457 total_compressed,
458 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400459 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100460 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500461 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100462 * inline extent creation worked or returned error,
463 * we don't need to create any more async work items.
464 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500465 */
Chris Masonc8b97812008-10-29 14:49:59 -0400466 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400467 &BTRFS_I(inode)->io_tree,
468 start, end, NULL,
469 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400470 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400471 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000472
473 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400474 goto free_pages_out;
475 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000476 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400477 }
478
479 if (will_compress) {
480 /*
481 * we aren't doing an inline extent round the compressed size
482 * up to a block size boundary so the allocator does sane
483 * things
484 */
485 total_compressed = (total_compressed + blocksize - 1) &
486 ~(blocksize - 1);
487
488 /*
489 * one last check to make sure the compression is really a
490 * win, compare the page count read with the blocks on disk
491 */
492 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
493 ~(PAGE_CACHE_SIZE - 1);
494 if (total_compressed >= total_in) {
495 will_compress = 0;
496 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400497 num_bytes = total_in;
498 }
499 }
500 if (!will_compress && pages) {
501 /*
502 * the compression code ran but failed to make things smaller,
503 * free any pages it allocated and our page pointer array
504 */
505 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400506 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400507 page_cache_release(pages[i]);
508 }
509 kfree(pages);
510 pages = NULL;
511 total_compressed = 0;
512 nr_pages_ret = 0;
513
514 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500515 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
516 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500517 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500518 }
Chris Masonc8b97812008-10-29 14:49:59 -0400519 }
Chris Mason771ed682008-11-06 22:02:51 -0500520 if (will_compress) {
521 *num_added += 1;
522
523 /* the async work queues will take care of doing actual
524 * allocation on disk for these compressed pages,
525 * and will submit them to the elevator.
526 */
527 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800528 total_compressed, pages, nr_pages_ret,
529 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500530
Yan, Zheng24ae6362010-12-06 07:02:36 +0000531 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500532 start += num_bytes;
533 pages = NULL;
534 cond_resched();
535 goto again;
536 }
537 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500538cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500539 /*
540 * No compression, but we still need to write the pages in
541 * the file we've been given so far. redirty the locked
542 * page if it corresponds to our extent and set things up
543 * for the async work queue to run cow_file_range to do
544 * the normal delalloc dance
545 */
546 if (page_offset(locked_page) >= start &&
547 page_offset(locked_page) <= end) {
548 __set_page_dirty_nobuffers(locked_page);
549 /* unlocked later on in the async handlers */
550 }
Li Zefan261507a02010-12-17 14:21:50 +0800551 add_async_extent(async_cow, start, end - start + 1,
552 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500553 *num_added += 1;
554 }
555
556out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100557 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500558
559free_pages_out:
560 for (i = 0; i < nr_pages_ret; i++) {
561 WARN_ON(pages[i]->mapping);
562 page_cache_release(pages[i]);
563 }
Chris Masond3977122009-01-05 21:25:51 -0500564 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500565
566 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100567
568cleanup_and_out:
569 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
570 start, end, NULL,
571 EXTENT_CLEAR_UNLOCK_PAGE |
572 EXTENT_CLEAR_DIRTY |
573 EXTENT_CLEAR_DELALLOC |
574 EXTENT_SET_WRITEBACK |
575 EXTENT_END_WRITEBACK);
576 if (!trans || IS_ERR(trans))
577 btrfs_error(root->fs_info, ret, "Failed to join transaction");
578 else
579 btrfs_abort_transaction(trans, root, ret);
580 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500581}
582
583/*
584 * phase two of compressed writeback. This is the ordered portion
585 * of the code, which only gets called in the order the work was
586 * queued. We walk all the async extents created by compress_file_range
587 * and send them down to the disk.
588 */
589static noinline int submit_compressed_extents(struct inode *inode,
590 struct async_cow *async_cow)
591{
592 struct async_extent *async_extent;
593 u64 alloc_hint = 0;
594 struct btrfs_trans_handle *trans;
595 struct btrfs_key ins;
596 struct extent_map *em;
597 struct btrfs_root *root = BTRFS_I(inode)->root;
598 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
599 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500600 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500601
602 if (list_empty(&async_cow->extents))
603 return 0;
604
Chris Mason771ed682008-11-06 22:02:51 -0500605
Chris Masond3977122009-01-05 21:25:51 -0500606 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500607 async_extent = list_entry(async_cow->extents.next,
608 struct async_extent, list);
609 list_del(&async_extent->list);
610
611 io_tree = &BTRFS_I(inode)->io_tree;
612
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500613retry:
Chris Mason771ed682008-11-06 22:02:51 -0500614 /* did the compression code fall back to uncompressed IO? */
615 if (!async_extent->pages) {
616 int page_started = 0;
617 unsigned long nr_written = 0;
618
619 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000620 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100621 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500622
623 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500624 ret = cow_file_range(inode, async_cow->locked_page,
625 async_extent->start,
626 async_extent->start +
627 async_extent->ram_size - 1,
628 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500629
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100630 /* JDM XXX */
631
Chris Mason771ed682008-11-06 22:02:51 -0500632 /*
633 * if page_started, cow_file_range inserted an
634 * inline extent and took care of all the unlocking
635 * and IO for us. Otherwise, we need to submit
636 * all those pages down to the drive.
637 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500638 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500639 extent_write_locked_range(io_tree,
640 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500641 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500642 async_extent->ram_size - 1,
643 btrfs_get_extent,
644 WB_SYNC_ALL);
645 kfree(async_extent);
646 cond_resched();
647 continue;
648 }
649
650 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100651 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500652
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400653 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100654 if (IS_ERR(trans)) {
655 ret = PTR_ERR(trans);
656 } else {
657 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
658 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500659 async_extent->compressed_size,
660 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500661 0, alloc_hint, &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100662 if (ret)
663 btrfs_abort_transaction(trans, root, ret);
664 btrfs_end_transaction(trans, root);
665 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000666
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500667 if (ret) {
668 int i;
669 for (i = 0; i < async_extent->nr_pages; i++) {
670 WARN_ON(async_extent->pages[i]->mapping);
671 page_cache_release(async_extent->pages[i]);
672 }
673 kfree(async_extent->pages);
674 async_extent->nr_pages = 0;
675 async_extent->pages = NULL;
676 unlock_extent(io_tree, async_extent->start,
677 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100678 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100679 if (ret == -ENOSPC)
680 goto retry;
681 goto out_free; /* JDM: Requeue? */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500682 }
683
Yan, Zhengc2167752009-11-12 09:34:21 +0000684 /*
685 * here we're doing allocation and writeback of the
686 * compressed pages
687 */
688 btrfs_drop_extent_cache(inode, async_extent->start,
689 async_extent->start +
690 async_extent->ram_size - 1, 0);
691
David Sterba172ddd62011-04-21 00:48:27 +0200692 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100693 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500694 em->start = async_extent->start;
695 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500696 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500697
698 em->block_start = ins.objectid;
699 em->block_len = ins.offset;
700 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800701 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500702 set_bit(EXTENT_FLAG_PINNED, &em->flags);
703 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
704
Chris Masond3977122009-01-05 21:25:51 -0500705 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400706 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500707 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400708 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500709 if (ret != -EEXIST) {
710 free_extent_map(em);
711 break;
712 }
713 btrfs_drop_extent_cache(inode, async_extent->start,
714 async_extent->start +
715 async_extent->ram_size - 1, 0);
716 }
717
Li Zefan261507a02010-12-17 14:21:50 +0800718 ret = btrfs_add_ordered_extent_compress(inode,
719 async_extent->start,
720 ins.objectid,
721 async_extent->ram_size,
722 ins.offset,
723 BTRFS_ORDERED_COMPRESSED,
724 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100725 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500726
Chris Mason771ed682008-11-06 22:02:51 -0500727 /*
728 * clear dirty, set writeback and unlock the pages.
729 */
730 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400731 &BTRFS_I(inode)->io_tree,
732 async_extent->start,
733 async_extent->start +
734 async_extent->ram_size - 1,
735 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
736 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400737 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400738 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500739
740 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500741 async_extent->start,
742 async_extent->ram_size,
743 ins.objectid,
744 ins.offset, async_extent->pages,
745 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500746
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100747 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500748 alloc_hint = ins.objectid + ins.offset;
749 kfree(async_extent);
750 cond_resched();
751 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100752 ret = 0;
753out:
754 return ret;
755out_free:
756 kfree(async_extent);
757 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500758}
759
Josef Bacik4b46fce2010-05-23 11:00:55 -0400760static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
761 u64 num_bytes)
762{
763 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
764 struct extent_map *em;
765 u64 alloc_hint = 0;
766
767 read_lock(&em_tree->lock);
768 em = search_extent_mapping(em_tree, start, num_bytes);
769 if (em) {
770 /*
771 * if block start isn't an actual block number then find the
772 * first block in this inode and use that as a hint. If that
773 * block is also bogus then just don't worry about it.
774 */
775 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
776 free_extent_map(em);
777 em = search_extent_mapping(em_tree, 0, 0);
778 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
779 alloc_hint = em->block_start;
780 if (em)
781 free_extent_map(em);
782 } else {
783 alloc_hint = em->block_start;
784 free_extent_map(em);
785 }
786 }
787 read_unlock(&em_tree->lock);
788
789 return alloc_hint;
790}
791
Chris Mason771ed682008-11-06 22:02:51 -0500792/*
793 * when extent_io.c finds a delayed allocation range in the file,
794 * the call backs end up in this code. The basic idea is to
795 * allocate extents on disk for the range, and create ordered data structs
796 * in ram to track those extents.
797 *
798 * locked_page is the page that writepage had locked already. We use
799 * it to make sure we don't do extra locks or unlocks.
800 *
801 * *page_started is set to one if we unlock locked_page and do everything
802 * required to start IO on it. It may be clean and already done with
803 * IO when we return.
804 */
805static noinline int cow_file_range(struct inode *inode,
806 struct page *locked_page,
807 u64 start, u64 end, int *page_started,
808 unsigned long *nr_written,
809 int unlock)
810{
811 struct btrfs_root *root = BTRFS_I(inode)->root;
812 struct btrfs_trans_handle *trans;
813 u64 alloc_hint = 0;
814 u64 num_bytes;
815 unsigned long ram_size;
816 u64 disk_num_bytes;
817 u64 cur_alloc_size;
818 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500819 struct btrfs_key ins;
820 struct extent_map *em;
821 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
822 int ret = 0;
823
Chris Mason2cf85722011-07-26 15:35:09 -0400824 BUG_ON(btrfs_is_free_space_inode(root, inode));
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400825 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100826 if (IS_ERR(trans)) {
827 extent_clear_unlock_delalloc(inode,
828 &BTRFS_I(inode)->io_tree,
829 start, end, NULL,
830 EXTENT_CLEAR_UNLOCK_PAGE |
831 EXTENT_CLEAR_UNLOCK |
832 EXTENT_CLEAR_DELALLOC |
833 EXTENT_CLEAR_DIRTY |
834 EXTENT_SET_WRITEBACK |
835 EXTENT_END_WRITEBACK);
836 return PTR_ERR(trans);
837 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400838 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500839
Chris Mason771ed682008-11-06 22:02:51 -0500840 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
841 num_bytes = max(blocksize, num_bytes);
842 disk_num_bytes = num_bytes;
843 ret = 0;
844
Chris Mason4cb53002011-05-24 15:35:30 -0400845 /* if this is a small write inside eof, kick off defrag */
846 if (end <= BTRFS_I(inode)->disk_i_size && num_bytes < 64 * 1024)
847 btrfs_add_inode_defrag(trans, inode);
848
Chris Mason771ed682008-11-06 22:02:51 -0500849 if (start == 0) {
850 /* lets try to make an inline extent */
851 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000852 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500853 if (ret == 0) {
854 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400855 &BTRFS_I(inode)->io_tree,
856 start, end, NULL,
857 EXTENT_CLEAR_UNLOCK_PAGE |
858 EXTENT_CLEAR_UNLOCK |
859 EXTENT_CLEAR_DELALLOC |
860 EXTENT_CLEAR_DIRTY |
861 EXTENT_SET_WRITEBACK |
862 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000863
Chris Mason771ed682008-11-06 22:02:51 -0500864 *nr_written = *nr_written +
865 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
866 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500867 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100868 } else if (ret < 0) {
869 btrfs_abort_transaction(trans, root, ret);
870 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500871 }
872 }
Chris Masonc8b97812008-10-29 14:49:59 -0400873
874 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200875 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400876
Josef Bacik4b46fce2010-05-23 11:00:55 -0400877 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400878 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400879
Chris Masond3977122009-01-05 21:25:51 -0500880 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400881 unsigned long op;
882
Josef Bacik287a0ab2010-03-19 18:07:23 +0000883 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400884 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500885 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500886 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100887 if (ret < 0) {
888 btrfs_abort_transaction(trans, root, ret);
889 goto out_unlock;
890 }
Chris Masond3977122009-01-05 21:25:51 -0500891
David Sterba172ddd62011-04-21 00:48:27 +0200892 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100893 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400894 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500895 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500896 ram_size = ins.offset;
897 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400898
Chris Masone6dcd2d2008-07-17 12:53:50 -0400899 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400900 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400901 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400902 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400903
Chris Masond3977122009-01-05 21:25:51 -0500904 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400905 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400906 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400907 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400908 if (ret != -EEXIST) {
909 free_extent_map(em);
910 break;
911 }
912 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400913 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400914 }
915
Chris Mason98d20f62008-04-14 09:46:10 -0400916 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400917 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500918 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100919 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400920
Yan Zheng17d217f2008-12-12 10:03:38 -0500921 if (root->root_key.objectid ==
922 BTRFS_DATA_RELOC_TREE_OBJECTID) {
923 ret = btrfs_reloc_clone_csums(inode, start,
924 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100925 if (ret) {
926 btrfs_abort_transaction(trans, root, ret);
927 goto out_unlock;
928 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500929 }
930
Chris Masond3977122009-01-05 21:25:51 -0500931 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400932 break;
Chris Masond3977122009-01-05 21:25:51 -0500933
Chris Masonc8b97812008-10-29 14:49:59 -0400934 /* we're not doing compressed IO, don't unlock the first
935 * page (which the caller expects to stay locked), don't
936 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400937 *
938 * Do set the Private2 bit so we know this page was properly
939 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400940 */
Chris Masona791e352009-10-08 11:27:10 -0400941 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
942 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
943 EXTENT_SET_PRIVATE2;
944
Chris Masonc8b97812008-10-29 14:49:59 -0400945 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
946 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400947 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400948 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500949 num_bytes -= cur_alloc_size;
950 alloc_hint = ins.objectid + ins.offset;
951 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400952 }
Chris Mason771ed682008-11-06 22:02:51 -0500953 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100954out:
Chris Masonb888db22007-08-27 16:49:44 -0400955 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400956
Chris Masonbe20aa92007-12-17 20:14:01 -0500957 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100958out_unlock:
959 extent_clear_unlock_delalloc(inode,
960 &BTRFS_I(inode)->io_tree,
961 start, end, NULL,
962 EXTENT_CLEAR_UNLOCK_PAGE |
963 EXTENT_CLEAR_UNLOCK |
964 EXTENT_CLEAR_DELALLOC |
965 EXTENT_CLEAR_DIRTY |
966 EXTENT_SET_WRITEBACK |
967 EXTENT_END_WRITEBACK);
968
969 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500970}
Chris Masonc8b97812008-10-29 14:49:59 -0400971
Chris Mason771ed682008-11-06 22:02:51 -0500972/*
973 * work queue call back to started compression on a file and pages
974 */
975static noinline void async_cow_start(struct btrfs_work *work)
976{
977 struct async_cow *async_cow;
978 int num_added = 0;
979 async_cow = container_of(work, struct async_cow, work);
980
981 compress_file_range(async_cow->inode, async_cow->locked_page,
982 async_cow->start, async_cow->end, async_cow,
983 &num_added);
984 if (num_added == 0)
985 async_cow->inode = NULL;
986}
987
988/*
989 * work queue call back to submit previously compressed pages
990 */
991static noinline void async_cow_submit(struct btrfs_work *work)
992{
993 struct async_cow *async_cow;
994 struct btrfs_root *root;
995 unsigned long nr_pages;
996
997 async_cow = container_of(work, struct async_cow, work);
998
999 root = async_cow->root;
1000 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1001 PAGE_CACHE_SHIFT;
1002
1003 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
1004
1005 if (atomic_read(&root->fs_info->async_delalloc_pages) <
1006 5 * 1042 * 1024 &&
1007 waitqueue_active(&root->fs_info->async_submit_wait))
1008 wake_up(&root->fs_info->async_submit_wait);
1009
Chris Masond3977122009-01-05 21:25:51 -05001010 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001011 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001012}
Chris Masonc8b97812008-10-29 14:49:59 -04001013
Chris Mason771ed682008-11-06 22:02:51 -05001014static noinline void async_cow_free(struct btrfs_work *work)
1015{
1016 struct async_cow *async_cow;
1017 async_cow = container_of(work, struct async_cow, work);
1018 kfree(async_cow);
1019}
1020
1021static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1022 u64 start, u64 end, int *page_started,
1023 unsigned long *nr_written)
1024{
1025 struct async_cow *async_cow;
1026 struct btrfs_root *root = BTRFS_I(inode)->root;
1027 unsigned long nr_pages;
1028 u64 cur_end;
1029 int limit = 10 * 1024 * 1042;
1030
Chris Masona3429ab2009-10-08 12:30:20 -04001031 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1032 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001033 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001034 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001035 BUG_ON(!async_cow); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001036 async_cow->inode = inode;
1037 async_cow->root = root;
1038 async_cow->locked_page = locked_page;
1039 async_cow->start = start;
1040
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001041 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001042 cur_end = end;
1043 else
1044 cur_end = min(end, start + 512 * 1024 - 1);
1045
1046 async_cow->end = cur_end;
1047 INIT_LIST_HEAD(&async_cow->extents);
1048
1049 async_cow->work.func = async_cow_start;
1050 async_cow->work.ordered_func = async_cow_submit;
1051 async_cow->work.ordered_free = async_cow_free;
1052 async_cow->work.flags = 0;
1053
Chris Mason771ed682008-11-06 22:02:51 -05001054 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1055 PAGE_CACHE_SHIFT;
1056 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1057
1058 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1059 &async_cow->work);
1060
1061 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1062 wait_event(root->fs_info->async_submit_wait,
1063 (atomic_read(&root->fs_info->async_delalloc_pages) <
1064 limit));
1065 }
1066
Chris Masond3977122009-01-05 21:25:51 -05001067 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001068 atomic_read(&root->fs_info->async_delalloc_pages)) {
1069 wait_event(root->fs_info->async_submit_wait,
1070 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1071 0));
1072 }
1073
1074 *nr_written += nr_pages;
1075 start = cur_end + 1;
1076 }
1077 *page_started = 1;
1078 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001079}
1080
Chris Masond3977122009-01-05 21:25:51 -05001081static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001082 u64 bytenr, u64 num_bytes)
1083{
1084 int ret;
1085 struct btrfs_ordered_sum *sums;
1086 LIST_HEAD(list);
1087
Yan Zheng07d400a2009-01-06 11:42:00 -05001088 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001089 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001090 if (ret == 0 && list_empty(&list))
1091 return 0;
1092
1093 while (!list_empty(&list)) {
1094 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1095 list_del(&sums->list);
1096 kfree(sums);
1097 }
1098 return 1;
1099}
1100
Chris Masond352ac62008-09-29 15:18:18 -04001101/*
1102 * when nowcow writeback call back. This checks for snapshots or COW copies
1103 * of the extents that exist in the file, and COWs the file as required.
1104 *
1105 * If no cow copies or snapshots exist, we write directly to the existing
1106 * blocks on disk
1107 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001108static noinline int run_delalloc_nocow(struct inode *inode,
1109 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001110 u64 start, u64 end, int *page_started, int force,
1111 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001112{
Chris Masonbe20aa92007-12-17 20:14:01 -05001113 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001114 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001115 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001116 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001117 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001118 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001119 u64 cow_start;
1120 u64 cur_offset;
1121 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001122 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001123 u64 disk_bytenr;
1124 u64 num_bytes;
1125 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001126 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001127 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001128 int nocow;
1129 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001130 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001131 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001132
1133 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001134 if (!path)
1135 return -ENOMEM;
Li Zefan82d59022011-04-20 10:33:24 +08001136
Chris Mason2cf85722011-07-26 15:35:09 -04001137 nolock = btrfs_is_free_space_inode(root, inode);
Li Zefan82d59022011-04-20 10:33:24 +08001138
1139 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001140 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001141 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001142 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001143
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001144 if (IS_ERR(trans)) {
1145 btrfs_free_path(path);
1146 return PTR_ERR(trans);
1147 }
1148
Josef Bacik74b21072011-04-13 12:02:53 -04001149 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001150
Yan Zheng80ff3852008-10-30 14:20:02 -04001151 cow_start = (u64)-1;
1152 cur_offset = start;
1153 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001154 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001155 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001156 if (ret < 0) {
1157 btrfs_abort_transaction(trans, root, ret);
1158 goto error;
1159 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001160 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1161 leaf = path->nodes[0];
1162 btrfs_item_key_to_cpu(leaf, &found_key,
1163 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001164 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001165 found_key.type == BTRFS_EXTENT_DATA_KEY)
1166 path->slots[0]--;
1167 }
1168 check_prev = 0;
1169next_slot:
1170 leaf = path->nodes[0];
1171 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1172 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001173 if (ret < 0) {
1174 btrfs_abort_transaction(trans, root, ret);
1175 goto error;
1176 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001177 if (ret > 0)
1178 break;
1179 leaf = path->nodes[0];
1180 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001181
Yan Zheng80ff3852008-10-30 14:20:02 -04001182 nocow = 0;
1183 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001184 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001185 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001186
Li Zefan33345d012011-04-20 10:31:50 +08001187 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001188 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1189 found_key.offset > end)
1190 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001191
Yan Zheng80ff3852008-10-30 14:20:02 -04001192 if (found_key.offset > cur_offset) {
1193 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001194 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001195 goto out_check;
1196 }
Chris Masonc31f8832008-01-08 15:46:31 -05001197
Yan Zheng80ff3852008-10-30 14:20:02 -04001198 fi = btrfs_item_ptr(leaf, path->slots[0],
1199 struct btrfs_file_extent_item);
1200 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001201
Yan Zhengd899e052008-10-30 14:25:28 -04001202 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1203 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001204 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001205 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001206 extent_end = found_key.offset +
1207 btrfs_file_extent_num_bytes(leaf, fi);
1208 if (extent_end <= start) {
1209 path->slots[0]++;
1210 goto next_slot;
1211 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001212 if (disk_bytenr == 0)
1213 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001214 if (btrfs_file_extent_compression(leaf, fi) ||
1215 btrfs_file_extent_encryption(leaf, fi) ||
1216 btrfs_file_extent_other_encoding(leaf, fi))
1217 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001218 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1219 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001220 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001221 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001222 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001223 found_key.offset -
1224 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001225 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001226 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001227 disk_bytenr += cur_offset - found_key.offset;
1228 num_bytes = min(end + 1, extent_end) - cur_offset;
1229 /*
1230 * force cow if csum exists in the range.
1231 * this ensure that csum for a given extent are
1232 * either valid or do not exist.
1233 */
1234 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1235 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 nocow = 1;
1237 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1238 extent_end = found_key.offset +
1239 btrfs_file_extent_inline_len(leaf, fi);
1240 extent_end = ALIGN(extent_end, root->sectorsize);
1241 } else {
1242 BUG_ON(1);
1243 }
1244out_check:
1245 if (extent_end <= start) {
1246 path->slots[0]++;
1247 goto next_slot;
1248 }
1249 if (!nocow) {
1250 if (cow_start == (u64)-1)
1251 cow_start = cur_offset;
1252 cur_offset = extent_end;
1253 if (cur_offset > end)
1254 break;
1255 path->slots[0]++;
1256 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001257 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001258
David Sterbab3b4aa72011-04-21 01:20:15 +02001259 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001260 if (cow_start != (u64)-1) {
1261 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001262 found_key.offset - 1, page_started,
1263 nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001264 if (ret) {
1265 btrfs_abort_transaction(trans, root, ret);
1266 goto error;
1267 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001269 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001270
Yan Zhengd899e052008-10-30 14:25:28 -04001271 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1272 struct extent_map *em;
1273 struct extent_map_tree *em_tree;
1274 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001275 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001276 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001277 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001278 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001279 em->len = num_bytes;
1280 em->block_len = num_bytes;
1281 em->block_start = disk_bytenr;
1282 em->bdev = root->fs_info->fs_devices->latest_bdev;
1283 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1284 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001285 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001286 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001287 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001288 if (ret != -EEXIST) {
1289 free_extent_map(em);
1290 break;
1291 }
1292 btrfs_drop_extent_cache(inode, em->start,
1293 em->start + em->len - 1, 0);
1294 }
1295 type = BTRFS_ORDERED_PREALLOC;
1296 } else {
1297 type = BTRFS_ORDERED_NOCOW;
1298 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001299
1300 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001301 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001302 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001303
Yan, Zhengefa56462010-05-16 10:49:59 -04001304 if (root->root_key.objectid ==
1305 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1306 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1307 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001308 if (ret) {
1309 btrfs_abort_transaction(trans, root, ret);
1310 goto error;
1311 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001312 }
1313
Yan Zhengd899e052008-10-30 14:25:28 -04001314 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001315 cur_offset, cur_offset + num_bytes - 1,
1316 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1317 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1318 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 cur_offset = extent_end;
1320 if (cur_offset > end)
1321 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001322 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001323 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001324
1325 if (cur_offset <= end && cow_start == (u64)-1)
1326 cow_start = cur_offset;
1327 if (cow_start != (u64)-1) {
1328 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001329 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001330 if (ret) {
1331 btrfs_abort_transaction(trans, root, ret);
1332 goto error;
1333 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001334 }
1335
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001336error:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001337 if (nolock) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001338 err = btrfs_end_transaction_nolock(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001339 } else {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001340 err = btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001341 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001342 if (!ret)
1343 ret = err;
1344
Yan Zheng7ea394f2008-08-05 13:05:02 -04001345 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001346 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001347}
1348
Chris Masond352ac62008-09-29 15:18:18 -04001349/*
1350 * extent_io.c call back to do delayed allocation processing
1351 */
Chris Masonc8b97812008-10-29 14:49:59 -04001352static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001353 u64 start, u64 end, int *page_started,
1354 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001355{
Chris Masonbe20aa92007-12-17 20:14:01 -05001356 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001357 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001358
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001359 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001360 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001361 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001362 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001363 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001364 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001365 else if (!btrfs_test_opt(root, COMPRESS) &&
Liu Bo75e7cb72011-03-22 10:12:20 +00001366 !(BTRFS_I(inode)->force_compress) &&
1367 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
Chris Mason7f366cf2009-03-12 20:12:45 -04001368 ret = cow_file_range(inode, locked_page, start, end,
1369 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001370 else
Chris Mason771ed682008-11-06 22:02:51 -05001371 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001372 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001373 return ret;
1374}
1375
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001376static void btrfs_split_extent_hook(struct inode *inode,
1377 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001378{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001379 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001380 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001381 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001382
Josef Bacik9e0baf62011-07-15 15:16:44 +00001383 spin_lock(&BTRFS_I(inode)->lock);
1384 BTRFS_I(inode)->outstanding_extents++;
1385 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001386}
1387
1388/*
1389 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1390 * extents so we can keep track of new extents that are just merged onto old
1391 * extents, such as when we are doing sequential writes, so we can properly
1392 * account for the metadata space we'll need.
1393 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001394static void btrfs_merge_extent_hook(struct inode *inode,
1395 struct extent_state *new,
1396 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001397{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001398 /* not delalloc, ignore it */
1399 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001400 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001401
Josef Bacik9e0baf62011-07-15 15:16:44 +00001402 spin_lock(&BTRFS_I(inode)->lock);
1403 BTRFS_I(inode)->outstanding_extents--;
1404 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001405}
1406
Chris Masond352ac62008-09-29 15:18:18 -04001407/*
1408 * extent_io.c set_bit_hook, used to track delayed allocation
1409 * bytes in this file, and to maintain the list of inodes that
1410 * have pending delalloc work to be done.
1411 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001412static void btrfs_set_bit_hook(struct inode *inode,
1413 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001414{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001415
Chris Mason75eff682008-12-15 15:54:40 -05001416 /*
1417 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001418 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001419 * bit, which is only set or cleared with irqs on
1420 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001421 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001422 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001423 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001424 bool do_list = !btrfs_is_free_space_inode(root, inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001425
Josef Bacik9e0baf62011-07-15 15:16:44 +00001426 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001427 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001428 } else {
1429 spin_lock(&BTRFS_I(inode)->lock);
1430 BTRFS_I(inode)->outstanding_extents++;
1431 spin_unlock(&BTRFS_I(inode)->lock);
1432 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001433
Chris Mason75eff682008-12-15 15:54:40 -05001434 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001435 BTRFS_I(inode)->delalloc_bytes += len;
1436 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001437 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001438 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1439 &root->fs_info->delalloc_inodes);
1440 }
Chris Mason75eff682008-12-15 15:54:40 -05001441 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001442 }
Chris Mason291d6732008-01-29 15:55:23 -05001443}
1444
Chris Masond352ac62008-09-29 15:18:18 -04001445/*
1446 * extent_io.c clear_bit_hook, see set_bit_hook for why
1447 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001448static void btrfs_clear_bit_hook(struct inode *inode,
1449 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001450{
Chris Mason75eff682008-12-15 15:54:40 -05001451 /*
1452 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001453 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001454 * bit, which is only set or cleared with irqs on
1455 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001456 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001457 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001458 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001459 bool do_list = !btrfs_is_free_space_inode(root, inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001460
Josef Bacik9e0baf62011-07-15 15:16:44 +00001461 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001462 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001463 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1464 spin_lock(&BTRFS_I(inode)->lock);
1465 BTRFS_I(inode)->outstanding_extents--;
1466 spin_unlock(&BTRFS_I(inode)->lock);
1467 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001468
1469 if (*bits & EXTENT_DO_ACCOUNTING)
1470 btrfs_delalloc_release_metadata(inode, len);
1471
Josef Bacik0cb59c92010-07-02 12:14:14 -04001472 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1473 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001474 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001475
Chris Mason75eff682008-12-15 15:54:40 -05001476 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001477 root->fs_info->delalloc_bytes -= len;
1478 BTRFS_I(inode)->delalloc_bytes -= len;
1479
Josef Bacik0cb59c92010-07-02 12:14:14 -04001480 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001481 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1482 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1483 }
Chris Mason75eff682008-12-15 15:54:40 -05001484 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001485 }
Chris Mason291d6732008-01-29 15:55:23 -05001486}
1487
Chris Masond352ac62008-09-29 15:18:18 -04001488/*
1489 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1490 * we don't create bios that span stripes or chunks
1491 */
Chris Mason239b14b2008-03-24 15:02:07 -04001492int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001493 size_t size, struct bio *bio,
1494 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001495{
1496 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1497 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001498 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001499 u64 length = 0;
1500 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001501 int ret;
1502
Chris Mason771ed682008-11-06 22:02:51 -05001503 if (bio_flags & EXTENT_BIO_COMPRESSED)
1504 return 0;
1505
Chris Masonf2d8d742008-04-21 10:03:05 -04001506 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001507 map_tree = &root->fs_info->mapping_tree;
1508 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001509 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001510 &map_length, NULL, 0);
Jeff Mahoney3444a972011-10-03 23:23:13 -04001511 /* Will always return 0 or 1 with map_multi == NULL */
1512 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001513 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001514 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001515 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001516}
1517
Chris Masond352ac62008-09-29 15:18:18 -04001518/*
1519 * in order to insert checksums into the metadata in large chunks,
1520 * we wait until bio submission time. All the pages in the bio are
1521 * checksummed and sums are attached onto the ordered extent record.
1522 *
1523 * At IO completion time the cums attached on the ordered extent record
1524 * are inserted into the btree
1525 */
Chris Masond3977122009-01-05 21:25:51 -05001526static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1527 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001528 unsigned long bio_flags,
1529 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001530{
Chris Mason065631f2008-02-20 12:07:25 -05001531 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001532 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001533
Chris Masond20f7042008-12-08 16:58:54 -05001534 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001535 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001536 return 0;
1537}
Chris Masone0156402008-04-16 11:15:20 -04001538
Chris Mason4a69a412008-11-06 22:03:00 -05001539/*
1540 * in order to insert checksums into the metadata in large chunks,
1541 * we wait until bio submission time. All the pages in the bio are
1542 * checksummed and sums are attached onto the ordered extent record.
1543 *
1544 * At IO completion time the cums attached on the ordered extent record
1545 * are inserted into the btree
1546 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001547static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001548 int mirror_num, unsigned long bio_flags,
1549 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001550{
1551 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001552 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001553}
1554
Chris Masond352ac62008-09-29 15:18:18 -04001555/*
Chris Masoncad321a2008-12-17 14:51:42 -05001556 * extent_io.c submission hook. This does the right thing for csum calculation
1557 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001558 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001559static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001560 int mirror_num, unsigned long bio_flags,
1561 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001562{
1563 struct btrfs_root *root = BTRFS_I(inode)->root;
1564 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001565 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001566 int metadata = 0;
Chris Mason44b8bd72008-04-16 11:14:51 -04001567
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001568 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001569
Chris Mason2cf85722011-07-26 15:35:09 -04001570 if (btrfs_is_free_space_inode(root, inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001571 metadata = 2;
1572
1573 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
Jeff Mahoney355808c2011-10-03 23:23:14 -04001574 if (ret)
1575 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001576
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001577 if (!(rw & REQ_WRITE)) {
Chris Masond20f7042008-12-08 16:58:54 -05001578 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001579 return btrfs_submit_compressed_read(inode, bio,
1580 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001581 } else if (!skip_sum) {
1582 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1583 if (ret)
1584 return ret;
1585 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001586 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001587 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001588 /* csum items have already been cloned */
1589 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1590 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001591 /* we're doing a write, do the async checksumming */
1592 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001593 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001594 bio_flags, bio_offset,
1595 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001596 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001597 }
1598
Chris Mason0b86a832008-03-24 15:01:56 -04001599mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001600 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001601}
Chris Mason6885f302008-02-20 16:11:05 -05001602
Chris Masond352ac62008-09-29 15:18:18 -04001603/*
1604 * given a list of ordered sums record them in the inode. This happens
1605 * at IO completion time based on sums calculated at bio submission time.
1606 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001607static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001608 struct inode *inode, u64 file_offset,
1609 struct list_head *list)
1610{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001611 struct btrfs_ordered_sum *sum;
1612
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001613 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001614 btrfs_csum_file_blocks(trans,
1615 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001616 }
1617 return 0;
1618}
1619
Josef Bacik2ac55d42010-02-03 19:33:23 +00001620int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1621 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001622{
Chris Masond3977122009-01-05 21:25:51 -05001623 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001624 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001625 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001626 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001627}
1628
Chris Masond352ac62008-09-29 15:18:18 -04001629/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001630struct btrfs_writepage_fixup {
1631 struct page *page;
1632 struct btrfs_work work;
1633};
1634
Christoph Hellwigb2950862008-12-02 09:54:17 -05001635static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001636{
1637 struct btrfs_writepage_fixup *fixup;
1638 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001639 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001640 struct page *page;
1641 struct inode *inode;
1642 u64 page_start;
1643 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001644 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001645
1646 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1647 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001648again:
Chris Mason247e7432008-07-17 12:53:51 -04001649 lock_page(page);
1650 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1651 ClearPageChecked(page);
1652 goto out_page;
1653 }
1654
1655 inode = page->mapping->host;
1656 page_start = page_offset(page);
1657 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1658
Josef Bacik2ac55d42010-02-03 19:33:23 +00001659 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001660 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001661
1662 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001663 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001664 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001665
1666 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1667 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001668 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1669 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001670 unlock_page(page);
1671 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001672 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001673 goto again;
1674 }
Chris Mason247e7432008-07-17 12:53:51 -04001675
Jeff Mahoney87826df2012-02-15 16:23:57 +01001676 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1677 if (ret) {
1678 mapping_set_error(page->mapping, ret);
1679 end_extent_writepage(page, ret, page_start, page_end);
1680 ClearPageChecked(page);
1681 goto out;
1682 }
1683
Josef Bacik2ac55d42010-02-03 19:33:23 +00001684 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001685 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001686 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001687out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001688 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1689 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001690out_page:
1691 unlock_page(page);
1692 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001693 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001694}
1695
1696/*
1697 * There are a few paths in the higher layers of the kernel that directly
1698 * set the page dirty bit without asking the filesystem if it is a
1699 * good idea. This causes problems because we want to make sure COW
1700 * properly happens and the data=ordered rules are followed.
1701 *
Chris Masonc8b97812008-10-29 14:49:59 -04001702 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001703 * hasn't been properly setup for IO. We kick off an async process
1704 * to fix it up. The async helper will wait for ordered extents, set
1705 * the delalloc bit and make it safe to write the page.
1706 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001707static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001708{
1709 struct inode *inode = page->mapping->host;
1710 struct btrfs_writepage_fixup *fixup;
1711 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001712
Chris Mason8b62b722009-09-02 16:53:46 -04001713 /* this page is properly in the ordered list */
1714 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001715 return 0;
1716
1717 if (PageChecked(page))
1718 return -EAGAIN;
1719
1720 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1721 if (!fixup)
1722 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001723
Chris Mason247e7432008-07-17 12:53:51 -04001724 SetPageChecked(page);
1725 page_cache_get(page);
1726 fixup->work.func = btrfs_writepage_fixup_worker;
1727 fixup->page = page;
1728 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001729 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001730}
1731
Yan Zhengd899e052008-10-30 14:25:28 -04001732static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1733 struct inode *inode, u64 file_pos,
1734 u64 disk_bytenr, u64 disk_num_bytes,
1735 u64 num_bytes, u64 ram_bytes,
1736 u8 compression, u8 encryption,
1737 u16 other_encoding, int extent_type)
1738{
1739 struct btrfs_root *root = BTRFS_I(inode)->root;
1740 struct btrfs_file_extent_item *fi;
1741 struct btrfs_path *path;
1742 struct extent_buffer *leaf;
1743 struct btrfs_key ins;
1744 u64 hint;
1745 int ret;
1746
1747 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001748 if (!path)
1749 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001750
Chris Masonb9473432009-03-13 11:00:37 -04001751 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001752
1753 /*
1754 * we may be replacing one extent in the tree with another.
1755 * The new extent is pinned in the extent map, and we don't want
1756 * to drop it from the cache until it is completely in the btree.
1757 *
1758 * So, tell btrfs_drop_extents to leave this extent in the cache.
1759 * the caller is expected to unpin it and allow it to be merged
1760 * with the others.
1761 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001762 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1763 &hint, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001764 if (ret)
1765 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001766
Li Zefan33345d012011-04-20 10:31:50 +08001767 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001768 ins.offset = file_pos;
1769 ins.type = BTRFS_EXTENT_DATA_KEY;
1770 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001771 if (ret)
1772 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001773 leaf = path->nodes[0];
1774 fi = btrfs_item_ptr(leaf, path->slots[0],
1775 struct btrfs_file_extent_item);
1776 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1777 btrfs_set_file_extent_type(leaf, fi, extent_type);
1778 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1779 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1780 btrfs_set_file_extent_offset(leaf, fi, 0);
1781 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1782 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1783 btrfs_set_file_extent_compression(leaf, fi, compression);
1784 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1785 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001786
1787 btrfs_unlock_up_safe(path, 1);
1788 btrfs_set_lock_blocking(leaf);
1789
Yan Zhengd899e052008-10-30 14:25:28 -04001790 btrfs_mark_buffer_dirty(leaf);
1791
1792 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001793
1794 ins.objectid = disk_bytenr;
1795 ins.offset = disk_num_bytes;
1796 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001797 ret = btrfs_alloc_reserved_file_extent(trans, root,
1798 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001799 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001800out:
Yan Zhengd899e052008-10-30 14:25:28 -04001801 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001802
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001803 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001804}
1805
Chris Mason5d13a982009-03-13 11:41:46 -04001806/*
1807 * helper function for btrfs_finish_ordered_io, this
1808 * just reads in some of the csum leaves to prime them into ram
1809 * before we start the transaction. It limits the amount of btree
1810 * reads required while inside the transaction.
1811 */
Chris Masond352ac62008-09-29 15:18:18 -04001812/* as ordered data IO finishes, this gets called so we can finish
1813 * an ordered extent if the range of bytes in the file it covers are
1814 * fully written.
1815 */
Chris Mason211f90e2008-07-18 11:56:15 -04001816static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001817{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001818 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001819 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001820 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001821 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001822 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001823 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001824 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001825 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001826
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001827 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1828 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001829 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001830 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001831 BUG_ON(!ordered_extent); /* Logic error */
Josef Bacikefd049f2010-02-02 20:50:10 +00001832
Chris Mason2cf85722011-07-26 15:35:09 -04001833 nolock = btrfs_is_free_space_inode(root, inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001834
Yan, Zhengc2167752009-11-12 09:34:21 +00001835 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001836 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Yan, Zhengc2167752009-11-12 09:34:21 +00001837 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1838 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001839 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001840 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001841 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001842 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001843 if (IS_ERR(trans))
1844 return PTR_ERR(trans);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001845 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason21151332011-11-10 20:39:08 -05001846 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001847 if (ret) /* -ENOMEM or corruption */
1848 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001849 }
1850 goto out;
1851 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001852
Josef Bacik2ac55d42010-02-03 19:33:23 +00001853 lock_extent_bits(io_tree, ordered_extent->file_offset,
1854 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001855 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001856
Josef Bacik0cb59c92010-07-02 12:14:14 -04001857 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001858 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001859 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001860 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001861 if (IS_ERR(trans)) {
1862 ret = PTR_ERR(trans);
1863 trans = NULL;
1864 goto out_unlock;
1865 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001866 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001867
Chris Masonc8b97812008-10-29 14:49:59 -04001868 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001869 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001870 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001871 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001872 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001873 ordered_extent->file_offset,
1874 ordered_extent->file_offset +
1875 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001876 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001877 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001878 ret = insert_reserved_file_extent(trans, inode,
1879 ordered_extent->file_offset,
1880 ordered_extent->start,
1881 ordered_extent->disk_len,
1882 ordered_extent->len,
1883 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001884 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001885 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001886 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1887 ordered_extent->file_offset,
1888 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001889 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001890 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1891 ordered_extent->file_offset +
1892 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001893 if (ret < 0) {
1894 btrfs_abort_transaction(trans, root, ret);
1895 goto out;
1896 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001897
Chris Masone6dcd2d2008-07-17 12:53:50 -04001898 add_pending_csums(trans, inode, ordered_extent->file_offset,
1899 &ordered_extent->list);
1900
Josef Bacik1ef30be2011-04-05 19:25:36 -04001901 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Miao Xiea39f7522011-09-11 10:52:25 -04001902 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Chris Mason21151332011-11-10 20:39:08 -05001903 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001904 if (ret) { /* -ENOMEM or corruption */
1905 btrfs_abort_transaction(trans, root, ret);
1906 goto out;
1907 }
Josef Bacik1ef30be2011-04-05 19:25:36 -04001908 }
1909 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00001910out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001911 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001912 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001913 if (trans) {
1914 if (nolock)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001915 btrfs_end_transaction_nolock(trans, root);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001916 else
Josef Bacik0cb59c92010-07-02 12:14:14 -04001917 btrfs_end_transaction(trans, root);
1918 }
1919
Chris Masone6dcd2d2008-07-17 12:53:50 -04001920 /* once for us */
1921 btrfs_put_ordered_extent(ordered_extent);
1922 /* once for the tree */
1923 btrfs_put_ordered_extent(ordered_extent);
1924
Chris Masone6dcd2d2008-07-17 12:53:50 -04001925 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001926out_unlock:
1927 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1928 ordered_extent->file_offset +
1929 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1930 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001931}
1932
Christoph Hellwigb2950862008-12-02 09:54:17 -05001933static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001934 struct extent_state *state, int uptodate)
1935{
liubo1abe9b82011-03-24 11:18:59 +00001936 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1937
Chris Mason8b62b722009-09-02 16:53:46 -04001938 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001939 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1940}
1941
Chris Masond352ac62008-09-29 15:18:18 -04001942/*
Chris Masond352ac62008-09-29 15:18:18 -04001943 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001944 * if there's a match, we allow the bio to finish. If not, the code in
1945 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04001946 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001947static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001948 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001949{
Chris Mason35ebb932007-10-30 16:56:53 -04001950 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001951 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001952 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001953 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001954 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001955 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001956 struct btrfs_root *root = BTRFS_I(inode)->root;
1957 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001958
Chris Masond20f7042008-12-08 16:58:54 -05001959 if (PageChecked(page)) {
1960 ClearPageChecked(page);
1961 goto good;
1962 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001963
1964 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02001965 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05001966
Yan Zheng17d217f2008-12-12 10:03:38 -05001967 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001968 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001969 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1970 GFP_NOFS);
1971 return 0;
1972 }
1973
Yanc2e639f2008-02-04 08:57:25 -05001974 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001975 private = state->private;
1976 ret = 0;
1977 } else {
1978 ret = get_state_private(io_tree, start, &private);
1979 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001980 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001981 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001982 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001983
Chris Masonff79f812007-10-15 16:22:25 -04001984 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1985 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001986 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001987 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001988
Chris Mason9ab86c82009-01-07 09:48:51 -05001989 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001990good:
Chris Mason07157aa2007-08-30 08:50:51 -04001991 return 0;
1992
1993zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04001994 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08001995 "private %llu\n",
1996 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04001997 (unsigned long long)start, csum,
1998 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04001999 memset(kaddr + offset, 1, end - start + 1);
2000 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05002001 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04002002 if (private == 0)
2003 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002004 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002005}
Chris Masonb888db22007-08-27 16:49:44 -04002006
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002007struct delayed_iput {
2008 struct list_head list;
2009 struct inode *inode;
2010};
2011
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002012/* JDM: If this is fs-wide, why can't we add a pointer to
2013 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002014void btrfs_add_delayed_iput(struct inode *inode)
2015{
2016 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2017 struct delayed_iput *delayed;
2018
2019 if (atomic_add_unless(&inode->i_count, -1, 1))
2020 return;
2021
2022 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2023 delayed->inode = inode;
2024
2025 spin_lock(&fs_info->delayed_iput_lock);
2026 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2027 spin_unlock(&fs_info->delayed_iput_lock);
2028}
2029
2030void btrfs_run_delayed_iputs(struct btrfs_root *root)
2031{
2032 LIST_HEAD(list);
2033 struct btrfs_fs_info *fs_info = root->fs_info;
2034 struct delayed_iput *delayed;
2035 int empty;
2036
2037 spin_lock(&fs_info->delayed_iput_lock);
2038 empty = list_empty(&fs_info->delayed_iputs);
2039 spin_unlock(&fs_info->delayed_iput_lock);
2040 if (empty)
2041 return;
2042
2043 down_read(&root->fs_info->cleanup_work_sem);
2044 spin_lock(&fs_info->delayed_iput_lock);
2045 list_splice_init(&fs_info->delayed_iputs, &list);
2046 spin_unlock(&fs_info->delayed_iput_lock);
2047
2048 while (!list_empty(&list)) {
2049 delayed = list_entry(list.next, struct delayed_iput, list);
2050 list_del(&delayed->list);
2051 iput(delayed->inode);
2052 kfree(delayed);
2053 }
2054 up_read(&root->fs_info->cleanup_work_sem);
2055}
2056
Yan, Zhengd68fc572010-05-16 10:49:58 -04002057enum btrfs_orphan_cleanup_state {
2058 ORPHAN_CLEANUP_STARTED = 1,
2059 ORPHAN_CLEANUP_DONE = 2,
2060};
2061
2062/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002063 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002064 * files in the subvolume, it removes orphan item and frees block_rsv
2065 * structure.
2066 */
2067void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2068 struct btrfs_root *root)
2069{
Josef Bacik90290e12011-12-02 15:44:12 -05002070 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002071 int ret;
2072
2073 if (!list_empty(&root->orphan_list) ||
2074 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2075 return;
2076
Josef Bacik90290e12011-12-02 15:44:12 -05002077 spin_lock(&root->orphan_lock);
2078 if (!list_empty(&root->orphan_list)) {
2079 spin_unlock(&root->orphan_lock);
2080 return;
2081 }
2082
2083 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2084 spin_unlock(&root->orphan_lock);
2085 return;
2086 }
2087
2088 block_rsv = root->orphan_block_rsv;
2089 root->orphan_block_rsv = NULL;
2090 spin_unlock(&root->orphan_lock);
2091
Yan, Zhengd68fc572010-05-16 10:49:58 -04002092 if (root->orphan_item_inserted &&
2093 btrfs_root_refs(&root->root_item) > 0) {
2094 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2095 root->root_key.objectid);
2096 BUG_ON(ret);
2097 root->orphan_item_inserted = 0;
2098 }
2099
Josef Bacik90290e12011-12-02 15:44:12 -05002100 if (block_rsv) {
2101 WARN_ON(block_rsv->size > 0);
2102 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002103 }
2104}
2105
2106/*
Josef Bacik7b128762008-07-24 12:17:14 -04002107 * This creates an orphan entry for the given inode in case something goes
2108 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002109 *
2110 * NOTE: caller of this function should reserve 5 units of metadata for
2111 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002112 */
2113int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2114{
2115 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002116 struct btrfs_block_rsv *block_rsv = NULL;
2117 int reserve = 0;
2118 int insert = 0;
2119 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002120
Yan, Zhengd68fc572010-05-16 10:49:58 -04002121 if (!root->orphan_block_rsv) {
2122 block_rsv = btrfs_alloc_block_rsv(root);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002123 if (!block_rsv)
2124 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002125 }
2126
Yan, Zhengd68fc572010-05-16 10:49:58 -04002127 spin_lock(&root->orphan_lock);
2128 if (!root->orphan_block_rsv) {
2129 root->orphan_block_rsv = block_rsv;
2130 } else if (block_rsv) {
2131 btrfs_free_block_rsv(root, block_rsv);
2132 block_rsv = NULL;
2133 }
Josef Bacik7b128762008-07-24 12:17:14 -04002134
Yan, Zhengd68fc572010-05-16 10:49:58 -04002135 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2136 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2137#if 0
2138 /*
2139 * For proper ENOSPC handling, we should do orphan
2140 * cleanup when mounting. But this introduces backward
2141 * compatibility issue.
2142 */
2143 if (!xchg(&root->orphan_item_inserted, 1))
2144 insert = 2;
2145 else
2146 insert = 1;
2147#endif
2148 insert = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002149 }
Josef Bacik7b128762008-07-24 12:17:14 -04002150
Yan, Zhengd68fc572010-05-16 10:49:58 -04002151 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2152 BTRFS_I(inode)->orphan_meta_reserved = 1;
2153 reserve = 1;
2154 }
2155 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002156
Yan, Zhengd68fc572010-05-16 10:49:58 -04002157 /* grab metadata reservation from transaction handle */
2158 if (reserve) {
2159 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002160 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002161 }
2162
2163 /* insert an orphan item to track this unlinked/truncated file */
2164 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002165 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002166 if (ret && ret != -EEXIST) {
2167 btrfs_abort_transaction(trans, root, ret);
2168 return ret;
2169 }
2170 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002171 }
2172
2173 /* insert an orphan item to track subvolume contains orphan files */
2174 if (insert >= 2) {
2175 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2176 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002177 if (ret && ret != -EEXIST) {
2178 btrfs_abort_transaction(trans, root, ret);
2179 return ret;
2180 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002181 }
2182 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002183}
2184
2185/*
2186 * We have done the truncate/delete so we can go ahead and remove the orphan
2187 * item for this particular inode.
2188 */
2189int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2190{
2191 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002192 int delete_item = 0;
2193 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002194 int ret = 0;
2195
Yan, Zhengd68fc572010-05-16 10:49:58 -04002196 spin_lock(&root->orphan_lock);
2197 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2198 list_del_init(&BTRFS_I(inode)->i_orphan);
2199 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002200 }
2201
Yan, Zhengd68fc572010-05-16 10:49:58 -04002202 if (BTRFS_I(inode)->orphan_meta_reserved) {
2203 BTRFS_I(inode)->orphan_meta_reserved = 0;
2204 release_rsv = 1;
2205 }
2206 spin_unlock(&root->orphan_lock);
2207
2208 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002209 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002210 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002211 }
2212
Yan, Zhengd68fc572010-05-16 10:49:58 -04002213 if (release_rsv)
2214 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002215
Yan, Zhengd68fc572010-05-16 10:49:58 -04002216 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002217}
2218
2219/*
2220 * this cleans up any orphans that may be left on the list from the last use
2221 * of this root.
2222 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002223int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002224{
2225 struct btrfs_path *path;
2226 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002227 struct btrfs_key key, found_key;
2228 struct btrfs_trans_handle *trans;
2229 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002230 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002231 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2232
Yan, Zhengd68fc572010-05-16 10:49:58 -04002233 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002234 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002235
2236 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002237 if (!path) {
2238 ret = -ENOMEM;
2239 goto out;
2240 }
Josef Bacik7b128762008-07-24 12:17:14 -04002241 path->reada = -1;
2242
2243 key.objectid = BTRFS_ORPHAN_OBJECTID;
2244 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2245 key.offset = (u64)-1;
2246
Josef Bacik7b128762008-07-24 12:17:14 -04002247 while (1) {
2248 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002249 if (ret < 0)
2250 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002251
2252 /*
2253 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002254 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002255 * find the key and see if we have stuff that matches
2256 */
2257 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002258 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002259 if (path->slots[0] == 0)
2260 break;
2261 path->slots[0]--;
2262 }
2263
2264 /* pull out the item */
2265 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002266 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2267
2268 /* make sure the item matches what we want */
2269 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2270 break;
2271 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2272 break;
2273
2274 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002275 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002276
2277 /*
2278 * this is where we are basically btrfs_lookup, without the
2279 * crossing root thing. we store the inode number in the
2280 * offset of the orphan item.
2281 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002282
2283 if (found_key.offset == last_objectid) {
2284 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2285 "stopping orphan cleanup\n");
2286 ret = -EINVAL;
2287 goto out;
2288 }
2289
2290 last_objectid = found_key.offset;
2291
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002292 found_key.objectid = found_key.offset;
2293 found_key.type = BTRFS_INODE_ITEM_KEY;
2294 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002295 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002296 ret = PTR_RET(inode);
2297 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002298 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002299
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002300 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2301 struct btrfs_root *dead_root;
2302 struct btrfs_fs_info *fs_info = root->fs_info;
2303 int is_dead_root = 0;
2304
2305 /*
2306 * this is an orphan in the tree root. Currently these
2307 * could come from 2 sources:
2308 * a) a snapshot deletion in progress
2309 * b) a free space cache inode
2310 * We need to distinguish those two, as the snapshot
2311 * orphan must not get deleted.
2312 * find_dead_roots already ran before us, so if this
2313 * is a snapshot deletion, we should find the root
2314 * in the dead_roots list
2315 */
2316 spin_lock(&fs_info->trans_lock);
2317 list_for_each_entry(dead_root, &fs_info->dead_roots,
2318 root_list) {
2319 if (dead_root->root_key.objectid ==
2320 found_key.objectid) {
2321 is_dead_root = 1;
2322 break;
2323 }
2324 }
2325 spin_unlock(&fs_info->trans_lock);
2326 if (is_dead_root) {
2327 /* prevent this orphan from being found again */
2328 key.offset = found_key.objectid - 1;
2329 continue;
2330 }
2331 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002332 /*
2333 * Inode is already gone but the orphan item is still there,
2334 * kill the orphan item.
2335 */
2336 if (ret == -ESTALE) {
2337 trans = btrfs_start_transaction(root, 1);
2338 if (IS_ERR(trans)) {
2339 ret = PTR_ERR(trans);
2340 goto out;
2341 }
2342 ret = btrfs_del_orphan_item(trans, root,
2343 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002344 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002345 btrfs_end_transaction(trans, root);
2346 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002347 }
Josef Bacik7b128762008-07-24 12:17:14 -04002348
Josef Bacik7b128762008-07-24 12:17:14 -04002349 /*
2350 * add this inode to the orphan list so btrfs_orphan_del does
2351 * the proper thing when we hit it
2352 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002353 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002354 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002355 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002356
Josef Bacik7b128762008-07-24 12:17:14 -04002357 /* if we have links, this was a truncate, lets do that */
2358 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002359 if (!S_ISREG(inode->i_mode)) {
2360 WARN_ON(1);
2361 iput(inode);
2362 continue;
2363 }
Josef Bacik7b128762008-07-24 12:17:14 -04002364 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002365 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002366 } else {
2367 nr_unlink++;
2368 }
2369
2370 /* this will do delete_inode and everything for us */
2371 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002372 if (ret)
2373 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002374 }
Miao Xie3254c872011-11-10 20:45:05 -05002375 /* release the path since we're done with it */
2376 btrfs_release_path(path);
2377
Yan, Zhengd68fc572010-05-16 10:49:58 -04002378 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2379
2380 if (root->orphan_block_rsv)
2381 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2382 (u64)-1);
2383
2384 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002385 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002386 if (!IS_ERR(trans))
2387 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002388 }
Josef Bacik7b128762008-07-24 12:17:14 -04002389
2390 if (nr_unlink)
2391 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2392 if (nr_truncate)
2393 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002394
2395out:
2396 if (ret)
2397 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2398 btrfs_free_path(path);
2399 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002400}
2401
Chris Masond352ac62008-09-29 15:18:18 -04002402/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002403 * very simple check to peek ahead in the leaf looking for xattrs. If we
2404 * don't find any xattrs, we know there can't be any acls.
2405 *
2406 * slot is the slot the inode is in, objectid is the objectid of the inode
2407 */
2408static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2409 int slot, u64 objectid)
2410{
2411 u32 nritems = btrfs_header_nritems(leaf);
2412 struct btrfs_key found_key;
2413 int scanned = 0;
2414
2415 slot++;
2416 while (slot < nritems) {
2417 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2418
2419 /* we found a different objectid, there must not be acls */
2420 if (found_key.objectid != objectid)
2421 return 0;
2422
2423 /* we found an xattr, assume we've got an acl */
2424 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2425 return 1;
2426
2427 /*
2428 * we found a key greater than an xattr key, there can't
2429 * be any acls later on
2430 */
2431 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2432 return 0;
2433
2434 slot++;
2435 scanned++;
2436
2437 /*
2438 * it goes inode, inode backrefs, xattrs, extents,
2439 * so if there are a ton of hard links to an inode there can
2440 * be a lot of backrefs. Don't waste time searching too hard,
2441 * this is just an optimization
2442 */
2443 if (scanned >= 8)
2444 break;
2445 }
2446 /* we hit the end of the leaf before we found an xattr or
2447 * something larger than an xattr. We have to assume the inode
2448 * has acls
2449 */
2450 return 1;
2451}
2452
2453/*
Chris Masond352ac62008-09-29 15:18:18 -04002454 * read an inode from the btree into the in-memory inode
2455 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002456static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002457{
2458 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002459 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002460 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002461 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002462 struct btrfs_root *root = BTRFS_I(inode)->root;
2463 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002464 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002465 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002466 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002467 bool filled = false;
2468
2469 ret = btrfs_fill_inode(inode, &rdev);
2470 if (!ret)
2471 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002472
2473 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002474 if (!path)
2475 goto make_bad;
2476
Josef Bacikd90c7322011-05-17 09:50:54 -04002477 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002478 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002479
Chris Mason39279cc2007-06-12 06:35:45 -04002480 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002481 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002482 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002483
Chris Mason5f39d392007-10-15 16:14:19 -04002484 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002485
2486 if (filled)
2487 goto cache_acl;
2488
Chris Mason5f39d392007-10-15 16:14:19 -04002489 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2490 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002491 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002492 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002493 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2494 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002495 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002496
2497 tspec = btrfs_inode_atime(inode_item);
2498 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2499 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2500
2501 tspec = btrfs_inode_mtime(inode_item);
2502 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2503 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2504
2505 tspec = btrfs_inode_ctime(inode_item);
2506 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2507 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2508
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002509 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002510 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002511 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002512 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002513 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002514 rdev = btrfs_inode_rdev(leaf, inode_item);
2515
Josef Bacikaec74772008-07-24 12:12:38 -04002516 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002517 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002518cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002519 /*
2520 * try to precache a NULL acl entry for files that don't have
2521 * any xattrs or acls
2522 */
Li Zefan33345d012011-04-20 10:31:50 +08002523 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2524 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002525 if (!maybe_acls)
2526 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002527
Chris Mason39279cc2007-06-12 06:35:45 -04002528 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002529
Chris Mason39279cc2007-06-12 06:35:45 -04002530 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002531 case S_IFREG:
2532 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002533 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002534 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002535 inode->i_fop = &btrfs_file_operations;
2536 inode->i_op = &btrfs_file_inode_operations;
2537 break;
2538 case S_IFDIR:
2539 inode->i_fop = &btrfs_dir_file_operations;
2540 if (root == root->fs_info->tree_root)
2541 inode->i_op = &btrfs_dir_ro_inode_operations;
2542 else
2543 inode->i_op = &btrfs_dir_inode_operations;
2544 break;
2545 case S_IFLNK:
2546 inode->i_op = &btrfs_symlink_inode_operations;
2547 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002548 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002549 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002550 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002551 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002552 init_special_inode(inode, inode->i_mode, rdev);
2553 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002554 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002555
2556 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002557 return;
2558
2559make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002560 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002561 make_bad_inode(inode);
2562}
2563
Chris Masond352ac62008-09-29 15:18:18 -04002564/*
2565 * given a leaf and an inode, copy the inode fields into the leaf
2566 */
Chris Masone02119d2008-09-05 16:13:11 -04002567static void fill_inode_item(struct btrfs_trans_handle *trans,
2568 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002569 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002570 struct inode *inode)
2571{
Chris Mason5f39d392007-10-15 16:14:19 -04002572 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2573 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002574 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002575 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2576 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2577
2578 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2579 inode->i_atime.tv_sec);
2580 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2581 inode->i_atime.tv_nsec);
2582
2583 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2584 inode->i_mtime.tv_sec);
2585 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2586 inode->i_mtime.tv_nsec);
2587
2588 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2589 inode->i_ctime.tv_sec);
2590 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2591 inode->i_ctime.tv_nsec);
2592
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002593 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002594 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002595 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002596 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002597 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002598 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f12011-05-11 15:26:06 -04002599 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002600}
2601
Chris Masond352ac62008-09-29 15:18:18 -04002602/*
2603 * copy everything in the in-memory inode into the btree.
2604 */
Chris Mason21151332011-11-10 20:39:08 -05002605static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002606 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002607{
2608 struct btrfs_inode_item *inode_item;
2609 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002610 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002611 int ret;
2612
2613 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002614 if (!path)
2615 return -ENOMEM;
2616
Chris Masonb9473432009-03-13 11:00:37 -04002617 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002618 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2619 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002620 if (ret) {
2621 if (ret > 0)
2622 ret = -ENOENT;
2623 goto failed;
2624 }
2625
Chris Masonb4ce94d2009-02-04 09:25:08 -05002626 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002627 leaf = path->nodes[0];
2628 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002629 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002630
Chris Masone02119d2008-09-05 16:13:11 -04002631 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002632 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002633 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002634 ret = 0;
2635failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002636 btrfs_free_path(path);
2637 return ret;
2638}
2639
Chris Masond352ac62008-09-29 15:18:18 -04002640/*
Chris Mason21151332011-11-10 20:39:08 -05002641 * copy everything in the in-memory inode into the btree.
2642 */
2643noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2644 struct btrfs_root *root, struct inode *inode)
2645{
2646 int ret;
2647
2648 /*
2649 * If the inode is a free space inode, we can deadlock during commit
2650 * if we put it into the delayed code.
2651 *
2652 * The data relocation inode should also be directly updated
2653 * without delay
2654 */
2655 if (!btrfs_is_free_space_inode(root, inode)
2656 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
2657 ret = btrfs_delayed_update_inode(trans, root, inode);
2658 if (!ret)
2659 btrfs_set_inode_last_trans(trans, inode);
2660 return ret;
2661 }
2662
2663 return btrfs_update_inode_item(trans, root, inode);
2664}
2665
2666static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2667 struct btrfs_root *root, struct inode *inode)
2668{
2669 int ret;
2670
2671 ret = btrfs_update_inode(trans, root, inode);
2672 if (ret == -ENOSPC)
2673 return btrfs_update_inode_item(trans, root, inode);
2674 return ret;
2675}
2676
2677/*
Chris Masond352ac62008-09-29 15:18:18 -04002678 * unlink helper that gets used here in inode.c and in the tree logging
2679 * recovery code. It remove a link in a directory with a given name, and
2680 * also drops the back refs in the inode to the directory
2681 */
Al Viro92986792011-03-04 17:14:37 +00002682static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2683 struct btrfs_root *root,
2684 struct inode *dir, struct inode *inode,
2685 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002686{
2687 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002688 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002689 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002690 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002691 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002692 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002693 u64 ino = btrfs_ino(inode);
2694 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002695
2696 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002697 if (!path) {
2698 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002699 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002700 }
2701
Chris Masonb9473432009-03-13 11:00:37 -04002702 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002703 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002704 name, name_len, -1);
2705 if (IS_ERR(di)) {
2706 ret = PTR_ERR(di);
2707 goto err;
2708 }
2709 if (!di) {
2710 ret = -ENOENT;
2711 goto err;
2712 }
Chris Mason5f39d392007-10-15 16:14:19 -04002713 leaf = path->nodes[0];
2714 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002715 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002716 if (ret)
2717 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002718 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002719
Li Zefan33345d012011-04-20 10:31:50 +08002720 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2721 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002722 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002723 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002724 "inode %llu parent %llu\n", name_len, name,
2725 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002726 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002727 goto err;
2728 }
2729
Miao Xie16cdcec2011-04-22 18:12:22 +08002730 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002731 if (ret) {
2732 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002733 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002734 }
Chris Mason39279cc2007-06-12 06:35:45 -04002735
Chris Masone02119d2008-09-05 16:13:11 -04002736 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002737 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002738 if (ret != 0 && ret != -ENOENT) {
2739 btrfs_abort_transaction(trans, root, ret);
2740 goto err;
2741 }
Chris Masone02119d2008-09-05 16:13:11 -04002742
2743 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2744 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002745 if (ret == -ENOENT)
2746 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002747err:
2748 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002749 if (ret)
2750 goto out;
2751
2752 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2753 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2754 btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002755out:
Chris Mason39279cc2007-06-12 06:35:45 -04002756 return ret;
2757}
2758
Al Viro92986792011-03-04 17:14:37 +00002759int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2760 struct btrfs_root *root,
2761 struct inode *dir, struct inode *inode,
2762 const char *name, int name_len)
2763{
2764 int ret;
2765 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2766 if (!ret) {
2767 btrfs_drop_nlink(inode);
2768 ret = btrfs_update_inode(trans, root, inode);
2769 }
2770 return ret;
2771}
2772
2773
Yan, Zhenga22285a2010-05-16 10:48:46 -04002774/* helper to check if there is any shared block in the path */
2775static int check_path_shared(struct btrfs_root *root,
2776 struct btrfs_path *path)
2777{
2778 struct extent_buffer *eb;
2779 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002780 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002781
2782 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002783 int ret;
2784
Yan, Zhenga22285a2010-05-16 10:48:46 -04002785 if (!path->nodes[level])
2786 break;
2787 eb = path->nodes[level];
2788 if (!btrfs_block_can_be_shared(root, eb))
2789 continue;
2790 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2791 &refs, NULL);
2792 if (refs > 1)
2793 return 1;
2794 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002795 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002796}
2797
2798/*
2799 * helper to start transaction for unlink and rmdir.
2800 *
2801 * unlink and rmdir are special in btrfs, they do not always free space.
2802 * so in enospc case, we should make sure they will free space before
2803 * allowing them to use the global metadata reservation.
2804 */
2805static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2806 struct dentry *dentry)
2807{
2808 struct btrfs_trans_handle *trans;
2809 struct btrfs_root *root = BTRFS_I(dir)->root;
2810 struct btrfs_path *path;
2811 struct btrfs_inode_ref *ref;
2812 struct btrfs_dir_item *di;
2813 struct inode *inode = dentry->d_inode;
2814 u64 index;
2815 int check_link = 1;
2816 int err = -ENOSPC;
2817 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002818 u64 ino = btrfs_ino(inode);
2819 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002820
Josef Bacike70bea52011-10-11 14:18:24 -04002821 /*
2822 * 1 for the possible orphan item
2823 * 1 for the dir item
2824 * 1 for the dir index
2825 * 1 for the inode ref
2826 * 1 for the inode ref in the tree log
2827 * 2 for the dir entries in the log
2828 * 1 for the inode
2829 */
2830 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002831 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2832 return trans;
2833
Li Zefan33345d012011-04-20 10:31:50 +08002834 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002835 return ERR_PTR(-ENOSPC);
2836
2837 /* check if there is someone else holds reference */
2838 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2839 return ERR_PTR(-ENOSPC);
2840
2841 if (atomic_read(&inode->i_count) > 2)
2842 return ERR_PTR(-ENOSPC);
2843
2844 if (xchg(&root->fs_info->enospc_unlink, 1))
2845 return ERR_PTR(-ENOSPC);
2846
2847 path = btrfs_alloc_path();
2848 if (!path) {
2849 root->fs_info->enospc_unlink = 0;
2850 return ERR_PTR(-ENOMEM);
2851 }
2852
Josef Bacik3880a1b2011-10-14 14:46:51 -04002853 /* 1 for the orphan item */
2854 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002855 if (IS_ERR(trans)) {
2856 btrfs_free_path(path);
2857 root->fs_info->enospc_unlink = 0;
2858 return trans;
2859 }
2860
2861 path->skip_locking = 1;
2862 path->search_commit_root = 1;
2863
2864 ret = btrfs_lookup_inode(trans, root, path,
2865 &BTRFS_I(dir)->location, 0);
2866 if (ret < 0) {
2867 err = ret;
2868 goto out;
2869 }
2870 if (ret == 0) {
2871 if (check_path_shared(root, path))
2872 goto out;
2873 } else {
2874 check_link = 0;
2875 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002876 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002877
2878 ret = btrfs_lookup_inode(trans, root, path,
2879 &BTRFS_I(inode)->location, 0);
2880 if (ret < 0) {
2881 err = ret;
2882 goto out;
2883 }
2884 if (ret == 0) {
2885 if (check_path_shared(root, path))
2886 goto out;
2887 } else {
2888 check_link = 0;
2889 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002890 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002891
2892 if (ret == 0 && S_ISREG(inode->i_mode)) {
2893 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002894 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002895 if (ret < 0) {
2896 err = ret;
2897 goto out;
2898 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002899 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002900 if (check_path_shared(root, path))
2901 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02002902 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002903 }
2904
2905 if (!check_link) {
2906 err = 0;
2907 goto out;
2908 }
2909
Li Zefan33345d012011-04-20 10:31:50 +08002910 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002911 dentry->d_name.name, dentry->d_name.len, 0);
2912 if (IS_ERR(di)) {
2913 err = PTR_ERR(di);
2914 goto out;
2915 }
2916 if (di) {
2917 if (check_path_shared(root, path))
2918 goto out;
2919 } else {
2920 err = 0;
2921 goto out;
2922 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002923 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002924
2925 ref = btrfs_lookup_inode_ref(trans, root, path,
2926 dentry->d_name.name, dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08002927 ino, dir_ino, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002928 if (IS_ERR(ref)) {
2929 err = PTR_ERR(ref);
2930 goto out;
2931 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002932 BUG_ON(!ref); /* Logic error */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002933 if (check_path_shared(root, path))
2934 goto out;
2935 index = btrfs_inode_ref_index(path->nodes[0], ref);
David Sterbab3b4aa72011-04-21 01:20:15 +02002936 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002937
Miao Xie16cdcec2011-04-22 18:12:22 +08002938 /*
2939 * This is a commit root search, if we can lookup inode item and other
2940 * relative items in the commit root, it means the transaction of
2941 * dir/file creation has been committed, and the dir index item that we
2942 * delay to insert has also been inserted into the commit root. So
2943 * we needn't worry about the delayed insertion of the dir index item
2944 * here.
2945 */
Li Zefan33345d012011-04-20 10:31:50 +08002946 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002947 dentry->d_name.name, dentry->d_name.len, 0);
2948 if (IS_ERR(di)) {
2949 err = PTR_ERR(di);
2950 goto out;
2951 }
2952 BUG_ON(ret == -ENOENT);
2953 if (check_path_shared(root, path))
2954 goto out;
2955
2956 err = 0;
2957out:
2958 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04002959 /* Migrate the orphan reservation over */
2960 if (!err)
2961 err = btrfs_block_rsv_migrate(trans->block_rsv,
2962 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04002963 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04002964
Yan, Zhenga22285a2010-05-16 10:48:46 -04002965 if (err) {
2966 btrfs_end_transaction(trans, root);
2967 root->fs_info->enospc_unlink = 0;
2968 return ERR_PTR(err);
2969 }
2970
2971 trans->block_rsv = &root->fs_info->global_block_rsv;
2972 return trans;
2973}
2974
2975static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2976 struct btrfs_root *root)
2977{
2978 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
Josef Bacik5a77d762011-11-01 14:32:23 -04002979 btrfs_block_rsv_release(root, trans->block_rsv,
2980 trans->bytes_reserved);
2981 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002982 BUG_ON(!root->fs_info->enospc_unlink);
2983 root->fs_info->enospc_unlink = 0;
2984 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05002985 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002986}
2987
Chris Mason39279cc2007-06-12 06:35:45 -04002988static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2989{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002990 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002991 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002992 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002993 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002994 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002995
Yan, Zhenga22285a2010-05-16 10:48:46 -04002996 trans = __unlink_start_trans(dir, dentry);
2997 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002998 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002999
Chris Mason12fcfd22009-03-24 10:24:20 -04003000 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3001
Chris Masone02119d2008-09-05 16:13:11 -04003002 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3003 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003004 if (ret)
3005 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003006
Yan, Zhenga22285a2010-05-16 10:48:46 -04003007 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003008 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003009 if (ret)
3010 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003011 }
Josef Bacik7b128762008-07-24 12:17:14 -04003012
Tsutomu Itohb5324022011-07-19 07:27:20 +00003013out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003014 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003015 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003016 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003017 return ret;
3018}
3019
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003020int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3021 struct btrfs_root *root,
3022 struct inode *dir, u64 objectid,
3023 const char *name, int name_len)
3024{
3025 struct btrfs_path *path;
3026 struct extent_buffer *leaf;
3027 struct btrfs_dir_item *di;
3028 struct btrfs_key key;
3029 u64 index;
3030 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003031 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003032
3033 path = btrfs_alloc_path();
3034 if (!path)
3035 return -ENOMEM;
3036
Li Zefan33345d012011-04-20 10:31:50 +08003037 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003038 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003039 if (IS_ERR_OR_NULL(di)) {
3040 if (!di)
3041 ret = -ENOENT;
3042 else
3043 ret = PTR_ERR(di);
3044 goto out;
3045 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003046
3047 leaf = path->nodes[0];
3048 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3049 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3050 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003051 if (ret) {
3052 btrfs_abort_transaction(trans, root, ret);
3053 goto out;
3054 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003055 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003056
3057 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3058 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003059 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003060 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003061 if (ret != -ENOENT) {
3062 btrfs_abort_transaction(trans, root, ret);
3063 goto out;
3064 }
Li Zefan33345d012011-04-20 10:31:50 +08003065 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003066 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003067 if (IS_ERR_OR_NULL(di)) {
3068 if (!di)
3069 ret = -ENOENT;
3070 else
3071 ret = PTR_ERR(di);
3072 btrfs_abort_transaction(trans, root, ret);
3073 goto out;
3074 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003075
3076 leaf = path->nodes[0];
3077 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003078 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003079 index = key.offset;
3080 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003081 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003082
Miao Xie16cdcec2011-04-22 18:12:22 +08003083 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003084 if (ret) {
3085 btrfs_abort_transaction(trans, root, ret);
3086 goto out;
3087 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003088
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003089 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3090 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3091 ret = btrfs_update_inode(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003092 if (ret)
3093 btrfs_abort_transaction(trans, root, ret);
3094out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003095 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003096 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003097}
3098
Chris Mason39279cc2007-06-12 06:35:45 -04003099static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3100{
3101 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003102 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003103 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003104 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003105 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003106
Chris Mason3394e162008-11-17 20:42:26 -05003107 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Li Zefan33345d012011-04-20 10:31:50 +08003108 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003109 return -ENOTEMPTY;
3110
Yan, Zhenga22285a2010-05-16 10:48:46 -04003111 trans = __unlink_start_trans(dir, dentry);
3112 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003113 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003114
Li Zefan33345d012011-04-20 10:31:50 +08003115 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003116 err = btrfs_unlink_subvol(trans, root, dir,
3117 BTRFS_I(inode)->location.objectid,
3118 dentry->d_name.name,
3119 dentry->d_name.len);
3120 goto out;
3121 }
3122
Josef Bacik7b128762008-07-24 12:17:14 -04003123 err = btrfs_orphan_add(trans, inode);
3124 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003125 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003126
Chris Mason39279cc2007-06-12 06:35:45 -04003127 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003128 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3129 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003130 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003131 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003132out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003133 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003134 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003135 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003136
Chris Mason39279cc2007-06-12 06:35:45 -04003137 return err;
3138}
3139
Chris Mason323ac952008-10-01 19:05:46 -04003140/*
Chris Mason39279cc2007-06-12 06:35:45 -04003141 * this can truncate away extent items, csum items and directory items.
3142 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003143 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003144 *
3145 * csum items that cross the new i_size are truncated to the new size
3146 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003147 *
3148 * min_type is the minimum key type to truncate down to. If set to 0, this
3149 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003150 */
Yan, Zheng80825102009-11-12 09:35:36 +00003151int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3152 struct btrfs_root *root,
3153 struct inode *inode,
3154 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003155{
Chris Mason39279cc2007-06-12 06:35:45 -04003156 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003157 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003158 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003159 struct btrfs_key key;
3160 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003161 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003162 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003163 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003164 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003165 u64 mask = root->sectorsize - 1;
3166 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003167 int found_extent;
3168 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003169 int pending_del_nr = 0;
3170 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003171 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003172 int ret;
3173 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003174 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003175
3176 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003177
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003178 path = btrfs_alloc_path();
3179 if (!path)
3180 return -ENOMEM;
3181 path->reada = -1;
3182
Josef Bacik0af3d002010-06-21 14:48:16 -04003183 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003184 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003185
Miao Xie16cdcec2011-04-22 18:12:22 +08003186 /*
3187 * This function is also used to drop the items in the log tree before
3188 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3189 * it is used to drop the loged items. So we shouldn't kill the delayed
3190 * items.
3191 */
3192 if (min_type == 0 && root == BTRFS_I(inode)->root)
3193 btrfs_kill_delayed_inode_items(inode);
3194
Li Zefan33345d012011-04-20 10:31:50 +08003195 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003196 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003197 key.type = (u8)-1;
3198
Chris Mason85e21ba2008-01-29 15:11:36 -05003199search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003200 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003201 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003202 if (ret < 0) {
3203 err = ret;
3204 goto out;
3205 }
Chris Masond3977122009-01-05 21:25:51 -05003206
Chris Mason85e21ba2008-01-29 15:11:36 -05003207 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003208 /* there are no items in the tree for us to truncate, we're
3209 * done
3210 */
Yan, Zheng80825102009-11-12 09:35:36 +00003211 if (path->slots[0] == 0)
3212 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003213 path->slots[0]--;
3214 }
3215
Chris Masond3977122009-01-05 21:25:51 -05003216 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003217 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003218 leaf = path->nodes[0];
3219 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3220 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003221
Li Zefan33345d012011-04-20 10:31:50 +08003222 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003223 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003224
Chris Mason85e21ba2008-01-29 15:11:36 -05003225 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003226 break;
3227
Chris Mason5f39d392007-10-15 16:14:19 -04003228 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003229 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003230 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003231 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003232 extent_type = btrfs_file_extent_type(leaf, fi);
3233 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003234 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003235 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003236 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003237 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003238 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003239 }
Yan008630c2007-11-07 13:31:09 -05003240 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003241 }
Yan, Zheng80825102009-11-12 09:35:36 +00003242 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003243 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003244 } else {
3245 if (item_end < new_size)
3246 break;
3247 if (found_key.offset >= new_size)
3248 del_item = 1;
3249 else
3250 del_item = 0;
3251 }
Chris Mason39279cc2007-06-12 06:35:45 -04003252 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003253 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003254 if (found_type != BTRFS_EXTENT_DATA_KEY)
3255 goto delete;
3256
3257 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003258 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003259 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003260 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003261 u64 orig_num_bytes =
3262 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003263 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003264 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003265 extent_num_bytes = extent_num_bytes &
3266 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003267 btrfs_set_file_extent_num_bytes(leaf, fi,
3268 extent_num_bytes);
3269 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003270 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003271 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003272 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003273 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003274 } else {
Chris Masondb945352007-10-15 16:15:53 -04003275 extent_num_bytes =
3276 btrfs_file_extent_disk_num_bytes(leaf,
3277 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003278 extent_offset = found_key.offset -
3279 btrfs_file_extent_offset(leaf, fi);
3280
Chris Mason39279cc2007-06-12 06:35:45 -04003281 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003282 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003283 if (extent_start != 0) {
3284 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003285 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003286 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003287 }
3288 }
Chris Mason90692182008-02-08 13:49:28 -05003289 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003290 /*
3291 * we can't truncate inline items that have had
3292 * special encodings
3293 */
3294 if (!del_item &&
3295 btrfs_file_extent_compression(leaf, fi) == 0 &&
3296 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3297 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003298 u32 size = new_size - found_key.offset;
3299
3300 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003301 inode_sub_bytes(inode, item_end + 1 -
3302 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003303 }
3304 size =
3305 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003306 btrfs_truncate_item(trans, root, path,
3307 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003308 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003309 inode_sub_bytes(inode, item_end + 1 -
3310 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003311 }
Chris Mason39279cc2007-06-12 06:35:45 -04003312 }
Chris Mason179e29e2007-11-01 11:28:41 -04003313delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003314 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003315 if (!pending_del_nr) {
3316 /* no pending yet, add ourselves */
3317 pending_del_slot = path->slots[0];
3318 pending_del_nr = 1;
3319 } else if (pending_del_nr &&
3320 path->slots[0] + 1 == pending_del_slot) {
3321 /* hop on the pending chunk */
3322 pending_del_nr++;
3323 pending_del_slot = path->slots[0];
3324 } else {
Chris Masond3977122009-01-05 21:25:51 -05003325 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003326 }
Chris Mason39279cc2007-06-12 06:35:45 -04003327 } else {
3328 break;
3329 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003330 if (found_extent && (root->ref_cows ||
3331 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003332 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003333 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003334 extent_num_bytes, 0,
3335 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003336 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003337 BUG_ON(ret);
3338 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003339
Yan, Zheng80825102009-11-12 09:35:36 +00003340 if (found_type == BTRFS_INODE_ITEM_KEY)
3341 break;
3342
3343 if (path->slots[0] == 0 ||
3344 path->slots[0] != pending_del_slot) {
Li Zefan82d59022011-04-20 10:33:24 +08003345 if (root->ref_cows &&
3346 BTRFS_I(inode)->location.objectid !=
3347 BTRFS_FREE_INO_OBJECTID) {
Yan, Zheng80825102009-11-12 09:35:36 +00003348 err = -EAGAIN;
3349 goto out;
3350 }
3351 if (pending_del_nr) {
3352 ret = btrfs_del_items(trans, root, path,
3353 pending_del_slot,
3354 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003355 if (ret) {
3356 btrfs_abort_transaction(trans,
3357 root, ret);
3358 goto error;
3359 }
Yan, Zheng80825102009-11-12 09:35:36 +00003360 pending_del_nr = 0;
3361 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003362 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003363 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003364 } else {
3365 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003366 }
Chris Mason39279cc2007-06-12 06:35:45 -04003367 }
Yan, Zheng80825102009-11-12 09:35:36 +00003368out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003369 if (pending_del_nr) {
3370 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3371 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003372 if (ret)
3373 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003374 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003375error:
Chris Mason39279cc2007-06-12 06:35:45 -04003376 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003377 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003378}
3379
Chris Masona52d9a82007-08-27 16:49:44 -04003380/*
3381 * taken from block_truncate_page, but does cow as it zeros out
3382 * any bytes left in the last page in the file.
3383 */
3384static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3385{
3386 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003387 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003388 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3389 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003390 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003391 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003392 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003393 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3394 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3395 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003396 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003397 int ret = 0;
3398 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003399 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003400
3401 if ((offset & (blocksize - 1)) == 0)
3402 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003403 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003404 if (ret)
3405 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003406
3407 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003408again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003409 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003410 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003411 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003412 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003413 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003414
3415 page_start = page_offset(page);
3416 page_end = page_start + PAGE_CACHE_SIZE - 1;
3417
Chris Masona52d9a82007-08-27 16:49:44 -04003418 if (!PageUptodate(page)) {
3419 ret = btrfs_readpage(NULL, page);
3420 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003421 if (page->mapping != mapping) {
3422 unlock_page(page);
3423 page_cache_release(page);
3424 goto again;
3425 }
Chris Masona52d9a82007-08-27 16:49:44 -04003426 if (!PageUptodate(page)) {
3427 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003428 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003429 }
3430 }
Chris Mason211c17f2008-05-15 09:13:45 -04003431 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003432
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003433 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003434 set_page_extent_mapped(page);
3435
3436 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3437 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003438 unlock_extent_cached(io_tree, page_start, page_end,
3439 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003440 unlock_page(page);
3441 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003442 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003443 btrfs_put_ordered_extent(ordered);
3444 goto again;
3445 }
3446
Josef Bacik2ac55d42010-02-03 19:33:23 +00003447 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003448 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003449 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003450
Josef Bacik2ac55d42010-02-03 19:33:23 +00003451 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3452 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003453 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003454 unlock_extent_cached(io_tree, page_start, page_end,
3455 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003456 goto out_unlock;
3457 }
3458
Chris Masone6dcd2d2008-07-17 12:53:50 -04003459 ret = 0;
3460 if (offset != PAGE_CACHE_SIZE) {
3461 kaddr = kmap(page);
3462 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3463 flush_dcache_page(page);
3464 kunmap(page);
3465 }
Chris Mason247e7432008-07-17 12:53:51 -04003466 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003467 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003468 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3469 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003470
Chris Mason89642222008-07-24 09:41:53 -04003471out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003472 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003473 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003474 unlock_page(page);
3475 page_cache_release(page);
3476out:
3477 return ret;
3478}
3479
Josef Bacik695a0d02011-03-04 15:46:53 -05003480/*
3481 * This function puts in dummy file extents for the area we're creating a hole
3482 * for. So if we are truncating this file to a larger size we need to insert
3483 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3484 * the range between oldsize and size
3485 */
Josef Bacika41ad392011-01-31 15:30:16 -05003486int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003487{
3488 struct btrfs_trans_handle *trans;
3489 struct btrfs_root *root = BTRFS_I(inode)->root;
3490 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003491 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003492 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003493 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003494 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003495 u64 block_end = (size + mask) & ~mask;
3496 u64 last_byte;
3497 u64 cur_offset;
3498 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003499 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003500
3501 if (size <= hole_start)
3502 return 0;
3503
Yan Zheng9036c102008-10-30 14:19:41 -04003504 while (1) {
3505 struct btrfs_ordered_extent *ordered;
3506 btrfs_wait_ordered_range(inode, hole_start,
3507 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003508 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003509 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003510 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3511 if (!ordered)
3512 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003513 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3514 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003515 btrfs_put_ordered_extent(ordered);
3516 }
3517
Yan Zheng9036c102008-10-30 14:19:41 -04003518 cur_offset = hole_start;
3519 while (1) {
3520 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3521 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003522 if (IS_ERR(em)) {
3523 err = PTR_ERR(em);
3524 break;
3525 }
Yan Zheng9036c102008-10-30 14:19:41 -04003526 last_byte = min(extent_map_end(em), block_end);
3527 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003528 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003529 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003530 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003531
Miao Xie36423202011-12-14 20:12:02 -05003532 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003533 if (IS_ERR(trans)) {
3534 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003535 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003536 }
Yan, Zheng80825102009-11-12 09:35:36 +00003537
3538 err = btrfs_drop_extents(trans, inode, cur_offset,
3539 cur_offset + hole_size,
3540 &hint_byte, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003541 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003542 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003543 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003544 break;
Miao Xie5b397372011-09-11 10:52:24 -04003545 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003546
Yan Zheng9036c102008-10-30 14:19:41 -04003547 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003548 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003549 0, hole_size, 0, hole_size,
3550 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003551 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003552 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003553 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003554 break;
Miao Xie5b397372011-09-11 10:52:24 -04003555 }
Yan, Zheng80825102009-11-12 09:35:36 +00003556
Yan Zheng9036c102008-10-30 14:19:41 -04003557 btrfs_drop_extent_cache(inode, hole_start,
3558 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003559
Miao Xie36423202011-12-14 20:12:02 -05003560 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003561 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003562 }
3563 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003564 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003565 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003566 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003567 break;
3568 }
3569
Yan, Zhenga22285a2010-05-16 10:48:46 -04003570 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003571 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3572 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003573 return err;
3574}
3575
Josef Bacika41ad392011-01-31 15:30:16 -05003576static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003577{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003578 struct btrfs_root *root = BTRFS_I(inode)->root;
3579 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003580 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003581 int ret;
3582
Josef Bacika41ad392011-01-31 15:30:16 -05003583 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003584 return 0;
3585
Josef Bacika41ad392011-01-31 15:30:16 -05003586 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003587 truncate_pagecache(inode, oldsize, newsize);
3588 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003589 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003590 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003591
Miao Xief4a2f4c2011-12-14 20:12:01 -05003592 trans = btrfs_start_transaction(root, 1);
3593 if (IS_ERR(trans))
3594 return PTR_ERR(trans);
3595
3596 i_size_write(inode, newsize);
3597 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3598 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003599 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003600 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003601
Josef Bacika41ad392011-01-31 15:30:16 -05003602 /*
3603 * We're truncating a file that used to have good data down to
3604 * zero. Make sure it gets into the ordered flush list so that
3605 * any new writes get down to disk quickly.
3606 */
3607 if (newsize == 0)
3608 BTRFS_I(inode)->ordered_data_close = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003609
Josef Bacika41ad392011-01-31 15:30:16 -05003610 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3611 truncate_setsize(inode, newsize);
3612 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003613 }
3614
Josef Bacika41ad392011-01-31 15:30:16 -05003615 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003616}
3617
Chris Mason39279cc2007-06-12 06:35:45 -04003618static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3619{
3620 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003621 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003622 int err;
3623
Li Zefanb83cc962010-12-20 16:04:08 +08003624 if (btrfs_root_readonly(root))
3625 return -EROFS;
3626
Chris Mason39279cc2007-06-12 06:35:45 -04003627 err = inode_change_ok(inode, attr);
3628 if (err)
3629 return err;
3630
Chris Mason5a3f23d2009-03-31 13:27:11 -04003631 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003632 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003633 if (err)
3634 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003635 }
Yan Zheng9036c102008-10-30 14:19:41 -04003636
Christoph Hellwig10257742010-06-04 11:30:02 +02003637 if (attr->ia_valid) {
3638 setattr_copy(inode, attr);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003639 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003640
Josef Bacik22c44fe2011-11-30 10:45:38 -05003641 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003642 err = btrfs_acl_chmod(inode);
3643 }
3644
Chris Mason39279cc2007-06-12 06:35:45 -04003645 return err;
3646}
Chris Mason61295eb2008-01-14 16:24:38 -05003647
Al Virobd555972010-06-07 11:35:40 -04003648void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003649{
3650 struct btrfs_trans_handle *trans;
3651 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003652 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003653 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003654 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003655 int ret;
3656
liubo1abe9b82011-03-24 11:18:59 +00003657 trace_btrfs_inode_evict(inode);
3658
Chris Mason39279cc2007-06-12 06:35:45 -04003659 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003660 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Chris Mason2cf85722011-07-26 15:35:09 -04003661 btrfs_is_free_space_inode(root, inode)))
Al Virobd555972010-06-07 11:35:40 -04003662 goto no_delete;
3663
Chris Mason39279cc2007-06-12 06:35:45 -04003664 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003665 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003666 goto no_delete;
3667 }
Al Virobd555972010-06-07 11:35:40 -04003668 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003669 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003670
Yan, Zhengc71bf092009-11-12 09:34:40 +00003671 if (root->fs_info->log_root_recovering) {
3672 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3673 goto no_delete;
3674 }
3675
Yan, Zheng76dda932009-09-21 16:00:26 -04003676 if (inode->i_nlink > 0) {
3677 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3678 goto no_delete;
3679 }
3680
Josef Bacik4289a662011-08-05 13:22:24 -04003681 rsv = btrfs_alloc_block_rsv(root);
3682 if (!rsv) {
3683 btrfs_orphan_del(NULL, inode);
3684 goto no_delete;
3685 }
Josef Bacik4a338542011-08-29 11:01:31 -04003686 rsv->size = min_size;
Josef Bacik726c35f2011-09-26 15:46:06 -04003687 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003688
Chris Masondbe674a2008-07-17 12:54:05 -04003689 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003690
Josef Bacik4289a662011-08-05 13:22:24 -04003691 /*
3692 * This is a bit simpler than btrfs_truncate since
3693 *
3694 * 1) We've already reserved our space for our orphan item in the
3695 * unlink.
3696 * 2) We're going to delete the inode item, so we don't need to update
3697 * it at all.
3698 *
3699 * So we just need to reserve some slack space in case we add bytes when
3700 * doing the truncate.
3701 */
Yan, Zheng80825102009-11-12 09:35:36 +00003702 while (1) {
Miao Xieaa38a712011-11-18 17:43:00 +08003703 ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003704
Josef Bacik726c35f2011-09-26 15:46:06 -04003705 /*
3706 * Try and steal from the global reserve since we will
3707 * likely not use this space anyway, we want to try as
3708 * hard as possible to get this to work.
3709 */
3710 if (ret)
3711 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3712
Yan, Zhengd68fc572010-05-16 10:49:58 -04003713 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003714 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003715 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003716 btrfs_orphan_del(NULL, inode);
3717 btrfs_free_block_rsv(root, rsv);
3718 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003719 }
3720
Josef Bacik4289a662011-08-05 13:22:24 -04003721 trans = btrfs_start_transaction(root, 0);
3722 if (IS_ERR(trans)) {
3723 btrfs_orphan_del(NULL, inode);
3724 btrfs_free_block_rsv(root, rsv);
3725 goto no_delete;
3726 }
3727
3728 trans->block_rsv = rsv;
3729
Yan, Zhengd68fc572010-05-16 10:49:58 -04003730 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003731 if (ret != -EAGAIN)
3732 break;
3733
3734 nr = trans->blocks_used;
3735 btrfs_end_transaction(trans, root);
3736 trans = NULL;
3737 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003738 }
3739
Josef Bacik4289a662011-08-05 13:22:24 -04003740 btrfs_free_block_rsv(root, rsv);
3741
Yan, Zheng80825102009-11-12 09:35:36 +00003742 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003743 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003744 ret = btrfs_orphan_del(trans, inode);
3745 BUG_ON(ret);
3746 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003747
Josef Bacik4289a662011-08-05 13:22:24 -04003748 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003749 if (!(root == root->fs_info->tree_root ||
3750 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003751 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003752
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003753 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003754 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003755 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003756no_delete:
Al Virobd555972010-06-07 11:35:40 -04003757 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003758 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003759}
3760
3761/*
3762 * this returns the key found in the dir entry in the location pointer.
3763 * If no dir entries were found, location->objectid is 0.
3764 */
3765static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3766 struct btrfs_key *location)
3767{
3768 const char *name = dentry->d_name.name;
3769 int namelen = dentry->d_name.len;
3770 struct btrfs_dir_item *di;
3771 struct btrfs_path *path;
3772 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003773 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003774
3775 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003776 if (!path)
3777 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003778
Li Zefan33345d012011-04-20 10:31:50 +08003779 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003780 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003781 if (IS_ERR(di))
3782 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003783
David Sterbac7040052011-04-19 18:00:01 +02003784 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003785 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003786
Chris Mason5f39d392007-10-15 16:14:19 -04003787 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003788out:
Chris Mason39279cc2007-06-12 06:35:45 -04003789 btrfs_free_path(path);
3790 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003791out_err:
3792 location->objectid = 0;
3793 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003794}
3795
3796/*
3797 * when we hit a tree root in a directory, the btrfs part of the inode
3798 * needs to be changed to reflect the root directory of the tree root. This
3799 * is kind of like crossing a mount point.
3800 */
3801static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003802 struct inode *dir,
3803 struct dentry *dentry,
3804 struct btrfs_key *location,
3805 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003806{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003807 struct btrfs_path *path;
3808 struct btrfs_root *new_root;
3809 struct btrfs_root_ref *ref;
3810 struct extent_buffer *leaf;
3811 int ret;
3812 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003813
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003814 path = btrfs_alloc_path();
3815 if (!path) {
3816 err = -ENOMEM;
3817 goto out;
3818 }
Chris Mason39279cc2007-06-12 06:35:45 -04003819
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003820 err = -ENOENT;
3821 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3822 BTRFS_I(dir)->root->root_key.objectid,
3823 location->objectid);
3824 if (ret) {
3825 if (ret < 0)
3826 err = ret;
3827 goto out;
3828 }
Chris Mason39279cc2007-06-12 06:35:45 -04003829
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003830 leaf = path->nodes[0];
3831 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003832 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003833 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3834 goto out;
3835
3836 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3837 (unsigned long)(ref + 1),
3838 dentry->d_name.len);
3839 if (ret)
3840 goto out;
3841
David Sterbab3b4aa72011-04-21 01:20:15 +02003842 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003843
3844 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3845 if (IS_ERR(new_root)) {
3846 err = PTR_ERR(new_root);
3847 goto out;
3848 }
3849
3850 if (btrfs_root_refs(&new_root->root_item) == 0) {
3851 err = -ENOENT;
3852 goto out;
3853 }
3854
3855 *sub_root = new_root;
3856 location->objectid = btrfs_root_dirid(&new_root->root_item);
3857 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003858 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003859 err = 0;
3860out:
3861 btrfs_free_path(path);
3862 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003863}
3864
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003865static void inode_tree_add(struct inode *inode)
3866{
3867 struct btrfs_root *root = BTRFS_I(inode)->root;
3868 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003869 struct rb_node **p;
3870 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08003871 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003872again:
3873 p = &root->inode_tree.rb_node;
3874 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003875
Al Viro1d3382cb2010-10-23 15:19:20 -04003876 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003877 return;
3878
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003879 spin_lock(&root->inode_lock);
3880 while (*p) {
3881 parent = *p;
3882 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3883
Li Zefan33345d012011-04-20 10:31:50 +08003884 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003885 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003886 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003887 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003888 else {
3889 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003890 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003891 rb_erase(parent, &root->inode_tree);
3892 RB_CLEAR_NODE(parent);
3893 spin_unlock(&root->inode_lock);
3894 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003895 }
3896 }
3897 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3898 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3899 spin_unlock(&root->inode_lock);
3900}
3901
3902static void inode_tree_del(struct inode *inode)
3903{
3904 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003905 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003906
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003907 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003908 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003909 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003910 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003911 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003912 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003913 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003914
Josef Bacik0af3d002010-06-21 14:48:16 -04003915 /*
3916 * Free space cache has inodes in the tree root, but the tree root has a
3917 * root_refs of 0, so this could end up dropping the tree root as a
3918 * snapshot, so we need the extra !root->fs_info->tree_root check to
3919 * make sure we don't drop it.
3920 */
3921 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3922 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003923 synchronize_srcu(&root->fs_info->subvol_srcu);
3924 spin_lock(&root->inode_lock);
3925 empty = RB_EMPTY_ROOT(&root->inode_tree);
3926 spin_unlock(&root->inode_lock);
3927 if (empty)
3928 btrfs_add_dead_root(root);
3929 }
3930}
3931
Jeff Mahoney143bede2012-03-01 14:56:26 +01003932void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04003933{
3934 struct rb_node *node;
3935 struct rb_node *prev;
3936 struct btrfs_inode *entry;
3937 struct inode *inode;
3938 u64 objectid = 0;
3939
3940 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3941
3942 spin_lock(&root->inode_lock);
3943again:
3944 node = root->inode_tree.rb_node;
3945 prev = NULL;
3946 while (node) {
3947 prev = node;
3948 entry = rb_entry(node, struct btrfs_inode, rb_node);
3949
Li Zefan33345d012011-04-20 10:31:50 +08003950 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003951 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003952 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003953 node = node->rb_right;
3954 else
3955 break;
3956 }
3957 if (!node) {
3958 while (prev) {
3959 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08003960 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003961 node = prev;
3962 break;
3963 }
3964 prev = rb_next(prev);
3965 }
3966 }
3967 while (node) {
3968 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08003969 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04003970 inode = igrab(&entry->vfs_inode);
3971 if (inode) {
3972 spin_unlock(&root->inode_lock);
3973 if (atomic_read(&inode->i_count) > 1)
3974 d_prune_aliases(inode);
3975 /*
Al Viro45321ac2010-06-07 13:43:19 -04003976 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04003977 * the inode cache when its usage count
3978 * hits zero.
3979 */
3980 iput(inode);
3981 cond_resched();
3982 spin_lock(&root->inode_lock);
3983 goto again;
3984 }
3985
3986 if (cond_resched_lock(&root->inode_lock))
3987 goto again;
3988
3989 node = rb_next(node);
3990 }
3991 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003992}
3993
Chris Masone02119d2008-09-05 16:13:11 -04003994static int btrfs_init_locked_inode(struct inode *inode, void *p)
3995{
3996 struct btrfs_iget_args *args = p;
3997 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04003998 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003999 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004000 return 0;
4001}
4002
4003static int btrfs_find_actor(struct inode *inode, void *opaque)
4004{
4005 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004006 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004007 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004008}
4009
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004010static struct inode *btrfs_iget_locked(struct super_block *s,
4011 u64 objectid,
4012 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004013{
4014 struct inode *inode;
4015 struct btrfs_iget_args args;
4016 args.ino = objectid;
4017 args.root = root;
4018
4019 inode = iget5_locked(s, objectid, btrfs_find_actor,
4020 btrfs_init_locked_inode,
4021 (void *)&args);
4022 return inode;
4023}
4024
Balaji Rao1a54ef82008-07-21 02:01:04 +05304025/* Get an inode object given its location and corresponding root.
4026 * Returns in *is_new if the inode was read from disk
4027 */
4028struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004029 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304030{
4031 struct inode *inode;
4032
4033 inode = btrfs_iget_locked(s, location->objectid, root);
4034 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004035 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304036
4037 if (inode->i_state & I_NEW) {
4038 BTRFS_I(inode)->root = root;
4039 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4040 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004041 if (!is_bad_inode(inode)) {
4042 inode_tree_add(inode);
4043 unlock_new_inode(inode);
4044 if (new)
4045 *new = 1;
4046 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004047 unlock_new_inode(inode);
4048 iput(inode);
4049 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004050 }
4051 }
4052
Balaji Rao1a54ef82008-07-21 02:01:04 +05304053 return inode;
4054}
4055
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004056static struct inode *new_simple_dir(struct super_block *s,
4057 struct btrfs_key *key,
4058 struct btrfs_root *root)
4059{
4060 struct inode *inode = new_inode(s);
4061
4062 if (!inode)
4063 return ERR_PTR(-ENOMEM);
4064
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004065 BTRFS_I(inode)->root = root;
4066 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4067 BTRFS_I(inode)->dummy_inode = 1;
4068
4069 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4070 inode->i_op = &simple_dir_inode_operations;
4071 inode->i_fop = &simple_dir_operations;
4072 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4073 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4074
4075 return inode;
4076}
4077
Chris Mason3de45862008-11-17 21:02:50 -05004078struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004079{
Chris Masond3977122009-01-05 21:25:51 -05004080 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004081 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004082 struct btrfs_root *sub_root = root;
4083 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004084 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004085 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004086
4087 if (dentry->d_name.len > BTRFS_NAME_LEN)
4088 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004089
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004090 if (unlikely(d_need_lookup(dentry))) {
4091 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4092 kfree(dentry->d_fsdata);
4093 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004094 /* This thing is hashed, drop it for now */
4095 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004096 } else {
4097 ret = btrfs_inode_by_name(dir, dentry, &location);
4098 }
Chris Mason5f39d392007-10-15 16:14:19 -04004099
Chris Mason39279cc2007-06-12 06:35:45 -04004100 if (ret < 0)
4101 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004102
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004103 if (location.objectid == 0)
4104 return NULL;
4105
4106 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004107 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004108 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004109 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004110
4111 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4112
Yan, Zheng76dda932009-09-21 16:00:26 -04004113 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004114 ret = fixup_tree_root_location(root, dir, dentry,
4115 &location, &sub_root);
4116 if (ret < 0) {
4117 if (ret != -ENOENT)
4118 inode = ERR_PTR(ret);
4119 else
4120 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4121 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004122 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004123 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004124 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4125
Julia Lawall34d19ba2011-01-24 19:55:19 +00004126 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004127 down_read(&root->fs_info->cleanup_work_sem);
4128 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004129 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004130 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004131 if (ret)
4132 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004133 }
4134
Chris Mason3de45862008-11-17 21:02:50 -05004135 return inode;
4136}
4137
Nick Pigginfe15ce42011-01-07 17:49:23 +11004138static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004139{
4140 struct btrfs_root *root;
4141
Yan, Zhengefefb142009-10-09 09:25:16 -04004142 if (!dentry->d_inode && !IS_ROOT(dentry))
4143 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004144
Yan, Zhengefefb142009-10-09 09:25:16 -04004145 if (dentry->d_inode) {
4146 root = BTRFS_I(dentry->d_inode)->root;
4147 if (btrfs_root_refs(&root->root_item) == 0)
4148 return 1;
4149 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004150 return 0;
4151}
4152
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004153static void btrfs_dentry_release(struct dentry *dentry)
4154{
4155 if (dentry->d_fsdata)
4156 kfree(dentry->d_fsdata);
4157}
4158
Chris Mason3de45862008-11-17 21:02:50 -05004159static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4160 struct nameidata *nd)
4161{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004162 struct dentry *ret;
4163
4164 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4165 if (unlikely(d_need_lookup(dentry))) {
4166 spin_lock(&dentry->d_lock);
4167 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4168 spin_unlock(&dentry->d_lock);
4169 }
4170 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004171}
4172
Miao Xie16cdcec2011-04-22 18:12:22 +08004173unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004174 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4175};
4176
David Woodhousecbdf5a22008-08-06 19:42:33 +01004177static int btrfs_real_readdir(struct file *filp, void *dirent,
4178 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004179{
Chris Mason6da6aba2007-12-18 16:15:09 -05004180 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004181 struct btrfs_root *root = BTRFS_I(inode)->root;
4182 struct btrfs_item *item;
4183 struct btrfs_dir_item *di;
4184 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004185 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004186 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004187 struct list_head ins_list;
4188 struct list_head del_list;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004189 struct qstr q;
Chris Mason39279cc2007-06-12 06:35:45 -04004190 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004191 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004192 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004193 unsigned char d_type;
4194 int over = 0;
4195 u32 di_cur;
4196 u32 di_total;
4197 u32 di_len;
4198 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004199 char tmp_name[32];
4200 char *name_ptr;
4201 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004202 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004203
4204 /* FIXME, use a real flag for deciding about the key type */
4205 if (root->fs_info->tree_root == root)
4206 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004207
Chris Mason39544012007-12-12 14:38:19 -05004208 /* special case for "." */
4209 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004210 over = filldir(dirent, ".", 1,
4211 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004212 if (over)
4213 return 0;
4214 filp->f_pos = 1;
4215 }
Chris Mason39544012007-12-12 14:38:19 -05004216 /* special case for .., just use the back ref */
4217 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004218 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004219 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004220 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004221 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004222 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004223 filp->f_pos = 2;
4224 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004225 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004226 if (!path)
4227 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004228
Josef Bacik026fd312011-05-13 10:32:11 -04004229 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004230
Miao Xie16cdcec2011-04-22 18:12:22 +08004231 if (key_type == BTRFS_DIR_INDEX_KEY) {
4232 INIT_LIST_HEAD(&ins_list);
4233 INIT_LIST_HEAD(&del_list);
4234 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4235 }
4236
Chris Mason39279cc2007-06-12 06:35:45 -04004237 btrfs_set_key_type(&key, key_type);
4238 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004239 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004240
Chris Mason39279cc2007-06-12 06:35:45 -04004241 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4242 if (ret < 0)
4243 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004244
4245 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004246 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004247 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004248 if (slot >= btrfs_header_nritems(leaf)) {
4249 ret = btrfs_next_leaf(root, path);
4250 if (ret < 0)
4251 goto err;
4252 else if (ret > 0)
4253 break;
4254 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004255 }
Chris Mason3de45862008-11-17 21:02:50 -05004256
Chris Mason5f39d392007-10-15 16:14:19 -04004257 item = btrfs_item_nr(leaf, slot);
4258 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4259
4260 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004261 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004262 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004263 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004264 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004265 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004266 if (key_type == BTRFS_DIR_INDEX_KEY &&
4267 btrfs_should_delete_dir_index(&del_list,
4268 found_key.offset))
4269 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004270
4271 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004272 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004273
Chris Mason39279cc2007-06-12 06:35:45 -04004274 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4275 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004276 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004277
4278 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004279 struct btrfs_key location;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004280 struct dentry *tmp;
Chris Mason5f39d392007-10-15 16:14:19 -04004281
Josef Bacik22a94d42011-03-16 16:47:17 -04004282 if (verify_dir_item(root, leaf, di))
4283 break;
4284
Chris Mason5f39d392007-10-15 16:14:19 -04004285 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004286 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004287 name_ptr = tmp_name;
4288 } else {
4289 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004290 if (!name_ptr) {
4291 ret = -ENOMEM;
4292 goto err;
4293 }
Chris Mason5f39d392007-10-15 16:14:19 -04004294 }
4295 read_extent_buffer(leaf, name_ptr,
4296 (unsigned long)(di + 1), name_len);
4297
4298 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4299 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004300
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004301 q.name = name_ptr;
4302 q.len = name_len;
4303 q.hash = full_name_hash(q.name, q.len);
4304 tmp = d_lookup(filp->f_dentry, &q);
4305 if (!tmp) {
4306 struct btrfs_key *newkey;
4307
4308 newkey = kzalloc(sizeof(struct btrfs_key),
4309 GFP_NOFS);
4310 if (!newkey)
4311 goto no_dentry;
4312 tmp = d_alloc(filp->f_dentry, &q);
4313 if (!tmp) {
4314 kfree(newkey);
4315 dput(tmp);
4316 goto no_dentry;
4317 }
4318 memcpy(newkey, &location,
4319 sizeof(struct btrfs_key));
4320 tmp->d_fsdata = newkey;
4321 tmp->d_flags |= DCACHE_NEED_LOOKUP;
4322 d_rehash(tmp);
4323 dput(tmp);
4324 } else {
4325 dput(tmp);
4326 }
4327no_dentry:
Chris Mason3de45862008-11-17 21:02:50 -05004328 /* is this a reference to our own snapshot? If so
4329 * skip it
4330 */
4331 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4332 location.objectid == root->root_key.objectid) {
4333 over = 0;
4334 goto skip;
4335 }
Chris Mason5f39d392007-10-15 16:14:19 -04004336 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004337 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004338 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004339
Chris Mason3de45862008-11-17 21:02:50 -05004340skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004341 if (name_ptr != tmp_name)
4342 kfree(name_ptr);
4343
Chris Mason39279cc2007-06-12 06:35:45 -04004344 if (over)
4345 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004346 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004347 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004348 di_cur += di_len;
4349 di = (struct btrfs_dir_item *)((char *)di + di_len);
4350 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004351next:
4352 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004353 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004354
Miao Xie16cdcec2011-04-22 18:12:22 +08004355 if (key_type == BTRFS_DIR_INDEX_KEY) {
4356 if (is_curr)
4357 filp->f_pos++;
4358 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4359 &ins_list);
4360 if (ret)
4361 goto nopos;
4362 }
4363
David Woodhouse49593bf2008-08-17 17:08:36 +01004364 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004365 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004366 /*
4367 * 32-bit glibc will use getdents64, but then strtol -
4368 * so the last number we can serve is this.
4369 */
4370 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004371 else
4372 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004373nopos:
4374 ret = 0;
4375err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004376 if (key_type == BTRFS_DIR_INDEX_KEY)
4377 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004378 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004379 return ret;
4380}
4381
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004382int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004383{
4384 struct btrfs_root *root = BTRFS_I(inode)->root;
4385 struct btrfs_trans_handle *trans;
4386 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004387 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004388
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004389 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004390 return 0;
4391
Chris Mason2cf85722011-07-26 15:35:09 -04004392 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(root, inode))
Li Zefan82d59022011-04-20 10:33:24 +08004393 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004394
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004395 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004396 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004397 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004398 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004399 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004400 if (IS_ERR(trans))
4401 return PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04004402 if (nolock)
4403 ret = btrfs_end_transaction_nolock(trans, root);
4404 else
4405 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004406 }
4407 return ret;
4408}
4409
4410/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004411 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004412 * inode changes. But, it is most likely to find the inode in cache.
4413 * FIXME, needs more benchmarking...there are no reasons other than performance
4414 * to keep or drop this code.
4415 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004416int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004417{
4418 struct btrfs_root *root = BTRFS_I(inode)->root;
4419 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004420 int ret;
4421
4422 if (BTRFS_I(inode)->dummy_inode)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004423 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004424
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004425 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004426 if (IS_ERR(trans))
4427 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004428
4429 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004430 if (ret && ret == -ENOSPC) {
4431 /* whoops, lets try again with the full transaction */
4432 btrfs_end_transaction(trans, root);
4433 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004434 if (IS_ERR(trans))
4435 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004436
Chris Mason94b60442010-05-26 11:02:00 -04004437 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004438 }
Chris Mason39279cc2007-06-12 06:35:45 -04004439 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004440 if (BTRFS_I(inode)->delayed_node)
4441 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004442
4443 return ret;
4444}
4445
4446/*
4447 * This is a copy of file_update_time. We need this so we can return error on
4448 * ENOSPC for updating the inode in the case of file write and mmap writes.
4449 */
4450int btrfs_update_time(struct file *file)
4451{
4452 struct inode *inode = file->f_path.dentry->d_inode;
4453 struct timespec now;
4454 int ret;
4455 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
4456
4457 /* First try to exhaust all avenues to not sync */
4458 if (IS_NOCMTIME(inode))
4459 return 0;
4460
4461 now = current_fs_time(inode->i_sb);
4462 if (!timespec_equal(&inode->i_mtime, &now))
4463 sync_it = S_MTIME;
4464
4465 if (!timespec_equal(&inode->i_ctime, &now))
4466 sync_it |= S_CTIME;
4467
4468 if (IS_I_VERSION(inode))
4469 sync_it |= S_VERSION;
4470
4471 if (!sync_it)
4472 return 0;
4473
4474 /* Finally allowed to write? Takes lock. */
4475 if (mnt_want_write_file(file))
4476 return 0;
4477
4478 /* Only change inode inside the lock region */
4479 if (sync_it & S_VERSION)
4480 inode_inc_iversion(inode);
4481 if (sync_it & S_CTIME)
4482 inode->i_ctime = now;
4483 if (sync_it & S_MTIME)
4484 inode->i_mtime = now;
4485 ret = btrfs_dirty_inode(inode);
4486 if (!ret)
4487 mark_inode_dirty_sync(inode);
4488 mnt_drop_write(file->f_path.mnt);
4489 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004490}
4491
Chris Masond352ac62008-09-29 15:18:18 -04004492/*
4493 * find the highest existing sequence number in a directory
4494 * and then set the in-memory index_cnt variable to reflect
4495 * free sequence numbers
4496 */
Josef Bacikaec74772008-07-24 12:12:38 -04004497static int btrfs_set_inode_index_count(struct inode *inode)
4498{
4499 struct btrfs_root *root = BTRFS_I(inode)->root;
4500 struct btrfs_key key, found_key;
4501 struct btrfs_path *path;
4502 struct extent_buffer *leaf;
4503 int ret;
4504
Li Zefan33345d012011-04-20 10:31:50 +08004505 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004506 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4507 key.offset = (u64)-1;
4508
4509 path = btrfs_alloc_path();
4510 if (!path)
4511 return -ENOMEM;
4512
4513 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4514 if (ret < 0)
4515 goto out;
4516 /* FIXME: we should be able to handle this */
4517 if (ret == 0)
4518 goto out;
4519 ret = 0;
4520
4521 /*
4522 * MAGIC NUMBER EXPLANATION:
4523 * since we search a directory based on f_pos we have to start at 2
4524 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4525 * else has to start at 2
4526 */
4527 if (path->slots[0] == 0) {
4528 BTRFS_I(inode)->index_cnt = 2;
4529 goto out;
4530 }
4531
4532 path->slots[0]--;
4533
4534 leaf = path->nodes[0];
4535 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4536
Li Zefan33345d012011-04-20 10:31:50 +08004537 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004538 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4539 BTRFS_I(inode)->index_cnt = 2;
4540 goto out;
4541 }
4542
4543 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4544out:
4545 btrfs_free_path(path);
4546 return ret;
4547}
4548
Chris Masond352ac62008-09-29 15:18:18 -04004549/*
4550 * helper to find a free sequence number in a given directory. This current
4551 * code is very simple, later versions will do smarter things in the btree
4552 */
Chris Mason3de45862008-11-17 21:02:50 -05004553int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004554{
4555 int ret = 0;
4556
4557 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004558 ret = btrfs_inode_delayed_dir_index_count(dir);
4559 if (ret) {
4560 ret = btrfs_set_inode_index_count(dir);
4561 if (ret)
4562 return ret;
4563 }
Josef Bacikaec74772008-07-24 12:12:38 -04004564 }
4565
Chris Mason00e4e6b2008-08-05 11:18:09 -04004566 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004567 BTRFS_I(dir)->index_cnt++;
4568
4569 return ret;
4570}
4571
Chris Mason39279cc2007-06-12 06:35:45 -04004572static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4573 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004574 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004575 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004576 u64 ref_objectid, u64 objectid,
4577 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004578{
4579 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004580 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004581 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004582 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004583 struct btrfs_inode_ref *ref;
4584 struct btrfs_key key[2];
4585 u32 sizes[2];
4586 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004587 int ret;
4588 int owner;
4589
Chris Mason5f39d392007-10-15 16:14:19 -04004590 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004591 if (!path)
4592 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004593
Chris Mason39279cc2007-06-12 06:35:45 -04004594 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004595 if (!inode) {
4596 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004597 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004598 }
Chris Mason39279cc2007-06-12 06:35:45 -04004599
Li Zefan581bb052011-04-20 10:06:11 +08004600 /*
4601 * we have to initialize this early, so we can reclaim the inode
4602 * number if we fail afterwards in this function.
4603 */
4604 inode->i_ino = objectid;
4605
Josef Bacikaec74772008-07-24 12:12:38 -04004606 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004607 trace_btrfs_inode_request(dir);
4608
Chris Mason3de45862008-11-17 21:02:50 -05004609 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004610 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004611 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004612 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004613 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004614 }
Josef Bacikaec74772008-07-24 12:12:38 -04004615 }
4616 /*
4617 * index_cnt is ignored for everything but a dir,
4618 * btrfs_get_inode_index_count has an explanation for the magic
4619 * number
4620 */
4621 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004622 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004623 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004624 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004625 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004626
Al Viro569254b2011-07-24 17:08:40 -04004627 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004628 owner = 0;
4629 else
4630 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004631
4632 key[0].objectid = objectid;
4633 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4634 key[0].offset = 0;
4635
4636 key[1].objectid = objectid;
4637 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4638 key[1].offset = ref_objectid;
4639
4640 sizes[0] = sizeof(struct btrfs_inode_item);
4641 sizes[1] = name_len + sizeof(*ref);
4642
Chris Masonb9473432009-03-13 11:00:37 -04004643 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004644 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4645 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004646 goto fail;
4647
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004648 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004649 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004650 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004651 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4652 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004653 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004654
4655 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4656 struct btrfs_inode_ref);
4657 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004658 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004659 ptr = (unsigned long)(ref + 1);
4660 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4661
Chris Mason5f39d392007-10-15 16:14:19 -04004662 btrfs_mark_buffer_dirty(path->nodes[0]);
4663 btrfs_free_path(path);
4664
Chris Mason39279cc2007-06-12 06:35:45 -04004665 location = &BTRFS_I(inode)->location;
4666 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004667 location->offset = 0;
4668 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4669
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004670 btrfs_inherit_iflags(inode, dir);
4671
Al Viro569254b2011-07-24 17:08:40 -04004672 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004673 if (btrfs_test_opt(root, NODATASUM))
4674 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004675 if (btrfs_test_opt(root, NODATACOW) ||
4676 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004677 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4678 }
4679
Chris Mason39279cc2007-06-12 06:35:45 -04004680 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004681 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004682
4683 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004684 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004685
Chris Mason39279cc2007-06-12 06:35:45 -04004686 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004687fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004688 if (dir)
4689 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004690 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004691 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004692 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004693}
4694
4695static inline u8 btrfs_inode_type(struct inode *inode)
4696{
4697 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4698}
4699
Chris Masond352ac62008-09-29 15:18:18 -04004700/*
4701 * utility function to add 'inode' into 'parent_inode' with
4702 * a give name and a given sequence number.
4703 * if 'add_backref' is true, also insert a backref from the
4704 * inode to the parent directory.
4705 */
Chris Masone02119d2008-09-05 16:13:11 -04004706int btrfs_add_link(struct btrfs_trans_handle *trans,
4707 struct inode *parent_inode, struct inode *inode,
4708 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004709{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004710 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004711 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004712 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004713 u64 ino = btrfs_ino(inode);
4714 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004715
Li Zefan33345d012011-04-20 10:31:50 +08004716 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004717 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4718 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004719 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004720 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4721 key.offset = 0;
4722 }
Chris Mason39279cc2007-06-12 06:35:45 -04004723
Li Zefan33345d012011-04-20 10:31:50 +08004724 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004725 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4726 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004727 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004728 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004729 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4730 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004731 }
4732
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004733 /* Nothing to clean up yet */
4734 if (ret)
4735 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004736
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004737 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4738 parent_inode, &key,
4739 btrfs_inode_type(inode), index);
4740 if (ret == -EEXIST)
4741 goto fail_dir_item;
4742 else if (ret) {
4743 btrfs_abort_transaction(trans, root, ret);
4744 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004745 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004746
4747 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4748 name_len * 2);
4749 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4750 ret = btrfs_update_inode(trans, root, parent_inode);
4751 if (ret)
4752 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004753 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004754
4755fail_dir_item:
4756 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4757 u64 local_index;
4758 int err;
4759 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4760 key.objectid, root->root_key.objectid,
4761 parent_ino, &local_index, name, name_len);
4762
4763 } else if (add_backref) {
4764 u64 local_index;
4765 int err;
4766
4767 err = btrfs_del_inode_ref(trans, root, name, name_len,
4768 ino, parent_ino, &local_index);
4769 }
4770 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004771}
4772
4773static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004774 struct inode *dir, struct dentry *dentry,
4775 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004776{
Josef Bacika1b075d2010-11-19 20:36:11 +00004777 int err = btrfs_add_link(trans, dir, inode,
4778 dentry->d_name.name, dentry->d_name.len,
4779 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004780 if (err > 0)
4781 err = -EEXIST;
4782 return err;
4783}
4784
Josef Bacik618e21d2007-07-11 10:18:17 -04004785static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004786 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004787{
4788 struct btrfs_trans_handle *trans;
4789 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004790 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004791 int err;
4792 int drop_inode = 0;
4793 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004794 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004795 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004796
4797 if (!new_valid_dev(rdev))
4798 return -EINVAL;
4799
Josef Bacik9ed74f22009-09-11 16:12:44 -04004800 /*
4801 * 2 for inode item and ref
4802 * 2 for dir items
4803 * 1 for xattr if selinux is on
4804 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004805 trans = btrfs_start_transaction(root, 5);
4806 if (IS_ERR(trans))
4807 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004808
Li Zefan581bb052011-04-20 10:06:11 +08004809 err = btrfs_find_free_ino(root, &objectid);
4810 if (err)
4811 goto out_unlock;
4812
Josef Bacikaec74772008-07-24 12:12:38 -04004813 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004814 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004815 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004816 if (IS_ERR(inode)) {
4817 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004818 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004819 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004820
Eric Paris2a7dba32011-02-01 11:05:39 -05004821 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004822 if (err) {
4823 drop_inode = 1;
4824 goto out_unlock;
4825 }
4826
Casey Schauflerad19db72011-12-15 10:09:07 -05004827 /*
4828 * If the active LSM wants to access the inode during
4829 * d_instantiate it needs these. Smack checks to see
4830 * if the filesystem supports xattrs by looking at the
4831 * ops vector.
4832 */
4833
4834 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004835 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004836 if (err)
4837 drop_inode = 1;
4838 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004839 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004840 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05004841 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004842 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004843out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004844 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004845 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004846 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004847 if (drop_inode) {
4848 inode_dec_link_count(inode);
4849 iput(inode);
4850 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004851 return err;
4852}
4853
Chris Mason39279cc2007-06-12 06:35:45 -04004854static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viro4acdaf22011-07-26 01:42:34 -04004855 umode_t mode, struct nameidata *nd)
Chris Mason39279cc2007-06-12 06:35:45 -04004856{
4857 struct btrfs_trans_handle *trans;
4858 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004859 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004860 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004861 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004862 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004863 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004864 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004865
Josef Bacik9ed74f22009-09-11 16:12:44 -04004866 /*
4867 * 2 for inode item and ref
4868 * 2 for dir items
4869 * 1 for xattr if selinux is on
4870 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004871 trans = btrfs_start_transaction(root, 5);
4872 if (IS_ERR(trans))
4873 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004874
Li Zefan581bb052011-04-20 10:06:11 +08004875 err = btrfs_find_free_ino(root, &objectid);
4876 if (err)
4877 goto out_unlock;
4878
Josef Bacikaec74772008-07-24 12:12:38 -04004879 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004880 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004881 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004882 if (IS_ERR(inode)) {
4883 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004884 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004885 }
Chris Mason39279cc2007-06-12 06:35:45 -04004886
Eric Paris2a7dba32011-02-01 11:05:39 -05004887 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004888 if (err) {
4889 drop_inode = 1;
4890 goto out_unlock;
4891 }
4892
Casey Schauflerad19db72011-12-15 10:09:07 -05004893 /*
4894 * If the active LSM wants to access the inode during
4895 * d_instantiate it needs these. Smack checks to see
4896 * if the filesystem supports xattrs by looking at the
4897 * ops vector.
4898 */
4899 inode->i_fop = &btrfs_file_operations;
4900 inode->i_op = &btrfs_file_inode_operations;
4901
Josef Bacika1b075d2010-11-19 20:36:11 +00004902 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004903 if (err)
4904 drop_inode = 1;
4905 else {
4906 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004907 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05004908 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro08c422c2011-12-23 07:58:13 -05004909 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004910 }
Chris Mason39279cc2007-06-12 06:35:45 -04004911out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004912 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004913 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004914 if (drop_inode) {
4915 inode_dec_link_count(inode);
4916 iput(inode);
4917 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004918 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004919 return err;
4920}
4921
4922static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4923 struct dentry *dentry)
4924{
4925 struct btrfs_trans_handle *trans;
4926 struct btrfs_root *root = BTRFS_I(dir)->root;
4927 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004928 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004929 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004930 int err;
4931 int drop_inode = 0;
4932
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004933 /* do not allow sys_link's with other subvols of the same device */
4934 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00004935 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004936
Al Viroc055e992011-03-04 17:15:18 +00004937 if (inode->i_nlink == ~0U)
4938 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004939
Chris Mason3de45862008-11-17 21:02:50 -05004940 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004941 if (err)
4942 goto fail;
4943
Yan, Zhenga22285a2010-05-16 10:48:46 -04004944 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00004945 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04004946 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00004947 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04004948 */
Miao Xie7e6b6462011-02-18 09:21:17 +00004949 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004950 if (IS_ERR(trans)) {
4951 err = PTR_ERR(trans);
4952 goto fail;
4953 }
Chris Mason5f39d392007-10-15 16:14:19 -04004954
Miao Xie31534952011-04-13 13:19:21 +08004955 btrfs_inc_nlink(inode);
4956 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04004957 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004958
Josef Bacika1b075d2010-11-19 20:36:11 +00004959 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004960
Yan, Zhenga5719522009-09-24 09:17:31 -04004961 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004962 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004963 } else {
Al Viro10d9f302011-07-16 23:09:10 -04004964 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04004965 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004966 if (err)
4967 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05004968 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00004969 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004970 }
Chris Mason39279cc2007-06-12 06:35:45 -04004971
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004972 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004973 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004974fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004975 if (drop_inode) {
4976 inode_dec_link_count(inode);
4977 iput(inode);
4978 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004979 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004980 return err;
4981}
4982
Al Viro18bb1db2011-07-26 01:41:39 -04004983static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04004984{
Chris Masonb9d86662008-05-02 16:13:49 -04004985 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004986 struct btrfs_trans_handle *trans;
4987 struct btrfs_root *root = BTRFS_I(dir)->root;
4988 int err = 0;
4989 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004990 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004991 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004992 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004993
Josef Bacik9ed74f22009-09-11 16:12:44 -04004994 /*
4995 * 2 items for inode and ref
4996 * 2 items for dir items
4997 * 1 for xattr if selinux is on
4998 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004999 trans = btrfs_start_transaction(root, 5);
5000 if (IS_ERR(trans))
5001 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005002
Li Zefan581bb052011-04-20 10:06:11 +08005003 err = btrfs_find_free_ino(root, &objectid);
5004 if (err)
5005 goto out_fail;
5006
Josef Bacikaec74772008-07-24 12:12:38 -04005007 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005008 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005009 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005010 if (IS_ERR(inode)) {
5011 err = PTR_ERR(inode);
5012 goto out_fail;
5013 }
Chris Mason5f39d392007-10-15 16:14:19 -04005014
Chris Mason39279cc2007-06-12 06:35:45 -04005015 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005016
Eric Paris2a7dba32011-02-01 11:05:39 -05005017 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005018 if (err)
5019 goto out_fail;
5020
Chris Mason39279cc2007-06-12 06:35:45 -04005021 inode->i_op = &btrfs_dir_inode_operations;
5022 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005023
Chris Masondbe674a2008-07-17 12:54:05 -04005024 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005025 err = btrfs_update_inode(trans, root, inode);
5026 if (err)
5027 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005028
Josef Bacika1b075d2010-11-19 20:36:11 +00005029 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5030 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005031 if (err)
5032 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005033
Chris Mason39279cc2007-06-12 06:35:45 -04005034 d_instantiate(dentry, inode);
5035 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005036
5037out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005038 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005039 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005040 if (drop_on_err)
5041 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005042 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005043 return err;
5044}
5045
Chris Masond352ac62008-09-29 15:18:18 -04005046/* helper for btfs_get_extent. Given an existing extent in the tree,
5047 * and an extent that you want to insert, deal with overlap and insert
5048 * the new extent into the tree.
5049 */
Chris Mason3b951512008-04-17 11:29:12 -04005050static int merge_extent_mapping(struct extent_map_tree *em_tree,
5051 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005052 struct extent_map *em,
5053 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005054{
5055 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005056
Chris Masone6dcd2d2008-07-17 12:53:50 -04005057 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5058 start_diff = map_start - em->start;
5059 em->start = map_start;
5060 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005061 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5062 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005063 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005064 em->block_len -= start_diff;
5065 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005066 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005067}
5068
Chris Masonc8b97812008-10-29 14:49:59 -04005069static noinline int uncompress_inline(struct btrfs_path *path,
5070 struct inode *inode, struct page *page,
5071 size_t pg_offset, u64 extent_offset,
5072 struct btrfs_file_extent_item *item)
5073{
5074 int ret;
5075 struct extent_buffer *leaf = path->nodes[0];
5076 char *tmp;
5077 size_t max_size;
5078 unsigned long inline_size;
5079 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005080 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005081
5082 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005083 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005084 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5085 inline_size = btrfs_file_extent_inline_item_len(leaf,
5086 btrfs_item_nr(leaf, path->slots[0]));
5087 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005088 if (!tmp)
5089 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005090 ptr = btrfs_file_extent_inline_start(item);
5091
5092 read_extent_buffer(leaf, tmp, ptr, inline_size);
5093
Chris Mason5b050f02008-11-11 09:34:41 -05005094 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005095 ret = btrfs_decompress(compress_type, tmp, page,
5096 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005097 if (ret) {
5098 char *kaddr = kmap_atomic(page, KM_USER0);
5099 unsigned long copy_size = min_t(u64,
5100 PAGE_CACHE_SIZE - pg_offset,
5101 max_size - extent_offset);
5102 memset(kaddr + pg_offset, 0, copy_size);
5103 kunmap_atomic(kaddr, KM_USER0);
5104 }
5105 kfree(tmp);
5106 return 0;
5107}
5108
Chris Masond352ac62008-09-29 15:18:18 -04005109/*
5110 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005111 * the ugly parts come from merging extents from the disk with the in-ram
5112 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005113 * where the in-ram extents might be locked pending data=ordered completion.
5114 *
5115 * This also copies inline extents directly into the page.
5116 */
Chris Masond3977122009-01-05 21:25:51 -05005117
Chris Masona52d9a82007-08-27 16:49:44 -04005118struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005119 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005120 int create)
5121{
5122 int ret;
5123 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005124 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005125 u64 extent_start = 0;
5126 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005127 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005128 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005129 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005130 struct btrfs_root *root = BTRFS_I(inode)->root;
5131 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005132 struct extent_buffer *leaf;
5133 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005134 struct extent_map *em = NULL;
5135 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005136 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005137 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005138 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005139
Chris Masona52d9a82007-08-27 16:49:44 -04005140again:
Chris Mason890871b2009-09-02 16:24:52 -04005141 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005142 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005143 if (em)
5144 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005145 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005146
Chris Masona52d9a82007-08-27 16:49:44 -04005147 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005148 if (em->start > start || em->start + em->len <= start)
5149 free_extent_map(em);
5150 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005151 free_extent_map(em);
5152 else
5153 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005154 }
David Sterba172ddd62011-04-21 00:48:27 +02005155 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005156 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005157 err = -ENOMEM;
5158 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005159 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005160 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005161 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005162 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005163 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005164 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005165
5166 if (!path) {
5167 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005168 if (!path) {
5169 err = -ENOMEM;
5170 goto out;
5171 }
5172 /*
5173 * Chances are we'll be called again, so go ahead and do
5174 * readahead
5175 */
5176 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005177 }
5178
Chris Mason179e29e2007-11-01 11:28:41 -04005179 ret = btrfs_lookup_file_extent(trans, root, path,
5180 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005181 if (ret < 0) {
5182 err = ret;
5183 goto out;
5184 }
5185
5186 if (ret != 0) {
5187 if (path->slots[0] == 0)
5188 goto not_found;
5189 path->slots[0]--;
5190 }
5191
Chris Mason5f39d392007-10-15 16:14:19 -04005192 leaf = path->nodes[0];
5193 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005194 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005195 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005196 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5197 found_type = btrfs_key_type(&found_key);
5198 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005199 found_type != BTRFS_EXTENT_DATA_KEY) {
5200 goto not_found;
5201 }
5202
Chris Mason5f39d392007-10-15 16:14:19 -04005203 found_type = btrfs_file_extent_type(leaf, item);
5204 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005205 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005206 if (found_type == BTRFS_FILE_EXTENT_REG ||
5207 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005208 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005209 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005210 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5211 size_t size;
5212 size = btrfs_file_extent_inline_len(leaf, item);
5213 extent_end = (extent_start + size + root->sectorsize - 1) &
5214 ~((u64)root->sectorsize - 1);
5215 }
5216
5217 if (start >= extent_end) {
5218 path->slots[0]++;
5219 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5220 ret = btrfs_next_leaf(root, path);
5221 if (ret < 0) {
5222 err = ret;
5223 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005224 }
Yan Zheng9036c102008-10-30 14:19:41 -04005225 if (ret > 0)
5226 goto not_found;
5227 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005228 }
Yan Zheng9036c102008-10-30 14:19:41 -04005229 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5230 if (found_key.objectid != objectid ||
5231 found_key.type != BTRFS_EXTENT_DATA_KEY)
5232 goto not_found;
5233 if (start + len <= found_key.offset)
5234 goto not_found;
5235 em->start = start;
5236 em->len = found_key.offset - start;
5237 goto not_found_em;
5238 }
5239
Yan Zhengd899e052008-10-30 14:25:28 -04005240 if (found_type == BTRFS_FILE_EXTENT_REG ||
5241 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005242 em->start = extent_start;
5243 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005244 em->orig_start = extent_start -
5245 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005246 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5247 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005248 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005249 goto insert;
5250 }
Li Zefan261507a02010-12-17 14:21:50 +08005251 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005252 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005253 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005254 em->block_start = bytenr;
5255 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5256 item);
5257 } else {
5258 bytenr += btrfs_file_extent_offset(leaf, item);
5259 em->block_start = bytenr;
5260 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005261 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5262 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005263 }
Chris Masona52d9a82007-08-27 16:49:44 -04005264 goto insert;
5265 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005266 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005267 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005268 size_t size;
5269 size_t extent_offset;
5270 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005271
Chris Masona52d9a82007-08-27 16:49:44 -04005272 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005273 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005274 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005275 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005276 goto out;
5277 }
5278
Yan Zheng9036c102008-10-30 14:19:41 -04005279 size = btrfs_file_extent_inline_len(leaf, item);
5280 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005281 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005282 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005283 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005284 em->len = (copy_size + root->sectorsize - 1) &
5285 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005286 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005287 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005288 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005289 em->compress_type = compress_type;
5290 }
Yan689f9342007-10-29 11:41:07 -04005291 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005292 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005293 if (btrfs_file_extent_compression(leaf, item) !=
5294 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005295 ret = uncompress_inline(path, inode, page,
5296 pg_offset,
5297 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005298 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005299 } else {
5300 map = kmap(page);
5301 read_extent_buffer(leaf, map + pg_offset, ptr,
5302 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005303 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5304 memset(map + pg_offset + copy_size, 0,
5305 PAGE_CACHE_SIZE - pg_offset -
5306 copy_size);
5307 }
Chris Masonc8b97812008-10-29 14:49:59 -04005308 kunmap(page);
5309 }
Chris Mason179e29e2007-11-01 11:28:41 -04005310 flush_dcache_page(page);
5311 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005312 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005313 if (!trans) {
5314 kunmap(page);
5315 free_extent_map(em);
5316 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005317
David Sterbab3b4aa72011-04-21 01:20:15 +02005318 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005319 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005320
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005321 if (IS_ERR(trans))
5322 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005323 goto again;
5324 }
Chris Masonc8b97812008-10-29 14:49:59 -04005325 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005326 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005327 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005328 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005329 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005330 }
Chris Masond1310b22008-01-24 16:13:08 -05005331 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005332 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005333 goto insert;
5334 } else {
Chris Masond3977122009-01-05 21:25:51 -05005335 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005336 WARN_ON(1);
5337 }
5338not_found:
5339 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005340 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005341not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005342 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005343 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005344insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005345 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005346 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005347 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5348 "[%llu %llu]\n", (unsigned long long)em->start,
5349 (unsigned long long)em->len,
5350 (unsigned long long)start,
5351 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005352 err = -EIO;
5353 goto out;
5354 }
Chris Masond1310b22008-01-24 16:13:08 -05005355
5356 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005357 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005358 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005359 /* it is possible that someone inserted the extent into the tree
5360 * while we had the lock dropped. It is also possible that
5361 * an overlapping map exists in the tree
5362 */
Chris Masona52d9a82007-08-27 16:49:44 -04005363 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005364 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005365
5366 ret = 0;
5367
Chris Mason3b951512008-04-17 11:29:12 -04005368 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005369 if (existing && (existing->start > start ||
5370 existing->start + existing->len <= start)) {
5371 free_extent_map(existing);
5372 existing = NULL;
5373 }
Chris Mason3b951512008-04-17 11:29:12 -04005374 if (!existing) {
5375 existing = lookup_extent_mapping(em_tree, em->start,
5376 em->len);
5377 if (existing) {
5378 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005379 em, start,
5380 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005381 free_extent_map(existing);
5382 if (err) {
5383 free_extent_map(em);
5384 em = NULL;
5385 }
5386 } else {
5387 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005388 free_extent_map(em);
5389 em = NULL;
5390 }
5391 } else {
5392 free_extent_map(em);
5393 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005394 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005395 }
Chris Masona52d9a82007-08-27 16:49:44 -04005396 }
Chris Mason890871b2009-09-02 16:24:52 -04005397 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005398out:
liubo1abe9b82011-03-24 11:18:59 +00005399
5400 trace_btrfs_get_extent(root, em);
5401
Chris Masonf4219502008-07-22 11:18:09 -04005402 if (path)
5403 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005404 if (trans) {
5405 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005406 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005407 err = ret;
5408 }
Chris Masona52d9a82007-08-27 16:49:44 -04005409 if (err) {
5410 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005411 return ERR_PTR(err);
5412 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005413 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005414 return em;
5415}
5416
Chris Masonec29ed52011-02-23 16:23:20 -05005417struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5418 size_t pg_offset, u64 start, u64 len,
5419 int create)
5420{
5421 struct extent_map *em;
5422 struct extent_map *hole_em = NULL;
5423 u64 range_start = start;
5424 u64 end;
5425 u64 found;
5426 u64 found_end;
5427 int err = 0;
5428
5429 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5430 if (IS_ERR(em))
5431 return em;
5432 if (em) {
5433 /*
5434 * if our em maps to a hole, there might
5435 * actually be delalloc bytes behind it
5436 */
5437 if (em->block_start != EXTENT_MAP_HOLE)
5438 return em;
5439 else
5440 hole_em = em;
5441 }
5442
5443 /* check to see if we've wrapped (len == -1 or similar) */
5444 end = start + len;
5445 if (end < start)
5446 end = (u64)-1;
5447 else
5448 end -= 1;
5449
5450 em = NULL;
5451
5452 /* ok, we didn't find anything, lets look for delalloc */
5453 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5454 end, len, EXTENT_DELALLOC, 1);
5455 found_end = range_start + found;
5456 if (found_end < range_start)
5457 found_end = (u64)-1;
5458
5459 /*
5460 * we didn't find anything useful, return
5461 * the original results from get_extent()
5462 */
5463 if (range_start > end || found_end <= start) {
5464 em = hole_em;
5465 hole_em = NULL;
5466 goto out;
5467 }
5468
5469 /* adjust the range_start to make sure it doesn't
5470 * go backwards from the start they passed in
5471 */
5472 range_start = max(start,range_start);
5473 found = found_end - range_start;
5474
5475 if (found > 0) {
5476 u64 hole_start = start;
5477 u64 hole_len = len;
5478
David Sterba172ddd62011-04-21 00:48:27 +02005479 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005480 if (!em) {
5481 err = -ENOMEM;
5482 goto out;
5483 }
5484 /*
5485 * when btrfs_get_extent can't find anything it
5486 * returns one huge hole
5487 *
5488 * make sure what it found really fits our range, and
5489 * adjust to make sure it is based on the start from
5490 * the caller
5491 */
5492 if (hole_em) {
5493 u64 calc_end = extent_map_end(hole_em);
5494
5495 if (calc_end <= start || (hole_em->start > end)) {
5496 free_extent_map(hole_em);
5497 hole_em = NULL;
5498 } else {
5499 hole_start = max(hole_em->start, start);
5500 hole_len = calc_end - hole_start;
5501 }
5502 }
5503 em->bdev = NULL;
5504 if (hole_em && range_start > hole_start) {
5505 /* our hole starts before our delalloc, so we
5506 * have to return just the parts of the hole
5507 * that go until the delalloc starts
5508 */
5509 em->len = min(hole_len,
5510 range_start - hole_start);
5511 em->start = hole_start;
5512 em->orig_start = hole_start;
5513 /*
5514 * don't adjust block start at all,
5515 * it is fixed at EXTENT_MAP_HOLE
5516 */
5517 em->block_start = hole_em->block_start;
5518 em->block_len = hole_len;
5519 } else {
5520 em->start = range_start;
5521 em->len = found;
5522 em->orig_start = range_start;
5523 em->block_start = EXTENT_MAP_DELALLOC;
5524 em->block_len = found;
5525 }
5526 } else if (hole_em) {
5527 return hole_em;
5528 }
5529out:
5530
5531 free_extent_map(hole_em);
5532 if (err) {
5533 free_extent_map(em);
5534 return ERR_PTR(err);
5535 }
5536 return em;
5537}
5538
Josef Bacik4b46fce2010-05-23 11:00:55 -04005539static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005540 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005541 u64 start, u64 len)
5542{
5543 struct btrfs_root *root = BTRFS_I(inode)->root;
5544 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005545 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5546 struct btrfs_key ins;
5547 u64 alloc_hint;
5548 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005549 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005550
Josef Bacik16d299a2011-04-06 14:53:07 -04005551 /*
5552 * Ok if the extent map we looked up is a hole and is for the exact
5553 * range we want, there is no reason to allocate a new one, however if
5554 * it is not right then we need to free this one and drop the cache for
5555 * our range.
5556 */
5557 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5558 em->len != len) {
5559 free_extent_map(em);
5560 em = NULL;
5561 insert = true;
5562 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5563 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005564
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005565 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005566 if (IS_ERR(trans))
5567 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005568
Chris Mason4cb53002011-05-24 15:35:30 -04005569 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5570 btrfs_add_inode_defrag(trans, inode);
5571
Josef Bacik4b46fce2010-05-23 11:00:55 -04005572 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5573
5574 alloc_hint = get_extent_allocation_hint(inode, start, len);
5575 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005576 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005577 if (ret) {
5578 em = ERR_PTR(ret);
5579 goto out;
5580 }
5581
Josef Bacik4b46fce2010-05-23 11:00:55 -04005582 if (!em) {
David Sterba172ddd62011-04-21 00:48:27 +02005583 em = alloc_extent_map();
Josef Bacik16d299a2011-04-06 14:53:07 -04005584 if (!em) {
5585 em = ERR_PTR(-ENOMEM);
5586 goto out;
5587 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005588 }
5589
5590 em->start = start;
5591 em->orig_start = em->start;
5592 em->len = ins.offset;
5593
5594 em->block_start = ins.objectid;
5595 em->block_len = ins.offset;
5596 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005597
5598 /*
5599 * We need to do this because if we're using the original em we searched
5600 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5601 */
5602 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005603 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5604
Josef Bacik16d299a2011-04-06 14:53:07 -04005605 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005606 write_lock(&em_tree->lock);
5607 ret = add_extent_mapping(em_tree, em);
5608 write_unlock(&em_tree->lock);
5609 if (ret != -EEXIST)
5610 break;
5611 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5612 }
5613
5614 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5615 ins.offset, ins.offset, 0);
5616 if (ret) {
5617 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5618 em = ERR_PTR(ret);
5619 }
5620out:
5621 btrfs_end_transaction(trans, root);
5622 return em;
5623}
5624
Chris Mason46bfbb52010-05-26 11:04:10 -04005625/*
5626 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5627 * block must be cow'd
5628 */
5629static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5630 struct inode *inode, u64 offset, u64 len)
5631{
5632 struct btrfs_path *path;
5633 int ret;
5634 struct extent_buffer *leaf;
5635 struct btrfs_root *root = BTRFS_I(inode)->root;
5636 struct btrfs_file_extent_item *fi;
5637 struct btrfs_key key;
5638 u64 disk_bytenr;
5639 u64 backref_offset;
5640 u64 extent_end;
5641 u64 num_bytes;
5642 int slot;
5643 int found_type;
5644
5645 path = btrfs_alloc_path();
5646 if (!path)
5647 return -ENOMEM;
5648
Li Zefan33345d012011-04-20 10:31:50 +08005649 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005650 offset, 0);
5651 if (ret < 0)
5652 goto out;
5653
5654 slot = path->slots[0];
5655 if (ret == 1) {
5656 if (slot == 0) {
5657 /* can't find the item, must cow */
5658 ret = 0;
5659 goto out;
5660 }
5661 slot--;
5662 }
5663 ret = 0;
5664 leaf = path->nodes[0];
5665 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005666 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005667 key.type != BTRFS_EXTENT_DATA_KEY) {
5668 /* not our file or wrong item type, must cow */
5669 goto out;
5670 }
5671
5672 if (key.offset > offset) {
5673 /* Wrong offset, must cow */
5674 goto out;
5675 }
5676
5677 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5678 found_type = btrfs_file_extent_type(leaf, fi);
5679 if (found_type != BTRFS_FILE_EXTENT_REG &&
5680 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5681 /* not a regular extent, must cow */
5682 goto out;
5683 }
5684 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5685 backref_offset = btrfs_file_extent_offset(leaf, fi);
5686
5687 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5688 if (extent_end < offset + len) {
5689 /* extent doesn't include our full range, must cow */
5690 goto out;
5691 }
5692
5693 if (btrfs_extent_readonly(root, disk_bytenr))
5694 goto out;
5695
5696 /*
5697 * look for other files referencing this extent, if we
5698 * find any we must cow
5699 */
Li Zefan33345d012011-04-20 10:31:50 +08005700 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005701 key.offset - backref_offset, disk_bytenr))
5702 goto out;
5703
5704 /*
5705 * adjust disk_bytenr and num_bytes to cover just the bytes
5706 * in this extent we are about to write. If there
5707 * are any csums in that range we have to cow in order
5708 * to keep the csums correct
5709 */
5710 disk_bytenr += backref_offset;
5711 disk_bytenr += offset - key.offset;
5712 num_bytes = min(offset + len, extent_end) - offset;
5713 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5714 goto out;
5715 /*
5716 * all of the above have passed, it is safe to overwrite this extent
5717 * without cow
5718 */
5719 ret = 1;
5720out:
5721 btrfs_free_path(path);
5722 return ret;
5723}
5724
Josef Bacik4b46fce2010-05-23 11:00:55 -04005725static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5726 struct buffer_head *bh_result, int create)
5727{
5728 struct extent_map *em;
5729 struct btrfs_root *root = BTRFS_I(inode)->root;
5730 u64 start = iblock << inode->i_blkbits;
5731 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005732 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005733
5734 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5735 if (IS_ERR(em))
5736 return PTR_ERR(em);
5737
5738 /*
5739 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5740 * io. INLINE is special, and we could probably kludge it in here, but
5741 * it's still buffered so for safety lets just fall back to the generic
5742 * buffered path.
5743 *
5744 * For COMPRESSED we _have_ to read the entire extent in so we can
5745 * decompress it, so there will be buffering required no matter what we
5746 * do, so go ahead and fallback to buffered.
5747 *
5748 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5749 * to buffered IO. Don't blame me, this is the price we pay for using
5750 * the generic code.
5751 */
5752 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5753 em->block_start == EXTENT_MAP_INLINE) {
5754 free_extent_map(em);
5755 return -ENOTBLK;
5756 }
5757
5758 /* Just a good old fashioned hole, return */
5759 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5760 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5761 free_extent_map(em);
5762 /* DIO will do one hole at a time, so just unlock a sector */
5763 unlock_extent(&BTRFS_I(inode)->io_tree, start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005764 start + root->sectorsize - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005765 return 0;
5766 }
5767
5768 /*
5769 * We don't allocate a new extent in the following cases
5770 *
5771 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5772 * existing extent.
5773 * 2) The extent is marked as PREALLOC. We're good to go here and can
5774 * just use the extent.
5775 *
5776 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005777 if (!create) {
5778 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005779 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005780 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005781
5782 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5783 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5784 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005785 int type;
5786 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005787 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005788
5789 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5790 type = BTRFS_ORDERED_PREALLOC;
5791 else
5792 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005793 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005794 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005795
5796 /*
5797 * we're not going to log anything, but we do need
5798 * to make sure the current transaction stays open
5799 * while we look for nocow cross refs
5800 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005801 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005802 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005803 goto must_cow;
5804
5805 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5806 ret = btrfs_add_ordered_extent_dio(inode, start,
5807 block_start, len, len, type);
5808 btrfs_end_transaction(trans, root);
5809 if (ret) {
5810 free_extent_map(em);
5811 return ret;
5812 }
5813 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005814 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005815 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005816 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005817must_cow:
5818 /*
5819 * this will cow the extent, reset the len in case we changed
5820 * it above
5821 */
5822 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04005823 em = btrfs_new_extent_direct(inode, em, start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04005824 if (IS_ERR(em))
5825 return PTR_ERR(em);
5826 len = min(len, em->len - (start - em->start));
5827unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005828 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5829 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5830 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005831map:
5832 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5833 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005834 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005835 bh_result->b_bdev = em->bdev;
5836 set_buffer_mapped(bh_result);
5837 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5838 set_buffer_new(bh_result);
5839
5840 free_extent_map(em);
5841
5842 return 0;
5843}
5844
5845struct btrfs_dio_private {
5846 struct inode *inode;
5847 u64 logical_offset;
5848 u64 disk_bytenr;
5849 u64 bytes;
5850 u32 *csums;
5851 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005852
5853 /* number of bios pending for this dio */
5854 atomic_t pending_bios;
5855
5856 /* IO errors */
5857 int errors;
5858
5859 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005860};
5861
5862static void btrfs_endio_direct_read(struct bio *bio, int err)
5863{
Miao Xiee65e1532010-11-22 03:04:43 +00005864 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005865 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5866 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005867 struct inode *inode = dip->inode;
5868 struct btrfs_root *root = BTRFS_I(inode)->root;
5869 u64 start;
5870 u32 *private = dip->csums;
5871
5872 start = dip->logical_offset;
5873 do {
5874 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5875 struct page *page = bvec->bv_page;
5876 char *kaddr;
5877 u32 csum = ~(u32)0;
5878 unsigned long flags;
5879
5880 local_irq_save(flags);
5881 kaddr = kmap_atomic(page, KM_IRQ0);
5882 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5883 csum, bvec->bv_len);
5884 btrfs_csum_final(csum, (char *)&csum);
5885 kunmap_atomic(kaddr, KM_IRQ0);
5886 local_irq_restore(flags);
5887
5888 flush_dcache_page(bvec->bv_page);
5889 if (csum != *private) {
Li Zefan33345d012011-04-20 10:31:50 +08005890 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04005891 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08005892 (unsigned long long)btrfs_ino(inode),
5893 (unsigned long long)start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005894 csum, *private);
5895 err = -EIO;
5896 }
5897 }
5898
5899 start += bvec->bv_len;
5900 private++;
5901 bvec++;
5902 } while (bvec <= bvec_end);
5903
5904 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005905 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005906 bio->bi_private = dip->private;
5907
5908 kfree(dip->csums);
5909 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005910
5911 /* If we had a csum failure make sure to clear the uptodate flag */
5912 if (err)
5913 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005914 dio_end_io(bio, err);
5915}
5916
5917static void btrfs_endio_direct_write(struct bio *bio, int err)
5918{
5919 struct btrfs_dio_private *dip = bio->bi_private;
5920 struct inode *inode = dip->inode;
5921 struct btrfs_root *root = BTRFS_I(inode)->root;
5922 struct btrfs_trans_handle *trans;
5923 struct btrfs_ordered_extent *ordered = NULL;
5924 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005925 u64 ordered_offset = dip->logical_offset;
5926 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005927 int ret;
5928
5929 if (err)
5930 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005931again:
5932 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5933 &ordered_offset,
5934 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005935 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005936 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005937
5938 BUG_ON(!ordered);
5939
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005940 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005941 if (IS_ERR(trans)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005942 err = -ENOMEM;
5943 goto out;
5944 }
5945 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5946
5947 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5948 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5949 if (!ret)
Chris Mason21151332011-11-10 20:39:08 -05005950 err = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005951 goto out;
5952 }
5953
5954 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5955 ordered->file_offset + ordered->len - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005956 &cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005957
5958 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5959 ret = btrfs_mark_extent_written(trans, inode,
5960 ordered->file_offset,
5961 ordered->file_offset +
5962 ordered->len);
5963 if (ret) {
5964 err = ret;
5965 goto out_unlock;
5966 }
5967 } else {
5968 ret = insert_reserved_file_extent(trans, inode,
5969 ordered->file_offset,
5970 ordered->start,
5971 ordered->disk_len,
5972 ordered->len,
5973 ordered->len,
5974 0, 0, 0,
5975 BTRFS_FILE_EXTENT_REG);
5976 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5977 ordered->file_offset, ordered->len);
5978 if (ret) {
5979 err = ret;
5980 WARN_ON(1);
5981 goto out_unlock;
5982 }
5983 }
5984
5985 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005986 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
Miao Xiea39f7522011-09-11 10:52:25 -04005987 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags))
Chris Mason21151332011-11-10 20:39:08 -05005988 btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005989 ret = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005990out_unlock:
5991 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5992 ordered->file_offset + ordered->len - 1,
5993 &cached_state, GFP_NOFS);
5994out:
5995 btrfs_delalloc_release_metadata(inode, ordered->len);
5996 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005997 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005998 btrfs_put_ordered_extent(ordered);
5999 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05006000
6001out_test:
6002 /*
6003 * our bio might span multiple ordered extents. If we haven't
6004 * completed the accounting for the whole dio, go back and try again
6005 */
6006 if (ordered_offset < dip->logical_offset + dip->bytes) {
6007 ordered_bytes = dip->logical_offset + dip->bytes -
6008 ordered_offset;
6009 goto again;
6010 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006011out_done:
6012 bio->bi_private = dip->private;
6013
6014 kfree(dip->csums);
6015 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006016
6017 /* If we had an error make sure to clear the uptodate flag */
6018 if (err)
6019 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006020 dio_end_io(bio, err);
6021}
6022
Chris Masoneaf25d92010-05-25 09:48:28 -04006023static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6024 struct bio *bio, int mirror_num,
6025 unsigned long bio_flags, u64 offset)
6026{
6027 int ret;
6028 struct btrfs_root *root = BTRFS_I(inode)->root;
6029 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006030 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006031 return 0;
6032}
6033
Miao Xiee65e1532010-11-22 03:04:43 +00006034static void btrfs_end_dio_bio(struct bio *bio, int err)
6035{
6036 struct btrfs_dio_private *dip = bio->bi_private;
6037
6038 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006039 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006040 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006041 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006042 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006043 dip->errors = 1;
6044
6045 /*
6046 * before atomic variable goto zero, we must make sure
6047 * dip->errors is perceived to be set.
6048 */
6049 smp_mb__before_atomic_dec();
6050 }
6051
6052 /* if there are more bios still pending for this dio, just exit */
6053 if (!atomic_dec_and_test(&dip->pending_bios))
6054 goto out;
6055
6056 if (dip->errors)
6057 bio_io_error(dip->orig_bio);
6058 else {
6059 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6060 bio_endio(dip->orig_bio, 0);
6061 }
6062out:
6063 bio_put(bio);
6064}
6065
6066static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6067 u64 first_sector, gfp_t gfp_flags)
6068{
6069 int nr_vecs = bio_get_nr_vecs(bdev);
6070 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6071}
6072
6073static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6074 int rw, u64 file_offset, int skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006075 u32 *csums, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006076{
6077 int write = rw & REQ_WRITE;
6078 struct btrfs_root *root = BTRFS_I(inode)->root;
6079 int ret;
6080
6081 bio_get(bio);
6082 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6083 if (ret)
6084 goto err;
6085
Josef Bacik1ae39932011-04-06 14:41:34 -04006086 if (skip_sum)
6087 goto map;
6088
6089 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006090 ret = btrfs_wq_submit_bio(root->fs_info,
6091 inode, rw, bio, 0, 0,
6092 file_offset,
6093 __btrfs_submit_bio_start_direct_io,
6094 __btrfs_submit_bio_done);
6095 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006096 } else if (write) {
6097 /*
6098 * If we aren't doing async submit, calculate the csum of the
6099 * bio now.
6100 */
6101 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6102 if (ret)
6103 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006104 } else if (!skip_sum) {
6105 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
Miao Xiee65e1532010-11-22 03:04:43 +00006106 file_offset, csums);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006107 if (ret)
6108 goto err;
6109 }
Miao Xiee65e1532010-11-22 03:04:43 +00006110
Josef Bacik1ae39932011-04-06 14:41:34 -04006111map:
6112 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006113err:
6114 bio_put(bio);
6115 return ret;
6116}
6117
6118static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6119 int skip_sum)
6120{
6121 struct inode *inode = dip->inode;
6122 struct btrfs_root *root = BTRFS_I(inode)->root;
6123 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6124 struct bio *bio;
6125 struct bio *orig_bio = dip->orig_bio;
6126 struct bio_vec *bvec = orig_bio->bi_io_vec;
6127 u64 start_sector = orig_bio->bi_sector;
6128 u64 file_offset = dip->logical_offset;
6129 u64 submit_len = 0;
6130 u64 map_length;
6131 int nr_pages = 0;
6132 u32 *csums = dip->csums;
6133 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006134 int async_submit = 0;
Josef Bacik98bc3142011-03-22 11:00:46 -04006135 int write = rw & REQ_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00006136
Miao Xiee65e1532010-11-22 03:04:43 +00006137 map_length = orig_bio->bi_size;
6138 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6139 &map_length, NULL, 0);
6140 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006141 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006142 return -EIO;
6143 }
6144
Josef Bacik02f57c72011-04-06 14:25:44 -04006145 if (map_length >= orig_bio->bi_size) {
6146 bio = orig_bio;
6147 goto submit;
6148 }
6149
Josef Bacik1ae39932011-04-06 14:41:34 -04006150 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006151 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6152 if (!bio)
6153 return -ENOMEM;
6154 bio->bi_private = dip;
6155 bio->bi_end_io = btrfs_end_dio_bio;
6156 atomic_inc(&dip->pending_bios);
6157
Miao Xiee65e1532010-11-22 03:04:43 +00006158 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6159 if (unlikely(map_length < submit_len + bvec->bv_len ||
6160 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6161 bvec->bv_offset) < bvec->bv_len)) {
6162 /*
6163 * inc the count before we submit the bio so
6164 * we know the end IO handler won't happen before
6165 * we inc the count. Otherwise, the dip might get freed
6166 * before we're done setting it up
6167 */
6168 atomic_inc(&dip->pending_bios);
6169 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6170 file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006171 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006172 if (ret) {
6173 bio_put(bio);
6174 atomic_dec(&dip->pending_bios);
6175 goto out_err;
6176 }
6177
Josef Bacik98bc3142011-03-22 11:00:46 -04006178 /* Write's use the ordered csums */
6179 if (!write && !skip_sum)
Miao Xiee65e1532010-11-22 03:04:43 +00006180 csums = csums + nr_pages;
6181 start_sector += submit_len >> 9;
6182 file_offset += submit_len;
6183
6184 submit_len = 0;
6185 nr_pages = 0;
6186
6187 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6188 start_sector, GFP_NOFS);
6189 if (!bio)
6190 goto out_err;
6191 bio->bi_private = dip;
6192 bio->bi_end_io = btrfs_end_dio_bio;
6193
6194 map_length = orig_bio->bi_size;
6195 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6196 &map_length, NULL, 0);
6197 if (ret) {
6198 bio_put(bio);
6199 goto out_err;
6200 }
6201 } else {
6202 submit_len += bvec->bv_len;
6203 nr_pages ++;
6204 bvec++;
6205 }
6206 }
6207
Josef Bacik02f57c72011-04-06 14:25:44 -04006208submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006209 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006210 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006211 if (!ret)
6212 return 0;
6213
6214 bio_put(bio);
6215out_err:
6216 dip->errors = 1;
6217 /*
6218 * before atomic variable goto zero, we must
6219 * make sure dip->errors is perceived to be set.
6220 */
6221 smp_mb__before_atomic_dec();
6222 if (atomic_dec_and_test(&dip->pending_bios))
6223 bio_io_error(dip->orig_bio);
6224
6225 /* bio_end_io() will handle error, so we needn't return it */
6226 return 0;
6227}
6228
Josef Bacik4b46fce2010-05-23 11:00:55 -04006229static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6230 loff_t file_offset)
6231{
6232 struct btrfs_root *root = BTRFS_I(inode)->root;
6233 struct btrfs_dio_private *dip;
6234 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006235 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006236 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006237 int ret = 0;
6238
6239 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6240
6241 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6242 if (!dip) {
6243 ret = -ENOMEM;
6244 goto free_ordered;
6245 }
6246 dip->csums = NULL;
6247
Josef Bacik98bc3142011-03-22 11:00:46 -04006248 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6249 if (!write && !skip_sum) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006250 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6251 if (!dip->csums) {
Daniel J Bluemanb4966b72011-03-09 16:46:42 +00006252 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006253 ret = -ENOMEM;
6254 goto free_ordered;
6255 }
6256 }
6257
6258 dip->private = bio->bi_private;
6259 dip->inode = inode;
6260 dip->logical_offset = file_offset;
6261
Josef Bacik4b46fce2010-05-23 11:00:55 -04006262 dip->bytes = 0;
6263 do {
6264 dip->bytes += bvec->bv_len;
6265 bvec++;
6266 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6267
Chris Mason46bfbb52010-05-26 11:04:10 -04006268 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006269 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006270 dip->errors = 0;
6271 dip->orig_bio = bio;
6272 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006273
6274 if (write)
6275 bio->bi_end_io = btrfs_endio_direct_write;
6276 else
6277 bio->bi_end_io = btrfs_endio_direct_read;
6278
Miao Xiee65e1532010-11-22 03:04:43 +00006279 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6280 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006281 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006282free_ordered:
6283 /*
6284 * If this is a write, we need to clean up the reserved space and kill
6285 * the ordered extent.
6286 */
6287 if (write) {
6288 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006289 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006290 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6291 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6292 btrfs_free_reserved_extent(root, ordered->start,
6293 ordered->disk_len);
6294 btrfs_put_ordered_extent(ordered);
6295 btrfs_put_ordered_extent(ordered);
6296 }
6297 bio_endio(bio, ret);
6298}
6299
Chris Mason5a5f79b2010-05-26 21:33:37 -04006300static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6301 const struct iovec *iov, loff_t offset,
6302 unsigned long nr_segs)
6303{
6304 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006305 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006306 size_t size;
6307 unsigned long addr;
6308 unsigned blocksize_mask = root->sectorsize - 1;
6309 ssize_t retval = -EINVAL;
6310 loff_t end = offset;
6311
6312 if (offset & blocksize_mask)
6313 goto out;
6314
6315 /* Check the memory alignment. Blocks cannot straddle pages */
6316 for (seg = 0; seg < nr_segs; seg++) {
6317 addr = (unsigned long)iov[seg].iov_base;
6318 size = iov[seg].iov_len;
6319 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006320 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006321 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006322
6323 /* If this is a write we don't need to check anymore */
6324 if (rw & WRITE)
6325 continue;
6326
6327 /*
6328 * Check to make sure we don't have duplicate iov_base's in this
6329 * iovec, if so return EINVAL, otherwise we'll get csum errors
6330 * when reading back.
6331 */
6332 for (i = seg + 1; i < nr_segs; i++) {
6333 if (iov[seg].iov_base == iov[i].iov_base)
6334 goto out;
6335 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006336 }
6337 retval = 0;
6338out:
6339 return retval;
6340}
Chris Mason16432982008-04-10 10:23:21 -04006341static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6342 const struct iovec *iov, loff_t offset,
6343 unsigned long nr_segs)
6344{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006345 struct file *file = iocb->ki_filp;
6346 struct inode *inode = file->f_mapping->host;
6347 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006348 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006349 u64 lockstart, lockend;
6350 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006351 int writing = rw & WRITE;
6352 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006353 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006354
Chris Mason5a5f79b2010-05-26 21:33:37 -04006355 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6356 offset, nr_segs)) {
6357 return 0;
6358 }
6359
Josef Bacik4b46fce2010-05-23 11:00:55 -04006360 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006361 lockend = offset + count - 1;
6362
6363 if (writing) {
6364 ret = btrfs_delalloc_reserve_space(inode, count);
6365 if (ret)
6366 goto out;
6367 }
Chris Mason4845e442010-05-25 20:56:50 -04006368
Josef Bacik4b46fce2010-05-23 11:00:55 -04006369 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006370 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006371 0, &cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006372 /*
6373 * We're concerned with the entire range that we're going to be
6374 * doing DIO to, so we need to make sure theres no ordered
6375 * extents in this range.
6376 */
6377 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6378 lockend - lockstart + 1);
6379 if (!ordered)
6380 break;
Chris Mason4845e442010-05-25 20:56:50 -04006381 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6382 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006383 btrfs_start_ordered_extent(inode, ordered, 1);
6384 btrfs_put_ordered_extent(ordered);
6385 cond_resched();
6386 }
6387
Chris Mason4845e442010-05-25 20:56:50 -04006388 /*
6389 * we don't use btrfs_set_extent_delalloc because we don't want
6390 * the dirty or uptodate bits
6391 */
6392 if (writing) {
6393 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6394 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01006395 EXTENT_DELALLOC, NULL, &cached_state,
Chris Mason4845e442010-05-25 20:56:50 -04006396 GFP_NOFS);
6397 if (ret) {
6398 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6399 lockend, EXTENT_LOCKED | write_bits,
6400 1, 0, &cached_state, GFP_NOFS);
6401 goto out;
6402 }
6403 }
6404
6405 free_extent_state(cached_state);
6406 cached_state = NULL;
6407
Chris Mason5a5f79b2010-05-26 21:33:37 -04006408 ret = __blockdev_direct_IO(rw, iocb, inode,
6409 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6410 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6411 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006412
6413 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006414 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6415 offset + iov_length(iov, nr_segs) - 1,
6416 EXTENT_LOCKED | write_bits, 1, 0,
6417 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006418 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6419 /*
6420 * We're falling back to buffered, unlock the section we didn't
6421 * do IO on.
6422 */
Chris Mason4845e442010-05-25 20:56:50 -04006423 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6424 offset + iov_length(iov, nr_segs) - 1,
6425 EXTENT_LOCKED | write_bits, 1, 0,
6426 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006427 }
Chris Mason4845e442010-05-25 20:56:50 -04006428out:
6429 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006430 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006431}
6432
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006433static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6434 __u64 start, __u64 len)
6435{
Chris Masonec29ed52011-02-23 16:23:20 -05006436 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006437}
6438
Chris Mason9ebefb182007-06-15 13:50:00 -04006439int btrfs_readpage(struct file *file, struct page *page)
6440{
Chris Masond1310b22008-01-24 16:13:08 -05006441 struct extent_io_tree *tree;
6442 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006443 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006444}
Chris Mason1832a6d2007-12-21 16:27:21 -05006445
Chris Mason39279cc2007-06-12 06:35:45 -04006446static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6447{
Chris Masond1310b22008-01-24 16:13:08 -05006448 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006449
6450
6451 if (current->flags & PF_MEMALLOC) {
6452 redirty_page_for_writepage(wbc, page);
6453 unlock_page(page);
6454 return 0;
6455 }
Chris Masond1310b22008-01-24 16:13:08 -05006456 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006457 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6458}
Chris Mason39279cc2007-06-12 06:35:45 -04006459
Chris Masonf4219502008-07-22 11:18:09 -04006460int btrfs_writepages(struct address_space *mapping,
6461 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006462{
Chris Masond1310b22008-01-24 16:13:08 -05006463 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006464
Chris Masond1310b22008-01-24 16:13:08 -05006465 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006466 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6467}
6468
Chris Mason3ab2fb52007-11-08 10:59:22 -05006469static int
6470btrfs_readpages(struct file *file, struct address_space *mapping,
6471 struct list_head *pages, unsigned nr_pages)
6472{
Chris Masond1310b22008-01-24 16:13:08 -05006473 struct extent_io_tree *tree;
6474 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006475 return extent_readpages(tree, mapping, pages, nr_pages,
6476 btrfs_get_extent);
6477}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006478static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006479{
Chris Masond1310b22008-01-24 16:13:08 -05006480 struct extent_io_tree *tree;
6481 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006482 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006483
Chris Masond1310b22008-01-24 16:13:08 -05006484 tree = &BTRFS_I(page->mapping->host)->io_tree;
6485 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006486 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006487 if (ret == 1) {
6488 ClearPagePrivate(page);
6489 set_page_private(page, 0);
6490 page_cache_release(page);
6491 }
6492 return ret;
6493}
Chris Mason39279cc2007-06-12 06:35:45 -04006494
Chris Masone6dcd2d2008-07-17 12:53:50 -04006495static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6496{
Chris Mason98509cf2008-09-11 15:51:43 -04006497 if (PageWriteback(page) || PageDirty(page))
6498 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006499 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006500}
6501
Chris Masona52d9a82007-08-27 16:49:44 -04006502static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6503{
Chris Masond1310b22008-01-24 16:13:08 -05006504 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006505 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006506 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006507 u64 page_start = page_offset(page);
6508 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006509
Chris Mason8b62b722009-09-02 16:53:46 -04006510
6511 /*
6512 * we have the page locked, so new writeback can't start,
6513 * and the dirty bit won't be cleared while we are here.
6514 *
6515 * Wait for IO on this page so that we can safely clear
6516 * the PagePrivate2 bit and do ordered accounting
6517 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006518 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006519
Chris Masond1310b22008-01-24 16:13:08 -05006520 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006521 if (offset) {
6522 btrfs_releasepage(page, GFP_NOFS);
6523 return;
6524 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006525 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006526 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6527 page_offset(page));
6528 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006529 /*
6530 * IO on this page will never be started, so we need
6531 * to account for any ordered extents now
6532 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006533 clear_extent_bit(tree, page_start, page_end,
6534 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006535 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006536 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006537 /*
6538 * whoever cleared the private bit is responsible
6539 * for the finish_ordered_io
6540 */
6541 if (TestClearPagePrivate2(page)) {
6542 btrfs_finish_ordered_io(page->mapping->host,
6543 page_start, page_end);
6544 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006545 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006546 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006547 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006548 }
6549 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006550 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006551 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006552 __btrfs_releasepage(page, GFP_NOFS);
6553
Chris Mason4a096752008-07-21 10:29:44 -04006554 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006555 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006556 ClearPagePrivate(page);
6557 set_page_private(page, 0);
6558 page_cache_release(page);
6559 }
Chris Mason39279cc2007-06-12 06:35:45 -04006560}
6561
Chris Mason9ebefb182007-06-15 13:50:00 -04006562/*
6563 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6564 * called from a page fault handler when a page is first dirtied. Hence we must
6565 * be careful to check for EOF conditions here. We set the page up correctly
6566 * for a written page which means we get ENOSPC checking when writing into
6567 * holes and correct delalloc and unwritten extent mapping on filesystems that
6568 * support these features.
6569 *
6570 * We are not allowed to take the i_mutex here so we have to play games to
6571 * protect against truncate races as the page could now be beyond EOF. Because
6572 * vmtruncate() writes the inode size before removing pages, once we have the
6573 * page lock we can determine safely if the page is beyond EOF. If it is not
6574 * beyond EOF, then the page is guaranteed safe against truncation until we
6575 * unlock the page.
6576 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006577int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006578{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006579 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006580 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006581 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006582 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6583 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006584 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006585 char *kaddr;
6586 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006587 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006588 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006589 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006590 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006591 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006592
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006593 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006594 if (!ret) {
Josef Bacik22c44fe2011-11-30 10:45:38 -05006595 ret = btrfs_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006596 reserved = 1;
6597 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006598 if (ret) {
6599 if (ret == -ENOMEM)
6600 ret = VM_FAULT_OOM;
6601 else /* -ENOSPC, -EIO, etc */
6602 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006603 if (reserved)
6604 goto out;
6605 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006606 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006607
Nick Piggin56a76f82009-03-31 15:23:23 -07006608 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006609again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006610 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006611 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006612 page_start = page_offset(page);
6613 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006614
Chris Mason9ebefb182007-06-15 13:50:00 -04006615 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006616 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006617 /* page got truncated out from underneath us */
6618 goto out_unlock;
6619 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006620 wait_on_page_writeback(page);
6621
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006622 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006623 set_page_extent_mapped(page);
6624
Chris Masoneb84ae02008-07-17 13:53:27 -04006625 /*
6626 * we can't set the delalloc bits if there are pending ordered
6627 * extents. Drop our locks and wait for them to finish
6628 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006629 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6630 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006631 unlock_extent_cached(io_tree, page_start, page_end,
6632 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006633 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006634 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006635 btrfs_put_ordered_extent(ordered);
6636 goto again;
6637 }
6638
Josef Bacikfbf19082009-10-01 17:10:23 -04006639 /*
6640 * XXX - page_mkwrite gets called every time the page is dirtied, even
6641 * if it was already dirty, so for space accounting reasons we need to
6642 * clear any delalloc bits for the range we are fixing to save. There
6643 * is probably a better way to do this, but for now keep consistent with
6644 * prepare_pages in the normal write path.
6645 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006646 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006647 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006648 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006649
Josef Bacik2ac55d42010-02-03 19:33:23 +00006650 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6651 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006652 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006653 unlock_extent_cached(io_tree, page_start, page_end,
6654 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006655 ret = VM_FAULT_SIGBUS;
6656 goto out_unlock;
6657 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006658 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006659
6660 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006661 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006662 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006663 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006664 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006665
Chris Masone6dcd2d2008-07-17 12:53:50 -04006666 if (zero_start != PAGE_CACHE_SIZE) {
6667 kaddr = kmap(page);
6668 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6669 flush_dcache_page(page);
6670 kunmap(page);
6671 }
Chris Mason247e7432008-07-17 12:53:51 -04006672 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006673 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006674 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006675
Chris Mason257c62e2009-10-13 13:21:08 -04006676 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6677 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6678
Josef Bacik2ac55d42010-02-03 19:33:23 +00006679 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006680
6681out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006682 if (!ret)
6683 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006684 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006685out:
Josef Bacikec39e182012-01-12 19:10:12 -05006686 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006687out_noreserve:
Chris Mason9ebefb182007-06-15 13:50:00 -04006688 return ret;
6689}
6690
Josef Bacika41ad392011-01-31 15:30:16 -05006691static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006692{
6693 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006694 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006695 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006696 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006697 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006698 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006699 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006700 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006701
Josef Bacik5d5e1032009-10-13 16:46:49 -04006702 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6703 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006704 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006705
Chris Mason4a096752008-07-21 10:29:44 -04006706 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006707 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006708
Josef Bacikfcb80c22011-05-03 10:40:22 -04006709 /*
6710 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6711 * 3 things going on here
6712 *
6713 * 1) We need to reserve space for our orphan item and the space to
6714 * delete our orphan item. Lord knows we don't want to have a dangling
6715 * orphan item because we didn't reserve space to remove it.
6716 *
6717 * 2) We need to reserve space to update our inode.
6718 *
6719 * 3) We need to have something to cache all the space that is going to
6720 * be free'd up by the truncate operation, but also have some slack
6721 * space reserved in case it uses space during the truncate (thank you
6722 * very much snapshotting).
6723 *
6724 * And we need these to all be seperate. The fact is we can use alot of
6725 * space doing the truncate, and we have no earthly idea how much space
6726 * we will use, so we need the truncate reservation to be seperate so it
6727 * doesn't end up using space reserved for updating the inode or
6728 * removing the orphan item. We also need to be able to stop the
6729 * transaction and start a new one, which means we need to be able to
6730 * update the inode several times, and we have no idea of knowing how
6731 * many times that will be, so we can't just reserve 1 item for the
6732 * entirety of the opration, so that has to be done seperately as well.
6733 * Then there is the orphan item, which does indeed need to be held on
6734 * to for the whole operation, and we need nobody to touch this reserved
6735 * space except the orphan code.
6736 *
6737 * So that leaves us with
6738 *
6739 * 1) root->orphan_block_rsv - for the orphan deletion.
6740 * 2) rsv - for the truncate reservation, which we will steal from the
6741 * transaction reservation.
6742 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6743 * updating the inode.
6744 */
6745 rsv = btrfs_alloc_block_rsv(root);
6746 if (!rsv)
6747 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006748 rsv->size = min_size;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006749
Josef Bacik907cbce2011-08-08 13:46:15 -04006750 /*
Josef Bacik07127182011-08-19 10:29:59 -04006751 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006752 * 1 for the orphan item we're going to add
6753 * 1 for the orphan item deletion
6754 * 1 for updating the inode.
6755 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04006756 trans = btrfs_start_transaction(root, 4);
6757 if (IS_ERR(trans)) {
6758 err = PTR_ERR(trans);
6759 goto out;
6760 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006761
Josef Bacik907cbce2011-08-08 13:46:15 -04006762 /* Migrate the slack space for the truncate to our reserve */
6763 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6764 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006765 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006766
6767 ret = btrfs_orphan_add(trans, inode);
6768 if (ret) {
6769 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006770 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006771 }
6772
Chris Mason5a3f23d2009-03-31 13:27:11 -04006773 /*
6774 * setattr is responsible for setting the ordered_data_close flag,
6775 * but that is only tested during the last file release. That
6776 * could happen well after the next commit, leaving a great big
6777 * window where new writes may get lost if someone chooses to write
6778 * to this file after truncating to zero
6779 *
6780 * The inode doesn't have any dirty data here, and so if we commit
6781 * this is a noop. If someone immediately starts writing to the inode
6782 * it is very likely we'll catch some of their writes in this
6783 * transaction, and the commit will find this file on the ordered
6784 * data list with good things to send down.
6785 *
6786 * This is a best effort solution, there is still a window where
6787 * using truncate to replace the contents of the file will
6788 * end up with a zero length file after a crash.
6789 */
6790 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6791 btrfs_add_ordered_operation(trans, root, inode);
6792
Yan, Zheng80825102009-11-12 09:35:36 +00006793 while (1) {
Josef Bacik36ba0222011-10-18 12:15:48 -04006794 ret = btrfs_block_rsv_refill(root, rsv, min_size);
Josef Bacik907cbce2011-08-08 13:46:15 -04006795 if (ret) {
6796 /*
6797 * This can only happen with the original transaction we
6798 * started above, every other time we shouldn't have a
6799 * transaction started yet.
6800 */
6801 if (ret == -EAGAIN)
6802 goto end_trans;
6803 err = ret;
6804 break;
6805 }
6806
Yan, Zhengd68fc572010-05-16 10:49:58 -04006807 if (!trans) {
Josef Bacik907cbce2011-08-08 13:46:15 -04006808 /* Just need the 1 for updating the inode */
6809 trans = btrfs_start_transaction(root, 1);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006810 if (IS_ERR(trans)) {
Josef Bacik7041ee92011-12-09 13:26:22 -05006811 ret = err = PTR_ERR(trans);
6812 trans = NULL;
6813 break;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006814 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006815 }
6816
Josef Bacik907cbce2011-08-08 13:46:15 -04006817 trans->block_rsv = rsv;
6818
Yan, Zheng80825102009-11-12 09:35:36 +00006819 ret = btrfs_truncate_inode_items(trans, root, inode,
6820 inode->i_size,
6821 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006822 if (ret != -EAGAIN) {
6823 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006824 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006825 }
Chris Mason39279cc2007-06-12 06:35:45 -04006826
Josef Bacikfcb80c22011-05-03 10:40:22 -04006827 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006828 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006829 if (ret) {
6830 err = ret;
6831 break;
6832 }
Josef Bacik907cbce2011-08-08 13:46:15 -04006833end_trans:
Yan, Zheng80825102009-11-12 09:35:36 +00006834 nr = trans->blocks_used;
6835 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006836 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006837 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006838 }
6839
6840 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04006841 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006842 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006843 if (ret)
6844 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006845 } else if (ret && inode->i_nlink > 0) {
6846 /*
6847 * Failed to do the truncate, remove us from the in memory
6848 * orphan list.
6849 */
6850 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006851 }
6852
Chris Mason917c16b2011-11-08 14:49:59 -05006853 if (trans) {
6854 trans->block_rsv = &root->fs_info->trans_block_rsv;
6855 ret = btrfs_update_inode(trans, root, inode);
6856 if (ret && !err)
6857 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006858
Chris Mason917c16b2011-11-08 14:49:59 -05006859 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006860 ret = btrfs_end_transaction(trans, root);
Chris Mason917c16b2011-11-08 14:49:59 -05006861 btrfs_btree_balance_dirty(root, nr);
6862 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04006863
6864out:
6865 btrfs_free_block_rsv(root, rsv);
6866
Josef Bacik3893e332011-01-31 16:03:11 -05006867 if (ret && !err)
6868 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05006869
Josef Bacik3893e332011-01-31 16:03:11 -05006870 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006871}
6872
Sven Wegener3b963622008-06-09 21:57:42 -04006873/*
Chris Masond352ac62008-09-29 15:18:18 -04006874 * create a new subvolume directory/inode (helper for the ioctl).
6875 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006876int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006877 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04006878{
Chris Mason39279cc2007-06-12 06:35:45 -04006879 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006880 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006881 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006882
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01006883 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
6884 new_dirid, new_dirid,
6885 S_IFDIR | (~current_umask() & S_IRWXUGO),
6886 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006887 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006888 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006889 inode->i_op = &btrfs_dir_inode_operations;
6890 inode->i_fop = &btrfs_dir_file_operations;
6891
Miklos Szeredibfe86842011-10-28 14:13:29 +02006892 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04006893 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006894
Yan, Zheng76dda932009-09-21 16:00:26 -04006895 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006896
Yan, Zheng76dda932009-09-21 16:00:26 -04006897 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07006898 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006899}
6900
Chris Mason39279cc2007-06-12 06:35:45 -04006901struct inode *btrfs_alloc_inode(struct super_block *sb)
6902{
6903 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006904 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006905
6906 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6907 if (!ei)
6908 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006909
6910 ei->root = NULL;
6911 ei->space_info = NULL;
6912 ei->generation = 0;
6913 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006914 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006915 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006916 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006917 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006918 ei->disk_i_size = 0;
6919 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04006920 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006921 ei->index_cnt = (u64)-1;
6922 ei->last_unlink_trans = 0;
6923
Josef Bacik9e0baf62011-07-15 15:16:44 +00006924 spin_lock_init(&ei->lock);
6925 ei->outstanding_extents = 0;
6926 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006927
6928 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006929 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006930 ei->dummy_inode = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04006931 ei->in_defrag = 0;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05006932 ei->delalloc_meta_reserved = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006933 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006934
Miao Xie16cdcec2011-04-22 18:12:22 +08006935 ei->delayed_node = NULL;
6936
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006937 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02006938 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02006939 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6940 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006941 ei->io_tree.track_uptodate = 1;
6942 ei->io_failure_tree.track_uptodate = 1;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006943 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05006944 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006945 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006946 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006947 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006948 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006949 RB_CLEAR_NODE(&ei->rb_node);
6950
6951 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006952}
6953
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006954static void btrfs_i_callback(struct rcu_head *head)
6955{
6956 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006957 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6958}
6959
Chris Mason39279cc2007-06-12 06:35:45 -04006960void btrfs_destroy_inode(struct inode *inode)
6961{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006962 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006963 struct btrfs_root *root = BTRFS_I(inode)->root;
6964
Chris Mason39279cc2007-06-12 06:35:45 -04006965 WARN_ON(!list_empty(&inode->i_dentry));
6966 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006967 WARN_ON(BTRFS_I(inode)->outstanding_extents);
6968 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04006969 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
6970 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04006971
Chris Mason5a3f23d2009-03-31 13:27:11 -04006972 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006973 * This can happen where we create an inode, but somebody else also
6974 * created the same inode and we need to destroy the one we already
6975 * created.
6976 */
6977 if (!root)
6978 goto free;
6979
6980 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006981 * Make sure we're properly removed from the ordered operation
6982 * lists.
6983 */
6984 smp_mb();
6985 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6986 spin_lock(&root->fs_info->ordered_extent_lock);
6987 list_del_init(&BTRFS_I(inode)->ordered_operations);
6988 spin_unlock(&root->fs_info->ordered_extent_lock);
6989 }
6990
Yan, Zhengd68fc572010-05-16 10:49:58 -04006991 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006992 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Li Zefan33345d012011-04-20 10:31:50 +08006993 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6994 (unsigned long long)btrfs_ino(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00006995 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006996 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006997 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006998
Chris Masond3977122009-01-05 21:25:51 -05006999 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007000 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7001 if (!ordered)
7002 break;
7003 else {
Chris Masond3977122009-01-05 21:25:51 -05007004 printk(KERN_ERR "btrfs found ordered "
7005 "extent %llu %llu on inode cleanup\n",
7006 (unsigned long long)ordered->file_offset,
7007 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007008 btrfs_remove_ordered_extent(inode, ordered);
7009 btrfs_put_ordered_extent(ordered);
7010 btrfs_put_ordered_extent(ordered);
7011 }
7012 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007013 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007014 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007015free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007016 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007017 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007018}
7019
Al Viro45321ac2010-06-07 13:43:19 -04007020int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007021{
7022 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007023
Josef Bacik0af3d002010-06-21 14:48:16 -04007024 if (btrfs_root_refs(&root->root_item) == 0 &&
Chris Mason2cf85722011-07-26 15:35:09 -04007025 !btrfs_is_free_space_inode(root, inode))
Al Viro45321ac2010-06-07 13:43:19 -04007026 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007027 else
Al Viro45321ac2010-06-07 13:43:19 -04007028 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007029}
7030
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007031static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007032{
7033 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7034
7035 inode_init_once(&ei->vfs_inode);
7036}
7037
7038void btrfs_destroy_cachep(void)
7039{
7040 if (btrfs_inode_cachep)
7041 kmem_cache_destroy(btrfs_inode_cachep);
7042 if (btrfs_trans_handle_cachep)
7043 kmem_cache_destroy(btrfs_trans_handle_cachep);
7044 if (btrfs_transaction_cachep)
7045 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007046 if (btrfs_path_cachep)
7047 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007048 if (btrfs_free_space_cachep)
7049 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007050}
7051
7052int btrfs_init_cachep(void)
7053{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007054 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
7055 sizeof(struct btrfs_inode), 0,
7056 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007057 if (!btrfs_inode_cachep)
7058 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007059
7060 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
7061 sizeof(struct btrfs_trans_handle), 0,
7062 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007063 if (!btrfs_trans_handle_cachep)
7064 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007065
7066 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
7067 sizeof(struct btrfs_transaction), 0,
7068 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007069 if (!btrfs_transaction_cachep)
7070 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007071
7072 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
7073 sizeof(struct btrfs_path), 0,
7074 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007075 if (!btrfs_path_cachep)
7076 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007077
Josef Bacikdc89e982011-01-28 17:05:48 -05007078 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
7079 sizeof(struct btrfs_free_space), 0,
7080 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7081 if (!btrfs_free_space_cachep)
7082 goto fail;
7083
Chris Mason39279cc2007-06-12 06:35:45 -04007084 return 0;
7085fail:
7086 btrfs_destroy_cachep();
7087 return -ENOMEM;
7088}
7089
7090static int btrfs_getattr(struct vfsmount *mnt,
7091 struct dentry *dentry, struct kstat *stat)
7092{
7093 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007094 u32 blocksize = inode->i_sb->s_blocksize;
7095
Chris Mason39279cc2007-06-12 06:35:45 -04007096 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007097 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007098 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007099 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7100 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007101 return 0;
7102}
7103
Liu Bo75e7cb72011-03-22 10:12:20 +00007104/*
7105 * If a file is moved, it will inherit the cow and compression flags of the new
7106 * directory.
7107 */
7108static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7109{
7110 struct btrfs_inode *b_dir = BTRFS_I(dir);
7111 struct btrfs_inode *b_inode = BTRFS_I(inode);
7112
7113 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7114 b_inode->flags |= BTRFS_INODE_NODATACOW;
7115 else
7116 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7117
7118 if (b_dir->flags & BTRFS_INODE_COMPRESS)
7119 b_inode->flags |= BTRFS_INODE_COMPRESS;
7120 else
7121 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7122}
7123
Chris Masond3977122009-01-05 21:25:51 -05007124static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7125 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007126{
7127 struct btrfs_trans_handle *trans;
7128 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007129 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007130 struct inode *new_inode = new_dentry->d_inode;
7131 struct inode *old_inode = old_dentry->d_inode;
7132 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007133 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007134 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007135 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007136 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007137
Li Zefan33345d012011-04-20 10:31:50 +08007138 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007139 return -EPERM;
7140
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007141 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007142 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007143 return -EXDEV;
7144
Li Zefan33345d012011-04-20 10:31:50 +08007145 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7146 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007147 return -ENOTEMPTY;
7148
Chris Mason39279cc2007-06-12 06:35:45 -04007149 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007150 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007151 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007152 /*
7153 * we're using rename to replace one file with another.
7154 * and the replacement file is large. Start IO on it now so
7155 * we don't add too much work to the end of the transaction
7156 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007157 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007158 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7159 filemap_flush(old_inode->i_mapping);
7160
Yan, Zheng76dda932009-09-21 16:00:26 -04007161 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007162 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007163 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007164 /*
7165 * We want to reserve the absolute worst case amount of items. So if
7166 * both inodes are subvols and we need to unlink them then that would
7167 * require 4 item modifications, but if they are both normal inodes it
7168 * would require 5 item modifications, so we'll assume their normal
7169 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7170 * should cover the worst case number of items we'll modify.
7171 */
7172 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007173 if (IS_ERR(trans)) {
7174 ret = PTR_ERR(trans);
7175 goto out_notrans;
7176 }
Chris Mason5f39d392007-10-15 16:14:19 -04007177
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007178 if (dest != root)
7179 btrfs_record_root_in_trans(trans, dest);
7180
Yan, Zhenga5719522009-09-24 09:17:31 -04007181 ret = btrfs_set_inode_index(new_dir, &index);
7182 if (ret)
7183 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007184
Li Zefan33345d012011-04-20 10:31:50 +08007185 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007186 /* force full log commit if subvolume involved. */
7187 root->fs_info->last_trans_log_full_commit = trans->transid;
7188 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007189 ret = btrfs_insert_inode_ref(trans, dest,
7190 new_dentry->d_name.name,
7191 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007192 old_ino,
7193 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007194 if (ret)
7195 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007196 /*
7197 * this is an ugly little race, but the rename is required
7198 * to make sure that if we crash, the inode is either at the
7199 * old name or the new one. pinning the log transaction lets
7200 * us make sure we don't allow a log commit to come in after
7201 * we unlink the name but before we add the new name back in.
7202 */
7203 btrfs_pin_log_trans(root);
7204 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007205 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007206 * make sure the inode gets flushed if it is replacing
7207 * something.
7208 */
Li Zefan33345d012011-04-20 10:31:50 +08007209 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007210 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007211
Chris Mason39279cc2007-06-12 06:35:45 -04007212 old_dir->i_ctime = old_dir->i_mtime = ctime;
7213 new_dir->i_ctime = new_dir->i_mtime = ctime;
7214 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007215
Chris Mason12fcfd22009-03-24 10:24:20 -04007216 if (old_dentry->d_parent != new_dentry->d_parent)
7217 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7218
Li Zefan33345d012011-04-20 10:31:50 +08007219 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007220 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7221 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7222 old_dentry->d_name.name,
7223 old_dentry->d_name.len);
7224 } else {
Al Viro92986792011-03-04 17:14:37 +00007225 ret = __btrfs_unlink_inode(trans, root, old_dir,
7226 old_dentry->d_inode,
7227 old_dentry->d_name.name,
7228 old_dentry->d_name.len);
7229 if (!ret)
7230 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007231 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007232 if (ret) {
7233 btrfs_abort_transaction(trans, root, ret);
7234 goto out_fail;
7235 }
Chris Mason39279cc2007-06-12 06:35:45 -04007236
7237 if (new_inode) {
7238 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007239 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007240 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7241 root_objectid = BTRFS_I(new_inode)->location.objectid;
7242 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7243 root_objectid,
7244 new_dentry->d_name.name,
7245 new_dentry->d_name.len);
7246 BUG_ON(new_inode->i_nlink == 0);
7247 } else {
7248 ret = btrfs_unlink_inode(trans, dest, new_dir,
7249 new_dentry->d_inode,
7250 new_dentry->d_name.name,
7251 new_dentry->d_name.len);
7252 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007253 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007254 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007255 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007256 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007257 if (ret) {
7258 btrfs_abort_transaction(trans, root, ret);
7259 goto out_fail;
7260 }
Chris Mason39279cc2007-06-12 06:35:45 -04007261 }
Josef Bacikaec74772008-07-24 12:12:38 -04007262
Liu Bo75e7cb72011-03-22 10:12:20 +00007263 fixup_inode_flags(new_dir, old_inode);
7264
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007265 ret = btrfs_add_link(trans, new_dir, old_inode,
7266 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007267 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007268 if (ret) {
7269 btrfs_abort_transaction(trans, root, ret);
7270 goto out_fail;
7271 }
Chris Mason39279cc2007-06-12 06:35:45 -04007272
Li Zefan33345d012011-04-20 10:31:50 +08007273 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007274 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007275 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007276 btrfs_end_log_trans(root);
7277 }
Chris Mason39279cc2007-06-12 06:35:45 -04007278out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007279 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007280out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007281 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007282 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007283
Chris Mason39279cc2007-06-12 06:35:45 -04007284 return ret;
7285}
7286
Chris Masond352ac62008-09-29 15:18:18 -04007287/*
7288 * some fairly slow code that needs optimization. This walks the list
7289 * of all the inodes with pending delalloc and forces them to disk.
7290 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007291int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007292{
7293 struct list_head *head = &root->fs_info->delalloc_inodes;
7294 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007295 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007296
Yan Zhengc146afa2008-11-12 14:34:12 -05007297 if (root->fs_info->sb->s_flags & MS_RDONLY)
7298 return -EROFS;
7299
Chris Mason75eff682008-12-15 15:54:40 -05007300 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007301 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007302 binode = list_entry(head->next, struct btrfs_inode,
7303 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007304 inode = igrab(&binode->vfs_inode);
7305 if (!inode)
7306 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007307 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007308 if (inode) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04007309 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007310 if (delay_iput)
7311 btrfs_add_delayed_iput(inode);
7312 else
7313 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007314 }
7315 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007316 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007317 }
Chris Mason75eff682008-12-15 15:54:40 -05007318 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee1d2008-09-29 11:19:10 -04007319
7320 /* the filemap_flush will queue IO into the worker threads, but
7321 * we have to make sure the IO is actually started and that
7322 * ordered extents get created before we return
7323 */
7324 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007325 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007326 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04007327 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007328 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7329 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee1d2008-09-29 11:19:10 -04007330 }
7331 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007332 return 0;
7333}
7334
Chris Mason39279cc2007-06-12 06:35:45 -04007335static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7336 const char *symname)
7337{
7338 struct btrfs_trans_handle *trans;
7339 struct btrfs_root *root = BTRFS_I(dir)->root;
7340 struct btrfs_path *path;
7341 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007342 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007343 int err;
7344 int drop_inode = 0;
7345 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007346 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007347 int name_len;
7348 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007349 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007350 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007351 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007352 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007353
7354 name_len = strlen(symname) + 1;
7355 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7356 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007357
Josef Bacik9ed74f22009-09-11 16:12:44 -04007358 /*
7359 * 2 items for inode item and ref
7360 * 2 items for dir items
7361 * 1 item for xattr if selinux is on
7362 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007363 trans = btrfs_start_transaction(root, 5);
7364 if (IS_ERR(trans))
7365 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007366
Li Zefan581bb052011-04-20 10:06:11 +08007367 err = btrfs_find_free_ino(root, &objectid);
7368 if (err)
7369 goto out_unlock;
7370
Josef Bacikaec74772008-07-24 12:12:38 -04007371 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007372 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007373 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007374 if (IS_ERR(inode)) {
7375 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007376 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007377 }
Chris Mason39279cc2007-06-12 06:35:45 -04007378
Eric Paris2a7dba32011-02-01 11:05:39 -05007379 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007380 if (err) {
7381 drop_inode = 1;
7382 goto out_unlock;
7383 }
7384
Casey Schauflerad19db72011-12-15 10:09:07 -05007385 /*
7386 * If the active LSM wants to access the inode during
7387 * d_instantiate it needs these. Smack checks to see
7388 * if the filesystem supports xattrs by looking at the
7389 * ops vector.
7390 */
7391 inode->i_fop = &btrfs_file_operations;
7392 inode->i_op = &btrfs_file_inode_operations;
7393
Josef Bacika1b075d2010-11-19 20:36:11 +00007394 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007395 if (err)
7396 drop_inode = 1;
7397 else {
7398 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007399 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007400 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007401 }
Chris Mason39279cc2007-06-12 06:35:45 -04007402 if (drop_inode)
7403 goto out_unlock;
7404
7405 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007406 if (!path) {
7407 err = -ENOMEM;
7408 drop_inode = 1;
7409 goto out_unlock;
7410 }
Li Zefan33345d012011-04-20 10:31:50 +08007411 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007412 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007413 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7414 datasize = btrfs_file_extent_calc_inline_size(name_len);
7415 err = btrfs_insert_empty_item(trans, root, path, &key,
7416 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007417 if (err) {
7418 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007419 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007420 goto out_unlock;
7421 }
Chris Mason5f39d392007-10-15 16:14:19 -04007422 leaf = path->nodes[0];
7423 ei = btrfs_item_ptr(leaf, path->slots[0],
7424 struct btrfs_file_extent_item);
7425 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7426 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007427 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007428 btrfs_set_file_extent_encryption(leaf, ei, 0);
7429 btrfs_set_file_extent_compression(leaf, ei, 0);
7430 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7431 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7432
Chris Mason39279cc2007-06-12 06:35:45 -04007433 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007434 write_extent_buffer(leaf, symname, ptr, name_len);
7435 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007436 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007437
Chris Mason39279cc2007-06-12 06:35:45 -04007438 inode->i_op = &btrfs_symlink_inode_operations;
7439 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007440 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007441 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007442 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007443 err = btrfs_update_inode(trans, root, inode);
7444 if (err)
7445 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007446
7447out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007448 if (!err)
7449 d_instantiate(dentry, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007450 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007451 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007452 if (drop_inode) {
7453 inode_dec_link_count(inode);
7454 iput(inode);
7455 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007456 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007457 return err;
7458}
Chris Mason16432982008-04-10 10:23:21 -04007459
Josef Bacik0af3d002010-06-21 14:48:16 -04007460static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7461 u64 start, u64 num_bytes, u64 min_size,
7462 loff_t actual_len, u64 *alloc_hint,
7463 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007464{
Yan Zhengd899e052008-10-30 14:25:28 -04007465 struct btrfs_root *root = BTRFS_I(inode)->root;
7466 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007467 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007468 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007469 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007470 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007471
Josef Bacik0af3d002010-06-21 14:48:16 -04007472 if (trans)
7473 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007474 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007475 if (own_trans) {
7476 trans = btrfs_start_transaction(root, 3);
7477 if (IS_ERR(trans)) {
7478 ret = PTR_ERR(trans);
7479 break;
7480 }
Yan Zhengd899e052008-10-30 14:25:28 -04007481 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007482
Yan, Zhengefa56462010-05-16 10:49:59 -04007483 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007484 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007485 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007486 if (own_trans)
7487 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007488 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007489 }
7490
Yan Zhengd899e052008-10-30 14:25:28 -04007491 ret = insert_reserved_file_extent(trans, inode,
7492 cur_offset, ins.objectid,
7493 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007494 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007495 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007496 if (ret) {
7497 btrfs_abort_transaction(trans, root, ret);
7498 if (own_trans)
7499 btrfs_end_transaction(trans, root);
7500 break;
7501 }
Chris Masona1ed8352009-09-11 12:27:37 -04007502 btrfs_drop_extent_cache(inode, cur_offset,
7503 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007504
Yan Zhengd899e052008-10-30 14:25:28 -04007505 num_bytes -= ins.offset;
7506 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007507 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007508
Yan Zhengd899e052008-10-30 14:25:28 -04007509 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007510 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007511 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007512 (actual_len > inode->i_size) &&
7513 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007514 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007515 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007516 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007517 i_size = cur_offset;
7518 i_size_write(inode, i_size);
7519 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007520 }
7521
Yan Zhengd899e052008-10-30 14:25:28 -04007522 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007523
7524 if (ret) {
7525 btrfs_abort_transaction(trans, root, ret);
7526 if (own_trans)
7527 btrfs_end_transaction(trans, root);
7528 break;
7529 }
Yan Zhengd899e052008-10-30 14:25:28 -04007530
Josef Bacik0af3d002010-06-21 14:48:16 -04007531 if (own_trans)
7532 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007533 }
Yan Zhengd899e052008-10-30 14:25:28 -04007534 return ret;
7535}
7536
Josef Bacik0af3d002010-06-21 14:48:16 -04007537int btrfs_prealloc_file_range(struct inode *inode, int mode,
7538 u64 start, u64 num_bytes, u64 min_size,
7539 loff_t actual_len, u64 *alloc_hint)
7540{
7541 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7542 min_size, actual_len, alloc_hint,
7543 NULL);
7544}
7545
7546int btrfs_prealloc_file_range_trans(struct inode *inode,
7547 struct btrfs_trans_handle *trans, int mode,
7548 u64 start, u64 num_bytes, u64 min_size,
7549 loff_t actual_len, u64 *alloc_hint)
7550{
7551 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7552 min_size, actual_len, alloc_hint, trans);
7553}
7554
Chris Masone6dcd2d2008-07-17 12:53:50 -04007555static int btrfs_set_page_dirty(struct page *page)
7556{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007557 return __set_page_dirty_nobuffers(page);
7558}
7559
Al Viro10556cb2011-06-20 19:28:19 -04007560static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007561{
Li Zefanb83cc962010-12-20 16:04:08 +08007562 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007563 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007564
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007565 if (mask & MAY_WRITE &&
7566 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7567 if (btrfs_root_readonly(root))
7568 return -EROFS;
7569 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7570 return -EACCES;
7571 }
Al Viro2830ba72011-06-20 19:16:29 -04007572 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007573}
Chris Mason39279cc2007-06-12 06:35:45 -04007574
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007575static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007576 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007577 .lookup = btrfs_lookup,
7578 .create = btrfs_create,
7579 .unlink = btrfs_unlink,
7580 .link = btrfs_link,
7581 .mkdir = btrfs_mkdir,
7582 .rmdir = btrfs_rmdir,
7583 .rename = btrfs_rename,
7584 .symlink = btrfs_symlink,
7585 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007586 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007587 .setxattr = btrfs_setxattr,
7588 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007589 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007590 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007591 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007592 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007593};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007594static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007595 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007596 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007597 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007598};
Yan, Zheng76dda932009-09-21 16:00:26 -04007599
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007600static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007601 .llseek = generic_file_llseek,
7602 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007603 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007604 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007605#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007606 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007607#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007608 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007609 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007610};
7611
Chris Masond1310b22008-01-24 16:13:08 -05007612static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007613 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007614 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007615 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007616 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007617 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007618 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007619 .set_bit_hook = btrfs_set_bit_hook,
7620 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007621 .merge_extent_hook = btrfs_merge_extent_hook,
7622 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007623};
7624
Chris Mason35054392009-01-21 13:11:13 -05007625/*
7626 * btrfs doesn't support the bmap operation because swapfiles
7627 * use bmap to make a mapping of extents in the file. They assume
7628 * these extents won't change over the life of the file and they
7629 * use the bmap result to do IO directly to the drive.
7630 *
7631 * the btrfs bmap call would return logical addresses that aren't
7632 * suitable for IO and they also will change frequently as COW
7633 * operations happen. So, swapfile + btrfs == corruption.
7634 *
7635 * For now we're avoiding this by dropping bmap.
7636 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007637static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007638 .readpage = btrfs_readpage,
7639 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007640 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007641 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007642 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007643 .invalidatepage = btrfs_invalidatepage,
7644 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007645 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007646 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007647};
7648
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007649static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007650 .readpage = btrfs_readpage,
7651 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007652 .invalidatepage = btrfs_invalidatepage,
7653 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007654};
7655
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007656static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007657 .getattr = btrfs_getattr,
7658 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007659 .setxattr = btrfs_setxattr,
7660 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007661 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007662 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007663 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007664 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007665 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007666};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007667static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007668 .getattr = btrfs_getattr,
7669 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007670 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007671 .setxattr = btrfs_setxattr,
7672 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007673 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007674 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007675 .get_acl = btrfs_get_acl,
Josef Bacik618e21d2007-07-11 10:18:17 -04007676};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007677static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007678 .readlink = generic_readlink,
7679 .follow_link = page_follow_link_light,
7680 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007681 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05007682 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007683 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007684 .setxattr = btrfs_setxattr,
7685 .getxattr = btrfs_getxattr,
7686 .listxattr = btrfs_listxattr,
7687 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007688 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007689};
Yan, Zheng76dda932009-09-21 16:00:26 -04007690
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007691const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007692 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007693 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04007694};