blob: bfd59bcc50d761f5a1568cd69f166760ffcb74f8 [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;
Miao Xie8ccf6f192012-10-25 09:28:04 +000074static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075struct kmem_cache *btrfs_trans_handle_cachep;
76struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050078struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
80#define S_SHIFT 12
81static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
82 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
83 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
84 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
85 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
86 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
87 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
88 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
89};
90
Josef Bacika41ad392011-01-31 15:30:16 -050091static int btrfs_setsize(struct inode *inode, loff_t newsize);
92static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040093static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050094static noinline int cow_file_range(struct inode *inode,
95 struct page *locked_page,
96 u64 start, u64 end, int *page_started,
97 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040098
Yan, Zhengf34f57a2009-11-12 09:35:27 +000099static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500100 struct inode *inode, struct inode *dir,
101 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500102{
103 int err;
104
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000105 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500106 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500107 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500108 return err;
109}
110
Chris Masond352ac62008-09-29 15:18:18 -0400111/*
Chris Masonc8b97812008-10-29 14:49:59 -0400112 * this does all the hard work for inserting an inline extent into
113 * the btree. The caller should have done a btrfs_drop_extents so that
114 * no overlapping inline items exist in the btree
115 */
Chris Masond3977122009-01-05 21:25:51 -0500116static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400117 struct btrfs_root *root, struct inode *inode,
118 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000119 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400120 struct page **compressed_pages)
121{
122 struct btrfs_key key;
123 struct btrfs_path *path;
124 struct extent_buffer *leaf;
125 struct page *page = NULL;
126 char *kaddr;
127 unsigned long ptr;
128 struct btrfs_file_extent_item *ei;
129 int err = 0;
130 int ret;
131 size_t cur_size = size;
132 size_t datasize;
133 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400134
Li Zefanfe3f5662011-03-28 08:30:38 +0000135 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400136 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400137
Chris Masond3977122009-01-05 21:25:51 -0500138 path = btrfs_alloc_path();
139 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400140 return -ENOMEM;
141
Chris Masonb9473432009-03-13 11:00:37 -0400142 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400143
Li Zefan33345d012011-04-20 10:31:50 +0800144 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400145 key.offset = start;
146 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400147 datasize = btrfs_file_extent_calc_inline_size(cur_size);
148
149 inode_add_bytes(inode, size);
150 ret = btrfs_insert_empty_item(trans, root, path, &key,
151 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400152 if (ret) {
153 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400154 goto fail;
155 }
156 leaf = path->nodes[0];
157 ei = btrfs_item_ptr(leaf, path->slots[0],
158 struct btrfs_file_extent_item);
159 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
160 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
161 btrfs_set_file_extent_encryption(leaf, ei, 0);
162 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
163 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
164 ptr = btrfs_file_extent_inline_start(ei);
165
Li Zefan261507a02010-12-17 14:21:50 +0800166 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400167 struct page *cpage;
168 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500169 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400170 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500171 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400172 PAGE_CACHE_SIZE);
173
Cong Wang7ac687d2011-11-25 23:14:28 +0800174 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400175 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800176 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400177
178 i++;
179 ptr += cur_size;
180 compressed_size -= cur_size;
181 }
182 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800183 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400184 } else {
185 page = find_get_page(inode->i_mapping,
186 start >> PAGE_CACHE_SHIFT);
187 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800188 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400189 offset = start & (PAGE_CACHE_SIZE - 1);
190 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800191 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400192 page_cache_release(page);
193 }
194 btrfs_mark_buffer_dirty(leaf);
195 btrfs_free_path(path);
196
Yan, Zhengc2167752009-11-12 09:34:21 +0000197 /*
198 * we're an inline extent, so nobody can
199 * extend the file past i_size without locking
200 * a page we already have locked.
201 *
202 * We must do any isize and inode updates
203 * before we unlock the pages. Otherwise we
204 * could end up racing with unlink.
205 */
Chris Masonc8b97812008-10-29 14:49:59 -0400206 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100207 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000208
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100209 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400210fail:
211 btrfs_free_path(path);
212 return err;
213}
214
215
216/*
217 * conditionally insert an inline extent into the file. This
218 * does the checks required to make sure the data is small enough
219 * to fit as an inline extent.
220 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400221static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400222 struct btrfs_root *root,
223 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000224 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400225 struct page **compressed_pages)
226{
227 u64 isize = i_size_read(inode);
228 u64 actual_end = min(end + 1, isize);
229 u64 inline_len = actual_end - start;
230 u64 aligned_end = (end + root->sectorsize - 1) &
231 ~((u64)root->sectorsize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400232 u64 data_len = inline_len;
233 int ret;
234
235 if (compressed_size)
236 data_len = compressed_size;
237
238 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400239 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400240 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
241 (!compressed_size &&
242 (actual_end & (root->sectorsize - 1)) == 0) ||
243 end + 1 < isize ||
244 data_len > root->fs_info->max_inline) {
245 return 1;
246 }
247
Josef Bacik26714852012-08-29 12:24:27 -0400248 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100249 if (ret)
250 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400251
252 if (isize > actual_end)
253 inline_len = min_t(u64, isize, actual_end);
254 ret = insert_inline_extent(trans, root, inode, start,
255 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000256 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400257 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100258 btrfs_abort_transaction(trans, root, ret);
259 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400260 } else if (ret == -ENOSPC) {
261 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100262 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400263
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
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300324 * are written in the same order that the flusher thread sent them
325 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400326 */
Chris Mason771ed682008-11-06 22:02:51 -0500327static noinline int compress_file_range(struct inode *inode,
328 struct page *locked_page,
329 u64 start, u64 end,
330 struct async_cow *async_cow,
331 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400332{
333 struct btrfs_root *root = BTRFS_I(inode)->root;
334 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400335 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400336 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400337 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500338 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400339 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400340 struct page **pages = NULL;
341 unsigned long nr_pages;
342 unsigned long nr_pages_ret = 0;
343 unsigned long total_compressed = 0;
344 unsigned long total_in = 0;
345 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500346 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400347 int i;
348 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800349 int compress_type = root->fs_info->compress_type;
Chris Masonb888db2b2007-08-27 16:49:44 -0400350
Liu Bo4cb13e52012-03-29 09:57:45 -0400351 /* if this is a small write inside eof, kick off a defrag */
352 if ((end - start + 1) < 16 * 1024 &&
353 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400354 btrfs_add_inode_defrag(NULL, inode);
355
Chris Mason42dc7ba2008-12-15 11:44:56 -0500356 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400357again:
358 will_compress = 0;
359 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
360 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
361
Chris Masonf03d9301f2009-02-04 09:31:06 -0500362 /*
363 * we don't want to send crud past the end of i_size through
364 * compression, that's just a waste of CPU time. So, if the
365 * end of the file is before the start of our current
366 * requested range of bytes, we bail out to the uncompressed
367 * cleanup code that can deal with all of this.
368 *
369 * It isn't really the fastest way to fix things, but this is a
370 * very uncommon corner.
371 */
372 if (actual_end <= start)
373 goto cleanup_and_bail_uncompressed;
374
Chris Masonc8b97812008-10-29 14:49:59 -0400375 total_compressed = actual_end - start;
376
377 /* we want to make sure that amount of ram required to uncompress
378 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500379 * of a compressed extent to 128k. This is a crucial number
380 * because it also controls how easily we can spread reads across
381 * cpus for decompression.
382 *
383 * We also want to make sure the amount of IO required to do
384 * a random read is reasonably small, so we limit the size of
385 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400386 */
387 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400388 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500389 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400390 total_in = 0;
391 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400392
Chris Mason771ed682008-11-06 22:02:51 -0500393 /*
394 * we do compression for mount -o compress and when the
395 * inode has not been flagged as nocompress. This flag can
396 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400397 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200398 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500399 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000400 (BTRFS_I(inode)->force_compress) ||
401 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400402 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400403 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800404 if (!pages) {
405 /* just bail out to the uncompressed code */
406 goto cont;
407 }
Chris Mason179e29e2007-11-01 11:28:41 -0400408
Li Zefan261507a02010-12-17 14:21:50 +0800409 if (BTRFS_I(inode)->force_compress)
410 compress_type = BTRFS_I(inode)->force_compress;
411
412 ret = btrfs_compress_pages(compress_type,
413 inode->i_mapping, start,
414 total_compressed, pages,
415 nr_pages, &nr_pages_ret,
416 &total_in,
417 &total_compressed,
418 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400419
420 if (!ret) {
421 unsigned long offset = total_compressed &
422 (PAGE_CACHE_SIZE - 1);
423 struct page *page = pages[nr_pages_ret - 1];
424 char *kaddr;
425
426 /* zero the tail end of the last page, we might be
427 * sending it down to disk
428 */
429 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800430 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400431 memset(kaddr + offset, 0,
432 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800433 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400434 }
435 will_compress = 1;
436 }
437 }
Li Zefan560f7d72011-09-08 10:22:01 +0800438cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400439 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400440 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100441 if (IS_ERR(trans)) {
442 ret = PTR_ERR(trans);
443 trans = NULL;
444 goto cleanup_and_out;
445 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400446 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500447
Chris Masonc8b97812008-10-29 14:49:59 -0400448 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500449 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400450 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500451 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400452 */
453 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000454 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400455 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500456 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400457 ret = cow_file_range_inline(trans, root, inode,
458 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000459 total_compressed,
460 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400461 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100462 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500463 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100464 * inline extent creation worked or returned error,
465 * we don't need to create any more async work items.
466 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500467 */
Chris Masonc8b97812008-10-29 14:49:59 -0400468 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400469 &BTRFS_I(inode)->io_tree,
470 start, end, NULL,
471 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400472 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400473 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000474
475 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400476 goto free_pages_out;
477 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000478 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 }
480
481 if (will_compress) {
482 /*
483 * we aren't doing an inline extent round the compressed size
484 * up to a block size boundary so the allocator does sane
485 * things
486 */
487 total_compressed = (total_compressed + blocksize - 1) &
488 ~(blocksize - 1);
489
490 /*
491 * one last check to make sure the compression is really a
492 * win, compare the page count read with the blocks on disk
493 */
494 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
495 ~(PAGE_CACHE_SIZE - 1);
496 if (total_compressed >= total_in) {
497 will_compress = 0;
498 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400499 num_bytes = total_in;
500 }
501 }
502 if (!will_compress && pages) {
503 /*
504 * the compression code ran but failed to make things smaller,
505 * free any pages it allocated and our page pointer array
506 */
507 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400508 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400509 page_cache_release(pages[i]);
510 }
511 kfree(pages);
512 pages = NULL;
513 total_compressed = 0;
514 nr_pages_ret = 0;
515
516 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500517 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
518 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500519 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500520 }
Chris Masonc8b97812008-10-29 14:49:59 -0400521 }
Chris Mason771ed682008-11-06 22:02:51 -0500522 if (will_compress) {
523 *num_added += 1;
524
525 /* the async work queues will take care of doing actual
526 * allocation on disk for these compressed pages,
527 * and will submit them to the elevator.
528 */
529 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800530 total_compressed, pages, nr_pages_ret,
531 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500532
Yan, Zheng24ae6362010-12-06 07:02:36 +0000533 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500534 start += num_bytes;
535 pages = NULL;
536 cond_resched();
537 goto again;
538 }
539 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500540cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500541 /*
542 * No compression, but we still need to write the pages in
543 * the file we've been given so far. redirty the locked
544 * page if it corresponds to our extent and set things up
545 * for the async work queue to run cow_file_range to do
546 * the normal delalloc dance
547 */
548 if (page_offset(locked_page) >= start &&
549 page_offset(locked_page) <= end) {
550 __set_page_dirty_nobuffers(locked_page);
551 /* unlocked later on in the async handlers */
552 }
Li Zefan261507a02010-12-17 14:21:50 +0800553 add_async_extent(async_cow, start, end - start + 1,
554 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500555 *num_added += 1;
556 }
557
558out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100559 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500560
561free_pages_out:
562 for (i = 0; i < nr_pages_ret; i++) {
563 WARN_ON(pages[i]->mapping);
564 page_cache_release(pages[i]);
565 }
Chris Masond3977122009-01-05 21:25:51 -0500566 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500567
568 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100569
570cleanup_and_out:
571 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
572 start, end, NULL,
573 EXTENT_CLEAR_UNLOCK_PAGE |
574 EXTENT_CLEAR_DIRTY |
575 EXTENT_CLEAR_DELALLOC |
576 EXTENT_SET_WRITEBACK |
577 EXTENT_END_WRITEBACK);
578 if (!trans || IS_ERR(trans))
579 btrfs_error(root->fs_info, ret, "Failed to join transaction");
580 else
581 btrfs_abort_transaction(trans, root, ret);
582 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500583}
584
585/*
586 * phase two of compressed writeback. This is the ordered portion
587 * of the code, which only gets called in the order the work was
588 * queued. We walk all the async extents created by compress_file_range
589 * and send them down to the disk.
590 */
591static noinline int submit_compressed_extents(struct inode *inode,
592 struct async_cow *async_cow)
593{
594 struct async_extent *async_extent;
595 u64 alloc_hint = 0;
596 struct btrfs_trans_handle *trans;
597 struct btrfs_key ins;
598 struct extent_map *em;
599 struct btrfs_root *root = BTRFS_I(inode)->root;
600 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
601 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500602 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500603
604 if (list_empty(&async_cow->extents))
605 return 0;
606
Chris Mason771ed682008-11-06 22:02:51 -0500607
Chris Masond3977122009-01-05 21:25:51 -0500608 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500609 async_extent = list_entry(async_cow->extents.next,
610 struct async_extent, list);
611 list_del(&async_extent->list);
612
613 io_tree = &BTRFS_I(inode)->io_tree;
614
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500615retry:
Chris Mason771ed682008-11-06 22:02:51 -0500616 /* did the compression code fall back to uncompressed IO? */
617 if (!async_extent->pages) {
618 int page_started = 0;
619 unsigned long nr_written = 0;
620
621 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000622 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100623 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500624
625 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500626 ret = cow_file_range(inode, async_cow->locked_page,
627 async_extent->start,
628 async_extent->start +
629 async_extent->ram_size - 1,
630 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500631
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100632 /* JDM XXX */
633
Chris Mason771ed682008-11-06 22:02:51 -0500634 /*
635 * if page_started, cow_file_range inserted an
636 * inline extent and took care of all the unlocking
637 * and IO for us. Otherwise, we need to submit
638 * all those pages down to the drive.
639 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500640 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500641 extent_write_locked_range(io_tree,
642 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500643 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500644 async_extent->ram_size - 1,
645 btrfs_get_extent,
646 WB_SYNC_ALL);
647 kfree(async_extent);
648 cond_resched();
649 continue;
650 }
651
652 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100653 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500654
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400655 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100656 if (IS_ERR(trans)) {
657 ret = PTR_ERR(trans);
658 } else {
659 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
660 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500661 async_extent->compressed_size,
662 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500663 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600664 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100665 btrfs_abort_transaction(trans, root, ret);
666 btrfs_end_transaction(trans, root);
667 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000668
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500669 if (ret) {
670 int i;
671 for (i = 0; i < async_extent->nr_pages; i++) {
672 WARN_ON(async_extent->pages[i]->mapping);
673 page_cache_release(async_extent->pages[i]);
674 }
675 kfree(async_extent->pages);
676 async_extent->nr_pages = 0;
677 async_extent->pages = NULL;
678 unlock_extent(io_tree, async_extent->start,
679 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100680 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100681 if (ret == -ENOSPC)
682 goto retry;
683 goto out_free; /* JDM: Requeue? */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500684 }
685
Yan, Zhengc2167752009-11-12 09:34:21 +0000686 /*
687 * here we're doing allocation and writeback of the
688 * compressed pages
689 */
690 btrfs_drop_extent_cache(inode, async_extent->start,
691 async_extent->start +
692 async_extent->ram_size - 1, 0);
693
David Sterba172ddd62011-04-21 00:48:27 +0200694 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100695 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500696 em->start = async_extent->start;
697 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500698 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500699
700 em->block_start = ins.objectid;
701 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500702 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500703 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800704 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500705 set_bit(EXTENT_FLAG_PINNED, &em->flags);
706 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
707
Chris Masond3977122009-01-05 21:25:51 -0500708 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400709 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500710 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400711 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500712 if (ret != -EEXIST) {
713 free_extent_map(em);
714 break;
715 }
716 btrfs_drop_extent_cache(inode, async_extent->start,
717 async_extent->start +
718 async_extent->ram_size - 1, 0);
719 }
720
Li Zefan261507a02010-12-17 14:21:50 +0800721 ret = btrfs_add_ordered_extent_compress(inode,
722 async_extent->start,
723 ins.objectid,
724 async_extent->ram_size,
725 ins.offset,
726 BTRFS_ORDERED_COMPRESSED,
727 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100728 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500729
Chris Mason771ed682008-11-06 22:02:51 -0500730 /*
731 * clear dirty, set writeback and unlock the pages.
732 */
733 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400734 &BTRFS_I(inode)->io_tree,
735 async_extent->start,
736 async_extent->start +
737 async_extent->ram_size - 1,
738 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
739 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400740 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400741 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500742
743 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500744 async_extent->start,
745 async_extent->ram_size,
746 ins.objectid,
747 ins.offset, async_extent->pages,
748 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500749
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100750 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500751 alloc_hint = ins.objectid + ins.offset;
752 kfree(async_extent);
753 cond_resched();
754 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100755 ret = 0;
756out:
757 return ret;
758out_free:
759 kfree(async_extent);
760 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500761}
762
Josef Bacik4b46fce2010-05-23 11:00:55 -0400763static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
764 u64 num_bytes)
765{
766 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
767 struct extent_map *em;
768 u64 alloc_hint = 0;
769
770 read_lock(&em_tree->lock);
771 em = search_extent_mapping(em_tree, start, num_bytes);
772 if (em) {
773 /*
774 * if block start isn't an actual block number then find the
775 * first block in this inode and use that as a hint. If that
776 * block is also bogus then just don't worry about it.
777 */
778 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
779 free_extent_map(em);
780 em = search_extent_mapping(em_tree, 0, 0);
781 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
782 alloc_hint = em->block_start;
783 if (em)
784 free_extent_map(em);
785 } else {
786 alloc_hint = em->block_start;
787 free_extent_map(em);
788 }
789 }
790 read_unlock(&em_tree->lock);
791
792 return alloc_hint;
793}
794
Chris Mason771ed682008-11-06 22:02:51 -0500795/*
796 * when extent_io.c finds a delayed allocation range in the file,
797 * the call backs end up in this code. The basic idea is to
798 * allocate extents on disk for the range, and create ordered data structs
799 * in ram to track those extents.
800 *
801 * locked_page is the page that writepage had locked already. We use
802 * it to make sure we don't do extra locks or unlocks.
803 *
804 * *page_started is set to one if we unlock locked_page and do everything
805 * required to start IO on it. It may be clean and already done with
806 * IO when we return.
807 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000808static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
809 struct inode *inode,
810 struct btrfs_root *root,
811 struct page *locked_page,
812 u64 start, u64 end, int *page_started,
813 unsigned long *nr_written,
814 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500815{
Chris Mason771ed682008-11-06 22:02:51 -0500816 u64 alloc_hint = 0;
817 u64 num_bytes;
818 unsigned long ram_size;
819 u64 disk_num_bytes;
820 u64 cur_alloc_size;
821 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500822 struct btrfs_key ins;
823 struct extent_map *em;
824 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
825 int ret = 0;
826
Liu Bo83eea1f2012-07-10 05:28:39 -0600827 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500828
Chris Mason771ed682008-11-06 22:02:51 -0500829 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
830 num_bytes = max(blocksize, num_bytes);
831 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500832
Chris Mason4cb53002011-05-24 15:35:30 -0400833 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400834 if (num_bytes < 64 * 1024 &&
835 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400836 btrfs_add_inode_defrag(trans, inode);
837
Chris Mason771ed682008-11-06 22:02:51 -0500838 if (start == 0) {
839 /* lets try to make an inline extent */
840 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000841 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500842 if (ret == 0) {
843 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400844 &BTRFS_I(inode)->io_tree,
845 start, end, NULL,
846 EXTENT_CLEAR_UNLOCK_PAGE |
847 EXTENT_CLEAR_UNLOCK |
848 EXTENT_CLEAR_DELALLOC |
849 EXTENT_CLEAR_DIRTY |
850 EXTENT_SET_WRITEBACK |
851 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000852
Chris Mason771ed682008-11-06 22:02:51 -0500853 *nr_written = *nr_written +
854 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
855 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500856 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100857 } else if (ret < 0) {
858 btrfs_abort_transaction(trans, root, ret);
859 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500860 }
861 }
Chris Masonc8b97812008-10-29 14:49:59 -0400862
863 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200864 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400865
Josef Bacik4b46fce2010-05-23 11:00:55 -0400866 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400867 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400868
Chris Masond3977122009-01-05 21:25:51 -0500869 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400870 unsigned long op;
871
Josef Bacik287a0ab2010-03-19 18:07:23 +0000872 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400873 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500874 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500875 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100876 if (ret < 0) {
877 btrfs_abort_transaction(trans, root, ret);
878 goto out_unlock;
879 }
Chris Masond3977122009-01-05 21:25:51 -0500880
David Sterba172ddd62011-04-21 00:48:27 +0200881 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100882 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400883 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500884 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500885 ram_size = ins.offset;
886 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400887
Chris Masone6dcd2d2008-07-17 12:53:50 -0400888 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400889 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500890 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400891 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400892 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400893
Chris Masond3977122009-01-05 21:25:51 -0500894 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400895 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400896 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400897 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400898 if (ret != -EEXIST) {
899 free_extent_map(em);
900 break;
901 }
902 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400903 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400904 }
905
Chris Mason98d20f62008-04-14 09:46:10 -0400906 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400907 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500908 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100909 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400910
Yan Zheng17d217f2008-12-12 10:03:38 -0500911 if (root->root_key.objectid ==
912 BTRFS_DATA_RELOC_TREE_OBJECTID) {
913 ret = btrfs_reloc_clone_csums(inode, start,
914 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100915 if (ret) {
916 btrfs_abort_transaction(trans, root, ret);
917 goto out_unlock;
918 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500919 }
920
Chris Masond3977122009-01-05 21:25:51 -0500921 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400922 break;
Chris Masond3977122009-01-05 21:25:51 -0500923
Chris Masonc8b97812008-10-29 14:49:59 -0400924 /* we're not doing compressed IO, don't unlock the first
925 * page (which the caller expects to stay locked), don't
926 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400927 *
928 * Do set the Private2 bit so we know this page was properly
929 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400930 */
Chris Masona791e352009-10-08 11:27:10 -0400931 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
932 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
933 EXTENT_SET_PRIVATE2;
934
Chris Masonc8b97812008-10-29 14:49:59 -0400935 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
936 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400937 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400938 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500939 num_bytes -= cur_alloc_size;
940 alloc_hint = ins.objectid + ins.offset;
941 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400942 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100943out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500944 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000945
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100946out_unlock:
947 extent_clear_unlock_delalloc(inode,
948 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400949 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100950 EXTENT_CLEAR_UNLOCK_PAGE |
951 EXTENT_CLEAR_UNLOCK |
952 EXTENT_CLEAR_DELALLOC |
953 EXTENT_CLEAR_DIRTY |
954 EXTENT_SET_WRITEBACK |
955 EXTENT_END_WRITEBACK);
956
957 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500958}
Chris Masonc8b97812008-10-29 14:49:59 -0400959
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000960static noinline int cow_file_range(struct inode *inode,
961 struct page *locked_page,
962 u64 start, u64 end, int *page_started,
963 unsigned long *nr_written,
964 int unlock)
965{
966 struct btrfs_trans_handle *trans;
967 struct btrfs_root *root = BTRFS_I(inode)->root;
968 int ret;
969
970 trans = btrfs_join_transaction(root);
971 if (IS_ERR(trans)) {
972 extent_clear_unlock_delalloc(inode,
973 &BTRFS_I(inode)->io_tree,
974 start, end, locked_page,
975 EXTENT_CLEAR_UNLOCK_PAGE |
976 EXTENT_CLEAR_UNLOCK |
977 EXTENT_CLEAR_DELALLOC |
978 EXTENT_CLEAR_DIRTY |
979 EXTENT_SET_WRITEBACK |
980 EXTENT_END_WRITEBACK);
981 return PTR_ERR(trans);
982 }
983 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
984
985 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
986 page_started, nr_written, unlock);
987
988 btrfs_end_transaction(trans, root);
989
990 return ret;
991}
992
Chris Mason771ed682008-11-06 22:02:51 -0500993/*
994 * work queue call back to started compression on a file and pages
995 */
996static noinline void async_cow_start(struct btrfs_work *work)
997{
998 struct async_cow *async_cow;
999 int num_added = 0;
1000 async_cow = container_of(work, struct async_cow, work);
1001
1002 compress_file_range(async_cow->inode, async_cow->locked_page,
1003 async_cow->start, async_cow->end, async_cow,
1004 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001005 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001006 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001007 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001008 }
Chris Mason771ed682008-11-06 22:02:51 -05001009}
1010
1011/*
1012 * work queue call back to submit previously compressed pages
1013 */
1014static noinline void async_cow_submit(struct btrfs_work *work)
1015{
1016 struct async_cow *async_cow;
1017 struct btrfs_root *root;
1018 unsigned long nr_pages;
1019
1020 async_cow = container_of(work, struct async_cow, work);
1021
1022 root = async_cow->root;
1023 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1024 PAGE_CACHE_SHIFT;
1025
Josef Bacik66657b32012-08-01 15:36:24 -04001026 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001027 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001028 waitqueue_active(&root->fs_info->async_submit_wait))
1029 wake_up(&root->fs_info->async_submit_wait);
1030
Chris Masond3977122009-01-05 21:25:51 -05001031 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001032 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001033}
Chris Masonc8b97812008-10-29 14:49:59 -04001034
Chris Mason771ed682008-11-06 22:02:51 -05001035static noinline void async_cow_free(struct btrfs_work *work)
1036{
1037 struct async_cow *async_cow;
1038 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001039 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001040 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001041 kfree(async_cow);
1042}
1043
1044static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1045 u64 start, u64 end, int *page_started,
1046 unsigned long *nr_written)
1047{
1048 struct async_cow *async_cow;
1049 struct btrfs_root *root = BTRFS_I(inode)->root;
1050 unsigned long nr_pages;
1051 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001052 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001053
Chris Masona3429ab2009-10-08 12:30:20 -04001054 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1055 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001056 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001057 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001058 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001059 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001060 async_cow->root = root;
1061 async_cow->locked_page = locked_page;
1062 async_cow->start = start;
1063
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001064 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001065 cur_end = end;
1066 else
1067 cur_end = min(end, start + 512 * 1024 - 1);
1068
1069 async_cow->end = cur_end;
1070 INIT_LIST_HEAD(&async_cow->extents);
1071
1072 async_cow->work.func = async_cow_start;
1073 async_cow->work.ordered_func = async_cow_submit;
1074 async_cow->work.ordered_free = async_cow_free;
1075 async_cow->work.flags = 0;
1076
Chris Mason771ed682008-11-06 22:02:51 -05001077 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1078 PAGE_CACHE_SHIFT;
1079 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1080
1081 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1082 &async_cow->work);
1083
1084 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1085 wait_event(root->fs_info->async_submit_wait,
1086 (atomic_read(&root->fs_info->async_delalloc_pages) <
1087 limit));
1088 }
1089
Chris Masond3977122009-01-05 21:25:51 -05001090 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001091 atomic_read(&root->fs_info->async_delalloc_pages)) {
1092 wait_event(root->fs_info->async_submit_wait,
1093 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1094 0));
1095 }
1096
1097 *nr_written += nr_pages;
1098 start = cur_end + 1;
1099 }
1100 *page_started = 1;
1101 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001102}
1103
Chris Masond3977122009-01-05 21:25:51 -05001104static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001105 u64 bytenr, u64 num_bytes)
1106{
1107 int ret;
1108 struct btrfs_ordered_sum *sums;
1109 LIST_HEAD(list);
1110
Yan Zheng07d400a2009-01-06 11:42:00 -05001111 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001112 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001113 if (ret == 0 && list_empty(&list))
1114 return 0;
1115
1116 while (!list_empty(&list)) {
1117 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1118 list_del(&sums->list);
1119 kfree(sums);
1120 }
1121 return 1;
1122}
1123
Chris Masond352ac62008-09-29 15:18:18 -04001124/*
1125 * when nowcow writeback call back. This checks for snapshots or COW copies
1126 * of the extents that exist in the file, and COWs the file as required.
1127 *
1128 * If no cow copies or snapshots exist, we write directly to the existing
1129 * blocks on disk
1130 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001131static noinline int run_delalloc_nocow(struct inode *inode,
1132 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001133 u64 start, u64 end, int *page_started, int force,
1134 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001135{
Chris Masonbe20aa92007-12-17 20:14:01 -05001136 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001137 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001138 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001139 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001140 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001141 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001142 u64 cow_start;
1143 u64 cur_offset;
1144 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001145 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001146 u64 disk_bytenr;
1147 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001148 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001149 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001150 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001151 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001152 int nocow;
1153 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001154 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001155 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001156
1157 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001158 if (!path) {
1159 extent_clear_unlock_delalloc(inode,
1160 &BTRFS_I(inode)->io_tree,
1161 start, end, locked_page,
1162 EXTENT_CLEAR_UNLOCK_PAGE |
1163 EXTENT_CLEAR_UNLOCK |
1164 EXTENT_CLEAR_DELALLOC |
1165 EXTENT_CLEAR_DIRTY |
1166 EXTENT_SET_WRITEBACK |
1167 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001168 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001169 }
Li Zefan82d59022011-04-20 10:33:24 +08001170
Liu Bo83eea1f2012-07-10 05:28:39 -06001171 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001172
1173 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001174 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001175 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001176 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001177
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001178 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001179 extent_clear_unlock_delalloc(inode,
1180 &BTRFS_I(inode)->io_tree,
1181 start, end, locked_page,
1182 EXTENT_CLEAR_UNLOCK_PAGE |
1183 EXTENT_CLEAR_UNLOCK |
1184 EXTENT_CLEAR_DELALLOC |
1185 EXTENT_CLEAR_DIRTY |
1186 EXTENT_SET_WRITEBACK |
1187 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001188 btrfs_free_path(path);
1189 return PTR_ERR(trans);
1190 }
1191
Josef Bacik74b21072011-04-13 12:02:53 -04001192 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001193
Yan Zheng80ff3852008-10-30 14:20:02 -04001194 cow_start = (u64)-1;
1195 cur_offset = start;
1196 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001197 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001198 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001199 if (ret < 0) {
1200 btrfs_abort_transaction(trans, root, ret);
1201 goto error;
1202 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001203 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1204 leaf = path->nodes[0];
1205 btrfs_item_key_to_cpu(leaf, &found_key,
1206 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001207 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 found_key.type == BTRFS_EXTENT_DATA_KEY)
1209 path->slots[0]--;
1210 }
1211 check_prev = 0;
1212next_slot:
1213 leaf = path->nodes[0];
1214 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1215 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001216 if (ret < 0) {
1217 btrfs_abort_transaction(trans, root, ret);
1218 goto error;
1219 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001220 if (ret > 0)
1221 break;
1222 leaf = path->nodes[0];
1223 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001224
Yan Zheng80ff3852008-10-30 14:20:02 -04001225 nocow = 0;
1226 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001227 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001229
Li Zefan33345d012011-04-20 10:31:50 +08001230 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001231 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1232 found_key.offset > end)
1233 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001234
Yan Zheng80ff3852008-10-30 14:20:02 -04001235 if (found_key.offset > cur_offset) {
1236 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001237 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 goto out_check;
1239 }
Chris Masonc31f8832008-01-08 15:46:31 -05001240
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 fi = btrfs_item_ptr(leaf, path->slots[0],
1242 struct btrfs_file_extent_item);
1243 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001244
Yan Zhengd899e052008-10-30 14:25:28 -04001245 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1246 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001248 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001249 extent_end = found_key.offset +
1250 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001251 disk_num_bytes =
1252 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001253 if (extent_end <= start) {
1254 path->slots[0]++;
1255 goto next_slot;
1256 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001257 if (disk_bytenr == 0)
1258 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 if (btrfs_file_extent_compression(leaf, fi) ||
1260 btrfs_file_extent_encryption(leaf, fi) ||
1261 btrfs_file_extent_other_encoding(leaf, fi))
1262 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001263 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1264 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001265 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001267 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001268 found_key.offset -
1269 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001270 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001271 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001272 disk_bytenr += cur_offset - found_key.offset;
1273 num_bytes = min(end + 1, extent_end) - cur_offset;
1274 /*
1275 * force cow if csum exists in the range.
1276 * this ensure that csum for a given extent are
1277 * either valid or do not exist.
1278 */
1279 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1280 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 nocow = 1;
1282 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1283 extent_end = found_key.offset +
1284 btrfs_file_extent_inline_len(leaf, fi);
1285 extent_end = ALIGN(extent_end, root->sectorsize);
1286 } else {
1287 BUG_ON(1);
1288 }
1289out_check:
1290 if (extent_end <= start) {
1291 path->slots[0]++;
1292 goto next_slot;
1293 }
1294 if (!nocow) {
1295 if (cow_start == (u64)-1)
1296 cow_start = cur_offset;
1297 cur_offset = extent_end;
1298 if (cur_offset > end)
1299 break;
1300 path->slots[0]++;
1301 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001302 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001303
David Sterbab3b4aa72011-04-21 01:20:15 +02001304 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001306 ret = __cow_file_range(trans, inode, root, locked_page,
1307 cow_start, found_key.offset - 1,
1308 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001309 if (ret) {
1310 btrfs_abort_transaction(trans, root, ret);
1311 goto error;
1312 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001314 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001315
Yan Zhengd899e052008-10-30 14:25:28 -04001316 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1317 struct extent_map *em;
1318 struct extent_map_tree *em_tree;
1319 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001320 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001321 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001322 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001323 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001324 em->len = num_bytes;
1325 em->block_len = num_bytes;
1326 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001327 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001328 em->bdev = root->fs_info->fs_devices->latest_bdev;
1329 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -06001330 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Yan Zhengd899e052008-10-30 14:25:28 -04001331 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001332 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001333 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001334 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001335 if (ret != -EEXIST) {
1336 free_extent_map(em);
1337 break;
1338 }
1339 btrfs_drop_extent_cache(inode, em->start,
1340 em->start + em->len - 1, 0);
1341 }
1342 type = BTRFS_ORDERED_PREALLOC;
1343 } else {
1344 type = BTRFS_ORDERED_NOCOW;
1345 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001346
1347 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001348 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001349 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001350
Yan, Zhengefa56462010-05-16 10:49:59 -04001351 if (root->root_key.objectid ==
1352 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1353 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1354 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001355 if (ret) {
1356 btrfs_abort_transaction(trans, root, ret);
1357 goto error;
1358 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001359 }
1360
Yan Zhengd899e052008-10-30 14:25:28 -04001361 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001362 cur_offset, cur_offset + num_bytes - 1,
1363 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1364 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1365 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 cur_offset = extent_end;
1367 if (cur_offset > end)
1368 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001369 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001370 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371
Josef Bacik17ca04a2012-05-31 15:58:55 -04001372 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001374 cur_offset = end;
1375 }
1376
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001378 ret = __cow_file_range(trans, inode, root, locked_page,
1379 cow_start, end,
1380 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001381 if (ret) {
1382 btrfs_abort_transaction(trans, root, ret);
1383 goto error;
1384 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 }
1386
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001387error:
Miao Xiea698d0752012-09-20 01:51:59 -06001388 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001389 if (!ret)
1390 ret = err;
1391
Josef Bacik17ca04a2012-05-31 15:58:55 -04001392 if (ret && cur_offset < end)
1393 extent_clear_unlock_delalloc(inode,
1394 &BTRFS_I(inode)->io_tree,
1395 cur_offset, end, locked_page,
1396 EXTENT_CLEAR_UNLOCK_PAGE |
1397 EXTENT_CLEAR_UNLOCK |
1398 EXTENT_CLEAR_DELALLOC |
1399 EXTENT_CLEAR_DIRTY |
1400 EXTENT_SET_WRITEBACK |
1401 EXTENT_END_WRITEBACK);
1402
Yan Zheng7ea394f2008-08-05 13:05:02 -04001403 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001404 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001405}
1406
Chris Masond352ac62008-09-29 15:18:18 -04001407/*
1408 * extent_io.c call back to do delayed allocation processing
1409 */
Chris Masonc8b97812008-10-29 14:49:59 -04001410static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001411 u64 start, u64 end, int *page_started,
1412 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001413{
Chris Masonbe20aa92007-12-17 20:14:01 -05001414 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001415 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001416
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001417 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001418 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001419 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001420 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001421 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001422 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001423 } else if (!btrfs_test_opt(root, COMPRESS) &&
1424 !(BTRFS_I(inode)->force_compress) &&
1425 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001426 ret = cow_file_range(inode, locked_page, start, end,
1427 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001428 } else {
1429 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1430 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001431 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001432 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001433 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001434 return ret;
1435}
1436
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001437static void btrfs_split_extent_hook(struct inode *inode,
1438 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001439{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001440 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001441 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001442 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001443
Josef Bacik9e0baf62011-07-15 15:16:44 +00001444 spin_lock(&BTRFS_I(inode)->lock);
1445 BTRFS_I(inode)->outstanding_extents++;
1446 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001447}
1448
1449/*
1450 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1451 * extents so we can keep track of new extents that are just merged onto old
1452 * extents, such as when we are doing sequential writes, so we can properly
1453 * account for the metadata space we'll need.
1454 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001455static void btrfs_merge_extent_hook(struct inode *inode,
1456 struct extent_state *new,
1457 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001458{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001459 /* not delalloc, ignore it */
1460 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001461 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001462
Josef Bacik9e0baf62011-07-15 15:16:44 +00001463 spin_lock(&BTRFS_I(inode)->lock);
1464 BTRFS_I(inode)->outstanding_extents--;
1465 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001466}
1467
Chris Masond352ac62008-09-29 15:18:18 -04001468/*
1469 * extent_io.c set_bit_hook, used to track delayed allocation
1470 * bytes in this file, and to maintain the list of inodes that
1471 * have pending delalloc work to be done.
1472 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001473static void btrfs_set_bit_hook(struct inode *inode,
1474 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001475{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001476
Chris Mason75eff682008-12-15 15:54:40 -05001477 /*
1478 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001479 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001480 * bit, which is only set or cleared with irqs on
1481 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001482 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001483 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001484 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001485 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001486
Josef Bacik9e0baf62011-07-15 15:16:44 +00001487 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001488 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001489 } else {
1490 spin_lock(&BTRFS_I(inode)->lock);
1491 BTRFS_I(inode)->outstanding_extents++;
1492 spin_unlock(&BTRFS_I(inode)->lock);
1493 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001494
Chris Mason75eff682008-12-15 15:54:40 -05001495 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001496 BTRFS_I(inode)->delalloc_bytes += len;
1497 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001498 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001499 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1500 &root->fs_info->delalloc_inodes);
1501 }
Chris Mason75eff682008-12-15 15:54:40 -05001502 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001503 }
Chris Mason291d6732008-01-29 15:55:23 -05001504}
1505
Chris Masond352ac62008-09-29 15:18:18 -04001506/*
1507 * extent_io.c clear_bit_hook, see set_bit_hook for why
1508 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001509static void btrfs_clear_bit_hook(struct inode *inode,
1510 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001511{
Chris Mason75eff682008-12-15 15:54:40 -05001512 /*
1513 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001514 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001515 * bit, which is only set or cleared with irqs on
1516 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001517 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001518 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001519 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001520 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001521
Josef Bacik9e0baf62011-07-15 15:16:44 +00001522 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001523 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001524 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1525 spin_lock(&BTRFS_I(inode)->lock);
1526 BTRFS_I(inode)->outstanding_extents--;
1527 spin_unlock(&BTRFS_I(inode)->lock);
1528 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001529
1530 if (*bits & EXTENT_DO_ACCOUNTING)
1531 btrfs_delalloc_release_metadata(inode, len);
1532
Josef Bacik0cb59c92010-07-02 12:14:14 -04001533 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1534 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001535 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001536
Chris Mason75eff682008-12-15 15:54:40 -05001537 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001538 root->fs_info->delalloc_bytes -= len;
1539 BTRFS_I(inode)->delalloc_bytes -= len;
1540
Josef Bacik0cb59c92010-07-02 12:14:14 -04001541 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001542 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1543 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1544 }
Chris Mason75eff682008-12-15 15:54:40 -05001545 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001546 }
Chris Mason291d6732008-01-29 15:55:23 -05001547}
1548
Chris Masond352ac62008-09-29 15:18:18 -04001549/*
1550 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1551 * we don't create bios that span stripes or chunks
1552 */
Chris Mason239b14b2008-03-24 15:02:07 -04001553int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001554 size_t size, struct bio *bio,
1555 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001556{
1557 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001558 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001559 u64 length = 0;
1560 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001561 int ret;
1562
Chris Mason771ed682008-11-06 22:02:51 -05001563 if (bio_flags & EXTENT_BIO_COMPRESSED)
1564 return 0;
1565
Chris Masonf2d8d742008-04-21 10:03:05 -04001566 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001567 map_length = length;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001568 ret = btrfs_map_block(root->fs_info, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001569 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001570 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001571 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001572 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001573 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001574 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001575}
1576
Chris Masond352ac62008-09-29 15:18:18 -04001577/*
1578 * in order to insert checksums into the metadata in large chunks,
1579 * we wait until bio submission time. All the pages in the bio are
1580 * checksummed and sums are attached onto the ordered extent record.
1581 *
1582 * At IO completion time the cums attached on the ordered extent record
1583 * are inserted into the btree
1584 */
Chris Masond3977122009-01-05 21:25:51 -05001585static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1586 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001587 unsigned long bio_flags,
1588 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001589{
Chris Mason065631f2008-02-20 12:07:25 -05001590 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001591 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001592
Chris Masond20f7042008-12-08 16:58:54 -05001593 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001594 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001595 return 0;
1596}
Chris Masone0156402008-04-16 11:15:20 -04001597
Chris Mason4a69a412008-11-06 22:03:00 -05001598/*
1599 * in order to insert checksums into the metadata in large chunks,
1600 * we wait until bio submission time. All the pages in the bio are
1601 * checksummed and sums are attached onto the ordered extent record.
1602 *
1603 * At IO completion time the cums attached on the ordered extent record
1604 * are inserted into the btree
1605 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001606static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001607 int mirror_num, unsigned long bio_flags,
1608 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001609{
1610 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001611 int ret;
1612
1613 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1614 if (ret)
1615 bio_endio(bio, ret);
1616 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001617}
1618
Chris Masond352ac62008-09-29 15:18:18 -04001619/*
Chris Masoncad321a2008-12-17 14:51:42 -05001620 * extent_io.c submission hook. This does the right thing for csum calculation
1621 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001622 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001623static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001624 int mirror_num, unsigned long bio_flags,
1625 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001626{
1627 struct btrfs_root *root = BTRFS_I(inode)->root;
1628 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001629 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001630 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001631 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001632
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001633 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001634
Liu Bo83eea1f2012-07-10 05:28:39 -06001635 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001636 metadata = 2;
1637
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001638 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001639 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1640 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001641 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001642
Chris Masond20f7042008-12-08 16:58:54 -05001643 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001644 ret = btrfs_submit_compressed_read(inode, bio,
1645 mirror_num,
1646 bio_flags);
1647 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001648 } else if (!skip_sum) {
1649 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1650 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001651 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001652 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001653 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001654 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001655 /* csum items have already been cloned */
1656 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1657 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001658 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001659 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001660 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001661 bio_flags, bio_offset,
1662 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001663 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001664 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001665 } else if (!skip_sum) {
1666 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1667 if (ret)
1668 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001669 }
1670
Chris Mason0b86a832008-03-24 15:01:56 -04001671mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001672 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1673
1674out:
1675 if (ret < 0)
1676 bio_endio(bio, ret);
1677 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001678}
Chris Mason6885f302008-02-20 16:11:05 -05001679
Chris Masond352ac62008-09-29 15:18:18 -04001680/*
1681 * given a list of ordered sums record them in the inode. This happens
1682 * at IO completion time based on sums calculated at bio submission time.
1683 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001684static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001685 struct inode *inode, u64 file_offset,
1686 struct list_head *list)
1687{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001688 struct btrfs_ordered_sum *sum;
1689
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001690 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001691 btrfs_csum_file_blocks(trans,
1692 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001693 }
1694 return 0;
1695}
1696
Josef Bacik2ac55d42010-02-03 19:33:23 +00001697int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1698 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001699{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001700 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001701 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001702 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001703}
1704
Chris Masond352ac62008-09-29 15:18:18 -04001705/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001706struct btrfs_writepage_fixup {
1707 struct page *page;
1708 struct btrfs_work work;
1709};
1710
Christoph Hellwigb2950862008-12-02 09:54:17 -05001711static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001712{
1713 struct btrfs_writepage_fixup *fixup;
1714 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001715 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001716 struct page *page;
1717 struct inode *inode;
1718 u64 page_start;
1719 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001720 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001721
1722 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1723 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001724again:
Chris Mason247e7432008-07-17 12:53:51 -04001725 lock_page(page);
1726 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1727 ClearPageChecked(page);
1728 goto out_page;
1729 }
1730
1731 inode = page->mapping->host;
1732 page_start = page_offset(page);
1733 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1734
Josef Bacik2ac55d42010-02-03 19:33:23 +00001735 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001736 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001737
1738 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001739 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001740 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001741
1742 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1743 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001744 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1745 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001746 unlock_page(page);
1747 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001748 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001749 goto again;
1750 }
Chris Mason247e7432008-07-17 12:53:51 -04001751
Jeff Mahoney87826df2012-02-15 16:23:57 +01001752 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1753 if (ret) {
1754 mapping_set_error(page->mapping, ret);
1755 end_extent_writepage(page, ret, page_start, page_end);
1756 ClearPageChecked(page);
1757 goto out;
1758 }
1759
Josef Bacik2ac55d42010-02-03 19:33:23 +00001760 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001761 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001762 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001763out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001764 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1765 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001766out_page:
1767 unlock_page(page);
1768 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001769 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001770}
1771
1772/*
1773 * There are a few paths in the higher layers of the kernel that directly
1774 * set the page dirty bit without asking the filesystem if it is a
1775 * good idea. This causes problems because we want to make sure COW
1776 * properly happens and the data=ordered rules are followed.
1777 *
Chris Masonc8b97812008-10-29 14:49:59 -04001778 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001779 * hasn't been properly setup for IO. We kick off an async process
1780 * to fix it up. The async helper will wait for ordered extents, set
1781 * the delalloc bit and make it safe to write the page.
1782 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001783static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001784{
1785 struct inode *inode = page->mapping->host;
1786 struct btrfs_writepage_fixup *fixup;
1787 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001788
Chris Mason8b62b722009-09-02 16:53:46 -04001789 /* this page is properly in the ordered list */
1790 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001791 return 0;
1792
1793 if (PageChecked(page))
1794 return -EAGAIN;
1795
1796 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1797 if (!fixup)
1798 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001799
Chris Mason247e7432008-07-17 12:53:51 -04001800 SetPageChecked(page);
1801 page_cache_get(page);
1802 fixup->work.func = btrfs_writepage_fixup_worker;
1803 fixup->page = page;
1804 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001805 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001806}
1807
Yan Zhengd899e052008-10-30 14:25:28 -04001808static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1809 struct inode *inode, u64 file_pos,
1810 u64 disk_bytenr, u64 disk_num_bytes,
1811 u64 num_bytes, u64 ram_bytes,
1812 u8 compression, u8 encryption,
1813 u16 other_encoding, int extent_type)
1814{
1815 struct btrfs_root *root = BTRFS_I(inode)->root;
1816 struct btrfs_file_extent_item *fi;
1817 struct btrfs_path *path;
1818 struct extent_buffer *leaf;
1819 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001820 int ret;
1821
1822 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001823 if (!path)
1824 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001825
Chris Masonb9473432009-03-13 11:00:37 -04001826 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001827
1828 /*
1829 * we may be replacing one extent in the tree with another.
1830 * The new extent is pinned in the extent map, and we don't want
1831 * to drop it from the cache until it is completely in the btree.
1832 *
1833 * So, tell btrfs_drop_extents to leave this extent in the cache.
1834 * the caller is expected to unpin it and allow it to be merged
1835 * with the others.
1836 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001837 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001838 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001839 if (ret)
1840 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001841
Li Zefan33345d012011-04-20 10:31:50 +08001842 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001843 ins.offset = file_pos;
1844 ins.type = BTRFS_EXTENT_DATA_KEY;
1845 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001846 if (ret)
1847 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001848 leaf = path->nodes[0];
1849 fi = btrfs_item_ptr(leaf, path->slots[0],
1850 struct btrfs_file_extent_item);
1851 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1852 btrfs_set_file_extent_type(leaf, fi, extent_type);
1853 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1854 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1855 btrfs_set_file_extent_offset(leaf, fi, 0);
1856 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1857 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1858 btrfs_set_file_extent_compression(leaf, fi, compression);
1859 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1860 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001861
Yan Zhengd899e052008-10-30 14:25:28 -04001862 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001863 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001864
1865 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001866
1867 ins.objectid = disk_bytenr;
1868 ins.offset = disk_num_bytes;
1869 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870 ret = btrfs_alloc_reserved_file_extent(trans, root,
1871 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001872 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001873out:
Yan Zhengd899e052008-10-30 14:25:28 -04001874 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001875
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001876 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001877}
1878
Chris Mason5d13a982009-03-13 11:41:46 -04001879/*
1880 * helper function for btrfs_finish_ordered_io, this
1881 * just reads in some of the csum leaves to prime them into ram
1882 * before we start the transaction. It limits the amount of btree
1883 * reads required while inside the transaction.
1884 */
Chris Masond352ac62008-09-29 15:18:18 -04001885/* as ordered data IO finishes, this gets called so we can finish
1886 * an ordered extent if the range of bytes in the file it covers are
1887 * fully written.
1888 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001889static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001890{
Josef Bacik5fd02042012-05-02 14:00:54 -04001891 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001892 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001893 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001894 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001895 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001896 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001897 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001898 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001899
Liu Bo83eea1f2012-07-10 05:28:39 -06001900 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001901
Josef Bacik5fd02042012-05-02 14:00:54 -04001902 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1903 ret = -EIO;
1904 goto out;
1905 }
1906
Yan, Zhengc2167752009-11-12 09:34:21 +00001907 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001908 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Yan, Zhengc2167752009-11-12 09:34:21 +00001909 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1910 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001911 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001912 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001913 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001914 trans = btrfs_join_transaction(root);
Liu Bod280e5b2012-08-21 21:13:25 -06001915 if (IS_ERR(trans)) {
1916 ret = PTR_ERR(trans);
1917 trans = NULL;
1918 goto out;
1919 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001920 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason21151332011-11-10 20:39:08 -05001921 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001922 if (ret) /* -ENOMEM or corruption */
1923 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001924 }
1925 goto out;
1926 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001927
Josef Bacik2ac55d42010-02-03 19:33:23 +00001928 lock_extent_bits(io_tree, ordered_extent->file_offset,
1929 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001930 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001931
Josef Bacik0cb59c92010-07-02 12:14:14 -04001932 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001933 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001934 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001935 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001936 if (IS_ERR(trans)) {
1937 ret = PTR_ERR(trans);
1938 trans = NULL;
1939 goto out_unlock;
1940 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001941 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001942
Chris Masonc8b97812008-10-29 14:49:59 -04001943 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001944 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001945 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001946 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001947 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001948 ordered_extent->file_offset,
1949 ordered_extent->file_offset +
1950 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001951 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001952 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001953 ret = insert_reserved_file_extent(trans, inode,
1954 ordered_extent->file_offset,
1955 ordered_extent->start,
1956 ordered_extent->disk_len,
1957 ordered_extent->len,
1958 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001959 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001960 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04001961 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04001962 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1963 ordered_extent->file_offset, ordered_extent->len,
1964 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001965 if (ret < 0) {
1966 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001967 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001968 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001969
Chris Masone6dcd2d2008-07-17 12:53:50 -04001970 add_pending_csums(trans, inode, ordered_extent->file_offset,
1971 &ordered_extent->list);
1972
Josef Bacik1ef30be2011-04-05 19:25:36 -04001973 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Miao Xiea39f7522011-09-11 10:52:25 -04001974 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Chris Mason21151332011-11-10 20:39:08 -05001975 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001976 if (ret) { /* -ENOMEM or corruption */
1977 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001978 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001979 }
Josef Bacik7c735312012-08-13 15:43:26 -04001980 } else {
1981 btrfs_set_inode_last_trans(trans, inode);
Josef Bacik1ef30be2011-04-05 19:25:36 -04001982 }
1983 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04001984out_unlock:
1985 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1986 ordered_extent->file_offset +
1987 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00001988out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001989 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001990 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06001991 if (trans)
1992 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001993
Josef Bacik5fd02042012-05-02 14:00:54 -04001994 if (ret)
1995 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
1996 ordered_extent->file_offset +
1997 ordered_extent->len - 1, NULL, GFP_NOFS);
1998
1999 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002000 * This needs to be done to make sure anybody waiting knows we are done
2001 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002002 */
2003 btrfs_remove_ordered_extent(inode, ordered_extent);
2004
Chris Masone6dcd2d2008-07-17 12:53:50 -04002005 /* once for us */
2006 btrfs_put_ordered_extent(ordered_extent);
2007 /* once for the tree */
2008 btrfs_put_ordered_extent(ordered_extent);
2009
Josef Bacik5fd02042012-05-02 14:00:54 -04002010 return ret;
2011}
2012
2013static void finish_ordered_fn(struct btrfs_work *work)
2014{
2015 struct btrfs_ordered_extent *ordered_extent;
2016 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2017 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002018}
2019
Christoph Hellwigb2950862008-12-02 09:54:17 -05002020static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002021 struct extent_state *state, int uptodate)
2022{
Josef Bacik5fd02042012-05-02 14:00:54 -04002023 struct inode *inode = page->mapping->host;
2024 struct btrfs_root *root = BTRFS_I(inode)->root;
2025 struct btrfs_ordered_extent *ordered_extent = NULL;
2026 struct btrfs_workers *workers;
2027
liubo1abe9b82011-03-24 11:18:59 +00002028 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2029
Chris Mason8b62b722009-09-02 16:53:46 -04002030 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002031 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2032 end - start + 1, uptodate))
2033 return 0;
2034
2035 ordered_extent->work.func = finish_ordered_fn;
2036 ordered_extent->work.flags = 0;
2037
Liu Bo83eea1f2012-07-10 05:28:39 -06002038 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002039 workers = &root->fs_info->endio_freespace_worker;
2040 else
2041 workers = &root->fs_info->endio_write_workers;
2042 btrfs_queue_worker(workers, &ordered_extent->work);
2043
2044 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002045}
2046
Chris Masond352ac62008-09-29 15:18:18 -04002047/*
Chris Masond352ac62008-09-29 15:18:18 -04002048 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002049 * if there's a match, we allow the bio to finish. If not, the code in
2050 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002051 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002052static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002053 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002054{
Chris Mason35ebb932007-10-30 16:56:53 -04002055 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04002056 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002057 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002058 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002059 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002060 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002061 struct btrfs_root *root = BTRFS_I(inode)->root;
2062 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002063
Chris Masond20f7042008-12-08 16:58:54 -05002064 if (PageChecked(page)) {
2065 ClearPageChecked(page);
2066 goto good;
2067 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002068
2069 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002070 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002071
Yan Zheng17d217f2008-12-12 10:03:38 -05002072 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002073 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002074 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2075 GFP_NOFS);
2076 return 0;
2077 }
2078
Yanc2e639f2008-02-04 08:57:25 -05002079 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002080 private = state->private;
2081 ret = 0;
2082 } else {
2083 ret = get_state_private(io_tree, start, &private);
2084 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002085 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002086 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002087 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002088
Chris Masonff79f812007-10-15 16:22:25 -04002089 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2090 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002091 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002092 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002093
Cong Wang7ac687d2011-11-25 23:14:28 +08002094 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002095good:
Chris Mason07157aa2007-08-30 08:50:51 -04002096 return 0;
2097
2098zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002099 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002100 "private %llu\n",
2101 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002102 (unsigned long long)start, csum,
2103 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002104 memset(kaddr + offset, 1, end - start + 1);
2105 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002106 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002107 if (private == 0)
2108 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002109 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002110}
Chris Masonb888db2b2007-08-27 16:49:44 -04002111
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002112struct delayed_iput {
2113 struct list_head list;
2114 struct inode *inode;
2115};
2116
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002117/* JDM: If this is fs-wide, why can't we add a pointer to
2118 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002119void btrfs_add_delayed_iput(struct inode *inode)
2120{
2121 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2122 struct delayed_iput *delayed;
2123
2124 if (atomic_add_unless(&inode->i_count, -1, 1))
2125 return;
2126
2127 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2128 delayed->inode = inode;
2129
2130 spin_lock(&fs_info->delayed_iput_lock);
2131 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2132 spin_unlock(&fs_info->delayed_iput_lock);
2133}
2134
2135void btrfs_run_delayed_iputs(struct btrfs_root *root)
2136{
2137 LIST_HEAD(list);
2138 struct btrfs_fs_info *fs_info = root->fs_info;
2139 struct delayed_iput *delayed;
2140 int empty;
2141
2142 spin_lock(&fs_info->delayed_iput_lock);
2143 empty = list_empty(&fs_info->delayed_iputs);
2144 spin_unlock(&fs_info->delayed_iput_lock);
2145 if (empty)
2146 return;
2147
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002148 spin_lock(&fs_info->delayed_iput_lock);
2149 list_splice_init(&fs_info->delayed_iputs, &list);
2150 spin_unlock(&fs_info->delayed_iput_lock);
2151
2152 while (!list_empty(&list)) {
2153 delayed = list_entry(list.next, struct delayed_iput, list);
2154 list_del(&delayed->list);
2155 iput(delayed->inode);
2156 kfree(delayed);
2157 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002158}
2159
Yan, Zhengd68fc572010-05-16 10:49:58 -04002160enum btrfs_orphan_cleanup_state {
2161 ORPHAN_CLEANUP_STARTED = 1,
2162 ORPHAN_CLEANUP_DONE = 2,
2163};
2164
2165/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002166 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002167 * files in the subvolume, it removes orphan item and frees block_rsv
2168 * structure.
2169 */
2170void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2171 struct btrfs_root *root)
2172{
Josef Bacik90290e12011-12-02 15:44:12 -05002173 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002174 int ret;
2175
Josef Bacik8a35d952012-05-23 14:26:42 -04002176 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002177 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2178 return;
2179
Josef Bacik90290e12011-12-02 15:44:12 -05002180 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002181 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002182 spin_unlock(&root->orphan_lock);
2183 return;
2184 }
2185
2186 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2187 spin_unlock(&root->orphan_lock);
2188 return;
2189 }
2190
2191 block_rsv = root->orphan_block_rsv;
2192 root->orphan_block_rsv = NULL;
2193 spin_unlock(&root->orphan_lock);
2194
Yan, Zhengd68fc572010-05-16 10:49:58 -04002195 if (root->orphan_item_inserted &&
2196 btrfs_root_refs(&root->root_item) > 0) {
2197 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2198 root->root_key.objectid);
2199 BUG_ON(ret);
2200 root->orphan_item_inserted = 0;
2201 }
2202
Josef Bacik90290e12011-12-02 15:44:12 -05002203 if (block_rsv) {
2204 WARN_ON(block_rsv->size > 0);
2205 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002206 }
2207}
2208
2209/*
Josef Bacik7b128762008-07-24 12:17:14 -04002210 * This creates an orphan entry for the given inode in case something goes
2211 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002212 *
2213 * NOTE: caller of this function should reserve 5 units of metadata for
2214 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002215 */
2216int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2217{
2218 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002219 struct btrfs_block_rsv *block_rsv = NULL;
2220 int reserve = 0;
2221 int insert = 0;
2222 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002223
Yan, Zhengd68fc572010-05-16 10:49:58 -04002224 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002225 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002226 if (!block_rsv)
2227 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002228 }
2229
Yan, Zhengd68fc572010-05-16 10:49:58 -04002230 spin_lock(&root->orphan_lock);
2231 if (!root->orphan_block_rsv) {
2232 root->orphan_block_rsv = block_rsv;
2233 } else if (block_rsv) {
2234 btrfs_free_block_rsv(root, block_rsv);
2235 block_rsv = NULL;
2236 }
Josef Bacik7b128762008-07-24 12:17:14 -04002237
Josef Bacik8a35d952012-05-23 14:26:42 -04002238 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2239 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002240#if 0
2241 /*
2242 * For proper ENOSPC handling, we should do orphan
2243 * cleanup when mounting. But this introduces backward
2244 * compatibility issue.
2245 */
2246 if (!xchg(&root->orphan_item_inserted, 1))
2247 insert = 2;
2248 else
2249 insert = 1;
2250#endif
2251 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002252 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002253 }
Josef Bacik7b128762008-07-24 12:17:14 -04002254
Josef Bacik72ac3c02012-05-23 14:13:11 -04002255 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2256 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002257 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002258 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002259
Yan, Zhengd68fc572010-05-16 10:49:58 -04002260 /* grab metadata reservation from transaction handle */
2261 if (reserve) {
2262 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002263 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002264 }
2265
2266 /* insert an orphan item to track this unlinked/truncated file */
2267 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002268 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002269 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002270 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2271 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002272 btrfs_abort_transaction(trans, root, ret);
2273 return ret;
2274 }
2275 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002276 }
2277
2278 /* insert an orphan item to track subvolume contains orphan files */
2279 if (insert >= 2) {
2280 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2281 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002282 if (ret && ret != -EEXIST) {
2283 btrfs_abort_transaction(trans, root, ret);
2284 return ret;
2285 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002286 }
2287 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002288}
2289
2290/*
2291 * We have done the truncate/delete so we can go ahead and remove the orphan
2292 * item for this particular inode.
2293 */
2294int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2295{
2296 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002297 int delete_item = 0;
2298 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002299 int ret = 0;
2300
Yan, Zhengd68fc572010-05-16 10:49:58 -04002301 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002302 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2303 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002304 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002305
Josef Bacik72ac3c02012-05-23 14:13:11 -04002306 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2307 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002308 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002309 spin_unlock(&root->orphan_lock);
2310
2311 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002312 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002313 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002314 }
2315
Josef Bacik8a35d952012-05-23 14:26:42 -04002316 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002317 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002318 atomic_dec(&root->orphan_inodes);
2319 }
Josef Bacik7b128762008-07-24 12:17:14 -04002320
Yan, Zhengd68fc572010-05-16 10:49:58 -04002321 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002322}
2323
2324/*
2325 * this cleans up any orphans that may be left on the list from the last use
2326 * of this root.
2327 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002328int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002329{
2330 struct btrfs_path *path;
2331 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002332 struct btrfs_key key, found_key;
2333 struct btrfs_trans_handle *trans;
2334 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002335 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002336 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2337
Yan, Zhengd68fc572010-05-16 10:49:58 -04002338 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002339 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002340
2341 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002342 if (!path) {
2343 ret = -ENOMEM;
2344 goto out;
2345 }
Josef Bacik7b128762008-07-24 12:17:14 -04002346 path->reada = -1;
2347
2348 key.objectid = BTRFS_ORPHAN_OBJECTID;
2349 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2350 key.offset = (u64)-1;
2351
Josef Bacik7b128762008-07-24 12:17:14 -04002352 while (1) {
2353 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002354 if (ret < 0)
2355 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002356
2357 /*
2358 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002359 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002360 * find the key and see if we have stuff that matches
2361 */
2362 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002363 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002364 if (path->slots[0] == 0)
2365 break;
2366 path->slots[0]--;
2367 }
2368
2369 /* pull out the item */
2370 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002371 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2372
2373 /* make sure the item matches what we want */
2374 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2375 break;
2376 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2377 break;
2378
2379 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002380 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002381
2382 /*
2383 * this is where we are basically btrfs_lookup, without the
2384 * crossing root thing. we store the inode number in the
2385 * offset of the orphan item.
2386 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002387
2388 if (found_key.offset == last_objectid) {
2389 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2390 "stopping orphan cleanup\n");
2391 ret = -EINVAL;
2392 goto out;
2393 }
2394
2395 last_objectid = found_key.offset;
2396
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002397 found_key.objectid = found_key.offset;
2398 found_key.type = BTRFS_INODE_ITEM_KEY;
2399 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002400 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002401 ret = PTR_RET(inode);
2402 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002403 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002404
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002405 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2406 struct btrfs_root *dead_root;
2407 struct btrfs_fs_info *fs_info = root->fs_info;
2408 int is_dead_root = 0;
2409
2410 /*
2411 * this is an orphan in the tree root. Currently these
2412 * could come from 2 sources:
2413 * a) a snapshot deletion in progress
2414 * b) a free space cache inode
2415 * We need to distinguish those two, as the snapshot
2416 * orphan must not get deleted.
2417 * find_dead_roots already ran before us, so if this
2418 * is a snapshot deletion, we should find the root
2419 * in the dead_roots list
2420 */
2421 spin_lock(&fs_info->trans_lock);
2422 list_for_each_entry(dead_root, &fs_info->dead_roots,
2423 root_list) {
2424 if (dead_root->root_key.objectid ==
2425 found_key.objectid) {
2426 is_dead_root = 1;
2427 break;
2428 }
2429 }
2430 spin_unlock(&fs_info->trans_lock);
2431 if (is_dead_root) {
2432 /* prevent this orphan from being found again */
2433 key.offset = found_key.objectid - 1;
2434 continue;
2435 }
2436 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002437 /*
2438 * Inode is already gone but the orphan item is still there,
2439 * kill the orphan item.
2440 */
2441 if (ret == -ESTALE) {
2442 trans = btrfs_start_transaction(root, 1);
2443 if (IS_ERR(trans)) {
2444 ret = PTR_ERR(trans);
2445 goto out;
2446 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002447 printk(KERN_ERR "auto deleting %Lu\n",
2448 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002449 ret = btrfs_del_orphan_item(trans, root,
2450 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002451 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002452 btrfs_end_transaction(trans, root);
2453 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002454 }
Josef Bacik7b128762008-07-24 12:17:14 -04002455
Josef Bacik7b128762008-07-24 12:17:14 -04002456 /*
2457 * add this inode to the orphan list so btrfs_orphan_del does
2458 * the proper thing when we hit it
2459 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002460 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2461 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002462
Josef Bacik7b128762008-07-24 12:17:14 -04002463 /* if we have links, this was a truncate, lets do that */
2464 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002465 if (!S_ISREG(inode->i_mode)) {
2466 WARN_ON(1);
2467 iput(inode);
2468 continue;
2469 }
Josef Bacik7b128762008-07-24 12:17:14 -04002470 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002471 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002472 } else {
2473 nr_unlink++;
2474 }
2475
2476 /* this will do delete_inode and everything for us */
2477 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002478 if (ret)
2479 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002480 }
Miao Xie3254c872011-11-10 20:45:05 -05002481 /* release the path since we're done with it */
2482 btrfs_release_path(path);
2483
Yan, Zhengd68fc572010-05-16 10:49:58 -04002484 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2485
2486 if (root->orphan_block_rsv)
2487 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2488 (u64)-1);
2489
2490 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002491 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002492 if (!IS_ERR(trans))
2493 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002494 }
Josef Bacik7b128762008-07-24 12:17:14 -04002495
2496 if (nr_unlink)
2497 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2498 if (nr_truncate)
2499 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002500
2501out:
2502 if (ret)
2503 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2504 btrfs_free_path(path);
2505 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002506}
2507
Chris Masond352ac62008-09-29 15:18:18 -04002508/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002509 * very simple check to peek ahead in the leaf looking for xattrs. If we
2510 * don't find any xattrs, we know there can't be any acls.
2511 *
2512 * slot is the slot the inode is in, objectid is the objectid of the inode
2513 */
2514static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2515 int slot, u64 objectid)
2516{
2517 u32 nritems = btrfs_header_nritems(leaf);
2518 struct btrfs_key found_key;
2519 int scanned = 0;
2520
2521 slot++;
2522 while (slot < nritems) {
2523 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2524
2525 /* we found a different objectid, there must not be acls */
2526 if (found_key.objectid != objectid)
2527 return 0;
2528
2529 /* we found an xattr, assume we've got an acl */
2530 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2531 return 1;
2532
2533 /*
2534 * we found a key greater than an xattr key, there can't
2535 * be any acls later on
2536 */
2537 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2538 return 0;
2539
2540 slot++;
2541 scanned++;
2542
2543 /*
2544 * it goes inode, inode backrefs, xattrs, extents,
2545 * so if there are a ton of hard links to an inode there can
2546 * be a lot of backrefs. Don't waste time searching too hard,
2547 * this is just an optimization
2548 */
2549 if (scanned >= 8)
2550 break;
2551 }
2552 /* we hit the end of the leaf before we found an xattr or
2553 * something larger than an xattr. We have to assume the inode
2554 * has acls
2555 */
2556 return 1;
2557}
2558
2559/*
Chris Masond352ac62008-09-29 15:18:18 -04002560 * read an inode from the btree into the in-memory inode
2561 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002562static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002563{
2564 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002565 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002566 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002567 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002568 struct btrfs_root *root = BTRFS_I(inode)->root;
2569 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002570 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002571 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002572 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002573 bool filled = false;
2574
2575 ret = btrfs_fill_inode(inode, &rdev);
2576 if (!ret)
2577 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002578
2579 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002580 if (!path)
2581 goto make_bad;
2582
Josef Bacikd90c7322011-05-17 09:50:54 -04002583 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002584 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002585
Chris Mason39279cc2007-06-12 06:35:45 -04002586 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002587 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002588 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002589
Chris Mason5f39d392007-10-15 16:14:19 -04002590 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002591
2592 if (filled)
2593 goto cache_acl;
2594
Chris Mason5f39d392007-10-15 16:14:19 -04002595 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2596 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002597 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002598 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002599 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2600 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002601 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002602
2603 tspec = btrfs_inode_atime(inode_item);
2604 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2605 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2606
2607 tspec = btrfs_inode_mtime(inode_item);
2608 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2609 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2610
2611 tspec = btrfs_inode_ctime(inode_item);
2612 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2613 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2614
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002615 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002616 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002617 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2618
2619 /*
2620 * If we were modified in the current generation and evicted from memory
2621 * and then re-read we need to do a full sync since we don't have any
2622 * idea about which extents were modified before we were evicted from
2623 * cache.
2624 */
2625 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2626 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2627 &BTRFS_I(inode)->runtime_flags);
2628
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002629 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002630 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002631 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002632 rdev = btrfs_inode_rdev(leaf, inode_item);
2633
Josef Bacikaec74772008-07-24 12:12:38 -04002634 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002635 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002636cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002637 /*
2638 * try to precache a NULL acl entry for files that don't have
2639 * any xattrs or acls
2640 */
Li Zefan33345d012011-04-20 10:31:50 +08002641 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2642 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002643 if (!maybe_acls)
2644 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002645
Chris Mason39279cc2007-06-12 06:35:45 -04002646 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002647
Chris Mason39279cc2007-06-12 06:35:45 -04002648 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002649 case S_IFREG:
2650 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002651 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002652 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002653 inode->i_fop = &btrfs_file_operations;
2654 inode->i_op = &btrfs_file_inode_operations;
2655 break;
2656 case S_IFDIR:
2657 inode->i_fop = &btrfs_dir_file_operations;
2658 if (root == root->fs_info->tree_root)
2659 inode->i_op = &btrfs_dir_ro_inode_operations;
2660 else
2661 inode->i_op = &btrfs_dir_inode_operations;
2662 break;
2663 case S_IFLNK:
2664 inode->i_op = &btrfs_symlink_inode_operations;
2665 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002666 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002667 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002668 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002669 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002670 init_special_inode(inode, inode->i_mode, rdev);
2671 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002672 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002673
2674 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002675 return;
2676
2677make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002678 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002679 make_bad_inode(inode);
2680}
2681
Chris Masond352ac62008-09-29 15:18:18 -04002682/*
2683 * given a leaf and an inode, copy the inode fields into the leaf
2684 */
Chris Masone02119d2008-09-05 16:13:11 -04002685static void fill_inode_item(struct btrfs_trans_handle *trans,
2686 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002687 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002688 struct inode *inode)
2689{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002690 btrfs_set_inode_uid(leaf, item, i_uid_read(inode));
2691 btrfs_set_inode_gid(leaf, item, i_gid_read(inode));
Chris Masondbe674a2008-07-17 12:54:05 -04002692 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002693 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2694 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2695
2696 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2697 inode->i_atime.tv_sec);
2698 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2699 inode->i_atime.tv_nsec);
2700
2701 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2702 inode->i_mtime.tv_sec);
2703 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2704 inode->i_mtime.tv_nsec);
2705
2706 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2707 inode->i_ctime.tv_sec);
2708 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2709 inode->i_ctime.tv_nsec);
2710
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002711 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002712 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002713 btrfs_set_inode_sequence(leaf, item, inode->i_version);
Chris Masone02119d2008-09-05 16:13:11 -04002714 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002715 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002716 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04002717 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002718}
2719
Chris Masond352ac62008-09-29 15:18:18 -04002720/*
2721 * copy everything in the in-memory inode into the btree.
2722 */
Chris Mason21151332011-11-10 20:39:08 -05002723static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002724 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002725{
2726 struct btrfs_inode_item *inode_item;
2727 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002728 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002729 int ret;
2730
2731 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002732 if (!path)
2733 return -ENOMEM;
2734
Chris Masonb9473432009-03-13 11:00:37 -04002735 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002736 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2737 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002738 if (ret) {
2739 if (ret > 0)
2740 ret = -ENOENT;
2741 goto failed;
2742 }
2743
Chris Masonb4ce94d2009-02-04 09:25:08 -05002744 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002745 leaf = path->nodes[0];
2746 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002747 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002748
Chris Masone02119d2008-09-05 16:13:11 -04002749 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002750 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002751 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002752 ret = 0;
2753failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002754 btrfs_free_path(path);
2755 return ret;
2756}
2757
Chris Masond352ac62008-09-29 15:18:18 -04002758/*
Chris Mason21151332011-11-10 20:39:08 -05002759 * copy everything in the in-memory inode into the btree.
2760 */
2761noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2762 struct btrfs_root *root, struct inode *inode)
2763{
2764 int ret;
2765
2766 /*
2767 * If the inode is a free space inode, we can deadlock during commit
2768 * if we put it into the delayed code.
2769 *
2770 * The data relocation inode should also be directly updated
2771 * without delay
2772 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002773 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002774 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002775 btrfs_update_root_times(trans, root);
2776
Chris Mason21151332011-11-10 20:39:08 -05002777 ret = btrfs_delayed_update_inode(trans, root, inode);
2778 if (!ret)
2779 btrfs_set_inode_last_trans(trans, inode);
2780 return ret;
2781 }
2782
2783 return btrfs_update_inode_item(trans, root, inode);
2784}
2785
Josef Bacikbe6aef62012-10-22 15:43:12 -04002786noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2787 struct btrfs_root *root,
2788 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002789{
2790 int ret;
2791
2792 ret = btrfs_update_inode(trans, root, inode);
2793 if (ret == -ENOSPC)
2794 return btrfs_update_inode_item(trans, root, inode);
2795 return ret;
2796}
2797
2798/*
Chris Masond352ac62008-09-29 15:18:18 -04002799 * unlink helper that gets used here in inode.c and in the tree logging
2800 * recovery code. It remove a link in a directory with a given name, and
2801 * also drops the back refs in the inode to the directory
2802 */
Al Viro92986792011-03-04 17:14:37 +00002803static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2804 struct btrfs_root *root,
2805 struct inode *dir, struct inode *inode,
2806 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002807{
2808 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002809 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002810 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002811 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002812 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002813 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002814 u64 ino = btrfs_ino(inode);
2815 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002816
2817 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002818 if (!path) {
2819 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002820 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002821 }
2822
Chris Masonb9473432009-03-13 11:00:37 -04002823 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002824 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002825 name, name_len, -1);
2826 if (IS_ERR(di)) {
2827 ret = PTR_ERR(di);
2828 goto err;
2829 }
2830 if (!di) {
2831 ret = -ENOENT;
2832 goto err;
2833 }
Chris Mason5f39d392007-10-15 16:14:19 -04002834 leaf = path->nodes[0];
2835 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002836 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002837 if (ret)
2838 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002839 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002840
Li Zefan33345d012011-04-20 10:31:50 +08002841 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2842 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002843 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002844 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002845 "inode %llu parent %llu\n", name_len, name,
2846 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002847 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002848 goto err;
2849 }
2850
Miao Xie16cdcec2011-04-22 18:12:22 +08002851 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002852 if (ret) {
2853 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002854 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002855 }
Chris Mason39279cc2007-06-12 06:35:45 -04002856
Chris Masone02119d2008-09-05 16:13:11 -04002857 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002858 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002859 if (ret != 0 && ret != -ENOENT) {
2860 btrfs_abort_transaction(trans, root, ret);
2861 goto err;
2862 }
Chris Masone02119d2008-09-05 16:13:11 -04002863
2864 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2865 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002866 if (ret == -ENOENT)
2867 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002868err:
2869 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002870 if (ret)
2871 goto out;
2872
2873 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002874 inode_inc_iversion(inode);
2875 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002876 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002877 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002878out:
Chris Mason39279cc2007-06-12 06:35:45 -04002879 return ret;
2880}
2881
Al Viro92986792011-03-04 17:14:37 +00002882int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2883 struct btrfs_root *root,
2884 struct inode *dir, struct inode *inode,
2885 const char *name, int name_len)
2886{
2887 int ret;
2888 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2889 if (!ret) {
2890 btrfs_drop_nlink(inode);
2891 ret = btrfs_update_inode(trans, root, inode);
2892 }
2893 return ret;
2894}
2895
2896
Yan, Zhenga22285a2010-05-16 10:48:46 -04002897/* helper to check if there is any shared block in the path */
2898static int check_path_shared(struct btrfs_root *root,
2899 struct btrfs_path *path)
2900{
2901 struct extent_buffer *eb;
2902 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002903 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002904
2905 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002906 int ret;
2907
Yan, Zhenga22285a2010-05-16 10:48:46 -04002908 if (!path->nodes[level])
2909 break;
2910 eb = path->nodes[level];
2911 if (!btrfs_block_can_be_shared(root, eb))
2912 continue;
2913 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2914 &refs, NULL);
2915 if (refs > 1)
2916 return 1;
2917 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002918 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002919}
2920
2921/*
2922 * helper to start transaction for unlink and rmdir.
2923 *
2924 * unlink and rmdir are special in btrfs, they do not always free space.
2925 * so in enospc case, we should make sure they will free space before
2926 * allowing them to use the global metadata reservation.
2927 */
2928static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2929 struct dentry *dentry)
2930{
2931 struct btrfs_trans_handle *trans;
2932 struct btrfs_root *root = BTRFS_I(dir)->root;
2933 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002934 struct btrfs_dir_item *di;
2935 struct inode *inode = dentry->d_inode;
2936 u64 index;
2937 int check_link = 1;
2938 int err = -ENOSPC;
2939 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002940 u64 ino = btrfs_ino(inode);
2941 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002942
Josef Bacike70bea52011-10-11 14:18:24 -04002943 /*
2944 * 1 for the possible orphan item
2945 * 1 for the dir item
2946 * 1 for the dir index
2947 * 1 for the inode ref
2948 * 1 for the inode ref in the tree log
2949 * 2 for the dir entries in the log
2950 * 1 for the inode
2951 */
2952 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002953 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2954 return trans;
2955
Li Zefan33345d012011-04-20 10:31:50 +08002956 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002957 return ERR_PTR(-ENOSPC);
2958
2959 /* check if there is someone else holds reference */
2960 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2961 return ERR_PTR(-ENOSPC);
2962
2963 if (atomic_read(&inode->i_count) > 2)
2964 return ERR_PTR(-ENOSPC);
2965
2966 if (xchg(&root->fs_info->enospc_unlink, 1))
2967 return ERR_PTR(-ENOSPC);
2968
2969 path = btrfs_alloc_path();
2970 if (!path) {
2971 root->fs_info->enospc_unlink = 0;
2972 return ERR_PTR(-ENOMEM);
2973 }
2974
Josef Bacik3880a1b2011-10-14 14:46:51 -04002975 /* 1 for the orphan item */
2976 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002977 if (IS_ERR(trans)) {
2978 btrfs_free_path(path);
2979 root->fs_info->enospc_unlink = 0;
2980 return trans;
2981 }
2982
2983 path->skip_locking = 1;
2984 path->search_commit_root = 1;
2985
2986 ret = btrfs_lookup_inode(trans, root, path,
2987 &BTRFS_I(dir)->location, 0);
2988 if (ret < 0) {
2989 err = ret;
2990 goto out;
2991 }
2992 if (ret == 0) {
2993 if (check_path_shared(root, path))
2994 goto out;
2995 } else {
2996 check_link = 0;
2997 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002998 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002999
3000 ret = btrfs_lookup_inode(trans, root, path,
3001 &BTRFS_I(inode)->location, 0);
3002 if (ret < 0) {
3003 err = ret;
3004 goto out;
3005 }
3006 if (ret == 0) {
3007 if (check_path_shared(root, path))
3008 goto out;
3009 } else {
3010 check_link = 0;
3011 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003012 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003013
3014 if (ret == 0 && S_ISREG(inode->i_mode)) {
3015 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003016 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003017 if (ret < 0) {
3018 err = ret;
3019 goto out;
3020 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003021 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003022 if (check_path_shared(root, path))
3023 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003024 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003025 }
3026
3027 if (!check_link) {
3028 err = 0;
3029 goto out;
3030 }
3031
Li Zefan33345d012011-04-20 10:31:50 +08003032 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003033 dentry->d_name.name, dentry->d_name.len, 0);
3034 if (IS_ERR(di)) {
3035 err = PTR_ERR(di);
3036 goto out;
3037 }
3038 if (di) {
3039 if (check_path_shared(root, path))
3040 goto out;
3041 } else {
3042 err = 0;
3043 goto out;
3044 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003045 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003046
Mark Fashehf1863732012-08-08 11:32:27 -07003047 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3048 dentry->d_name.len, ino, dir_ino, 0,
3049 &index);
3050 if (ret) {
3051 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003052 goto out;
3053 }
Mark Fashehf1863732012-08-08 11:32:27 -07003054
Yan, Zhenga22285a2010-05-16 10:48:46 -04003055 if (check_path_shared(root, path))
3056 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003057
David Sterbab3b4aa72011-04-21 01:20:15 +02003058 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003059
Miao Xie16cdcec2011-04-22 18:12:22 +08003060 /*
3061 * This is a commit root search, if we can lookup inode item and other
3062 * relative items in the commit root, it means the transaction of
3063 * dir/file creation has been committed, and the dir index item that we
3064 * delay to insert has also been inserted into the commit root. So
3065 * we needn't worry about the delayed insertion of the dir index item
3066 * here.
3067 */
Li Zefan33345d012011-04-20 10:31:50 +08003068 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003069 dentry->d_name.name, dentry->d_name.len, 0);
3070 if (IS_ERR(di)) {
3071 err = PTR_ERR(di);
3072 goto out;
3073 }
3074 BUG_ON(ret == -ENOENT);
3075 if (check_path_shared(root, path))
3076 goto out;
3077
3078 err = 0;
3079out:
3080 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003081 /* Migrate the orphan reservation over */
3082 if (!err)
3083 err = btrfs_block_rsv_migrate(trans->block_rsv,
3084 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003085 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003086
Yan, Zhenga22285a2010-05-16 10:48:46 -04003087 if (err) {
3088 btrfs_end_transaction(trans, root);
3089 root->fs_info->enospc_unlink = 0;
3090 return ERR_PTR(err);
3091 }
3092
3093 trans->block_rsv = &root->fs_info->global_block_rsv;
3094 return trans;
3095}
3096
3097static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3098 struct btrfs_root *root)
3099{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003100 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003101 btrfs_block_rsv_release(root, trans->block_rsv,
3102 trans->bytes_reserved);
3103 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003104 BUG_ON(!root->fs_info->enospc_unlink);
3105 root->fs_info->enospc_unlink = 0;
3106 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003107 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003108}
3109
Chris Mason39279cc2007-06-12 06:35:45 -04003110static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3111{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003112 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003113 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003114 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003115 int ret;
3116
Yan, Zhenga22285a2010-05-16 10:48:46 -04003117 trans = __unlink_start_trans(dir, dentry);
3118 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003119 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003120
Chris Mason12fcfd22009-03-24 10:24:20 -04003121 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3122
Chris Masone02119d2008-09-05 16:13:11 -04003123 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3124 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003125 if (ret)
3126 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003127
Yan, Zhenga22285a2010-05-16 10:48:46 -04003128 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003129 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003130 if (ret)
3131 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003132 }
Josef Bacik7b128762008-07-24 12:17:14 -04003133
Tsutomu Itohb5324022011-07-19 07:27:20 +00003134out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003135 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003136 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003137 return ret;
3138}
3139
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003140int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3141 struct btrfs_root *root,
3142 struct inode *dir, u64 objectid,
3143 const char *name, int name_len)
3144{
3145 struct btrfs_path *path;
3146 struct extent_buffer *leaf;
3147 struct btrfs_dir_item *di;
3148 struct btrfs_key key;
3149 u64 index;
3150 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003151 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003152
3153 path = btrfs_alloc_path();
3154 if (!path)
3155 return -ENOMEM;
3156
Li Zefan33345d012011-04-20 10:31:50 +08003157 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003158 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003159 if (IS_ERR_OR_NULL(di)) {
3160 if (!di)
3161 ret = -ENOENT;
3162 else
3163 ret = PTR_ERR(di);
3164 goto out;
3165 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003166
3167 leaf = path->nodes[0];
3168 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3169 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3170 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003171 if (ret) {
3172 btrfs_abort_transaction(trans, root, ret);
3173 goto out;
3174 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003175 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003176
3177 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3178 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003179 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003180 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003181 if (ret != -ENOENT) {
3182 btrfs_abort_transaction(trans, root, ret);
3183 goto out;
3184 }
Li Zefan33345d012011-04-20 10:31:50 +08003185 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003186 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003187 if (IS_ERR_OR_NULL(di)) {
3188 if (!di)
3189 ret = -ENOENT;
3190 else
3191 ret = PTR_ERR(di);
3192 btrfs_abort_transaction(trans, root, ret);
3193 goto out;
3194 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003195
3196 leaf = path->nodes[0];
3197 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003198 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003199 index = key.offset;
3200 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003201 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003202
Miao Xie16cdcec2011-04-22 18:12:22 +08003203 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003204 if (ret) {
3205 btrfs_abort_transaction(trans, root, ret);
3206 goto out;
3207 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003208
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003209 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003210 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003211 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003212 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003213 if (ret)
3214 btrfs_abort_transaction(trans, root, ret);
3215out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003216 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003217 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003218}
3219
Chris Mason39279cc2007-06-12 06:35:45 -04003220static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3221{
3222 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003223 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003224 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003225 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003226
David Sterbab3ae2442012-09-13 16:04:34 -06003227 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003228 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003229 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3230 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003231
Yan, Zhenga22285a2010-05-16 10:48:46 -04003232 trans = __unlink_start_trans(dir, dentry);
3233 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003234 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003235
Li Zefan33345d012011-04-20 10:31:50 +08003236 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003237 err = btrfs_unlink_subvol(trans, root, dir,
3238 BTRFS_I(inode)->location.objectid,
3239 dentry->d_name.name,
3240 dentry->d_name.len);
3241 goto out;
3242 }
3243
Josef Bacik7b128762008-07-24 12:17:14 -04003244 err = btrfs_orphan_add(trans, inode);
3245 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003246 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003247
Chris Mason39279cc2007-06-12 06:35:45 -04003248 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003249 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3250 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003251 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003252 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003253out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003254 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003255 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003256
Chris Mason39279cc2007-06-12 06:35:45 -04003257 return err;
3258}
3259
Chris Mason323ac952008-10-01 19:05:46 -04003260/*
Chris Mason39279cc2007-06-12 06:35:45 -04003261 * this can truncate away extent items, csum items and directory items.
3262 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003263 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003264 *
3265 * csum items that cross the new i_size are truncated to the new size
3266 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003267 *
3268 * min_type is the minimum key type to truncate down to. If set to 0, this
3269 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003270 */
Yan, Zheng80825102009-11-12 09:35:36 +00003271int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3272 struct btrfs_root *root,
3273 struct inode *inode,
3274 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003275{
Chris Mason39279cc2007-06-12 06:35:45 -04003276 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003277 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003278 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003279 struct btrfs_key key;
3280 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003281 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003282 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003283 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003284 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003285 u64 mask = root->sectorsize - 1;
3286 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003287 int found_extent;
3288 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003289 int pending_del_nr = 0;
3290 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003291 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003292 int ret;
3293 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003294 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003295
3296 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003297
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003298 path = btrfs_alloc_path();
3299 if (!path)
3300 return -ENOMEM;
3301 path->reada = -1;
3302
Josef Bacik5dc562c2012-08-17 13:14:17 -04003303 /*
3304 * We want to drop from the next block forward in case this new size is
3305 * not block aligned since we will be keeping the last block of the
3306 * extent just the way it is.
3307 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003308 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003309 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003310
Miao Xie16cdcec2011-04-22 18:12:22 +08003311 /*
3312 * This function is also used to drop the items in the log tree before
3313 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3314 * it is used to drop the loged items. So we shouldn't kill the delayed
3315 * items.
3316 */
3317 if (min_type == 0 && root == BTRFS_I(inode)->root)
3318 btrfs_kill_delayed_inode_items(inode);
3319
Li Zefan33345d012011-04-20 10:31:50 +08003320 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003321 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003322 key.type = (u8)-1;
3323
Chris Mason85e21ba2008-01-29 15:11:36 -05003324search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003325 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003326 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003327 if (ret < 0) {
3328 err = ret;
3329 goto out;
3330 }
Chris Masond3977122009-01-05 21:25:51 -05003331
Chris Mason85e21ba2008-01-29 15:11:36 -05003332 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003333 /* there are no items in the tree for us to truncate, we're
3334 * done
3335 */
Yan, Zheng80825102009-11-12 09:35:36 +00003336 if (path->slots[0] == 0)
3337 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003338 path->slots[0]--;
3339 }
3340
Chris Masond3977122009-01-05 21:25:51 -05003341 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003342 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003343 leaf = path->nodes[0];
3344 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3345 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003346
Li Zefan33345d012011-04-20 10:31:50 +08003347 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003348 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003349
Chris Mason85e21ba2008-01-29 15:11:36 -05003350 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003351 break;
3352
Chris Mason5f39d392007-10-15 16:14:19 -04003353 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003354 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003355 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003356 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003357 extent_type = btrfs_file_extent_type(leaf, fi);
3358 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003359 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003360 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003361 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003362 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003363 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003364 }
Yan008630c2007-11-07 13:31:09 -05003365 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003366 }
Yan, Zheng80825102009-11-12 09:35:36 +00003367 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003368 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003369 } else {
3370 if (item_end < new_size)
3371 break;
3372 if (found_key.offset >= new_size)
3373 del_item = 1;
3374 else
3375 del_item = 0;
3376 }
Chris Mason39279cc2007-06-12 06:35:45 -04003377 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003378 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003379 if (found_type != BTRFS_EXTENT_DATA_KEY)
3380 goto delete;
3381
3382 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003383 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003384 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003385 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003386 u64 orig_num_bytes =
3387 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003388 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003389 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003390 extent_num_bytes = extent_num_bytes &
3391 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003392 btrfs_set_file_extent_num_bytes(leaf, fi,
3393 extent_num_bytes);
3394 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003395 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003396 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003397 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003398 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003399 } else {
Chris Masondb945352007-10-15 16:15:53 -04003400 extent_num_bytes =
3401 btrfs_file_extent_disk_num_bytes(leaf,
3402 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003403 extent_offset = found_key.offset -
3404 btrfs_file_extent_offset(leaf, fi);
3405
Chris Mason39279cc2007-06-12 06:35:45 -04003406 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003407 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003408 if (extent_start != 0) {
3409 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003410 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003411 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003412 }
3413 }
Chris Mason90692182008-02-08 13:49:28 -05003414 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003415 /*
3416 * we can't truncate inline items that have had
3417 * special encodings
3418 */
3419 if (!del_item &&
3420 btrfs_file_extent_compression(leaf, fi) == 0 &&
3421 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3422 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003423 u32 size = new_size - found_key.offset;
3424
3425 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003426 inode_sub_bytes(inode, item_end + 1 -
3427 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003428 }
3429 size =
3430 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003431 btrfs_truncate_item(trans, root, path,
3432 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003433 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003434 inode_sub_bytes(inode, item_end + 1 -
3435 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003436 }
Chris Mason39279cc2007-06-12 06:35:45 -04003437 }
Chris Mason179e29e2007-11-01 11:28:41 -04003438delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003439 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003440 if (!pending_del_nr) {
3441 /* no pending yet, add ourselves */
3442 pending_del_slot = path->slots[0];
3443 pending_del_nr = 1;
3444 } else if (pending_del_nr &&
3445 path->slots[0] + 1 == pending_del_slot) {
3446 /* hop on the pending chunk */
3447 pending_del_nr++;
3448 pending_del_slot = path->slots[0];
3449 } else {
Chris Masond3977122009-01-05 21:25:51 -05003450 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003451 }
Chris Mason39279cc2007-06-12 06:35:45 -04003452 } else {
3453 break;
3454 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003455 if (found_extent && (root->ref_cows ||
3456 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003457 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003458 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003459 extent_num_bytes, 0,
3460 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003461 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003462 BUG_ON(ret);
3463 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003464
Yan, Zheng80825102009-11-12 09:35:36 +00003465 if (found_type == BTRFS_INODE_ITEM_KEY)
3466 break;
3467
3468 if (path->slots[0] == 0 ||
3469 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003470 if (pending_del_nr) {
3471 ret = btrfs_del_items(trans, root, path,
3472 pending_del_slot,
3473 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003474 if (ret) {
3475 btrfs_abort_transaction(trans,
3476 root, ret);
3477 goto error;
3478 }
Yan, Zheng80825102009-11-12 09:35:36 +00003479 pending_del_nr = 0;
3480 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003481 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003482 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003483 } else {
3484 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003485 }
Chris Mason39279cc2007-06-12 06:35:45 -04003486 }
Yan, Zheng80825102009-11-12 09:35:36 +00003487out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003488 if (pending_del_nr) {
3489 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3490 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003491 if (ret)
3492 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003493 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003494error:
Chris Mason39279cc2007-06-12 06:35:45 -04003495 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003496 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003497}
3498
Chris Masona52d9a82007-08-27 16:49:44 -04003499/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003500 * btrfs_truncate_page - read, zero a chunk and write a page
3501 * @inode - inode that we're zeroing
3502 * @from - the offset to start zeroing
3503 * @len - the length to zero, 0 to zero the entire range respective to the
3504 * offset
3505 * @front - zero up to the offset instead of from the offset on
3506 *
3507 * This will find the page for the "from" offset and cow the page and zero the
3508 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003509 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003510int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3511 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003512{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003513 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003514 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003515 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3516 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003517 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003518 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003519 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003520 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3521 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3522 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003523 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003524 int ret = 0;
3525 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003526 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003527
Josef Bacik2aaa6652012-08-29 14:27:18 -04003528 if ((offset & (blocksize - 1)) == 0 &&
3529 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003530 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003531 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003532 if (ret)
3533 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003534
Chris Mason211c17f2008-05-15 09:13:45 -04003535again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003536 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003537 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003538 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00003539 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04003540 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003541 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003542
3543 page_start = page_offset(page);
3544 page_end = page_start + PAGE_CACHE_SIZE - 1;
3545
Chris Masona52d9a82007-08-27 16:49:44 -04003546 if (!PageUptodate(page)) {
3547 ret = btrfs_readpage(NULL, page);
3548 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003549 if (page->mapping != mapping) {
3550 unlock_page(page);
3551 page_cache_release(page);
3552 goto again;
3553 }
Chris Masona52d9a82007-08-27 16:49:44 -04003554 if (!PageUptodate(page)) {
3555 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003556 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003557 }
3558 }
Chris Mason211c17f2008-05-15 09:13:45 -04003559 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003560
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003561 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003562 set_page_extent_mapped(page);
3563
3564 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3565 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003566 unlock_extent_cached(io_tree, page_start, page_end,
3567 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003568 unlock_page(page);
3569 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003570 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003571 btrfs_put_ordered_extent(ordered);
3572 goto again;
3573 }
3574
Josef Bacik2ac55d42010-02-03 19:33:23 +00003575 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003576 EXTENT_DIRTY | EXTENT_DELALLOC |
3577 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003578 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003579
Josef Bacik2ac55d42010-02-03 19:33:23 +00003580 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3581 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003582 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003583 unlock_extent_cached(io_tree, page_start, page_end,
3584 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003585 goto out_unlock;
3586 }
3587
Chris Masone6dcd2d2008-07-17 12:53:50 -04003588 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003589 if (!len)
3590 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003591 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003592 if (front)
3593 memset(kaddr, 0, offset);
3594 else
3595 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003596 flush_dcache_page(page);
3597 kunmap(page);
3598 }
Chris Mason247e7432008-07-17 12:53:51 -04003599 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003600 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003601 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3602 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003603
Chris Mason89642222008-07-24 09:41:53 -04003604out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003605 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003606 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003607 unlock_page(page);
3608 page_cache_release(page);
3609out:
3610 return ret;
3611}
3612
Josef Bacik695a0d02011-03-04 15:46:53 -05003613/*
3614 * This function puts in dummy file extents for the area we're creating a hole
3615 * for. So if we are truncating this file to a larger size we need to insert
3616 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3617 * the range between oldsize and size
3618 */
Josef Bacika41ad392011-01-31 15:30:16 -05003619int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003620{
3621 struct btrfs_trans_handle *trans;
3622 struct btrfs_root *root = BTRFS_I(inode)->root;
3623 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003624 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003625 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003626 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003627 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003628 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003629 u64 block_end = (size + mask) & ~mask;
3630 u64 last_byte;
3631 u64 cur_offset;
3632 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003633 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003634
3635 if (size <= hole_start)
3636 return 0;
3637
Yan Zheng9036c102008-10-30 14:19:41 -04003638 while (1) {
3639 struct btrfs_ordered_extent *ordered;
3640 btrfs_wait_ordered_range(inode, hole_start,
3641 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003642 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003643 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003644 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3645 if (!ordered)
3646 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003647 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3648 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003649 btrfs_put_ordered_extent(ordered);
3650 }
3651
Yan Zheng9036c102008-10-30 14:19:41 -04003652 cur_offset = hole_start;
3653 while (1) {
3654 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3655 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003656 if (IS_ERR(em)) {
3657 err = PTR_ERR(em);
3658 break;
3659 }
Yan Zheng9036c102008-10-30 14:19:41 -04003660 last_byte = min(extent_map_end(em), block_end);
3661 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003662 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003663 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003664 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003665
Miao Xie36423202011-12-14 20:12:02 -05003666 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003667 if (IS_ERR(trans)) {
3668 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003669 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003670 }
Yan, Zheng80825102009-11-12 09:35:36 +00003671
Josef Bacik5dc562c2012-08-17 13:14:17 -04003672 err = btrfs_drop_extents(trans, root, inode,
3673 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003674 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003675 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003676 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003677 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003678 break;
Miao Xie5b397372011-09-11 10:52:24 -04003679 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003680
Yan Zheng9036c102008-10-30 14:19:41 -04003681 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003682 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003683 0, hole_size, 0, hole_size,
3684 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003685 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003686 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003687 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003688 break;
Miao Xie5b397372011-09-11 10:52:24 -04003689 }
Yan, Zheng80825102009-11-12 09:35:36 +00003690
Josef Bacik5dc562c2012-08-17 13:14:17 -04003691 btrfs_drop_extent_cache(inode, cur_offset,
3692 cur_offset + hole_size - 1, 0);
3693 hole_em = alloc_extent_map();
3694 if (!hole_em) {
3695 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3696 &BTRFS_I(inode)->runtime_flags);
3697 goto next;
3698 }
3699 hole_em->start = cur_offset;
3700 hole_em->len = hole_size;
3701 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003702
Josef Bacik5dc562c2012-08-17 13:14:17 -04003703 hole_em->block_start = EXTENT_MAP_HOLE;
3704 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05003705 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003706 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3707 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3708 hole_em->generation = trans->transid;
3709
3710 while (1) {
3711 write_lock(&em_tree->lock);
3712 err = add_extent_mapping(em_tree, hole_em);
3713 if (!err)
3714 list_move(&hole_em->list,
3715 &em_tree->modified_extents);
3716 write_unlock(&em_tree->lock);
3717 if (err != -EEXIST)
3718 break;
3719 btrfs_drop_extent_cache(inode, cur_offset,
3720 cur_offset +
3721 hole_size - 1, 0);
3722 }
3723 free_extent_map(hole_em);
3724next:
Miao Xie36423202011-12-14 20:12:02 -05003725 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003726 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003727 }
3728 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003729 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003730 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003731 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003732 break;
3733 }
3734
Yan, Zhenga22285a2010-05-16 10:48:46 -04003735 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003736 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3737 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003738 return err;
3739}
3740
Josef Bacika41ad392011-01-31 15:30:16 -05003741static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003742{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003743 struct btrfs_root *root = BTRFS_I(inode)->root;
3744 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003745 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003746 int ret;
3747
Josef Bacika41ad392011-01-31 15:30:16 -05003748 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003749 return 0;
3750
Josef Bacika41ad392011-01-31 15:30:16 -05003751 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003752 truncate_pagecache(inode, oldsize, newsize);
3753 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003754 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003755 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003756
Miao Xief4a2f4c2011-12-14 20:12:01 -05003757 trans = btrfs_start_transaction(root, 1);
3758 if (IS_ERR(trans))
3759 return PTR_ERR(trans);
3760
3761 i_size_write(inode, newsize);
3762 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3763 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003764 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003765 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003766
Josef Bacika41ad392011-01-31 15:30:16 -05003767 /*
3768 * We're truncating a file that used to have good data down to
3769 * zero. Make sure it gets into the ordered flush list so that
3770 * any new writes get down to disk quickly.
3771 */
3772 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003773 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3774 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003775
Josef Bacika41ad392011-01-31 15:30:16 -05003776 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3777 truncate_setsize(inode, newsize);
3778 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003779 }
3780
Josef Bacika41ad392011-01-31 15:30:16 -05003781 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003782}
3783
Chris Mason39279cc2007-06-12 06:35:45 -04003784static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3785{
3786 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003787 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003788 int err;
3789
Li Zefanb83cc962010-12-20 16:04:08 +08003790 if (btrfs_root_readonly(root))
3791 return -EROFS;
3792
Chris Mason39279cc2007-06-12 06:35:45 -04003793 err = inode_change_ok(inode, attr);
3794 if (err)
3795 return err;
3796
Chris Mason5a3f23d2009-03-31 13:27:11 -04003797 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003798 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003799 if (err)
3800 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003801 }
Yan Zheng9036c102008-10-30 14:19:41 -04003802
Christoph Hellwig10257742010-06-04 11:30:02 +02003803 if (attr->ia_valid) {
3804 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003805 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003806 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003807
Josef Bacik22c44fe2011-11-30 10:45:38 -05003808 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003809 err = btrfs_acl_chmod(inode);
3810 }
3811
Chris Mason39279cc2007-06-12 06:35:45 -04003812 return err;
3813}
Chris Mason61295eb2008-01-14 16:24:38 -05003814
Al Virobd555972010-06-07 11:35:40 -04003815void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003816{
3817 struct btrfs_trans_handle *trans;
3818 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003819 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003820 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003821 int ret;
3822
liubo1abe9b82011-03-24 11:18:59 +00003823 trace_btrfs_inode_evict(inode);
3824
Chris Mason39279cc2007-06-12 06:35:45 -04003825 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003826 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003827 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003828 goto no_delete;
3829
Chris Mason39279cc2007-06-12 06:35:45 -04003830 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003831 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003832 goto no_delete;
3833 }
Al Virobd555972010-06-07 11:35:40 -04003834 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003835 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003836
Yan, Zhengc71bf092009-11-12 09:34:40 +00003837 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003838 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003839 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003840 goto no_delete;
3841 }
3842
Yan, Zheng76dda932009-09-21 16:00:26 -04003843 if (inode->i_nlink > 0) {
3844 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3845 goto no_delete;
3846 }
3847
Miao Xie66d8f3d2012-09-06 04:02:28 -06003848 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003849 if (!rsv) {
3850 btrfs_orphan_del(NULL, inode);
3851 goto no_delete;
3852 }
Josef Bacik4a338542011-08-29 11:01:31 -04003853 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003854 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003855 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003856
Chris Masondbe674a2008-07-17 12:54:05 -04003857 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003858
Josef Bacik4289a662011-08-05 13:22:24 -04003859 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003860 * This is a bit simpler than btrfs_truncate since we've already
3861 * reserved our space for our orphan item in the unlink, so we just
3862 * need to reserve some slack space in case we add bytes and update
3863 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003864 */
Yan, Zheng80825102009-11-12 09:35:36 +00003865 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00003866 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3867 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00003868
Josef Bacik726c35f2011-09-26 15:46:06 -04003869 /*
3870 * Try and steal from the global reserve since we will
3871 * likely not use this space anyway, we want to try as
3872 * hard as possible to get this to work.
3873 */
3874 if (ret)
3875 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3876
Yan, Zhengd68fc572010-05-16 10:49:58 -04003877 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003878 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003879 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003880 btrfs_orphan_del(NULL, inode);
3881 btrfs_free_block_rsv(root, rsv);
3882 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003883 }
3884
Miao Xie08e007d2012-10-16 11:33:38 +00003885 trans = btrfs_start_transaction_lflush(root, 1);
Josef Bacik4289a662011-08-05 13:22:24 -04003886 if (IS_ERR(trans)) {
3887 btrfs_orphan_del(NULL, inode);
3888 btrfs_free_block_rsv(root, rsv);
3889 goto no_delete;
3890 }
3891
3892 trans->block_rsv = rsv;
3893
Yan, Zhengd68fc572010-05-16 10:49:58 -04003894 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04003895 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00003896 break;
3897
Miao Xie8407aa42012-09-07 01:43:32 -06003898 trans->block_rsv = &root->fs_info->trans_block_rsv;
3899 ret = btrfs_update_inode(trans, root, inode);
3900 BUG_ON(ret);
3901
Yan, Zheng80825102009-11-12 09:35:36 +00003902 btrfs_end_transaction(trans, root);
3903 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00003904 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04003905 }
3906
Josef Bacik4289a662011-08-05 13:22:24 -04003907 btrfs_free_block_rsv(root, rsv);
3908
Yan, Zheng80825102009-11-12 09:35:36 +00003909 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003910 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003911 ret = btrfs_orphan_del(trans, inode);
3912 BUG_ON(ret);
3913 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003914
Josef Bacik4289a662011-08-05 13:22:24 -04003915 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003916 if (!(root == root->fs_info->tree_root ||
3917 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003918 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003919
Chris Mason54aa1f42007-06-22 14:16:25 -04003920 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003921 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003922no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02003923 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003924 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003925}
3926
3927/*
3928 * this returns the key found in the dir entry in the location pointer.
3929 * If no dir entries were found, location->objectid is 0.
3930 */
3931static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3932 struct btrfs_key *location)
3933{
3934 const char *name = dentry->d_name.name;
3935 int namelen = dentry->d_name.len;
3936 struct btrfs_dir_item *di;
3937 struct btrfs_path *path;
3938 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003939 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003940
3941 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003942 if (!path)
3943 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003944
Li Zefan33345d012011-04-20 10:31:50 +08003945 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003946 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003947 if (IS_ERR(di))
3948 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003949
David Sterbac7040052011-04-19 18:00:01 +02003950 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003951 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003952
Chris Mason5f39d392007-10-15 16:14:19 -04003953 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003954out:
Chris Mason39279cc2007-06-12 06:35:45 -04003955 btrfs_free_path(path);
3956 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003957out_err:
3958 location->objectid = 0;
3959 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003960}
3961
3962/*
3963 * when we hit a tree root in a directory, the btrfs part of the inode
3964 * needs to be changed to reflect the root directory of the tree root. This
3965 * is kind of like crossing a mount point.
3966 */
3967static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003968 struct inode *dir,
3969 struct dentry *dentry,
3970 struct btrfs_key *location,
3971 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003972{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003973 struct btrfs_path *path;
3974 struct btrfs_root *new_root;
3975 struct btrfs_root_ref *ref;
3976 struct extent_buffer *leaf;
3977 int ret;
3978 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003979
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003980 path = btrfs_alloc_path();
3981 if (!path) {
3982 err = -ENOMEM;
3983 goto out;
3984 }
Chris Mason39279cc2007-06-12 06:35:45 -04003985
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003986 err = -ENOENT;
3987 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3988 BTRFS_I(dir)->root->root_key.objectid,
3989 location->objectid);
3990 if (ret) {
3991 if (ret < 0)
3992 err = ret;
3993 goto out;
3994 }
Chris Mason39279cc2007-06-12 06:35:45 -04003995
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003996 leaf = path->nodes[0];
3997 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003998 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003999 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4000 goto out;
4001
4002 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4003 (unsigned long)(ref + 1),
4004 dentry->d_name.len);
4005 if (ret)
4006 goto out;
4007
David Sterbab3b4aa72011-04-21 01:20:15 +02004008 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004009
4010 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4011 if (IS_ERR(new_root)) {
4012 err = PTR_ERR(new_root);
4013 goto out;
4014 }
4015
4016 if (btrfs_root_refs(&new_root->root_item) == 0) {
4017 err = -ENOENT;
4018 goto out;
4019 }
4020
4021 *sub_root = new_root;
4022 location->objectid = btrfs_root_dirid(&new_root->root_item);
4023 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004024 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004025 err = 0;
4026out:
4027 btrfs_free_path(path);
4028 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004029}
4030
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004031static void inode_tree_add(struct inode *inode)
4032{
4033 struct btrfs_root *root = BTRFS_I(inode)->root;
4034 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004035 struct rb_node **p;
4036 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004037 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004038again:
4039 p = &root->inode_tree.rb_node;
4040 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004041
Al Viro1d3382cb2010-10-23 15:19:20 -04004042 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004043 return;
4044
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004045 spin_lock(&root->inode_lock);
4046 while (*p) {
4047 parent = *p;
4048 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4049
Li Zefan33345d012011-04-20 10:31:50 +08004050 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004051 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004052 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004053 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004054 else {
4055 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004056 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004057 rb_erase(parent, &root->inode_tree);
4058 RB_CLEAR_NODE(parent);
4059 spin_unlock(&root->inode_lock);
4060 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004061 }
4062 }
4063 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4064 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4065 spin_unlock(&root->inode_lock);
4066}
4067
4068static void inode_tree_del(struct inode *inode)
4069{
4070 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004071 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004072
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004073 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004074 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004075 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004076 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004077 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004078 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004079 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004080
Josef Bacik0af3d002010-06-21 14:48:16 -04004081 /*
4082 * Free space cache has inodes in the tree root, but the tree root has a
4083 * root_refs of 0, so this could end up dropping the tree root as a
4084 * snapshot, so we need the extra !root->fs_info->tree_root check to
4085 * make sure we don't drop it.
4086 */
4087 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4088 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004089 synchronize_srcu(&root->fs_info->subvol_srcu);
4090 spin_lock(&root->inode_lock);
4091 empty = RB_EMPTY_ROOT(&root->inode_tree);
4092 spin_unlock(&root->inode_lock);
4093 if (empty)
4094 btrfs_add_dead_root(root);
4095 }
4096}
4097
Jeff Mahoney143bede2012-03-01 14:56:26 +01004098void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004099{
4100 struct rb_node *node;
4101 struct rb_node *prev;
4102 struct btrfs_inode *entry;
4103 struct inode *inode;
4104 u64 objectid = 0;
4105
4106 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4107
4108 spin_lock(&root->inode_lock);
4109again:
4110 node = root->inode_tree.rb_node;
4111 prev = NULL;
4112 while (node) {
4113 prev = node;
4114 entry = rb_entry(node, struct btrfs_inode, rb_node);
4115
Li Zefan33345d012011-04-20 10:31:50 +08004116 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004117 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004118 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004119 node = node->rb_right;
4120 else
4121 break;
4122 }
4123 if (!node) {
4124 while (prev) {
4125 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004126 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004127 node = prev;
4128 break;
4129 }
4130 prev = rb_next(prev);
4131 }
4132 }
4133 while (node) {
4134 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004135 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004136 inode = igrab(&entry->vfs_inode);
4137 if (inode) {
4138 spin_unlock(&root->inode_lock);
4139 if (atomic_read(&inode->i_count) > 1)
4140 d_prune_aliases(inode);
4141 /*
Al Viro45321ac2010-06-07 13:43:19 -04004142 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004143 * the inode cache when its usage count
4144 * hits zero.
4145 */
4146 iput(inode);
4147 cond_resched();
4148 spin_lock(&root->inode_lock);
4149 goto again;
4150 }
4151
4152 if (cond_resched_lock(&root->inode_lock))
4153 goto again;
4154
4155 node = rb_next(node);
4156 }
4157 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004158}
4159
Chris Masone02119d2008-09-05 16:13:11 -04004160static int btrfs_init_locked_inode(struct inode *inode, void *p)
4161{
4162 struct btrfs_iget_args *args = p;
4163 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004164 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004165 return 0;
4166}
4167
4168static int btrfs_find_actor(struct inode *inode, void *opaque)
4169{
4170 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004171 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004172 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004173}
4174
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004175static struct inode *btrfs_iget_locked(struct super_block *s,
4176 u64 objectid,
4177 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004178{
4179 struct inode *inode;
4180 struct btrfs_iget_args args;
4181 args.ino = objectid;
4182 args.root = root;
4183
4184 inode = iget5_locked(s, objectid, btrfs_find_actor,
4185 btrfs_init_locked_inode,
4186 (void *)&args);
4187 return inode;
4188}
4189
Balaji Rao1a54ef82008-07-21 02:01:04 +05304190/* Get an inode object given its location and corresponding root.
4191 * Returns in *is_new if the inode was read from disk
4192 */
4193struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004194 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304195{
4196 struct inode *inode;
4197
4198 inode = btrfs_iget_locked(s, location->objectid, root);
4199 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004200 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304201
4202 if (inode->i_state & I_NEW) {
4203 BTRFS_I(inode)->root = root;
4204 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4205 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004206 if (!is_bad_inode(inode)) {
4207 inode_tree_add(inode);
4208 unlock_new_inode(inode);
4209 if (new)
4210 *new = 1;
4211 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004212 unlock_new_inode(inode);
4213 iput(inode);
4214 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004215 }
4216 }
4217
Balaji Rao1a54ef82008-07-21 02:01:04 +05304218 return inode;
4219}
4220
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004221static struct inode *new_simple_dir(struct super_block *s,
4222 struct btrfs_key *key,
4223 struct btrfs_root *root)
4224{
4225 struct inode *inode = new_inode(s);
4226
4227 if (!inode)
4228 return ERR_PTR(-ENOMEM);
4229
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004230 BTRFS_I(inode)->root = root;
4231 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004232 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004233
4234 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004235 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004236 inode->i_fop = &simple_dir_operations;
4237 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4238 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4239
4240 return inode;
4241}
4242
Chris Mason3de45862008-11-17 21:02:50 -05004243struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004244{
Chris Masond3977122009-01-05 21:25:51 -05004245 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004247 struct btrfs_root *sub_root = root;
4248 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004249 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004250 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004251
4252 if (dentry->d_name.len > BTRFS_NAME_LEN)
4253 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004254
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004255 if (unlikely(d_need_lookup(dentry))) {
4256 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4257 kfree(dentry->d_fsdata);
4258 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004259 /* This thing is hashed, drop it for now */
4260 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004261 } else {
4262 ret = btrfs_inode_by_name(dir, dentry, &location);
4263 }
Chris Mason5f39d392007-10-15 16:14:19 -04004264
Chris Mason39279cc2007-06-12 06:35:45 -04004265 if (ret < 0)
4266 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004267
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004268 if (location.objectid == 0)
4269 return NULL;
4270
4271 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004272 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004273 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004274 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004275
4276 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4277
Yan, Zheng76dda932009-09-21 16:00:26 -04004278 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004279 ret = fixup_tree_root_location(root, dir, dentry,
4280 &location, &sub_root);
4281 if (ret < 0) {
4282 if (ret != -ENOENT)
4283 inode = ERR_PTR(ret);
4284 else
4285 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4286 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004287 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004288 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004289 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4290
Julia Lawall34d19ba2011-01-24 19:55:19 +00004291 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004292 down_read(&root->fs_info->cleanup_work_sem);
4293 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004294 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004295 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004296 if (ret)
4297 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004298 }
4299
Chris Mason3de45862008-11-17 21:02:50 -05004300 return inode;
4301}
4302
Nick Pigginfe15ce42011-01-07 17:49:23 +11004303static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004304{
4305 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004306 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004307
Li Zefan848cce02012-02-21 17:04:28 +08004308 if (!inode && !IS_ROOT(dentry))
4309 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004310
Li Zefan848cce02012-02-21 17:04:28 +08004311 if (inode) {
4312 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004313 if (btrfs_root_refs(&root->root_item) == 0)
4314 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004315
4316 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4317 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004318 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004319 return 0;
4320}
4321
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004322static void btrfs_dentry_release(struct dentry *dentry)
4323{
4324 if (dentry->d_fsdata)
4325 kfree(dentry->d_fsdata);
4326}
4327
Chris Mason3de45862008-11-17 21:02:50 -05004328static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004329 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004330{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004331 struct dentry *ret;
4332
4333 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4334 if (unlikely(d_need_lookup(dentry))) {
4335 spin_lock(&dentry->d_lock);
4336 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4337 spin_unlock(&dentry->d_lock);
4338 }
4339 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004340}
4341
Miao Xie16cdcec2011-04-22 18:12:22 +08004342unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004343 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4344};
4345
David Woodhousecbdf5a22008-08-06 19:42:33 +01004346static int btrfs_real_readdir(struct file *filp, void *dirent,
4347 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004348{
Chris Mason6da6aba2007-12-18 16:15:09 -05004349 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004350 struct btrfs_root *root = BTRFS_I(inode)->root;
4351 struct btrfs_item *item;
4352 struct btrfs_dir_item *di;
4353 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004354 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004355 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004356 struct list_head ins_list;
4357 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004358 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004359 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004360 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004361 unsigned char d_type;
4362 int over = 0;
4363 u32 di_cur;
4364 u32 di_total;
4365 u32 di_len;
4366 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004367 char tmp_name[32];
4368 char *name_ptr;
4369 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004370 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004371
4372 /* FIXME, use a real flag for deciding about the key type */
4373 if (root->fs_info->tree_root == root)
4374 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004375
Chris Mason39544012007-12-12 14:38:19 -05004376 /* special case for "." */
4377 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004378 over = filldir(dirent, ".", 1,
4379 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004380 if (over)
4381 return 0;
4382 filp->f_pos = 1;
4383 }
Chris Mason39544012007-12-12 14:38:19 -05004384 /* special case for .., just use the back ref */
4385 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004386 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004387 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004388 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004389 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004390 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004391 filp->f_pos = 2;
4392 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004393 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004394 if (!path)
4395 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004396
Josef Bacik026fd312011-05-13 10:32:11 -04004397 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004398
Miao Xie16cdcec2011-04-22 18:12:22 +08004399 if (key_type == BTRFS_DIR_INDEX_KEY) {
4400 INIT_LIST_HEAD(&ins_list);
4401 INIT_LIST_HEAD(&del_list);
4402 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4403 }
4404
Chris Mason39279cc2007-06-12 06:35:45 -04004405 btrfs_set_key_type(&key, key_type);
4406 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004407 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004408
Chris Mason39279cc2007-06-12 06:35:45 -04004409 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4410 if (ret < 0)
4411 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004412
4413 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004414 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004415 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004416 if (slot >= btrfs_header_nritems(leaf)) {
4417 ret = btrfs_next_leaf(root, path);
4418 if (ret < 0)
4419 goto err;
4420 else if (ret > 0)
4421 break;
4422 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004423 }
Chris Mason3de45862008-11-17 21:02:50 -05004424
Chris Mason5f39d392007-10-15 16:14:19 -04004425 item = btrfs_item_nr(leaf, slot);
4426 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4427
4428 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004429 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004430 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004431 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004432 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004433 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004434 if (key_type == BTRFS_DIR_INDEX_KEY &&
4435 btrfs_should_delete_dir_index(&del_list,
4436 found_key.offset))
4437 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004438
4439 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004440 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004441
Chris Mason39279cc2007-06-12 06:35:45 -04004442 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4443 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004444 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004445
4446 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004447 struct btrfs_key location;
4448
Josef Bacik22a94d42011-03-16 16:47:17 -04004449 if (verify_dir_item(root, leaf, di))
4450 break;
4451
Chris Mason5f39d392007-10-15 16:14:19 -04004452 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004453 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004454 name_ptr = tmp_name;
4455 } else {
4456 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004457 if (!name_ptr) {
4458 ret = -ENOMEM;
4459 goto err;
4460 }
Chris Mason5f39d392007-10-15 16:14:19 -04004461 }
4462 read_extent_buffer(leaf, name_ptr,
4463 (unsigned long)(di + 1), name_len);
4464
4465 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4466 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004467
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004468
Chris Mason3de45862008-11-17 21:02:50 -05004469 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004470 * skip it.
4471 *
4472 * In contrast to old kernels, we insert the snapshot's
4473 * dir item and dir index after it has been created, so
4474 * we won't find a reference to our own snapshot. We
4475 * still keep the following code for backward
4476 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004477 */
4478 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4479 location.objectid == root->root_key.objectid) {
4480 over = 0;
4481 goto skip;
4482 }
Chris Mason5f39d392007-10-15 16:14:19 -04004483 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004484 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004485 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004486
Chris Mason3de45862008-11-17 21:02:50 -05004487skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004488 if (name_ptr != tmp_name)
4489 kfree(name_ptr);
4490
Chris Mason39279cc2007-06-12 06:35:45 -04004491 if (over)
4492 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004493 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004494 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004495 di_cur += di_len;
4496 di = (struct btrfs_dir_item *)((char *)di + di_len);
4497 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004498next:
4499 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004500 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004501
Miao Xie16cdcec2011-04-22 18:12:22 +08004502 if (key_type == BTRFS_DIR_INDEX_KEY) {
4503 if (is_curr)
4504 filp->f_pos++;
4505 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4506 &ins_list);
4507 if (ret)
4508 goto nopos;
4509 }
4510
David Woodhouse49593bf2008-08-17 17:08:36 +01004511 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004512 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004513 /*
4514 * 32-bit glibc will use getdents64, but then strtol -
4515 * so the last number we can serve is this.
4516 */
4517 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004518 else
4519 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004520nopos:
4521 ret = 0;
4522err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004523 if (key_type == BTRFS_DIR_INDEX_KEY)
4524 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004525 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004526 return ret;
4527}
4528
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004529int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004530{
4531 struct btrfs_root *root = BTRFS_I(inode)->root;
4532 struct btrfs_trans_handle *trans;
4533 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004534 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004535
Josef Bacik72ac3c02012-05-23 14:13:11 -04004536 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004537 return 0;
4538
Liu Bo83eea1f2012-07-10 05:28:39 -06004539 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004540 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004541
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004542 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004543 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004544 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004545 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004546 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004547 if (IS_ERR(trans))
4548 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004549 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004550 }
4551 return ret;
4552}
4553
4554/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004555 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004556 * inode changes. But, it is most likely to find the inode in cache.
4557 * FIXME, needs more benchmarking...there are no reasons other than performance
4558 * to keep or drop this code.
4559 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004560int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004561{
4562 struct btrfs_root *root = BTRFS_I(inode)->root;
4563 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004564 int ret;
4565
Josef Bacik72ac3c02012-05-23 14:13:11 -04004566 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004567 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004568
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004569 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004570 if (IS_ERR(trans))
4571 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004572
4573 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004574 if (ret && ret == -ENOSPC) {
4575 /* whoops, lets try again with the full transaction */
4576 btrfs_end_transaction(trans, root);
4577 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004578 if (IS_ERR(trans))
4579 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004580
Chris Mason94b60442010-05-26 11:02:00 -04004581 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004582 }
Chris Mason39279cc2007-06-12 06:35:45 -04004583 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004584 if (BTRFS_I(inode)->delayed_node)
4585 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004586
4587 return ret;
4588}
4589
4590/*
4591 * This is a copy of file_update_time. We need this so we can return error on
4592 * ENOSPC for updating the inode in the case of file write and mmap writes.
4593 */
Josef Bacike41f9412012-03-26 09:46:47 -04004594static int btrfs_update_time(struct inode *inode, struct timespec *now,
4595 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004596{
Alexander Block2bc5565282012-06-15 09:49:33 +02004597 struct btrfs_root *root = BTRFS_I(inode)->root;
4598
4599 if (btrfs_root_readonly(root))
4600 return -EROFS;
4601
Josef Bacike41f9412012-03-26 09:46:47 -04004602 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004603 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004604 if (flags & S_CTIME)
4605 inode->i_ctime = *now;
4606 if (flags & S_MTIME)
4607 inode->i_mtime = *now;
4608 if (flags & S_ATIME)
4609 inode->i_atime = *now;
4610 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004611}
4612
Chris Masond352ac62008-09-29 15:18:18 -04004613/*
4614 * find the highest existing sequence number in a directory
4615 * and then set the in-memory index_cnt variable to reflect
4616 * free sequence numbers
4617 */
Josef Bacikaec74772008-07-24 12:12:38 -04004618static int btrfs_set_inode_index_count(struct inode *inode)
4619{
4620 struct btrfs_root *root = BTRFS_I(inode)->root;
4621 struct btrfs_key key, found_key;
4622 struct btrfs_path *path;
4623 struct extent_buffer *leaf;
4624 int ret;
4625
Li Zefan33345d012011-04-20 10:31:50 +08004626 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004627 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4628 key.offset = (u64)-1;
4629
4630 path = btrfs_alloc_path();
4631 if (!path)
4632 return -ENOMEM;
4633
4634 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4635 if (ret < 0)
4636 goto out;
4637 /* FIXME: we should be able to handle this */
4638 if (ret == 0)
4639 goto out;
4640 ret = 0;
4641
4642 /*
4643 * MAGIC NUMBER EXPLANATION:
4644 * since we search a directory based on f_pos we have to start at 2
4645 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4646 * else has to start at 2
4647 */
4648 if (path->slots[0] == 0) {
4649 BTRFS_I(inode)->index_cnt = 2;
4650 goto out;
4651 }
4652
4653 path->slots[0]--;
4654
4655 leaf = path->nodes[0];
4656 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4657
Li Zefan33345d012011-04-20 10:31:50 +08004658 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004659 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4660 BTRFS_I(inode)->index_cnt = 2;
4661 goto out;
4662 }
4663
4664 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4665out:
4666 btrfs_free_path(path);
4667 return ret;
4668}
4669
Chris Masond352ac62008-09-29 15:18:18 -04004670/*
4671 * helper to find a free sequence number in a given directory. This current
4672 * code is very simple, later versions will do smarter things in the btree
4673 */
Chris Mason3de45862008-11-17 21:02:50 -05004674int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004675{
4676 int ret = 0;
4677
4678 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004679 ret = btrfs_inode_delayed_dir_index_count(dir);
4680 if (ret) {
4681 ret = btrfs_set_inode_index_count(dir);
4682 if (ret)
4683 return ret;
4684 }
Josef Bacikaec74772008-07-24 12:12:38 -04004685 }
4686
Chris Mason00e4e6b2008-08-05 11:18:09 -04004687 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004688 BTRFS_I(dir)->index_cnt++;
4689
4690 return ret;
4691}
4692
Chris Mason39279cc2007-06-12 06:35:45 -04004693static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4694 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004695 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004696 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004697 u64 ref_objectid, u64 objectid,
4698 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004699{
4700 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004701 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004702 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004703 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004704 struct btrfs_inode_ref *ref;
4705 struct btrfs_key key[2];
4706 u32 sizes[2];
4707 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004708 int ret;
4709 int owner;
4710
Chris Mason5f39d392007-10-15 16:14:19 -04004711 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004712 if (!path)
4713 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004714
Chris Mason39279cc2007-06-12 06:35:45 -04004715 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004716 if (!inode) {
4717 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004718 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004719 }
Chris Mason39279cc2007-06-12 06:35:45 -04004720
Li Zefan581bb052011-04-20 10:06:11 +08004721 /*
4722 * we have to initialize this early, so we can reclaim the inode
4723 * number if we fail afterwards in this function.
4724 */
4725 inode->i_ino = objectid;
4726
Josef Bacikaec74772008-07-24 12:12:38 -04004727 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004728 trace_btrfs_inode_request(dir);
4729
Chris Mason3de45862008-11-17 21:02:50 -05004730 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004731 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004732 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004733 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004734 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004735 }
Josef Bacikaec74772008-07-24 12:12:38 -04004736 }
4737 /*
4738 * index_cnt is ignored for everything but a dir,
4739 * btrfs_get_inode_index_count has an explanation for the magic
4740 * number
4741 */
4742 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004743 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004744 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004745 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04004746
Josef Bacik5dc562c2012-08-17 13:14:17 -04004747 /*
4748 * We could have gotten an inode number from somebody who was fsynced
4749 * and then removed in this same transaction, so let's just set full
4750 * sync since it will be a full sync anyway and this will blow away the
4751 * old info in the log.
4752 */
4753 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4754
Al Viro569254b2011-07-24 17:08:40 -04004755 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004756 owner = 0;
4757 else
4758 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004759
4760 key[0].objectid = objectid;
4761 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4762 key[0].offset = 0;
4763
Mark Fashehf1863732012-08-08 11:32:27 -07004764 /*
4765 * Start new inodes with an inode_ref. This is slightly more
4766 * efficient for small numbers of hard links since they will
4767 * be packed into one item. Extended refs will kick in if we
4768 * add more hard links than can fit in the ref item.
4769 */
Chris Mason9c583092008-01-29 15:15:18 -05004770 key[1].objectid = objectid;
4771 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4772 key[1].offset = ref_objectid;
4773
4774 sizes[0] = sizeof(struct btrfs_inode_item);
4775 sizes[1] = name_len + sizeof(*ref);
4776
Chris Masonb9473432009-03-13 11:00:37 -04004777 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004778 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4779 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004780 goto fail;
4781
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004782 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004783 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004784 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004785 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4786 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004787 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4788 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004789 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004790
4791 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4792 struct btrfs_inode_ref);
4793 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004794 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004795 ptr = (unsigned long)(ref + 1);
4796 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4797
Chris Mason5f39d392007-10-15 16:14:19 -04004798 btrfs_mark_buffer_dirty(path->nodes[0]);
4799 btrfs_free_path(path);
4800
Chris Mason39279cc2007-06-12 06:35:45 -04004801 location = &BTRFS_I(inode)->location;
4802 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004803 location->offset = 0;
4804 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4805
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004806 btrfs_inherit_iflags(inode, dir);
4807
Al Viro569254b2011-07-24 17:08:40 -04004808 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004809 if (btrfs_test_opt(root, NODATASUM))
4810 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004811 if (btrfs_test_opt(root, NODATACOW) ||
4812 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004813 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4814 }
4815
Chris Mason39279cc2007-06-12 06:35:45 -04004816 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004817 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004818
4819 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004820 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004821
Alexander Block8ea05e32012-07-25 17:35:53 +02004822 btrfs_update_root_times(trans, root);
4823
Chris Mason39279cc2007-06-12 06:35:45 -04004824 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004825fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004826 if (dir)
4827 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004828 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004829 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004830 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004831}
4832
4833static inline u8 btrfs_inode_type(struct inode *inode)
4834{
4835 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4836}
4837
Chris Masond352ac62008-09-29 15:18:18 -04004838/*
4839 * utility function to add 'inode' into 'parent_inode' with
4840 * a give name and a given sequence number.
4841 * if 'add_backref' is true, also insert a backref from the
4842 * inode to the parent directory.
4843 */
Chris Masone02119d2008-09-05 16:13:11 -04004844int btrfs_add_link(struct btrfs_trans_handle *trans,
4845 struct inode *parent_inode, struct inode *inode,
4846 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004847{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004848 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004849 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004850 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004851 u64 ino = btrfs_ino(inode);
4852 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004853
Li Zefan33345d012011-04-20 10:31:50 +08004854 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004855 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4856 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004857 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004858 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4859 key.offset = 0;
4860 }
Chris Mason39279cc2007-06-12 06:35:45 -04004861
Li Zefan33345d012011-04-20 10:31:50 +08004862 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004863 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4864 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004865 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004866 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004867 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4868 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004869 }
4870
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004871 /* Nothing to clean up yet */
4872 if (ret)
4873 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004874
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004875 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4876 parent_inode, &key,
4877 btrfs_inode_type(inode), index);
4878 if (ret == -EEXIST)
4879 goto fail_dir_item;
4880 else if (ret) {
4881 btrfs_abort_transaction(trans, root, ret);
4882 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004883 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004884
4885 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4886 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004887 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004888 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4889 ret = btrfs_update_inode(trans, root, parent_inode);
4890 if (ret)
4891 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004892 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004893
4894fail_dir_item:
4895 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4896 u64 local_index;
4897 int err;
4898 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4899 key.objectid, root->root_key.objectid,
4900 parent_ino, &local_index, name, name_len);
4901
4902 } else if (add_backref) {
4903 u64 local_index;
4904 int err;
4905
4906 err = btrfs_del_inode_ref(trans, root, name, name_len,
4907 ino, parent_ino, &local_index);
4908 }
4909 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004910}
4911
4912static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004913 struct inode *dir, struct dentry *dentry,
4914 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004915{
Josef Bacika1b075d2010-11-19 20:36:11 +00004916 int err = btrfs_add_link(trans, dir, inode,
4917 dentry->d_name.name, dentry->d_name.len,
4918 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004919 if (err > 0)
4920 err = -EEXIST;
4921 return err;
4922}
4923
Josef Bacik618e21d2007-07-11 10:18:17 -04004924static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004925 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004926{
4927 struct btrfs_trans_handle *trans;
4928 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004929 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004930 int err;
4931 int drop_inode = 0;
4932 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004933 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004934
4935 if (!new_valid_dev(rdev))
4936 return -EINVAL;
4937
Josef Bacik9ed74f22009-09-11 16:12:44 -04004938 /*
4939 * 2 for inode item and ref
4940 * 2 for dir items
4941 * 1 for xattr if selinux is on
4942 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004943 trans = btrfs_start_transaction(root, 5);
4944 if (IS_ERR(trans))
4945 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004946
Li Zefan581bb052011-04-20 10:06:11 +08004947 err = btrfs_find_free_ino(root, &objectid);
4948 if (err)
4949 goto out_unlock;
4950
Josef Bacikaec74772008-07-24 12:12:38 -04004951 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004952 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04004953 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004954 if (IS_ERR(inode)) {
4955 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004956 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004957 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004958
Eric Paris2a7dba32011-02-01 11:05:39 -05004959 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004960 if (err) {
4961 drop_inode = 1;
4962 goto out_unlock;
4963 }
4964
Filipe Brandenburger2794ed02012-11-30 03:40:08 +00004965 err = btrfs_update_inode(trans, root, inode);
4966 if (err) {
4967 drop_inode = 1;
4968 goto out_unlock;
4969 }
4970
Casey Schauflerad19db72011-12-15 10:09:07 -05004971 /*
4972 * If the active LSM wants to access the inode during
4973 * d_instantiate it needs these. Smack checks to see
4974 * if the filesystem supports xattrs by looking at the
4975 * ops vector.
4976 */
4977
4978 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004979 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004980 if (err)
4981 drop_inode = 1;
4982 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004983 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004984 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05004985 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004986 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004987out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004988 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004989 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04004990 if (drop_inode) {
4991 inode_dec_link_count(inode);
4992 iput(inode);
4993 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004994 return err;
4995}
4996
Chris Mason39279cc2007-06-12 06:35:45 -04004997static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04004998 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04004999{
5000 struct btrfs_trans_handle *trans;
5001 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005002 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005003 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005004 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005005 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005006 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005007
Josef Bacik9ed74f22009-09-11 16:12:44 -04005008 /*
5009 * 2 for inode item and ref
5010 * 2 for dir items
5011 * 1 for xattr if selinux is on
5012 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005013 trans = btrfs_start_transaction(root, 5);
5014 if (IS_ERR(trans))
5015 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005016
Li Zefan581bb052011-04-20 10:06:11 +08005017 err = btrfs_find_free_ino(root, &objectid);
5018 if (err)
5019 goto out_unlock;
5020
Josef Bacikaec74772008-07-24 12:12:38 -04005021 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005022 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005023 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005024 if (IS_ERR(inode)) {
5025 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005026 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005027 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005028 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005029
Eric Paris2a7dba32011-02-01 11:05:39 -05005030 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005031 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005032 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005033
Casey Schauflerad19db72011-12-15 10:09:07 -05005034 /*
5035 * If the active LSM wants to access the inode during
5036 * d_instantiate it needs these. Smack checks to see
5037 * if the filesystem supports xattrs by looking at the
5038 * ops vector.
5039 */
5040 inode->i_fop = &btrfs_file_operations;
5041 inode->i_op = &btrfs_file_inode_operations;
5042
Josef Bacika1b075d2010-11-19 20:36:11 +00005043 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005044 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005045 goto out_unlock;
5046
5047 inode->i_mapping->a_ops = &btrfs_aops;
5048 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5049 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5050 d_instantiate(dentry, inode);
5051
Chris Mason39279cc2007-06-12 06:35:45 -04005052out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005053 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005054 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005055 inode_dec_link_count(inode);
5056 iput(inode);
5057 }
Liu Bob53d3f52012-11-14 14:34:34 +00005058 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005059 return err;
5060}
5061
5062static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5063 struct dentry *dentry)
5064{
5065 struct btrfs_trans_handle *trans;
5066 struct btrfs_root *root = BTRFS_I(dir)->root;
5067 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005068 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005069 int err;
5070 int drop_inode = 0;
5071
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005072 /* do not allow sys_link's with other subvols of the same device */
5073 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005074 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005075
Mark Fashehf1863732012-08-08 11:32:27 -07005076 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005077 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005078
Chris Mason3de45862008-11-17 21:02:50 -05005079 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005080 if (err)
5081 goto fail;
5082
Yan, Zhenga22285a2010-05-16 10:48:46 -04005083 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005084 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005085 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005086 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005087 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005088 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005089 if (IS_ERR(trans)) {
5090 err = PTR_ERR(trans);
5091 goto fail;
5092 }
Chris Mason5f39d392007-10-15 16:14:19 -04005093
Miao Xie31534952011-04-13 13:19:21 +08005094 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005095 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005096 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005097 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005098 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005099
Josef Bacika1b075d2010-11-19 20:36:11 +00005100 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005101
Yan, Zhenga5719522009-09-24 09:17:31 -04005102 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005103 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005104 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005105 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005106 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005107 if (err)
5108 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005109 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005110 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005111 }
Chris Mason39279cc2007-06-12 06:35:45 -04005112
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005113 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005114fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005115 if (drop_inode) {
5116 inode_dec_link_count(inode);
5117 iput(inode);
5118 }
Liu Bob53d3f52012-11-14 14:34:34 +00005119 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005120 return err;
5121}
5122
Al Viro18bb1db2011-07-26 01:41:39 -04005123static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005124{
Chris Masonb9d86662008-05-02 16:13:49 -04005125 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005126 struct btrfs_trans_handle *trans;
5127 struct btrfs_root *root = BTRFS_I(dir)->root;
5128 int err = 0;
5129 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005130 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005131 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005132
Josef Bacik9ed74f22009-09-11 16:12:44 -04005133 /*
5134 * 2 items for inode and ref
5135 * 2 items for dir items
5136 * 1 for xattr if selinux is on
5137 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005138 trans = btrfs_start_transaction(root, 5);
5139 if (IS_ERR(trans))
5140 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005141
Li Zefan581bb052011-04-20 10:06:11 +08005142 err = btrfs_find_free_ino(root, &objectid);
5143 if (err)
5144 goto out_fail;
5145
Josef Bacikaec74772008-07-24 12:12:38 -04005146 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005147 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005148 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005149 if (IS_ERR(inode)) {
5150 err = PTR_ERR(inode);
5151 goto out_fail;
5152 }
Chris Mason5f39d392007-10-15 16:14:19 -04005153
Chris Mason39279cc2007-06-12 06:35:45 -04005154 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005155
Eric Paris2a7dba32011-02-01 11:05:39 -05005156 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005157 if (err)
5158 goto out_fail;
5159
Chris Mason39279cc2007-06-12 06:35:45 -04005160 inode->i_op = &btrfs_dir_inode_operations;
5161 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005162
Chris Masondbe674a2008-07-17 12:54:05 -04005163 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005164 err = btrfs_update_inode(trans, root, inode);
5165 if (err)
5166 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005167
Josef Bacika1b075d2010-11-19 20:36:11 +00005168 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5169 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005170 if (err)
5171 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005172
Chris Mason39279cc2007-06-12 06:35:45 -04005173 d_instantiate(dentry, inode);
5174 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005175
5176out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005177 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005178 if (drop_on_err)
5179 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005180 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005181 return err;
5182}
5183
Chris Masond352ac62008-09-29 15:18:18 -04005184/* helper for btfs_get_extent. Given an existing extent in the tree,
5185 * and an extent that you want to insert, deal with overlap and insert
5186 * the new extent into the tree.
5187 */
Chris Mason3b951512008-04-17 11:29:12 -04005188static int merge_extent_mapping(struct extent_map_tree *em_tree,
5189 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005190 struct extent_map *em,
5191 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005192{
5193 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005194
Chris Masone6dcd2d2008-07-17 12:53:50 -04005195 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5196 start_diff = map_start - em->start;
5197 em->start = map_start;
5198 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005199 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5200 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005201 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005202 em->block_len -= start_diff;
5203 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005204 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005205}
5206
Chris Masonc8b97812008-10-29 14:49:59 -04005207static noinline int uncompress_inline(struct btrfs_path *path,
5208 struct inode *inode, struct page *page,
5209 size_t pg_offset, u64 extent_offset,
5210 struct btrfs_file_extent_item *item)
5211{
5212 int ret;
5213 struct extent_buffer *leaf = path->nodes[0];
5214 char *tmp;
5215 size_t max_size;
5216 unsigned long inline_size;
5217 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005218 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005219
5220 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005221 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005222 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5223 inline_size = btrfs_file_extent_inline_item_len(leaf,
5224 btrfs_item_nr(leaf, path->slots[0]));
5225 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005226 if (!tmp)
5227 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005228 ptr = btrfs_file_extent_inline_start(item);
5229
5230 read_extent_buffer(leaf, tmp, ptr, inline_size);
5231
Chris Mason5b050f02008-11-11 09:34:41 -05005232 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005233 ret = btrfs_decompress(compress_type, tmp, page,
5234 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005235 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005236 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005237 unsigned long copy_size = min_t(u64,
5238 PAGE_CACHE_SIZE - pg_offset,
5239 max_size - extent_offset);
5240 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005241 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005242 }
5243 kfree(tmp);
5244 return 0;
5245}
5246
Chris Masond352ac62008-09-29 15:18:18 -04005247/*
5248 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005249 * the ugly parts come from merging extents from the disk with the in-ram
5250 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005251 * where the in-ram extents might be locked pending data=ordered completion.
5252 *
5253 * This also copies inline extents directly into the page.
5254 */
Chris Masond3977122009-01-05 21:25:51 -05005255
Chris Masona52d9a82007-08-27 16:49:44 -04005256struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005257 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005258 int create)
5259{
5260 int ret;
5261 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005262 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005263 u64 extent_start = 0;
5264 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005265 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005266 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005267 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005268 struct btrfs_root *root = BTRFS_I(inode)->root;
5269 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005270 struct extent_buffer *leaf;
5271 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005272 struct extent_map *em = NULL;
5273 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005274 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005275 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005276 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005277
Chris Masona52d9a82007-08-27 16:49:44 -04005278again:
Chris Mason890871b2009-09-02 16:24:52 -04005279 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005280 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005281 if (em)
5282 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005283 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005284
Chris Masona52d9a82007-08-27 16:49:44 -04005285 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005286 if (em->start > start || em->start + em->len <= start)
5287 free_extent_map(em);
5288 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005289 free_extent_map(em);
5290 else
5291 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005292 }
David Sterba172ddd62011-04-21 00:48:27 +02005293 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005294 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005295 err = -ENOMEM;
5296 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005297 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005298 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005299 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005300 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005301 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005302 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005303
5304 if (!path) {
5305 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005306 if (!path) {
5307 err = -ENOMEM;
5308 goto out;
5309 }
5310 /*
5311 * Chances are we'll be called again, so go ahead and do
5312 * readahead
5313 */
5314 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005315 }
5316
Chris Mason179e29e2007-11-01 11:28:41 -04005317 ret = btrfs_lookup_file_extent(trans, root, path,
5318 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005319 if (ret < 0) {
5320 err = ret;
5321 goto out;
5322 }
5323
5324 if (ret != 0) {
5325 if (path->slots[0] == 0)
5326 goto not_found;
5327 path->slots[0]--;
5328 }
5329
Chris Mason5f39d392007-10-15 16:14:19 -04005330 leaf = path->nodes[0];
5331 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005332 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005333 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005334 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5335 found_type = btrfs_key_type(&found_key);
5336 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005337 found_type != BTRFS_EXTENT_DATA_KEY) {
5338 goto not_found;
5339 }
5340
Chris Mason5f39d392007-10-15 16:14:19 -04005341 found_type = btrfs_file_extent_type(leaf, item);
5342 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005343 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005344 if (found_type == BTRFS_FILE_EXTENT_REG ||
5345 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005346 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005347 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005348 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5349 size_t size;
5350 size = btrfs_file_extent_inline_len(leaf, item);
5351 extent_end = (extent_start + size + root->sectorsize - 1) &
5352 ~((u64)root->sectorsize - 1);
5353 }
5354
5355 if (start >= extent_end) {
5356 path->slots[0]++;
5357 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5358 ret = btrfs_next_leaf(root, path);
5359 if (ret < 0) {
5360 err = ret;
5361 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005362 }
Yan Zheng9036c102008-10-30 14:19:41 -04005363 if (ret > 0)
5364 goto not_found;
5365 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005366 }
Yan Zheng9036c102008-10-30 14:19:41 -04005367 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5368 if (found_key.objectid != objectid ||
5369 found_key.type != BTRFS_EXTENT_DATA_KEY)
5370 goto not_found;
5371 if (start + len <= found_key.offset)
5372 goto not_found;
5373 em->start = start;
5374 em->len = found_key.offset - start;
5375 goto not_found_em;
5376 }
5377
Yan Zhengd899e052008-10-30 14:25:28 -04005378 if (found_type == BTRFS_FILE_EXTENT_REG ||
5379 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005380 em->start = extent_start;
5381 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005382 em->orig_start = extent_start -
5383 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05005384 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5385 item);
Chris Masondb945352007-10-15 16:15:53 -04005386 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5387 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005388 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005389 goto insert;
5390 }
Li Zefan261507a02010-12-17 14:21:50 +08005391 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005392 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005393 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005394 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05005395 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005396 } else {
5397 bytenr += btrfs_file_extent_offset(leaf, item);
5398 em->block_start = bytenr;
5399 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005400 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5401 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005402 }
Chris Masona52d9a82007-08-27 16:49:44 -04005403 goto insert;
5404 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005405 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005406 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005407 size_t size;
5408 size_t extent_offset;
5409 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005410
Chris Masona52d9a82007-08-27 16:49:44 -04005411 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005412 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005413 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005414 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005415 goto out;
5416 }
5417
Yan Zheng9036c102008-10-30 14:19:41 -04005418 size = btrfs_file_extent_inline_len(leaf, item);
5419 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005420 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005421 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005422 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005423 em->len = (copy_size + root->sectorsize - 1) &
5424 ~((u64)root->sectorsize - 1);
Josef Bacikb4939682012-12-03 10:31:19 -05005425 em->orig_block_len = em->len;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005426 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005427 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005428 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005429 em->compress_type = compress_type;
5430 }
Yan689f9342007-10-29 11:41:07 -04005431 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005432 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005433 if (btrfs_file_extent_compression(leaf, item) !=
5434 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005435 ret = uncompress_inline(path, inode, page,
5436 pg_offset,
5437 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005438 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005439 } else {
5440 map = kmap(page);
5441 read_extent_buffer(leaf, map + pg_offset, ptr,
5442 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005443 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5444 memset(map + pg_offset + copy_size, 0,
5445 PAGE_CACHE_SIZE - pg_offset -
5446 copy_size);
5447 }
Chris Masonc8b97812008-10-29 14:49:59 -04005448 kunmap(page);
5449 }
Chris Mason179e29e2007-11-01 11:28:41 -04005450 flush_dcache_page(page);
5451 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005452 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005453 if (!trans) {
5454 kunmap(page);
5455 free_extent_map(em);
5456 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005457
David Sterbab3b4aa72011-04-21 01:20:15 +02005458 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005459 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005460
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005461 if (IS_ERR(trans))
5462 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005463 goto again;
5464 }
Chris Masonc8b97812008-10-29 14:49:59 -04005465 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005466 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005467 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005468 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005469 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005470 }
Chris Masond1310b22008-01-24 16:13:08 -05005471 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005472 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005473 goto insert;
5474 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005475 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005476 }
5477not_found:
5478 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005479 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005480not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005481 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005482 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005483insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005484 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005485 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005486 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5487 "[%llu %llu]\n", (unsigned long long)em->start,
5488 (unsigned long long)em->len,
5489 (unsigned long long)start,
5490 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005491 err = -EIO;
5492 goto out;
5493 }
Chris Masond1310b22008-01-24 16:13:08 -05005494
5495 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005496 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005497 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005498 /* it is possible that someone inserted the extent into the tree
5499 * while we had the lock dropped. It is also possible that
5500 * an overlapping map exists in the tree
5501 */
Chris Masona52d9a82007-08-27 16:49:44 -04005502 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005503 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005504
5505 ret = 0;
5506
Chris Mason3b951512008-04-17 11:29:12 -04005507 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005508 if (existing && (existing->start > start ||
5509 existing->start + existing->len <= start)) {
5510 free_extent_map(existing);
5511 existing = NULL;
5512 }
Chris Mason3b951512008-04-17 11:29:12 -04005513 if (!existing) {
5514 existing = lookup_extent_mapping(em_tree, em->start,
5515 em->len);
5516 if (existing) {
5517 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005518 em, start,
5519 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005520 free_extent_map(existing);
5521 if (err) {
5522 free_extent_map(em);
5523 em = NULL;
5524 }
5525 } else {
5526 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005527 free_extent_map(em);
5528 em = NULL;
5529 }
5530 } else {
5531 free_extent_map(em);
5532 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005533 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005534 }
Chris Masona52d9a82007-08-27 16:49:44 -04005535 }
Chris Mason890871b2009-09-02 16:24:52 -04005536 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005537out:
liubo1abe9b82011-03-24 11:18:59 +00005538
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005539 if (em)
5540 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005541
Chris Masonf4219502008-07-22 11:18:09 -04005542 if (path)
5543 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005544 if (trans) {
5545 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005546 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005547 err = ret;
5548 }
Chris Masona52d9a82007-08-27 16:49:44 -04005549 if (err) {
5550 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005551 return ERR_PTR(err);
5552 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005553 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005554 return em;
5555}
5556
Chris Masonec29ed52011-02-23 16:23:20 -05005557struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5558 size_t pg_offset, u64 start, u64 len,
5559 int create)
5560{
5561 struct extent_map *em;
5562 struct extent_map *hole_em = NULL;
5563 u64 range_start = start;
5564 u64 end;
5565 u64 found;
5566 u64 found_end;
5567 int err = 0;
5568
5569 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5570 if (IS_ERR(em))
5571 return em;
5572 if (em) {
5573 /*
5574 * if our em maps to a hole, there might
5575 * actually be delalloc bytes behind it
5576 */
5577 if (em->block_start != EXTENT_MAP_HOLE)
5578 return em;
5579 else
5580 hole_em = em;
5581 }
5582
5583 /* check to see if we've wrapped (len == -1 or similar) */
5584 end = start + len;
5585 if (end < start)
5586 end = (u64)-1;
5587 else
5588 end -= 1;
5589
5590 em = NULL;
5591
5592 /* ok, we didn't find anything, lets look for delalloc */
5593 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5594 end, len, EXTENT_DELALLOC, 1);
5595 found_end = range_start + found;
5596 if (found_end < range_start)
5597 found_end = (u64)-1;
5598
5599 /*
5600 * we didn't find anything useful, return
5601 * the original results from get_extent()
5602 */
5603 if (range_start > end || found_end <= start) {
5604 em = hole_em;
5605 hole_em = NULL;
5606 goto out;
5607 }
5608
5609 /* adjust the range_start to make sure it doesn't
5610 * go backwards from the start they passed in
5611 */
5612 range_start = max(start,range_start);
5613 found = found_end - range_start;
5614
5615 if (found > 0) {
5616 u64 hole_start = start;
5617 u64 hole_len = len;
5618
David Sterba172ddd62011-04-21 00:48:27 +02005619 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005620 if (!em) {
5621 err = -ENOMEM;
5622 goto out;
5623 }
5624 /*
5625 * when btrfs_get_extent can't find anything it
5626 * returns one huge hole
5627 *
5628 * make sure what it found really fits our range, and
5629 * adjust to make sure it is based on the start from
5630 * the caller
5631 */
5632 if (hole_em) {
5633 u64 calc_end = extent_map_end(hole_em);
5634
5635 if (calc_end <= start || (hole_em->start > end)) {
5636 free_extent_map(hole_em);
5637 hole_em = NULL;
5638 } else {
5639 hole_start = max(hole_em->start, start);
5640 hole_len = calc_end - hole_start;
5641 }
5642 }
5643 em->bdev = NULL;
5644 if (hole_em && range_start > hole_start) {
5645 /* our hole starts before our delalloc, so we
5646 * have to return just the parts of the hole
5647 * that go until the delalloc starts
5648 */
5649 em->len = min(hole_len,
5650 range_start - hole_start);
5651 em->start = hole_start;
5652 em->orig_start = hole_start;
5653 /*
5654 * don't adjust block start at all,
5655 * it is fixed at EXTENT_MAP_HOLE
5656 */
5657 em->block_start = hole_em->block_start;
5658 em->block_len = hole_len;
5659 } else {
5660 em->start = range_start;
5661 em->len = found;
5662 em->orig_start = range_start;
5663 em->block_start = EXTENT_MAP_DELALLOC;
5664 em->block_len = found;
5665 }
5666 } else if (hole_em) {
5667 return hole_em;
5668 }
5669out:
5670
5671 free_extent_map(hole_em);
5672 if (err) {
5673 free_extent_map(em);
5674 return ERR_PTR(err);
5675 }
5676 return em;
5677}
5678
Josef Bacik4b46fce2010-05-23 11:00:55 -04005679static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005680 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005681 u64 start, u64 len)
5682{
5683 struct btrfs_root *root = BTRFS_I(inode)->root;
5684 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005685 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5686 struct btrfs_key ins;
5687 u64 alloc_hint;
5688 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005689 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005690
Josef Bacik16d299a2011-04-06 14:53:07 -04005691 /*
5692 * Ok if the extent map we looked up is a hole and is for the exact
5693 * range we want, there is no reason to allocate a new one, however if
5694 * it is not right then we need to free this one and drop the cache for
5695 * our range.
5696 */
5697 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5698 em->len != len) {
5699 free_extent_map(em);
5700 em = NULL;
5701 insert = true;
5702 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5703 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005704
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005705 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005706 if (IS_ERR(trans))
5707 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005708
5709 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5710
5711 alloc_hint = get_extent_allocation_hint(inode, start, len);
5712 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005713 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005714 if (ret) {
5715 em = ERR_PTR(ret);
5716 goto out;
5717 }
5718
Josef Bacik4b46fce2010-05-23 11:00:55 -04005719 if (!em) {
David Sterba172ddd62011-04-21 00:48:27 +02005720 em = alloc_extent_map();
Josef Bacik16d299a2011-04-06 14:53:07 -04005721 if (!em) {
5722 em = ERR_PTR(-ENOMEM);
5723 goto out;
5724 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005725 }
5726
5727 em->start = start;
5728 em->orig_start = em->start;
5729 em->len = ins.offset;
5730
5731 em->block_start = ins.objectid;
5732 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05005733 em->orig_block_len = ins.offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005734 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005735
5736 /*
5737 * We need to do this because if we're using the original em we searched
5738 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5739 */
5740 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005741 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5742
Josef Bacik16d299a2011-04-06 14:53:07 -04005743 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005744 write_lock(&em_tree->lock);
5745 ret = add_extent_mapping(em_tree, em);
5746 write_unlock(&em_tree->lock);
5747 if (ret != -EEXIST)
5748 break;
5749 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5750 }
5751
5752 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5753 ins.offset, ins.offset, 0);
5754 if (ret) {
5755 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5756 em = ERR_PTR(ret);
5757 }
5758out:
5759 btrfs_end_transaction(trans, root);
5760 return em;
5761}
5762
Chris Mason46bfbb52010-05-26 11:04:10 -04005763/*
5764 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5765 * block must be cow'd
5766 */
5767static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5768 struct inode *inode, u64 offset, u64 len)
5769{
5770 struct btrfs_path *path;
5771 int ret;
5772 struct extent_buffer *leaf;
5773 struct btrfs_root *root = BTRFS_I(inode)->root;
5774 struct btrfs_file_extent_item *fi;
5775 struct btrfs_key key;
5776 u64 disk_bytenr;
5777 u64 backref_offset;
5778 u64 extent_end;
5779 u64 num_bytes;
5780 int slot;
5781 int found_type;
5782
5783 path = btrfs_alloc_path();
5784 if (!path)
5785 return -ENOMEM;
5786
Li Zefan33345d012011-04-20 10:31:50 +08005787 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005788 offset, 0);
5789 if (ret < 0)
5790 goto out;
5791
5792 slot = path->slots[0];
5793 if (ret == 1) {
5794 if (slot == 0) {
5795 /* can't find the item, must cow */
5796 ret = 0;
5797 goto out;
5798 }
5799 slot--;
5800 }
5801 ret = 0;
5802 leaf = path->nodes[0];
5803 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005804 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005805 key.type != BTRFS_EXTENT_DATA_KEY) {
5806 /* not our file or wrong item type, must cow */
5807 goto out;
5808 }
5809
5810 if (key.offset > offset) {
5811 /* Wrong offset, must cow */
5812 goto out;
5813 }
5814
5815 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5816 found_type = btrfs_file_extent_type(leaf, fi);
5817 if (found_type != BTRFS_FILE_EXTENT_REG &&
5818 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5819 /* not a regular extent, must cow */
5820 goto out;
5821 }
5822 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5823 backref_offset = btrfs_file_extent_offset(leaf, fi);
5824
5825 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5826 if (extent_end < offset + len) {
5827 /* extent doesn't include our full range, must cow */
5828 goto out;
5829 }
5830
5831 if (btrfs_extent_readonly(root, disk_bytenr))
5832 goto out;
5833
5834 /*
5835 * look for other files referencing this extent, if we
5836 * find any we must cow
5837 */
Li Zefan33345d012011-04-20 10:31:50 +08005838 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005839 key.offset - backref_offset, disk_bytenr))
5840 goto out;
5841
5842 /*
5843 * adjust disk_bytenr and num_bytes to cover just the bytes
5844 * in this extent we are about to write. If there
5845 * are any csums in that range we have to cow in order
5846 * to keep the csums correct
5847 */
5848 disk_bytenr += backref_offset;
5849 disk_bytenr += offset - key.offset;
5850 num_bytes = min(offset + len, extent_end) - offset;
5851 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5852 goto out;
5853 /*
5854 * all of the above have passed, it is safe to overwrite this extent
5855 * without cow
5856 */
5857 ret = 1;
5858out:
5859 btrfs_free_path(path);
5860 return ret;
5861}
5862
Josef Bacikeb838e72012-07-31 16:28:48 -04005863static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5864 struct extent_state **cached_state, int writing)
5865{
5866 struct btrfs_ordered_extent *ordered;
5867 int ret = 0;
5868
5869 while (1) {
5870 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5871 0, cached_state);
5872 /*
5873 * We're concerned with the entire range that we're going to be
5874 * doing DIO to, so we need to make sure theres no ordered
5875 * extents in this range.
5876 */
5877 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5878 lockend - lockstart + 1);
5879
5880 /*
5881 * We need to make sure there are no buffered pages in this
5882 * range either, we could have raced between the invalidate in
5883 * generic_file_direct_write and locking the extent. The
5884 * invalidate needs to happen so that reads after a write do not
5885 * get stale data.
5886 */
5887 if (!ordered && (!writing ||
5888 !test_range_bit(&BTRFS_I(inode)->io_tree,
5889 lockstart, lockend, EXTENT_UPTODATE, 0,
5890 *cached_state)))
5891 break;
5892
5893 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5894 cached_state, GFP_NOFS);
5895
5896 if (ordered) {
5897 btrfs_start_ordered_extent(inode, ordered, 1);
5898 btrfs_put_ordered_extent(ordered);
5899 } else {
5900 /* Screw you mmap */
5901 ret = filemap_write_and_wait_range(inode->i_mapping,
5902 lockstart,
5903 lockend);
5904 if (ret)
5905 break;
5906
5907 /*
5908 * If we found a page that couldn't be invalidated just
5909 * fall back to buffered.
5910 */
5911 ret = invalidate_inode_pages2_range(inode->i_mapping,
5912 lockstart >> PAGE_CACHE_SHIFT,
5913 lockend >> PAGE_CACHE_SHIFT);
5914 if (ret)
5915 break;
5916 }
5917
5918 cond_resched();
5919 }
5920
5921 return ret;
5922}
5923
Josef Bacik69ffb542012-09-11 15:40:07 -04005924static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5925 u64 len, u64 orig_start,
5926 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05005927 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04005928{
5929 struct extent_map_tree *em_tree;
5930 struct extent_map *em;
5931 struct btrfs_root *root = BTRFS_I(inode)->root;
5932 int ret;
5933
5934 em_tree = &BTRFS_I(inode)->extent_tree;
5935 em = alloc_extent_map();
5936 if (!em)
5937 return ERR_PTR(-ENOMEM);
5938
5939 em->start = start;
5940 em->orig_start = orig_start;
5941 em->len = len;
5942 em->block_len = block_len;
5943 em->block_start = block_start;
5944 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05005945 em->orig_block_len = orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04005946 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5947 if (type == BTRFS_ORDERED_PREALLOC)
5948 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
5949
5950 do {
5951 btrfs_drop_extent_cache(inode, em->start,
5952 em->start + em->len - 1, 0);
5953 write_lock(&em_tree->lock);
5954 ret = add_extent_mapping(em_tree, em);
5955 write_unlock(&em_tree->lock);
5956 } while (ret == -EEXIST);
5957
5958 if (ret) {
5959 free_extent_map(em);
5960 return ERR_PTR(ret);
5961 }
5962
5963 return em;
5964}
5965
5966
Josef Bacik4b46fce2010-05-23 11:00:55 -04005967static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5968 struct buffer_head *bh_result, int create)
5969{
5970 struct extent_map *em;
5971 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04005972 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005973 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04005974 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005975 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005976 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04005977 int unlock_bits = EXTENT_LOCKED;
5978 int ret;
5979
Josef Bacikeb838e72012-07-31 16:28:48 -04005980 if (create) {
5981 ret = btrfs_delalloc_reserve_space(inode, len);
5982 if (ret)
5983 return ret;
5984 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04005985 } else {
5986 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04005987 }
5988
Josef Bacikc3298612012-08-03 16:49:19 -04005989 lockstart = start;
5990 lockend = start + len - 1;
5991
Josef Bacikeb838e72012-07-31 16:28:48 -04005992 /*
5993 * If this errors out it's because we couldn't invalidate pagecache for
5994 * this range and we need to fallback to buffered.
5995 */
5996 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
5997 return -ENOTBLK;
5998
5999 if (create) {
6000 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6001 lockend, EXTENT_DELALLOC, NULL,
6002 &cached_state, GFP_NOFS);
6003 if (ret)
6004 goto unlock_err;
6005 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006006
6007 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006008 if (IS_ERR(em)) {
6009 ret = PTR_ERR(em);
6010 goto unlock_err;
6011 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006012
6013 /*
6014 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6015 * io. INLINE is special, and we could probably kludge it in here, but
6016 * it's still buffered so for safety lets just fall back to the generic
6017 * buffered path.
6018 *
6019 * For COMPRESSED we _have_ to read the entire extent in so we can
6020 * decompress it, so there will be buffering required no matter what we
6021 * do, so go ahead and fallback to buffered.
6022 *
6023 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6024 * to buffered IO. Don't blame me, this is the price we pay for using
6025 * the generic code.
6026 */
6027 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6028 em->block_start == EXTENT_MAP_INLINE) {
6029 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006030 ret = -ENOTBLK;
6031 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006032 }
6033
6034 /* Just a good old fashioned hole, return */
6035 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6036 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6037 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006038 ret = 0;
6039 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006040 }
6041
6042 /*
6043 * We don't allocate a new extent in the following cases
6044 *
6045 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6046 * existing extent.
6047 * 2) The extent is marked as PREALLOC. We're good to go here and can
6048 * just use the extent.
6049 *
6050 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006051 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006052 len = min(len, em->len - (start - em->start));
6053 lockstart = start + len;
6054 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006055 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006056
6057 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6058 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6059 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006060 int type;
6061 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006062 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006063
6064 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6065 type = BTRFS_ORDERED_PREALLOC;
6066 else
6067 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006068 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006069 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006070
6071 /*
6072 * we're not going to log anything, but we do need
6073 * to make sure the current transaction stays open
6074 * while we look for nocow cross refs
6075 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006076 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006077 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006078 goto must_cow;
6079
6080 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006081 u64 orig_start = em->start;
Josef Bacikb4939682012-12-03 10:31:19 -05006082 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006083
6084 if (type == BTRFS_ORDERED_PREALLOC) {
6085 free_extent_map(em);
6086 em = create_pinned_em(inode, start, len,
6087 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006088 block_start, len,
6089 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006090 if (IS_ERR(em)) {
6091 btrfs_end_transaction(trans, root);
6092 goto unlock_err;
6093 }
6094 }
6095
Chris Mason46bfbb52010-05-26 11:04:10 -04006096 ret = btrfs_add_ordered_extent_dio(inode, start,
6097 block_start, len, len, type);
6098 btrfs_end_transaction(trans, root);
6099 if (ret) {
6100 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006101 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006102 }
6103 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006104 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006105 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006106 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006107must_cow:
6108 /*
6109 * this will cow the extent, reset the len in case we changed
6110 * it above
6111 */
6112 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04006113 em = btrfs_new_extent_direct(inode, em, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006114 if (IS_ERR(em)) {
6115 ret = PTR_ERR(em);
6116 goto unlock_err;
6117 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006118 len = min(len, em->len - (start - em->start));
6119unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006120 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6121 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006122 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006123 bh_result->b_bdev = em->bdev;
6124 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006125 if (create) {
6126 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6127 set_buffer_new(bh_result);
6128
6129 /*
6130 * Need to update the i_size under the extent lock so buffered
6131 * readers will get the updated i_size when we unlock.
6132 */
6133 if (start + len > i_size_read(inode))
6134 i_size_write(inode, start + len);
6135 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006136
Josef Bacikeb838e72012-07-31 16:28:48 -04006137 /*
6138 * In the case of write we need to clear and unlock the entire range,
6139 * in the case of read we need to unlock only the end area that we
6140 * aren't using if there is any left over space.
6141 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006142 if (lockstart < lockend) {
6143 if (create && len < lockend - lockstart) {
6144 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006145 lockstart + len - 1,
6146 unlock_bits | EXTENT_DEFRAG, 1, 0,
Liu Bo24c03fa2012-08-22 20:10:38 -06006147 &cached_state, GFP_NOFS);
6148 /*
6149 * Beside unlock, we also need to cleanup reserved space
6150 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6151 */
6152 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6153 lockstart + len, lockend,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006154 unlock_bits | EXTENT_DO_ACCOUNTING |
6155 EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006156 } else {
6157 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6158 lockend, unlock_bits, 1, 0,
6159 &cached_state, GFP_NOFS);
6160 }
6161 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006162 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006163 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006164
Josef Bacik4b46fce2010-05-23 11:00:55 -04006165 free_extent_map(em);
6166
6167 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006168
6169unlock_err:
6170 if (create)
6171 unlock_bits |= EXTENT_DO_ACCOUNTING;
6172
6173 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6174 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6175 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006176}
6177
6178struct btrfs_dio_private {
6179 struct inode *inode;
6180 u64 logical_offset;
6181 u64 disk_bytenr;
6182 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006183 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006184
6185 /* number of bios pending for this dio */
6186 atomic_t pending_bios;
6187
6188 /* IO errors */
6189 int errors;
6190
6191 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006192};
6193
6194static void btrfs_endio_direct_read(struct bio *bio, int err)
6195{
Miao Xiee65e1532010-11-22 03:04:43 +00006196 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006197 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6198 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006199 struct inode *inode = dip->inode;
6200 struct btrfs_root *root = BTRFS_I(inode)->root;
6201 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006202
6203 start = dip->logical_offset;
6204 do {
6205 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6206 struct page *page = bvec->bv_page;
6207 char *kaddr;
6208 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006209 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006210 unsigned long flags;
6211
Josef Bacikc3298612012-08-03 16:49:19 -04006212 if (get_state_private(&BTRFS_I(inode)->io_tree,
6213 start, &private))
6214 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006215 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006216 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006217 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6218 csum, bvec->bv_len);
6219 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006220 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006221 local_irq_restore(flags);
6222
6223 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006224 if (csum != private) {
6225failed:
Li Zefan33345d012011-04-20 10:31:50 +08006226 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006227 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006228 (unsigned long long)btrfs_ino(inode),
6229 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006230 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006231 err = -EIO;
6232 }
6233 }
6234
6235 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006236 bvec++;
6237 } while (bvec <= bvec_end);
6238
6239 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006240 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006241 bio->bi_private = dip->private;
6242
Josef Bacik4b46fce2010-05-23 11:00:55 -04006243 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006244
6245 /* If we had a csum failure make sure to clear the uptodate flag */
6246 if (err)
6247 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006248 dio_end_io(bio, err);
6249}
6250
6251static void btrfs_endio_direct_write(struct bio *bio, int err)
6252{
6253 struct btrfs_dio_private *dip = bio->bi_private;
6254 struct inode *inode = dip->inode;
6255 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006256 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006257 u64 ordered_offset = dip->logical_offset;
6258 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006259 int ret;
6260
6261 if (err)
6262 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006263again:
6264 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6265 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006266 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006267 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006268 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006269
Josef Bacik5fd02042012-05-02 14:00:54 -04006270 ordered->work.func = finish_ordered_fn;
6271 ordered->work.flags = 0;
6272 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6273 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006274out_test:
6275 /*
6276 * our bio might span multiple ordered extents. If we haven't
6277 * completed the accounting for the whole dio, go back and try again
6278 */
6279 if (ordered_offset < dip->logical_offset + dip->bytes) {
6280 ordered_bytes = dip->logical_offset + dip->bytes -
6281 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006282 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006283 goto again;
6284 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006285out_done:
6286 bio->bi_private = dip->private;
6287
Josef Bacik4b46fce2010-05-23 11:00:55 -04006288 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006289
6290 /* If we had an error make sure to clear the uptodate flag */
6291 if (err)
6292 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006293 dio_end_io(bio, err);
6294}
6295
Chris Masoneaf25d92010-05-25 09:48:28 -04006296static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6297 struct bio *bio, int mirror_num,
6298 unsigned long bio_flags, u64 offset)
6299{
6300 int ret;
6301 struct btrfs_root *root = BTRFS_I(inode)->root;
6302 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006303 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006304 return 0;
6305}
6306
Miao Xiee65e1532010-11-22 03:04:43 +00006307static void btrfs_end_dio_bio(struct bio *bio, int err)
6308{
6309 struct btrfs_dio_private *dip = bio->bi_private;
6310
6311 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006312 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006313 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006314 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006315 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006316 dip->errors = 1;
6317
6318 /*
6319 * before atomic variable goto zero, we must make sure
6320 * dip->errors is perceived to be set.
6321 */
6322 smp_mb__before_atomic_dec();
6323 }
6324
6325 /* if there are more bios still pending for this dio, just exit */
6326 if (!atomic_dec_and_test(&dip->pending_bios))
6327 goto out;
6328
6329 if (dip->errors)
6330 bio_io_error(dip->orig_bio);
6331 else {
6332 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6333 bio_endio(dip->orig_bio, 0);
6334 }
6335out:
6336 bio_put(bio);
6337}
6338
6339static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6340 u64 first_sector, gfp_t gfp_flags)
6341{
6342 int nr_vecs = bio_get_nr_vecs(bdev);
6343 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6344}
6345
6346static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6347 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006348 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006349{
6350 int write = rw & REQ_WRITE;
6351 struct btrfs_root *root = BTRFS_I(inode)->root;
6352 int ret;
6353
Josef Bacikb812ce22012-11-16 13:56:32 -05006354 if (async_submit)
6355 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6356
Miao Xiee65e1532010-11-22 03:04:43 +00006357 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006358
6359 if (!write) {
6360 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6361 if (ret)
6362 goto err;
6363 }
Miao Xiee65e1532010-11-22 03:04:43 +00006364
Josef Bacik1ae39932011-04-06 14:41:34 -04006365 if (skip_sum)
6366 goto map;
6367
6368 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006369 ret = btrfs_wq_submit_bio(root->fs_info,
6370 inode, rw, bio, 0, 0,
6371 file_offset,
6372 __btrfs_submit_bio_start_direct_io,
6373 __btrfs_submit_bio_done);
6374 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006375 } else if (write) {
6376 /*
6377 * If we aren't doing async submit, calculate the csum of the
6378 * bio now.
6379 */
6380 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6381 if (ret)
6382 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006383 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006384 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006385 if (ret)
6386 goto err;
6387 }
Miao Xiee65e1532010-11-22 03:04:43 +00006388
Josef Bacik1ae39932011-04-06 14:41:34 -04006389map:
6390 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006391err:
6392 bio_put(bio);
6393 return ret;
6394}
6395
6396static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6397 int skip_sum)
6398{
6399 struct inode *inode = dip->inode;
6400 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006401 struct bio *bio;
6402 struct bio *orig_bio = dip->orig_bio;
6403 struct bio_vec *bvec = orig_bio->bi_io_vec;
6404 u64 start_sector = orig_bio->bi_sector;
6405 u64 file_offset = dip->logical_offset;
6406 u64 submit_len = 0;
6407 u64 map_length;
6408 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006409 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006410 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006411
Miao Xiee65e1532010-11-22 03:04:43 +00006412 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006413 ret = btrfs_map_block(root->fs_info, READ, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006414 &map_length, NULL, 0);
6415 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006416 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006417 return -EIO;
6418 }
6419
Josef Bacik02f57c72011-04-06 14:25:44 -04006420 if (map_length >= orig_bio->bi_size) {
6421 bio = orig_bio;
6422 goto submit;
6423 }
6424
Josef Bacik1ae39932011-04-06 14:41:34 -04006425 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006426 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6427 if (!bio)
6428 return -ENOMEM;
6429 bio->bi_private = dip;
6430 bio->bi_end_io = btrfs_end_dio_bio;
6431 atomic_inc(&dip->pending_bios);
6432
Miao Xiee65e1532010-11-22 03:04:43 +00006433 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6434 if (unlikely(map_length < submit_len + bvec->bv_len ||
6435 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6436 bvec->bv_offset) < bvec->bv_len)) {
6437 /*
6438 * inc the count before we submit the bio so
6439 * we know the end IO handler won't happen before
6440 * we inc the count. Otherwise, the dip might get freed
6441 * before we're done setting it up
6442 */
6443 atomic_inc(&dip->pending_bios);
6444 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6445 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006446 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006447 if (ret) {
6448 bio_put(bio);
6449 atomic_dec(&dip->pending_bios);
6450 goto out_err;
6451 }
6452
Miao Xiee65e1532010-11-22 03:04:43 +00006453 start_sector += submit_len >> 9;
6454 file_offset += submit_len;
6455
6456 submit_len = 0;
6457 nr_pages = 0;
6458
6459 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6460 start_sector, GFP_NOFS);
6461 if (!bio)
6462 goto out_err;
6463 bio->bi_private = dip;
6464 bio->bi_end_io = btrfs_end_dio_bio;
6465
6466 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006467 ret = btrfs_map_block(root->fs_info, READ,
6468 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006469 &map_length, NULL, 0);
6470 if (ret) {
6471 bio_put(bio);
6472 goto out_err;
6473 }
6474 } else {
6475 submit_len += bvec->bv_len;
6476 nr_pages ++;
6477 bvec++;
6478 }
6479 }
6480
Josef Bacik02f57c72011-04-06 14:25:44 -04006481submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006482 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006483 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006484 if (!ret)
6485 return 0;
6486
6487 bio_put(bio);
6488out_err:
6489 dip->errors = 1;
6490 /*
6491 * before atomic variable goto zero, we must
6492 * make sure dip->errors is perceived to be set.
6493 */
6494 smp_mb__before_atomic_dec();
6495 if (atomic_dec_and_test(&dip->pending_bios))
6496 bio_io_error(dip->orig_bio);
6497
6498 /* bio_end_io() will handle error, so we needn't return it */
6499 return 0;
6500}
6501
Josef Bacik4b46fce2010-05-23 11:00:55 -04006502static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6503 loff_t file_offset)
6504{
6505 struct btrfs_root *root = BTRFS_I(inode)->root;
6506 struct btrfs_dio_private *dip;
6507 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006508 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006509 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006510 int ret = 0;
6511
6512 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6513
6514 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6515 if (!dip) {
6516 ret = -ENOMEM;
6517 goto free_ordered;
6518 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006519
6520 dip->private = bio->bi_private;
6521 dip->inode = inode;
6522 dip->logical_offset = file_offset;
6523
Josef Bacik4b46fce2010-05-23 11:00:55 -04006524 dip->bytes = 0;
6525 do {
6526 dip->bytes += bvec->bv_len;
6527 bvec++;
6528 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6529
Chris Mason46bfbb52010-05-26 11:04:10 -04006530 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006531 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006532 dip->errors = 0;
6533 dip->orig_bio = bio;
6534 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006535
6536 if (write)
6537 bio->bi_end_io = btrfs_endio_direct_write;
6538 else
6539 bio->bi_end_io = btrfs_endio_direct_read;
6540
Miao Xiee65e1532010-11-22 03:04:43 +00006541 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6542 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006543 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006544free_ordered:
6545 /*
6546 * If this is a write, we need to clean up the reserved space and kill
6547 * the ordered extent.
6548 */
6549 if (write) {
6550 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006551 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006552 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6553 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6554 btrfs_free_reserved_extent(root, ordered->start,
6555 ordered->disk_len);
6556 btrfs_put_ordered_extent(ordered);
6557 btrfs_put_ordered_extent(ordered);
6558 }
6559 bio_endio(bio, ret);
6560}
6561
Chris Mason5a5f79b2010-05-26 21:33:37 -04006562static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6563 const struct iovec *iov, loff_t offset,
6564 unsigned long nr_segs)
6565{
6566 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006567 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006568 size_t size;
6569 unsigned long addr;
6570 unsigned blocksize_mask = root->sectorsize - 1;
6571 ssize_t retval = -EINVAL;
6572 loff_t end = offset;
6573
6574 if (offset & blocksize_mask)
6575 goto out;
6576
6577 /* Check the memory alignment. Blocks cannot straddle pages */
6578 for (seg = 0; seg < nr_segs; seg++) {
6579 addr = (unsigned long)iov[seg].iov_base;
6580 size = iov[seg].iov_len;
6581 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006582 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006583 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006584
6585 /* If this is a write we don't need to check anymore */
6586 if (rw & WRITE)
6587 continue;
6588
6589 /*
6590 * Check to make sure we don't have duplicate iov_base's in this
6591 * iovec, if so return EINVAL, otherwise we'll get csum errors
6592 * when reading back.
6593 */
6594 for (i = seg + 1; i < nr_segs; i++) {
6595 if (iov[seg].iov_base == iov[i].iov_base)
6596 goto out;
6597 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006598 }
6599 retval = 0;
6600out:
6601 return retval;
6602}
Josef Bacikeb838e72012-07-31 16:28:48 -04006603
Chris Mason16432982008-04-10 10:23:21 -04006604static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6605 const struct iovec *iov, loff_t offset,
6606 unsigned long nr_segs)
6607{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006608 struct file *file = iocb->ki_filp;
6609 struct inode *inode = file->f_mapping->host;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006610
Chris Mason5a5f79b2010-05-26 21:33:37 -04006611 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006612 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006613 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006614
Josef Bacikeb838e72012-07-31 16:28:48 -04006615 return __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006616 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6617 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6618 btrfs_submit_direct, 0);
Chris Mason16432982008-04-10 10:23:21 -04006619}
6620
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006621#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6622
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006623static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6624 __u64 start, __u64 len)
6625{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006626 int ret;
6627
6628 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6629 if (ret)
6630 return ret;
6631
Chris Masonec29ed52011-02-23 16:23:20 -05006632 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006633}
6634
Chris Mason9ebefb182007-06-15 13:50:00 -04006635int btrfs_readpage(struct file *file, struct page *page)
6636{
Chris Masond1310b22008-01-24 16:13:08 -05006637 struct extent_io_tree *tree;
6638 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006639 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006640}
Chris Mason1832a6d2007-12-21 16:27:21 -05006641
Chris Mason39279cc2007-06-12 06:35:45 -04006642static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6643{
Chris Masond1310b22008-01-24 16:13:08 -05006644 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04006645
6646
6647 if (current->flags & PF_MEMALLOC) {
6648 redirty_page_for_writepage(wbc, page);
6649 unlock_page(page);
6650 return 0;
6651 }
Chris Masond1310b22008-01-24 16:13:08 -05006652 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006653 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6654}
Chris Mason39279cc2007-06-12 06:35:45 -04006655
Chris Masonf4219502008-07-22 11:18:09 -04006656int btrfs_writepages(struct address_space *mapping,
6657 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04006658{
Chris Masond1310b22008-01-24 16:13:08 -05006659 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006660
Chris Masond1310b22008-01-24 16:13:08 -05006661 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04006662 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6663}
6664
Chris Mason3ab2fb52007-11-08 10:59:22 -05006665static int
6666btrfs_readpages(struct file *file, struct address_space *mapping,
6667 struct list_head *pages, unsigned nr_pages)
6668{
Chris Masond1310b22008-01-24 16:13:08 -05006669 struct extent_io_tree *tree;
6670 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006671 return extent_readpages(tree, mapping, pages, nr_pages,
6672 btrfs_get_extent);
6673}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006674static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006675{
Chris Masond1310b22008-01-24 16:13:08 -05006676 struct extent_io_tree *tree;
6677 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006678 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006679
Chris Masond1310b22008-01-24 16:13:08 -05006680 tree = &BTRFS_I(page->mapping->host)->io_tree;
6681 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006682 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006683 if (ret == 1) {
6684 ClearPagePrivate(page);
6685 set_page_private(page, 0);
6686 page_cache_release(page);
6687 }
6688 return ret;
6689}
Chris Mason39279cc2007-06-12 06:35:45 -04006690
Chris Masone6dcd2d2008-07-17 12:53:50 -04006691static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6692{
Chris Mason98509cf2008-09-11 15:51:43 -04006693 if (PageWriteback(page) || PageDirty(page))
6694 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006695 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006696}
6697
Chris Masona52d9a82007-08-27 16:49:44 -04006698static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6699{
Josef Bacik5fd02042012-05-02 14:00:54 -04006700 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006701 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006702 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006703 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006704 u64 page_start = page_offset(page);
6705 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006706
Chris Mason8b62b722009-09-02 16:53:46 -04006707 /*
6708 * we have the page locked, so new writeback can't start,
6709 * and the dirty bit won't be cleared while we are here.
6710 *
6711 * Wait for IO on this page so that we can safely clear
6712 * the PagePrivate2 bit and do ordered accounting
6713 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006714 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006715
Josef Bacik5fd02042012-05-02 14:00:54 -04006716 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006717 if (offset) {
6718 btrfs_releasepage(page, GFP_NOFS);
6719 return;
6720 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006721 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Josef Bacik5fd02042012-05-02 14:00:54 -04006722 ordered = btrfs_lookup_ordered_extent(inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006723 page_offset(page));
6724 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006725 /*
6726 * IO on this page will never be started, so we need
6727 * to account for any ordered extents now
6728 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006729 clear_extent_bit(tree, page_start, page_end,
6730 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006731 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6732 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006733 /*
6734 * whoever cleared the private bit is responsible
6735 * for the finish_ordered_io
6736 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006737 if (TestClearPagePrivate2(page) &&
6738 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6739 PAGE_CACHE_SIZE, 1)) {
6740 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006741 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006742 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006743 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006744 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006745 }
6746 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006747 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006748 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6749 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006750 __btrfs_releasepage(page, GFP_NOFS);
6751
Chris Mason4a096752008-07-21 10:29:44 -04006752 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006753 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006754 ClearPagePrivate(page);
6755 set_page_private(page, 0);
6756 page_cache_release(page);
6757 }
Chris Mason39279cc2007-06-12 06:35:45 -04006758}
6759
Chris Mason9ebefb182007-06-15 13:50:00 -04006760/*
6761 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6762 * called from a page fault handler when a page is first dirtied. Hence we must
6763 * be careful to check for EOF conditions here. We set the page up correctly
6764 * for a written page which means we get ENOSPC checking when writing into
6765 * holes and correct delalloc and unwritten extent mapping on filesystems that
6766 * support these features.
6767 *
6768 * We are not allowed to take the i_mutex here so we have to play games to
6769 * protect against truncate races as the page could now be beyond EOF. Because
6770 * vmtruncate() writes the inode size before removing pages, once we have the
6771 * page lock we can determine safely if the page is beyond EOF. If it is not
6772 * beyond EOF, then the page is guaranteed safe against truncation until we
6773 * unlock the page.
6774 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006775int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006776{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006777 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006778 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006779 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006780 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6781 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006782 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006783 char *kaddr;
6784 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006785 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006786 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006787 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006788 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006789 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006790
Jan Karab2b5ef52012-06-12 16:20:45 +02006791 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006792 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006793 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006794 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006795 reserved = 1;
6796 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006797 if (ret) {
6798 if (ret == -ENOMEM)
6799 ret = VM_FAULT_OOM;
6800 else /* -ENOSPC, -EIO, etc */
6801 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006802 if (reserved)
6803 goto out;
6804 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006805 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006806
Nick Piggin56a76f82009-03-31 15:23:23 -07006807 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006808again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006809 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006810 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006811 page_start = page_offset(page);
6812 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006813
Chris Mason9ebefb182007-06-15 13:50:00 -04006814 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006815 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006816 /* page got truncated out from underneath us */
6817 goto out_unlock;
6818 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006819 wait_on_page_writeback(page);
6820
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006821 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006822 set_page_extent_mapped(page);
6823
Chris Masoneb84ae02008-07-17 13:53:27 -04006824 /*
6825 * we can't set the delalloc bits if there are pending ordered
6826 * extents. Drop our locks and wait for them to finish
6827 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006828 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6829 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006830 unlock_extent_cached(io_tree, page_start, page_end,
6831 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006832 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006833 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006834 btrfs_put_ordered_extent(ordered);
6835 goto again;
6836 }
6837
Josef Bacikfbf19082009-10-01 17:10:23 -04006838 /*
6839 * XXX - page_mkwrite gets called every time the page is dirtied, even
6840 * if it was already dirty, so for space accounting reasons we need to
6841 * clear any delalloc bits for the range we are fixing to save. There
6842 * is probably a better way to do this, but for now keep consistent with
6843 * prepare_pages in the normal write path.
6844 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006845 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006846 EXTENT_DIRTY | EXTENT_DELALLOC |
6847 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006848 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006849
Josef Bacik2ac55d42010-02-03 19:33:23 +00006850 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6851 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006852 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006853 unlock_extent_cached(io_tree, page_start, page_end,
6854 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006855 ret = VM_FAULT_SIGBUS;
6856 goto out_unlock;
6857 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006858 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006859
6860 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006861 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006862 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006863 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006864 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006865
Chris Masone6dcd2d2008-07-17 12:53:50 -04006866 if (zero_start != PAGE_CACHE_SIZE) {
6867 kaddr = kmap(page);
6868 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6869 flush_dcache_page(page);
6870 kunmap(page);
6871 }
Chris Mason247e7432008-07-17 12:53:51 -04006872 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006873 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006874 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006875
Chris Mason257c62e2009-10-13 13:21:08 -04006876 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6877 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006878 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006879
Josef Bacik2ac55d42010-02-03 19:33:23 +00006880 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006881
6882out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006883 if (!ret) {
6884 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006885 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006886 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006887 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006888out:
Josef Bacikec39e182012-01-12 19:10:12 -05006889 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006890out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006891 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006892 return ret;
6893}
6894
Josef Bacika41ad392011-01-31 15:30:16 -05006895static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006896{
6897 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006898 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006899 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006900 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006901 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04006902 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006903 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006904
Josef Bacik2aaa6652012-08-29 14:27:18 -04006905 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04006906 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006907 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006908
Chris Mason4a096752008-07-21 10:29:44 -04006909 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006910 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006911
Josef Bacikfcb80c22011-05-03 10:40:22 -04006912 /*
6913 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6914 * 3 things going on here
6915 *
6916 * 1) We need to reserve space for our orphan item and the space to
6917 * delete our orphan item. Lord knows we don't want to have a dangling
6918 * orphan item because we didn't reserve space to remove it.
6919 *
6920 * 2) We need to reserve space to update our inode.
6921 *
6922 * 3) We need to have something to cache all the space that is going to
6923 * be free'd up by the truncate operation, but also have some slack
6924 * space reserved in case it uses space during the truncate (thank you
6925 * very much snapshotting).
6926 *
6927 * And we need these to all be seperate. The fact is we can use alot of
6928 * space doing the truncate, and we have no earthly idea how much space
6929 * we will use, so we need the truncate reservation to be seperate so it
6930 * doesn't end up using space reserved for updating the inode or
6931 * removing the orphan item. We also need to be able to stop the
6932 * transaction and start a new one, which means we need to be able to
6933 * update the inode several times, and we have no idea of knowing how
6934 * many times that will be, so we can't just reserve 1 item for the
6935 * entirety of the opration, so that has to be done seperately as well.
6936 * Then there is the orphan item, which does indeed need to be held on
6937 * to for the whole operation, and we need nobody to touch this reserved
6938 * space except the orphan code.
6939 *
6940 * So that leaves us with
6941 *
6942 * 1) root->orphan_block_rsv - for the orphan deletion.
6943 * 2) rsv - for the truncate reservation, which we will steal from the
6944 * transaction reservation.
6945 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6946 * updating the inode.
6947 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06006948 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006949 if (!rsv)
6950 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006951 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006952 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006953
Josef Bacik907cbce2011-08-08 13:46:15 -04006954 /*
Josef Bacik07127182011-08-19 10:29:59 -04006955 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006956 * 1 for the orphan item we're going to add
6957 * 1 for the orphan item deletion
6958 * 1 for updating the inode.
6959 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04006960 trans = btrfs_start_transaction(root, 4);
6961 if (IS_ERR(trans)) {
6962 err = PTR_ERR(trans);
6963 goto out;
6964 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006965
Josef Bacik907cbce2011-08-08 13:46:15 -04006966 /* Migrate the slack space for the truncate to our reserve */
6967 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6968 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006969 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006970
6971 ret = btrfs_orphan_add(trans, inode);
6972 if (ret) {
6973 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006974 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006975 }
6976
Chris Mason5a3f23d2009-03-31 13:27:11 -04006977 /*
6978 * setattr is responsible for setting the ordered_data_close flag,
6979 * but that is only tested during the last file release. That
6980 * could happen well after the next commit, leaving a great big
6981 * window where new writes may get lost if someone chooses to write
6982 * to this file after truncating to zero
6983 *
6984 * The inode doesn't have any dirty data here, and so if we commit
6985 * this is a noop. If someone immediately starts writing to the inode
6986 * it is very likely we'll catch some of their writes in this
6987 * transaction, and the commit will find this file on the ordered
6988 * data list with good things to send down.
6989 *
6990 * This is a best effort solution, there is still a window where
6991 * using truncate to replace the contents of the file will
6992 * end up with a zero length file after a crash.
6993 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04006994 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
6995 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04006996 btrfs_add_ordered_operation(trans, root, inode);
6997
Josef Bacik5dc562c2012-08-17 13:14:17 -04006998 /*
6999 * So if we truncate and then write and fsync we normally would just
7000 * write the extents that changed, which is a problem if we need to
7001 * first truncate that entire inode. So set this flag so we write out
7002 * all of the extents in the inode to the sync log so we're completely
7003 * safe.
7004 */
7005 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007006 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007007
Yan, Zheng80825102009-11-12 09:35:36 +00007008 while (1) {
7009 ret = btrfs_truncate_inode_items(trans, root, inode,
7010 inode->i_size,
7011 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007012 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007013 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007014 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007015 }
Chris Mason39279cc2007-06-12 06:35:45 -04007016
Josef Bacikfcb80c22011-05-03 10:40:22 -04007017 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007018 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007019 if (ret) {
7020 err = ret;
7021 break;
7022 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007023
Yan, Zheng80825102009-11-12 09:35:36 +00007024 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007025 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007026
7027 trans = btrfs_start_transaction(root, 2);
7028 if (IS_ERR(trans)) {
7029 ret = err = PTR_ERR(trans);
7030 trans = NULL;
7031 break;
7032 }
7033
7034 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7035 rsv, min_size);
7036 BUG_ON(ret); /* shouldn't happen */
7037 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007038 }
7039
7040 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007041 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007042 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007043 if (ret)
7044 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05007045 } else if (ret && inode->i_nlink > 0) {
7046 /*
7047 * Failed to do the truncate, remove us from the in memory
7048 * orphan list.
7049 */
7050 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00007051 }
7052
Chris Mason917c16b2011-11-08 14:49:59 -05007053 if (trans) {
7054 trans->block_rsv = &root->fs_info->trans_block_rsv;
7055 ret = btrfs_update_inode(trans, root, inode);
7056 if (ret && !err)
7057 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007058
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007059 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007060 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007061 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007062
7063out:
7064 btrfs_free_block_rsv(root, rsv);
7065
Josef Bacik3893e332011-01-31 16:03:11 -05007066 if (ret && !err)
7067 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007068
Josef Bacik3893e332011-01-31 16:03:11 -05007069 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007070}
7071
Sven Wegener3b963622008-06-09 21:57:42 -04007072/*
Chris Masond352ac62008-09-29 15:18:18 -04007073 * create a new subvolume directory/inode (helper for the ioctl).
7074 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007075int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007076 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007077{
Chris Mason39279cc2007-06-12 06:35:45 -04007078 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007079 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007080 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007081
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007082 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7083 new_dirid, new_dirid,
7084 S_IFDIR | (~current_umask() & S_IRWXUGO),
7085 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007086 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007087 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007088 inode->i_op = &btrfs_dir_inode_operations;
7089 inode->i_fop = &btrfs_dir_file_operations;
7090
Miklos Szeredibfe86842011-10-28 14:13:29 +02007091 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007092 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007093
Yan, Zheng76dda932009-09-21 16:00:26 -04007094 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007095
Yan, Zheng76dda932009-09-21 16:00:26 -04007096 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007097 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007098}
7099
Chris Mason39279cc2007-06-12 06:35:45 -04007100struct inode *btrfs_alloc_inode(struct super_block *sb)
7101{
7102 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007103 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007104
7105 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7106 if (!ei)
7107 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007108
7109 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007110 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007111 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007112 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007113 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007114 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007115 ei->disk_i_size = 0;
7116 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007117 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007118 ei->index_cnt = (u64)-1;
7119 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007120 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007121
Josef Bacik9e0baf62011-07-15 15:16:44 +00007122 spin_lock_init(&ei->lock);
7123 ei->outstanding_extents = 0;
7124 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007125
Josef Bacik72ac3c02012-05-23 14:13:11 -04007126 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007127 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007128
Miao Xie16cdcec2011-04-22 18:12:22 +08007129 ei->delayed_node = NULL;
7130
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007131 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007132 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007133 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7134 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007135 ei->io_tree.track_uptodate = 1;
7136 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007137 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007138 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007139 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007140 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007141 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007142 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007143 RB_CLEAR_NODE(&ei->rb_node);
7144
7145 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007146}
7147
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007148static void btrfs_i_callback(struct rcu_head *head)
7149{
7150 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007151 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7152}
7153
Chris Mason39279cc2007-06-12 06:35:45 -04007154void btrfs_destroy_inode(struct inode *inode)
7155{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007156 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007157 struct btrfs_root *root = BTRFS_I(inode)->root;
7158
Al Virob3d9b7a2012-06-09 13:51:19 -04007159 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007160 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007161 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7162 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007163 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7164 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007165
Chris Mason5a3f23d2009-03-31 13:27:11 -04007166 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007167 * This can happen where we create an inode, but somebody else also
7168 * created the same inode and we need to destroy the one we already
7169 * created.
7170 */
7171 if (!root)
7172 goto free;
7173
7174 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007175 * Make sure we're properly removed from the ordered operation
7176 * lists.
7177 */
7178 smp_mb();
7179 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7180 spin_lock(&root->fs_info->ordered_extent_lock);
7181 list_del_init(&BTRFS_I(inode)->ordered_operations);
7182 spin_unlock(&root->fs_info->ordered_extent_lock);
7183 }
7184
Josef Bacik8a35d952012-05-23 14:26:42 -04007185 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7186 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007187 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7188 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007189 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007190 }
Josef Bacik7b128762008-07-24 12:17:14 -04007191
Chris Masond3977122009-01-05 21:25:51 -05007192 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007193 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7194 if (!ordered)
7195 break;
7196 else {
Chris Masond3977122009-01-05 21:25:51 -05007197 printk(KERN_ERR "btrfs found ordered "
7198 "extent %llu %llu on inode cleanup\n",
7199 (unsigned long long)ordered->file_offset,
7200 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007201 btrfs_remove_ordered_extent(inode, ordered);
7202 btrfs_put_ordered_extent(ordered);
7203 btrfs_put_ordered_extent(ordered);
7204 }
7205 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007206 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007207 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007208free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007209 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007210 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007211}
7212
Al Viro45321ac2010-06-07 13:43:19 -04007213int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007214{
7215 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007216
Josef Bacik0af3d002010-06-21 14:48:16 -04007217 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007218 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007219 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007220 else
Al Viro45321ac2010-06-07 13:43:19 -04007221 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007222}
7223
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007224static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007225{
7226 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7227
7228 inode_init_once(&ei->vfs_inode);
7229}
7230
7231void btrfs_destroy_cachep(void)
7232{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007233 /*
7234 * Make sure all delayed rcu free inodes are flushed before we
7235 * destroy cache.
7236 */
7237 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007238 if (btrfs_inode_cachep)
7239 kmem_cache_destroy(btrfs_inode_cachep);
7240 if (btrfs_trans_handle_cachep)
7241 kmem_cache_destroy(btrfs_trans_handle_cachep);
7242 if (btrfs_transaction_cachep)
7243 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007244 if (btrfs_path_cachep)
7245 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007246 if (btrfs_free_space_cachep)
7247 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007248 if (btrfs_delalloc_work_cachep)
7249 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007250}
7251
7252int btrfs_init_cachep(void)
7253{
David Sterba837e1972012-09-07 03:00:48 -06007254 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007255 sizeof(struct btrfs_inode), 0,
7256 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007257 if (!btrfs_inode_cachep)
7258 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007259
David Sterba837e1972012-09-07 03:00:48 -06007260 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007261 sizeof(struct btrfs_trans_handle), 0,
7262 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007263 if (!btrfs_trans_handle_cachep)
7264 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007265
David Sterba837e1972012-09-07 03:00:48 -06007266 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007267 sizeof(struct btrfs_transaction), 0,
7268 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007269 if (!btrfs_transaction_cachep)
7270 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007271
David Sterba837e1972012-09-07 03:00:48 -06007272 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007273 sizeof(struct btrfs_path), 0,
7274 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007275 if (!btrfs_path_cachep)
7276 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007277
David Sterba837e1972012-09-07 03:00:48 -06007278 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007279 sizeof(struct btrfs_free_space), 0,
7280 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7281 if (!btrfs_free_space_cachep)
7282 goto fail;
7283
Miao Xie8ccf6f192012-10-25 09:28:04 +00007284 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7285 sizeof(struct btrfs_delalloc_work), 0,
7286 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7287 NULL);
7288 if (!btrfs_delalloc_work_cachep)
7289 goto fail;
7290
Chris Mason39279cc2007-06-12 06:35:45 -04007291 return 0;
7292fail:
7293 btrfs_destroy_cachep();
7294 return -ENOMEM;
7295}
7296
7297static int btrfs_getattr(struct vfsmount *mnt,
7298 struct dentry *dentry, struct kstat *stat)
7299{
7300 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007301 u32 blocksize = inode->i_sb->s_blocksize;
7302
Chris Mason39279cc2007-06-12 06:35:45 -04007303 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007304 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007305 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007306 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7307 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007308 return 0;
7309}
7310
Liu Bo75e7cb72011-03-22 10:12:20 +00007311/*
7312 * If a file is moved, it will inherit the cow and compression flags of the new
7313 * directory.
7314 */
7315static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7316{
7317 struct btrfs_inode *b_dir = BTRFS_I(dir);
7318 struct btrfs_inode *b_inode = BTRFS_I(inode);
7319
7320 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7321 b_inode->flags |= BTRFS_INODE_NODATACOW;
7322 else
7323 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7324
Liu Bobc178232012-06-14 02:23:18 -06007325 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007326 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007327 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7328 } else {
7329 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7330 BTRFS_INODE_NOCOMPRESS);
7331 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007332}
7333
Chris Masond3977122009-01-05 21:25:51 -05007334static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7335 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007336{
7337 struct btrfs_trans_handle *trans;
7338 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007339 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007340 struct inode *new_inode = new_dentry->d_inode;
7341 struct inode *old_inode = old_dentry->d_inode;
7342 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007343 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007344 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007345 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007346 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007347
Li Zefan33345d012011-04-20 10:31:50 +08007348 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007349 return -EPERM;
7350
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007351 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007352 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007353 return -EXDEV;
7354
Li Zefan33345d012011-04-20 10:31:50 +08007355 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7356 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007357 return -ENOTEMPTY;
7358
Chris Mason39279cc2007-06-12 06:35:45 -04007359 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007360 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007361 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007362 /*
7363 * we're using rename to replace one file with another.
7364 * and the replacement file is large. Start IO on it now so
7365 * we don't add too much work to the end of the transaction
7366 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007367 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007368 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7369 filemap_flush(old_inode->i_mapping);
7370
Yan, Zheng76dda932009-09-21 16:00:26 -04007371 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007372 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007373 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007374 /*
7375 * We want to reserve the absolute worst case amount of items. So if
7376 * both inodes are subvols and we need to unlink them then that would
7377 * require 4 item modifications, but if they are both normal inodes it
7378 * would require 5 item modifications, so we'll assume their normal
7379 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7380 * should cover the worst case number of items we'll modify.
7381 */
7382 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007383 if (IS_ERR(trans)) {
7384 ret = PTR_ERR(trans);
7385 goto out_notrans;
7386 }
Chris Mason5f39d392007-10-15 16:14:19 -04007387
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007388 if (dest != root)
7389 btrfs_record_root_in_trans(trans, dest);
7390
Yan, Zhenga5719522009-09-24 09:17:31 -04007391 ret = btrfs_set_inode_index(new_dir, &index);
7392 if (ret)
7393 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007394
Li Zefan33345d012011-04-20 10:31:50 +08007395 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007396 /* force full log commit if subvolume involved. */
7397 root->fs_info->last_trans_log_full_commit = trans->transid;
7398 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007399 ret = btrfs_insert_inode_ref(trans, dest,
7400 new_dentry->d_name.name,
7401 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007402 old_ino,
7403 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007404 if (ret)
7405 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007406 /*
7407 * this is an ugly little race, but the rename is required
7408 * to make sure that if we crash, the inode is either at the
7409 * old name or the new one. pinning the log transaction lets
7410 * us make sure we don't allow a log commit to come in after
7411 * we unlink the name but before we add the new name back in.
7412 */
7413 btrfs_pin_log_trans(root);
7414 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007415 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007416 * make sure the inode gets flushed if it is replacing
7417 * something.
7418 */
Li Zefan33345d012011-04-20 10:31:50 +08007419 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007420 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007421
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007422 inode_inc_iversion(old_dir);
7423 inode_inc_iversion(new_dir);
7424 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007425 old_dir->i_ctime = old_dir->i_mtime = ctime;
7426 new_dir->i_ctime = new_dir->i_mtime = ctime;
7427 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007428
Chris Mason12fcfd22009-03-24 10:24:20 -04007429 if (old_dentry->d_parent != new_dentry->d_parent)
7430 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7431
Li Zefan33345d012011-04-20 10:31:50 +08007432 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007433 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7434 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7435 old_dentry->d_name.name,
7436 old_dentry->d_name.len);
7437 } else {
Al Viro92986792011-03-04 17:14:37 +00007438 ret = __btrfs_unlink_inode(trans, root, old_dir,
7439 old_dentry->d_inode,
7440 old_dentry->d_name.name,
7441 old_dentry->d_name.len);
7442 if (!ret)
7443 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007444 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007445 if (ret) {
7446 btrfs_abort_transaction(trans, root, ret);
7447 goto out_fail;
7448 }
Chris Mason39279cc2007-06-12 06:35:45 -04007449
7450 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007451 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007452 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007453 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007454 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7455 root_objectid = BTRFS_I(new_inode)->location.objectid;
7456 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7457 root_objectid,
7458 new_dentry->d_name.name,
7459 new_dentry->d_name.len);
7460 BUG_ON(new_inode->i_nlink == 0);
7461 } else {
7462 ret = btrfs_unlink_inode(trans, dest, new_dir,
7463 new_dentry->d_inode,
7464 new_dentry->d_name.name,
7465 new_dentry->d_name.len);
7466 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007467 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007468 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007469 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007470 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007471 if (ret) {
7472 btrfs_abort_transaction(trans, root, ret);
7473 goto out_fail;
7474 }
Chris Mason39279cc2007-06-12 06:35:45 -04007475 }
Josef Bacikaec74772008-07-24 12:12:38 -04007476
Liu Bo75e7cb72011-03-22 10:12:20 +00007477 fixup_inode_flags(new_dir, old_inode);
7478
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007479 ret = btrfs_add_link(trans, new_dir, old_inode,
7480 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007481 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007482 if (ret) {
7483 btrfs_abort_transaction(trans, root, ret);
7484 goto out_fail;
7485 }
Chris Mason39279cc2007-06-12 06:35:45 -04007486
Li Zefan33345d012011-04-20 10:31:50 +08007487 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007488 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007489 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007490 btrfs_end_log_trans(root);
7491 }
Chris Mason39279cc2007-06-12 06:35:45 -04007492out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007493 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007494out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007495 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007496 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007497
Chris Mason39279cc2007-06-12 06:35:45 -04007498 return ret;
7499}
7500
Miao Xie8ccf6f192012-10-25 09:28:04 +00007501static void btrfs_run_delalloc_work(struct btrfs_work *work)
7502{
7503 struct btrfs_delalloc_work *delalloc_work;
7504
7505 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7506 work);
7507 if (delalloc_work->wait)
7508 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7509 else
7510 filemap_flush(delalloc_work->inode->i_mapping);
7511
7512 if (delalloc_work->delay_iput)
7513 btrfs_add_delayed_iput(delalloc_work->inode);
7514 else
7515 iput(delalloc_work->inode);
7516 complete(&delalloc_work->completion);
7517}
7518
7519struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7520 int wait, int delay_iput)
7521{
7522 struct btrfs_delalloc_work *work;
7523
7524 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7525 if (!work)
7526 return NULL;
7527
7528 init_completion(&work->completion);
7529 INIT_LIST_HEAD(&work->list);
7530 work->inode = inode;
7531 work->wait = wait;
7532 work->delay_iput = delay_iput;
7533 work->work.func = btrfs_run_delalloc_work;
7534
7535 return work;
7536}
7537
7538void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7539{
7540 wait_for_completion(&work->completion);
7541 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7542}
7543
Chris Masond352ac62008-09-29 15:18:18 -04007544/*
7545 * some fairly slow code that needs optimization. This walks the list
7546 * of all the inodes with pending delalloc and forces them to disk.
7547 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007548int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007549{
7550 struct list_head *head = &root->fs_info->delalloc_inodes;
7551 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007552 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007553 struct btrfs_delalloc_work *work, *next;
7554 struct list_head works;
7555 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04007556
Yan Zhengc146afa2008-11-12 14:34:12 -05007557 if (root->fs_info->sb->s_flags & MS_RDONLY)
7558 return -EROFS;
7559
Miao Xie8ccf6f192012-10-25 09:28:04 +00007560 INIT_LIST_HEAD(&works);
7561
Chris Mason75eff682008-12-15 15:54:40 -05007562 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007563 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007564 binode = list_entry(head->next, struct btrfs_inode,
7565 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007566 inode = igrab(&binode->vfs_inode);
7567 if (!inode)
7568 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007569 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007570 if (inode) {
Miao Xie8ccf6f192012-10-25 09:28:04 +00007571 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7572 if (!work) {
7573 ret = -ENOMEM;
7574 goto out;
7575 }
7576 list_add_tail(&work->list, &works);
7577 btrfs_queue_worker(&root->fs_info->flush_workers,
7578 &work->work);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007579 }
7580 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007581 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007582 }
Chris Mason75eff682008-12-15 15:54:40 -05007583 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007584
7585 /* the filemap_flush will queue IO into the worker threads, but
7586 * we have to make sure the IO is actually started and that
7587 * ordered extents get created before we return
7588 */
7589 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007590 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007591 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007592 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007593 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7594 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007595 }
7596 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007597out:
7598 list_for_each_entry_safe(work, next, &works, list) {
7599 list_del_init(&work->list);
7600 btrfs_wait_and_free_delalloc_work(work);
7601 }
7602 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04007603}
7604
Chris Mason39279cc2007-06-12 06:35:45 -04007605static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7606 const char *symname)
7607{
7608 struct btrfs_trans_handle *trans;
7609 struct btrfs_root *root = BTRFS_I(dir)->root;
7610 struct btrfs_path *path;
7611 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007612 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007613 int err;
7614 int drop_inode = 0;
7615 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007616 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007617 int name_len;
7618 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007619 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007620 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007621 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04007622
7623 name_len = strlen(symname) + 1;
7624 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7625 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007626
Josef Bacik9ed74f22009-09-11 16:12:44 -04007627 /*
7628 * 2 items for inode item and ref
7629 * 2 items for dir items
7630 * 1 item for xattr if selinux is on
7631 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007632 trans = btrfs_start_transaction(root, 5);
7633 if (IS_ERR(trans))
7634 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007635
Li Zefan581bb052011-04-20 10:06:11 +08007636 err = btrfs_find_free_ino(root, &objectid);
7637 if (err)
7638 goto out_unlock;
7639
Josef Bacikaec74772008-07-24 12:12:38 -04007640 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007641 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007642 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007643 if (IS_ERR(inode)) {
7644 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007645 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007646 }
Chris Mason39279cc2007-06-12 06:35:45 -04007647
Eric Paris2a7dba32011-02-01 11:05:39 -05007648 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007649 if (err) {
7650 drop_inode = 1;
7651 goto out_unlock;
7652 }
7653
Casey Schauflerad19db72011-12-15 10:09:07 -05007654 /*
7655 * If the active LSM wants to access the inode during
7656 * d_instantiate it needs these. Smack checks to see
7657 * if the filesystem supports xattrs by looking at the
7658 * ops vector.
7659 */
7660 inode->i_fop = &btrfs_file_operations;
7661 inode->i_op = &btrfs_file_inode_operations;
7662
Josef Bacika1b075d2010-11-19 20:36:11 +00007663 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007664 if (err)
7665 drop_inode = 1;
7666 else {
7667 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007668 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007669 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007670 }
Chris Mason39279cc2007-06-12 06:35:45 -04007671 if (drop_inode)
7672 goto out_unlock;
7673
7674 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007675 if (!path) {
7676 err = -ENOMEM;
7677 drop_inode = 1;
7678 goto out_unlock;
7679 }
Li Zefan33345d012011-04-20 10:31:50 +08007680 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007681 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007682 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7683 datasize = btrfs_file_extent_calc_inline_size(name_len);
7684 err = btrfs_insert_empty_item(trans, root, path, &key,
7685 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007686 if (err) {
7687 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007688 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007689 goto out_unlock;
7690 }
Chris Mason5f39d392007-10-15 16:14:19 -04007691 leaf = path->nodes[0];
7692 ei = btrfs_item_ptr(leaf, path->slots[0],
7693 struct btrfs_file_extent_item);
7694 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7695 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007696 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007697 btrfs_set_file_extent_encryption(leaf, ei, 0);
7698 btrfs_set_file_extent_compression(leaf, ei, 0);
7699 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7700 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7701
Chris Mason39279cc2007-06-12 06:35:45 -04007702 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007703 write_extent_buffer(leaf, symname, ptr, name_len);
7704 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007705 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007706
Chris Mason39279cc2007-06-12 06:35:45 -04007707 inode->i_op = &btrfs_symlink_inode_operations;
7708 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007709 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007710 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007711 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007712 err = btrfs_update_inode(trans, root, inode);
7713 if (err)
7714 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007715
7716out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007717 if (!err)
7718 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007719 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007720 if (drop_inode) {
7721 inode_dec_link_count(inode);
7722 iput(inode);
7723 }
Liu Bob53d3f52012-11-14 14:34:34 +00007724 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04007725 return err;
7726}
Chris Mason16432982008-04-10 10:23:21 -04007727
Josef Bacik0af3d002010-06-21 14:48:16 -04007728static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7729 u64 start, u64 num_bytes, u64 min_size,
7730 loff_t actual_len, u64 *alloc_hint,
7731 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007732{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007733 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7734 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007735 struct btrfs_root *root = BTRFS_I(inode)->root;
7736 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007737 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007738 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007739 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007740 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007741
Josef Bacik0af3d002010-06-21 14:48:16 -04007742 if (trans)
7743 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007744 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007745 if (own_trans) {
7746 trans = btrfs_start_transaction(root, 3);
7747 if (IS_ERR(trans)) {
7748 ret = PTR_ERR(trans);
7749 break;
7750 }
Yan Zhengd899e052008-10-30 14:25:28 -04007751 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007752
Yan, Zhengefa56462010-05-16 10:49:59 -04007753 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007754 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007755 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007756 if (own_trans)
7757 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007758 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007759 }
7760
Yan Zhengd899e052008-10-30 14:25:28 -04007761 ret = insert_reserved_file_extent(trans, inode,
7762 cur_offset, ins.objectid,
7763 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007764 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007765 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007766 if (ret) {
7767 btrfs_abort_transaction(trans, root, ret);
7768 if (own_trans)
7769 btrfs_end_transaction(trans, root);
7770 break;
7771 }
Chris Masona1ed8352009-09-11 12:27:37 -04007772 btrfs_drop_extent_cache(inode, cur_offset,
7773 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007774
Josef Bacik5dc562c2012-08-17 13:14:17 -04007775 em = alloc_extent_map();
7776 if (!em) {
7777 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7778 &BTRFS_I(inode)->runtime_flags);
7779 goto next;
7780 }
7781
7782 em->start = cur_offset;
7783 em->orig_start = cur_offset;
7784 em->len = ins.offset;
7785 em->block_start = ins.objectid;
7786 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05007787 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007788 em->bdev = root->fs_info->fs_devices->latest_bdev;
7789 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7790 em->generation = trans->transid;
7791
7792 while (1) {
7793 write_lock(&em_tree->lock);
7794 ret = add_extent_mapping(em_tree, em);
7795 if (!ret)
7796 list_move(&em->list,
7797 &em_tree->modified_extents);
7798 write_unlock(&em_tree->lock);
7799 if (ret != -EEXIST)
7800 break;
7801 btrfs_drop_extent_cache(inode, cur_offset,
7802 cur_offset + ins.offset - 1,
7803 0);
7804 }
7805 free_extent_map(em);
7806next:
Yan Zhengd899e052008-10-30 14:25:28 -04007807 num_bytes -= ins.offset;
7808 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007809 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007810
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007811 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007812 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007813 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007814 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007815 (actual_len > inode->i_size) &&
7816 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007817 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007818 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007819 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007820 i_size = cur_offset;
7821 i_size_write(inode, i_size);
7822 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007823 }
7824
Yan Zhengd899e052008-10-30 14:25:28 -04007825 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007826
7827 if (ret) {
7828 btrfs_abort_transaction(trans, root, ret);
7829 if (own_trans)
7830 btrfs_end_transaction(trans, root);
7831 break;
7832 }
Yan Zhengd899e052008-10-30 14:25:28 -04007833
Josef Bacik0af3d002010-06-21 14:48:16 -04007834 if (own_trans)
7835 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007836 }
Yan Zhengd899e052008-10-30 14:25:28 -04007837 return ret;
7838}
7839
Josef Bacik0af3d002010-06-21 14:48:16 -04007840int btrfs_prealloc_file_range(struct inode *inode, int mode,
7841 u64 start, u64 num_bytes, u64 min_size,
7842 loff_t actual_len, u64 *alloc_hint)
7843{
7844 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7845 min_size, actual_len, alloc_hint,
7846 NULL);
7847}
7848
7849int btrfs_prealloc_file_range_trans(struct inode *inode,
7850 struct btrfs_trans_handle *trans, int mode,
7851 u64 start, u64 num_bytes, u64 min_size,
7852 loff_t actual_len, u64 *alloc_hint)
7853{
7854 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7855 min_size, actual_len, alloc_hint, trans);
7856}
7857
Chris Masone6dcd2d2008-07-17 12:53:50 -04007858static int btrfs_set_page_dirty(struct page *page)
7859{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007860 return __set_page_dirty_nobuffers(page);
7861}
7862
Al Viro10556cb2011-06-20 19:28:19 -04007863static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007864{
Li Zefanb83cc962010-12-20 16:04:08 +08007865 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007866 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007867
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007868 if (mask & MAY_WRITE &&
7869 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7870 if (btrfs_root_readonly(root))
7871 return -EROFS;
7872 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7873 return -EACCES;
7874 }
Al Viro2830ba72011-06-20 19:16:29 -04007875 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007876}
Chris Mason39279cc2007-06-12 06:35:45 -04007877
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007878static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007879 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007880 .lookup = btrfs_lookup,
7881 .create = btrfs_create,
7882 .unlink = btrfs_unlink,
7883 .link = btrfs_link,
7884 .mkdir = btrfs_mkdir,
7885 .rmdir = btrfs_rmdir,
7886 .rename = btrfs_rename,
7887 .symlink = btrfs_symlink,
7888 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007889 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007890 .setxattr = btrfs_setxattr,
7891 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007892 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007893 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007894 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007895 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007896};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007897static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007898 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007899 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007900 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007901};
Yan, Zheng76dda932009-09-21 16:00:26 -04007902
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007903static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007904 .llseek = generic_file_llseek,
7905 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007906 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007907 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007908#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007909 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007910#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007911 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007912 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007913};
7914
Chris Masond1310b22008-01-24 16:13:08 -05007915static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007916 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007917 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007918 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007919 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007920 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007921 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007922 .set_bit_hook = btrfs_set_bit_hook,
7923 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007924 .merge_extent_hook = btrfs_merge_extent_hook,
7925 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007926};
7927
Chris Mason35054392009-01-21 13:11:13 -05007928/*
7929 * btrfs doesn't support the bmap operation because swapfiles
7930 * use bmap to make a mapping of extents in the file. They assume
7931 * these extents won't change over the life of the file and they
7932 * use the bmap result to do IO directly to the drive.
7933 *
7934 * the btrfs bmap call would return logical addresses that aren't
7935 * suitable for IO and they also will change frequently as COW
7936 * operations happen. So, swapfile + btrfs == corruption.
7937 *
7938 * For now we're avoiding this by dropping bmap.
7939 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007940static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007941 .readpage = btrfs_readpage,
7942 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04007943 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007944 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007945 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007946 .invalidatepage = btrfs_invalidatepage,
7947 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007948 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007949 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007950};
7951
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007952static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007953 .readpage = btrfs_readpage,
7954 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007955 .invalidatepage = btrfs_invalidatepage,
7956 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007957};
7958
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007959static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007960 .getattr = btrfs_getattr,
7961 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007962 .setxattr = btrfs_setxattr,
7963 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007964 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007965 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007966 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007967 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007968 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007969 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007970};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007971static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007972 .getattr = btrfs_getattr,
7973 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007974 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007975 .setxattr = btrfs_setxattr,
7976 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007977 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007978 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007979 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007980 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04007981};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007982static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007983 .readlink = generic_readlink,
7984 .follow_link = page_follow_link_light,
7985 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007986 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05007987 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007988 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007989 .setxattr = btrfs_setxattr,
7990 .getxattr = btrfs_getxattr,
7991 .listxattr = btrfs_listxattr,
7992 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007993 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007994 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007995};
Yan, Zheng76dda932009-09-21 16:00:26 -04007996
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007997const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007998 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007999 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008000};