blob: dce9e218b845eb258732d36a3a40e1af91a37675 [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 Masonb888db22007-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 Masonb888db22007-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;
702 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800703 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500704 set_bit(EXTENT_FLAG_PINNED, &em->flags);
705 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
706
Chris Masond3977122009-01-05 21:25:51 -0500707 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400708 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500709 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400710 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500711 if (ret != -EEXIST) {
712 free_extent_map(em);
713 break;
714 }
715 btrfs_drop_extent_cache(inode, async_extent->start,
716 async_extent->start +
717 async_extent->ram_size - 1, 0);
718 }
719
Li Zefan261507a02010-12-17 14:21:50 +0800720 ret = btrfs_add_ordered_extent_compress(inode,
721 async_extent->start,
722 ins.objectid,
723 async_extent->ram_size,
724 ins.offset,
725 BTRFS_ORDERED_COMPRESSED,
726 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100727 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500728
Chris Mason771ed682008-11-06 22:02:51 -0500729 /*
730 * clear dirty, set writeback and unlock the pages.
731 */
732 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400733 &BTRFS_I(inode)->io_tree,
734 async_extent->start,
735 async_extent->start +
736 async_extent->ram_size - 1,
737 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
738 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400739 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400740 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500741
742 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500743 async_extent->start,
744 async_extent->ram_size,
745 ins.objectid,
746 ins.offset, async_extent->pages,
747 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500748
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100749 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500750 alloc_hint = ins.objectid + ins.offset;
751 kfree(async_extent);
752 cond_resched();
753 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100754 ret = 0;
755out:
756 return ret;
757out_free:
758 kfree(async_extent);
759 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500760}
761
Josef Bacik4b46fce2010-05-23 11:00:55 -0400762static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
763 u64 num_bytes)
764{
765 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
766 struct extent_map *em;
767 u64 alloc_hint = 0;
768
769 read_lock(&em_tree->lock);
770 em = search_extent_mapping(em_tree, start, num_bytes);
771 if (em) {
772 /*
773 * if block start isn't an actual block number then find the
774 * first block in this inode and use that as a hint. If that
775 * block is also bogus then just don't worry about it.
776 */
777 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
778 free_extent_map(em);
779 em = search_extent_mapping(em_tree, 0, 0);
780 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
781 alloc_hint = em->block_start;
782 if (em)
783 free_extent_map(em);
784 } else {
785 alloc_hint = em->block_start;
786 free_extent_map(em);
787 }
788 }
789 read_unlock(&em_tree->lock);
790
791 return alloc_hint;
792}
793
Chris Mason771ed682008-11-06 22:02:51 -0500794/*
795 * when extent_io.c finds a delayed allocation range in the file,
796 * the call backs end up in this code. The basic idea is to
797 * allocate extents on disk for the range, and create ordered data structs
798 * in ram to track those extents.
799 *
800 * locked_page is the page that writepage had locked already. We use
801 * it to make sure we don't do extra locks or unlocks.
802 *
803 * *page_started is set to one if we unlock locked_page and do everything
804 * required to start IO on it. It may be clean and already done with
805 * IO when we return.
806 */
807static noinline int cow_file_range(struct inode *inode,
808 struct page *locked_page,
809 u64 start, u64 end, int *page_started,
810 unsigned long *nr_written,
811 int unlock)
812{
813 struct btrfs_root *root = BTRFS_I(inode)->root;
814 struct btrfs_trans_handle *trans;
815 u64 alloc_hint = 0;
816 u64 num_bytes;
817 unsigned long ram_size;
818 u64 disk_num_bytes;
819 u64 cur_alloc_size;
820 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500821 struct btrfs_key ins;
822 struct extent_map *em;
823 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
824 int ret = 0;
825
Liu Bo83eea1f2012-07-10 05:28:39 -0600826 BUG_ON(btrfs_is_free_space_inode(inode));
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400827 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100828 if (IS_ERR(trans)) {
829 extent_clear_unlock_delalloc(inode,
830 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400831 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100832 EXTENT_CLEAR_UNLOCK_PAGE |
833 EXTENT_CLEAR_UNLOCK |
834 EXTENT_CLEAR_DELALLOC |
835 EXTENT_CLEAR_DIRTY |
836 EXTENT_SET_WRITEBACK |
837 EXTENT_END_WRITEBACK);
838 return PTR_ERR(trans);
839 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400840 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500841
Chris Mason771ed682008-11-06 22:02:51 -0500842 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
843 num_bytes = max(blocksize, num_bytes);
844 disk_num_bytes = num_bytes;
845 ret = 0;
846
Chris Mason4cb53002011-05-24 15:35:30 -0400847 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400848 if (num_bytes < 64 * 1024 &&
849 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400850 btrfs_add_inode_defrag(trans, inode);
851
Chris Mason771ed682008-11-06 22:02:51 -0500852 if (start == 0) {
853 /* lets try to make an inline extent */
854 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000855 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500856 if (ret == 0) {
857 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400858 &BTRFS_I(inode)->io_tree,
859 start, end, NULL,
860 EXTENT_CLEAR_UNLOCK_PAGE |
861 EXTENT_CLEAR_UNLOCK |
862 EXTENT_CLEAR_DELALLOC |
863 EXTENT_CLEAR_DIRTY |
864 EXTENT_SET_WRITEBACK |
865 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000866
Chris Mason771ed682008-11-06 22:02:51 -0500867 *nr_written = *nr_written +
868 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
869 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500870 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100871 } else if (ret < 0) {
872 btrfs_abort_transaction(trans, root, ret);
873 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500874 }
875 }
Chris Masonc8b97812008-10-29 14:49:59 -0400876
877 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200878 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400879
Josef Bacik4b46fce2010-05-23 11:00:55 -0400880 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400881 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400882
Chris Masond3977122009-01-05 21:25:51 -0500883 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400884 unsigned long op;
885
Josef Bacik287a0ab2010-03-19 18:07:23 +0000886 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400887 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500888 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500889 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890 if (ret < 0) {
891 btrfs_abort_transaction(trans, root, ret);
892 goto out_unlock;
893 }
Chris Masond3977122009-01-05 21:25:51 -0500894
David Sterba172ddd62011-04-21 00:48:27 +0200895 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100896 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400897 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500898 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500899 ram_size = ins.offset;
900 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400901
Chris Masone6dcd2d2008-07-17 12:53:50 -0400902 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400903 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400904 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400905 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400906
Chris Masond3977122009-01-05 21:25:51 -0500907 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400908 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400909 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400910 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400911 if (ret != -EEXIST) {
912 free_extent_map(em);
913 break;
914 }
915 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400916 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400917 }
918
Chris Mason98d20f62008-04-14 09:46:10 -0400919 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400920 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500921 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100922 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400923
Yan Zheng17d217f2008-12-12 10:03:38 -0500924 if (root->root_key.objectid ==
925 BTRFS_DATA_RELOC_TREE_OBJECTID) {
926 ret = btrfs_reloc_clone_csums(inode, start,
927 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100928 if (ret) {
929 btrfs_abort_transaction(trans, root, ret);
930 goto out_unlock;
931 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500932 }
933
Chris Masond3977122009-01-05 21:25:51 -0500934 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400935 break;
Chris Masond3977122009-01-05 21:25:51 -0500936
Chris Masonc8b97812008-10-29 14:49:59 -0400937 /* we're not doing compressed IO, don't unlock the first
938 * page (which the caller expects to stay locked), don't
939 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400940 *
941 * Do set the Private2 bit so we know this page was properly
942 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400943 */
Chris Masona791e352009-10-08 11:27:10 -0400944 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
945 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
946 EXTENT_SET_PRIVATE2;
947
Chris Masonc8b97812008-10-29 14:49:59 -0400948 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
949 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400950 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400951 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500952 num_bytes -= cur_alloc_size;
953 alloc_hint = ins.objectid + ins.offset;
954 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400955 }
Chris Mason771ed682008-11-06 22:02:51 -0500956 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100957out:
Chris Masonb888db22007-08-27 16:49:44 -0400958 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400959
Chris Masonbe20aa92007-12-17 20:14:01 -0500960 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100961out_unlock:
962 extent_clear_unlock_delalloc(inode,
963 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400964 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100965 EXTENT_CLEAR_UNLOCK_PAGE |
966 EXTENT_CLEAR_UNLOCK |
967 EXTENT_CLEAR_DELALLOC |
968 EXTENT_CLEAR_DIRTY |
969 EXTENT_SET_WRITEBACK |
970 EXTENT_END_WRITEBACK);
971
972 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500973}
Chris Masonc8b97812008-10-29 14:49:59 -0400974
Chris Mason771ed682008-11-06 22:02:51 -0500975/*
976 * work queue call back to started compression on a file and pages
977 */
978static noinline void async_cow_start(struct btrfs_work *work)
979{
980 struct async_cow *async_cow;
981 int num_added = 0;
982 async_cow = container_of(work, struct async_cow, work);
983
984 compress_file_range(async_cow->inode, async_cow->locked_page,
985 async_cow->start, async_cow->end, async_cow,
986 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -0400987 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -0600988 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -0500989 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -0400990 }
Chris Mason771ed682008-11-06 22:02:51 -0500991}
992
993/*
994 * work queue call back to submit previously compressed pages
995 */
996static noinline void async_cow_submit(struct btrfs_work *work)
997{
998 struct async_cow *async_cow;
999 struct btrfs_root *root;
1000 unsigned long nr_pages;
1001
1002 async_cow = container_of(work, struct async_cow, work);
1003
1004 root = async_cow->root;
1005 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1006 PAGE_CACHE_SHIFT;
1007
Josef Bacik66657b32012-08-01 15:36:24 -04001008 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001009 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001010 waitqueue_active(&root->fs_info->async_submit_wait))
1011 wake_up(&root->fs_info->async_submit_wait);
1012
Chris Masond3977122009-01-05 21:25:51 -05001013 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001014 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001015}
Chris Masonc8b97812008-10-29 14:49:59 -04001016
Chris Mason771ed682008-11-06 22:02:51 -05001017static noinline void async_cow_free(struct btrfs_work *work)
1018{
1019 struct async_cow *async_cow;
1020 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001021 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001022 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001023 kfree(async_cow);
1024}
1025
1026static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1027 u64 start, u64 end, int *page_started,
1028 unsigned long *nr_written)
1029{
1030 struct async_cow *async_cow;
1031 struct btrfs_root *root = BTRFS_I(inode)->root;
1032 unsigned long nr_pages;
1033 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001034 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001035
Chris Masona3429ab2009-10-08 12:30:20 -04001036 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1037 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001038 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001039 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001040 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001041 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001042 async_cow->root = root;
1043 async_cow->locked_page = locked_page;
1044 async_cow->start = start;
1045
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001046 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001047 cur_end = end;
1048 else
1049 cur_end = min(end, start + 512 * 1024 - 1);
1050
1051 async_cow->end = cur_end;
1052 INIT_LIST_HEAD(&async_cow->extents);
1053
1054 async_cow->work.func = async_cow_start;
1055 async_cow->work.ordered_func = async_cow_submit;
1056 async_cow->work.ordered_free = async_cow_free;
1057 async_cow->work.flags = 0;
1058
Chris Mason771ed682008-11-06 22:02:51 -05001059 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1060 PAGE_CACHE_SHIFT;
1061 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1062
1063 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1064 &async_cow->work);
1065
1066 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1067 wait_event(root->fs_info->async_submit_wait,
1068 (atomic_read(&root->fs_info->async_delalloc_pages) <
1069 limit));
1070 }
1071
Chris Masond3977122009-01-05 21:25:51 -05001072 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001073 atomic_read(&root->fs_info->async_delalloc_pages)) {
1074 wait_event(root->fs_info->async_submit_wait,
1075 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1076 0));
1077 }
1078
1079 *nr_written += nr_pages;
1080 start = cur_end + 1;
1081 }
1082 *page_started = 1;
1083 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001084}
1085
Chris Masond3977122009-01-05 21:25:51 -05001086static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001087 u64 bytenr, u64 num_bytes)
1088{
1089 int ret;
1090 struct btrfs_ordered_sum *sums;
1091 LIST_HEAD(list);
1092
Yan Zheng07d400a2009-01-06 11:42:00 -05001093 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001094 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001095 if (ret == 0 && list_empty(&list))
1096 return 0;
1097
1098 while (!list_empty(&list)) {
1099 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1100 list_del(&sums->list);
1101 kfree(sums);
1102 }
1103 return 1;
1104}
1105
Chris Masond352ac62008-09-29 15:18:18 -04001106/*
1107 * when nowcow writeback call back. This checks for snapshots or COW copies
1108 * of the extents that exist in the file, and COWs the file as required.
1109 *
1110 * If no cow copies or snapshots exist, we write directly to the existing
1111 * blocks on disk
1112 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001113static noinline int run_delalloc_nocow(struct inode *inode,
1114 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001115 u64 start, u64 end, int *page_started, int force,
1116 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001117{
Chris Masonbe20aa92007-12-17 20:14:01 -05001118 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001119 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001120 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001121 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001122 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001123 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001124 u64 cow_start;
1125 u64 cur_offset;
1126 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001127 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001128 u64 disk_bytenr;
1129 u64 num_bytes;
1130 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001131 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001132 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001133 int nocow;
1134 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001135 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001136 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001137
1138 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001139 if (!path) {
1140 extent_clear_unlock_delalloc(inode,
1141 &BTRFS_I(inode)->io_tree,
1142 start, end, locked_page,
1143 EXTENT_CLEAR_UNLOCK_PAGE |
1144 EXTENT_CLEAR_UNLOCK |
1145 EXTENT_CLEAR_DELALLOC |
1146 EXTENT_CLEAR_DIRTY |
1147 EXTENT_SET_WRITEBACK |
1148 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001149 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001150 }
Li Zefan82d59022011-04-20 10:33:24 +08001151
Liu Bo83eea1f2012-07-10 05:28:39 -06001152 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001153
1154 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001155 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001156 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001157 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001158
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001159 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001160 extent_clear_unlock_delalloc(inode,
1161 &BTRFS_I(inode)->io_tree,
1162 start, end, locked_page,
1163 EXTENT_CLEAR_UNLOCK_PAGE |
1164 EXTENT_CLEAR_UNLOCK |
1165 EXTENT_CLEAR_DELALLOC |
1166 EXTENT_CLEAR_DIRTY |
1167 EXTENT_SET_WRITEBACK |
1168 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001169 btrfs_free_path(path);
1170 return PTR_ERR(trans);
1171 }
1172
Josef Bacik74b21072011-04-13 12:02:53 -04001173 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001174
Yan Zheng80ff3852008-10-30 14:20:02 -04001175 cow_start = (u64)-1;
1176 cur_offset = start;
1177 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001178 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001179 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001180 if (ret < 0) {
1181 btrfs_abort_transaction(trans, root, ret);
1182 goto error;
1183 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001184 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1185 leaf = path->nodes[0];
1186 btrfs_item_key_to_cpu(leaf, &found_key,
1187 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001188 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001189 found_key.type == BTRFS_EXTENT_DATA_KEY)
1190 path->slots[0]--;
1191 }
1192 check_prev = 0;
1193next_slot:
1194 leaf = path->nodes[0];
1195 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1196 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001197 if (ret < 0) {
1198 btrfs_abort_transaction(trans, root, ret);
1199 goto error;
1200 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 if (ret > 0)
1202 break;
1203 leaf = path->nodes[0];
1204 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001205
Yan Zheng80ff3852008-10-30 14:20:02 -04001206 nocow = 0;
1207 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001208 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001210
Li Zefan33345d012011-04-20 10:31:50 +08001211 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001212 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1213 found_key.offset > end)
1214 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001215
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 if (found_key.offset > cur_offset) {
1217 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001218 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 goto out_check;
1220 }
Chris Masonc31f8832008-01-08 15:46:31 -05001221
Yan Zheng80ff3852008-10-30 14:20:02 -04001222 fi = btrfs_item_ptr(leaf, path->slots[0],
1223 struct btrfs_file_extent_item);
1224 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001225
Yan Zhengd899e052008-10-30 14:25:28 -04001226 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1227 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001229 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001230 extent_end = found_key.offset +
1231 btrfs_file_extent_num_bytes(leaf, fi);
1232 if (extent_end <= start) {
1233 path->slots[0]++;
1234 goto next_slot;
1235 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001236 if (disk_bytenr == 0)
1237 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 if (btrfs_file_extent_compression(leaf, fi) ||
1239 btrfs_file_extent_encryption(leaf, fi) ||
1240 btrfs_file_extent_other_encoding(leaf, fi))
1241 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001242 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1243 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001244 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001245 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001246 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001247 found_key.offset -
1248 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001249 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001250 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001251 disk_bytenr += cur_offset - found_key.offset;
1252 num_bytes = min(end + 1, extent_end) - cur_offset;
1253 /*
1254 * force cow if csum exists in the range.
1255 * this ensure that csum for a given extent are
1256 * either valid or do not exist.
1257 */
1258 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1259 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001260 nocow = 1;
1261 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1262 extent_end = found_key.offset +
1263 btrfs_file_extent_inline_len(leaf, fi);
1264 extent_end = ALIGN(extent_end, root->sectorsize);
1265 } else {
1266 BUG_ON(1);
1267 }
1268out_check:
1269 if (extent_end <= start) {
1270 path->slots[0]++;
1271 goto next_slot;
1272 }
1273 if (!nocow) {
1274 if (cow_start == (u64)-1)
1275 cow_start = cur_offset;
1276 cur_offset = extent_end;
1277 if (cur_offset > end)
1278 break;
1279 path->slots[0]++;
1280 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001281 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001282
David Sterbab3b4aa72011-04-21 01:20:15 +02001283 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 if (cow_start != (u64)-1) {
1285 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001286 found_key.offset - 1, page_started,
1287 nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001288 if (ret) {
1289 btrfs_abort_transaction(trans, root, ret);
1290 goto error;
1291 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001293 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001294
Yan Zhengd899e052008-10-30 14:25:28 -04001295 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1296 struct extent_map *em;
1297 struct extent_map_tree *em_tree;
1298 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001299 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001300 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001301 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001302 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001303 em->len = num_bytes;
1304 em->block_len = num_bytes;
1305 em->block_start = disk_bytenr;
1306 em->bdev = root->fs_info->fs_devices->latest_bdev;
1307 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -06001308 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Yan Zhengd899e052008-10-30 14:25:28 -04001309 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001310 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001311 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001312 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001313 if (ret != -EEXIST) {
1314 free_extent_map(em);
1315 break;
1316 }
1317 btrfs_drop_extent_cache(inode, em->start,
1318 em->start + em->len - 1, 0);
1319 }
1320 type = BTRFS_ORDERED_PREALLOC;
1321 } else {
1322 type = BTRFS_ORDERED_NOCOW;
1323 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001324
1325 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001326 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001327 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001328
Yan, Zhengefa56462010-05-16 10:49:59 -04001329 if (root->root_key.objectid ==
1330 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1331 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1332 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001333 if (ret) {
1334 btrfs_abort_transaction(trans, root, ret);
1335 goto error;
1336 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001337 }
1338
Yan Zhengd899e052008-10-30 14:25:28 -04001339 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001340 cur_offset, cur_offset + num_bytes - 1,
1341 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1342 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1343 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 cur_offset = extent_end;
1345 if (cur_offset > end)
1346 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001347 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001348 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001349
Josef Bacik17ca04a2012-05-31 15:58:55 -04001350 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001352 cur_offset = end;
1353 }
1354
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 if (cow_start != (u64)-1) {
1356 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001357 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001358 if (ret) {
1359 btrfs_abort_transaction(trans, root, ret);
1360 goto error;
1361 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 }
1363
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001364error:
Miao Xiea698d0752012-09-20 01:51:59 -06001365 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001366 if (!ret)
1367 ret = err;
1368
Josef Bacik17ca04a2012-05-31 15:58:55 -04001369 if (ret && cur_offset < end)
1370 extent_clear_unlock_delalloc(inode,
1371 &BTRFS_I(inode)->io_tree,
1372 cur_offset, end, locked_page,
1373 EXTENT_CLEAR_UNLOCK_PAGE |
1374 EXTENT_CLEAR_UNLOCK |
1375 EXTENT_CLEAR_DELALLOC |
1376 EXTENT_CLEAR_DIRTY |
1377 EXTENT_SET_WRITEBACK |
1378 EXTENT_END_WRITEBACK);
1379
Yan Zheng7ea394f2008-08-05 13:05:02 -04001380 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001381 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001382}
1383
Chris Masond352ac62008-09-29 15:18:18 -04001384/*
1385 * extent_io.c call back to do delayed allocation processing
1386 */
Chris Masonc8b97812008-10-29 14:49:59 -04001387static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001388 u64 start, u64 end, int *page_started,
1389 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001390{
Chris Masonbe20aa92007-12-17 20:14:01 -05001391 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001392 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001393
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001394 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001395 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001396 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001397 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001398 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001399 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001400 } else if (!btrfs_test_opt(root, COMPRESS) &&
1401 !(BTRFS_I(inode)->force_compress) &&
1402 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001403 ret = cow_file_range(inode, locked_page, start, end,
1404 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001405 } else {
1406 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1407 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001408 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001409 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001410 }
Chris Masonb888db22007-08-27 16:49:44 -04001411 return ret;
1412}
1413
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001414static void btrfs_split_extent_hook(struct inode *inode,
1415 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001416{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001417 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001418 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001419 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001420
Josef Bacik9e0baf62011-07-15 15:16:44 +00001421 spin_lock(&BTRFS_I(inode)->lock);
1422 BTRFS_I(inode)->outstanding_extents++;
1423 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001424}
1425
1426/*
1427 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1428 * extents so we can keep track of new extents that are just merged onto old
1429 * extents, such as when we are doing sequential writes, so we can properly
1430 * account for the metadata space we'll need.
1431 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001432static void btrfs_merge_extent_hook(struct inode *inode,
1433 struct extent_state *new,
1434 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001435{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001436 /* not delalloc, ignore it */
1437 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001438 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001439
Josef Bacik9e0baf62011-07-15 15:16:44 +00001440 spin_lock(&BTRFS_I(inode)->lock);
1441 BTRFS_I(inode)->outstanding_extents--;
1442 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001443}
1444
Chris Masond352ac62008-09-29 15:18:18 -04001445/*
1446 * extent_io.c set_bit_hook, used to track delayed allocation
1447 * bytes in this file, and to maintain the list of inodes that
1448 * have pending delalloc work to be done.
1449 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001450static void btrfs_set_bit_hook(struct inode *inode,
1451 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001452{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001453
Chris Mason75eff682008-12-15 15:54:40 -05001454 /*
1455 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001456 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001457 * bit, which is only set or cleared with irqs on
1458 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001459 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001460 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001461 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001462 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001463
Josef Bacik9e0baf62011-07-15 15:16:44 +00001464 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001465 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001466 } else {
1467 spin_lock(&BTRFS_I(inode)->lock);
1468 BTRFS_I(inode)->outstanding_extents++;
1469 spin_unlock(&BTRFS_I(inode)->lock);
1470 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001471
Chris Mason75eff682008-12-15 15:54:40 -05001472 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001473 BTRFS_I(inode)->delalloc_bytes += len;
1474 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001475 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001476 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1477 &root->fs_info->delalloc_inodes);
1478 }
Chris Mason75eff682008-12-15 15:54:40 -05001479 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001480 }
Chris Mason291d6732008-01-29 15:55:23 -05001481}
1482
Chris Masond352ac62008-09-29 15:18:18 -04001483/*
1484 * extent_io.c clear_bit_hook, see set_bit_hook for why
1485 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001486static void btrfs_clear_bit_hook(struct inode *inode,
1487 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001488{
Chris Mason75eff682008-12-15 15:54:40 -05001489 /*
1490 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001491 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001492 * bit, which is only set or cleared with irqs on
1493 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001494 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001495 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001496 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001497 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001498
Josef Bacik9e0baf62011-07-15 15:16:44 +00001499 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001500 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001501 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1502 spin_lock(&BTRFS_I(inode)->lock);
1503 BTRFS_I(inode)->outstanding_extents--;
1504 spin_unlock(&BTRFS_I(inode)->lock);
1505 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001506
1507 if (*bits & EXTENT_DO_ACCOUNTING)
1508 btrfs_delalloc_release_metadata(inode, len);
1509
Josef Bacik0cb59c92010-07-02 12:14:14 -04001510 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1511 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001512 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001513
Chris Mason75eff682008-12-15 15:54:40 -05001514 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001515 root->fs_info->delalloc_bytes -= len;
1516 BTRFS_I(inode)->delalloc_bytes -= len;
1517
Josef Bacik0cb59c92010-07-02 12:14:14 -04001518 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001519 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1520 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1521 }
Chris Mason75eff682008-12-15 15:54:40 -05001522 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001523 }
Chris Mason291d6732008-01-29 15:55:23 -05001524}
1525
Chris Masond352ac62008-09-29 15:18:18 -04001526/*
1527 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1528 * we don't create bios that span stripes or chunks
1529 */
Chris Mason239b14b2008-03-24 15:02:07 -04001530int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001531 size_t size, struct bio *bio,
1532 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001533{
1534 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1535 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001536 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001537 u64 length = 0;
1538 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001539 int ret;
1540
Chris Mason771ed682008-11-06 22:02:51 -05001541 if (bio_flags & EXTENT_BIO_COMPRESSED)
1542 return 0;
1543
Chris Masonf2d8d742008-04-21 10:03:05 -04001544 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001545 map_tree = &root->fs_info->mapping_tree;
1546 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001547 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001548 &map_length, NULL, 0);
Jeff Mahoney3444a972011-10-03 23:23:13 -04001549 /* Will always return 0 or 1 with map_multi == NULL */
1550 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001551 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001552 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001553 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001554}
1555
Chris Masond352ac62008-09-29 15:18:18 -04001556/*
1557 * in order to insert checksums into the metadata in large chunks,
1558 * we wait until bio submission time. All the pages in the bio are
1559 * checksummed and sums are attached onto the ordered extent record.
1560 *
1561 * At IO completion time the cums attached on the ordered extent record
1562 * are inserted into the btree
1563 */
Chris Masond3977122009-01-05 21:25:51 -05001564static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1565 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001566 unsigned long bio_flags,
1567 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001568{
Chris Mason065631f2008-02-20 12:07:25 -05001569 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001570 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001571
Chris Masond20f7042008-12-08 16:58:54 -05001572 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001573 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001574 return 0;
1575}
Chris Masone0156402008-04-16 11:15:20 -04001576
Chris Mason4a69a412008-11-06 22:03:00 -05001577/*
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 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001585static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001586 int mirror_num, unsigned long bio_flags,
1587 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001588{
1589 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001590 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001591}
1592
Chris Masond352ac62008-09-29 15:18:18 -04001593/*
Chris Masoncad321a2008-12-17 14:51:42 -05001594 * extent_io.c submission hook. This does the right thing for csum calculation
1595 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001596 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001597static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001598 int mirror_num, unsigned long bio_flags,
1599 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001600{
1601 struct btrfs_root *root = BTRFS_I(inode)->root;
1602 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001603 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001604 int metadata = 0;
Chris Mason44b8bd72008-04-16 11:14:51 -04001605
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001606 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001607
Liu Bo83eea1f2012-07-10 05:28:39 -06001608 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001609 metadata = 2;
1610
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001611 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001612 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1613 if (ret)
1614 return ret;
1615
Chris Masond20f7042008-12-08 16:58:54 -05001616 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001617 return btrfs_submit_compressed_read(inode, bio,
1618 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001619 } else if (!skip_sum) {
1620 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1621 if (ret)
1622 return ret;
1623 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001624 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001625 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001626 /* csum items have already been cloned */
1627 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1628 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001629 /* we're doing a write, do the async checksumming */
1630 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001631 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001632 bio_flags, bio_offset,
1633 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001634 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001635 }
1636
Chris Mason0b86a832008-03-24 15:01:56 -04001637mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001638 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001639}
Chris Mason6885f302008-02-20 16:11:05 -05001640
Chris Masond352ac62008-09-29 15:18:18 -04001641/*
1642 * given a list of ordered sums record them in the inode. This happens
1643 * at IO completion time based on sums calculated at bio submission time.
1644 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001645static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001646 struct inode *inode, u64 file_offset,
1647 struct list_head *list)
1648{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001649 struct btrfs_ordered_sum *sum;
1650
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001651 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001652 btrfs_csum_file_blocks(trans,
1653 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001654 }
1655 return 0;
1656}
1657
Josef Bacik2ac55d42010-02-03 19:33:23 +00001658int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1659 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001660{
Chris Masond3977122009-01-05 21:25:51 -05001661 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001662 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001663 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001664 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001665}
1666
Chris Masond352ac62008-09-29 15:18:18 -04001667/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001668struct btrfs_writepage_fixup {
1669 struct page *page;
1670 struct btrfs_work work;
1671};
1672
Christoph Hellwigb2950862008-12-02 09:54:17 -05001673static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001674{
1675 struct btrfs_writepage_fixup *fixup;
1676 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001677 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001678 struct page *page;
1679 struct inode *inode;
1680 u64 page_start;
1681 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001682 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001683
1684 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1685 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001686again:
Chris Mason247e7432008-07-17 12:53:51 -04001687 lock_page(page);
1688 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1689 ClearPageChecked(page);
1690 goto out_page;
1691 }
1692
1693 inode = page->mapping->host;
1694 page_start = page_offset(page);
1695 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1696
Josef Bacik2ac55d42010-02-03 19:33:23 +00001697 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001698 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001699
1700 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001701 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001702 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001703
1704 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1705 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001706 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1707 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001708 unlock_page(page);
1709 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001710 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001711 goto again;
1712 }
Chris Mason247e7432008-07-17 12:53:51 -04001713
Jeff Mahoney87826df2012-02-15 16:23:57 +01001714 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1715 if (ret) {
1716 mapping_set_error(page->mapping, ret);
1717 end_extent_writepage(page, ret, page_start, page_end);
1718 ClearPageChecked(page);
1719 goto out;
1720 }
1721
Josef Bacik2ac55d42010-02-03 19:33:23 +00001722 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001723 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001724 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001725out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001726 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1727 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001728out_page:
1729 unlock_page(page);
1730 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001731 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001732}
1733
1734/*
1735 * There are a few paths in the higher layers of the kernel that directly
1736 * set the page dirty bit without asking the filesystem if it is a
1737 * good idea. This causes problems because we want to make sure COW
1738 * properly happens and the data=ordered rules are followed.
1739 *
Chris Masonc8b97812008-10-29 14:49:59 -04001740 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001741 * hasn't been properly setup for IO. We kick off an async process
1742 * to fix it up. The async helper will wait for ordered extents, set
1743 * the delalloc bit and make it safe to write the page.
1744 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001745static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001746{
1747 struct inode *inode = page->mapping->host;
1748 struct btrfs_writepage_fixup *fixup;
1749 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001750
Chris Mason8b62b722009-09-02 16:53:46 -04001751 /* this page is properly in the ordered list */
1752 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001753 return 0;
1754
1755 if (PageChecked(page))
1756 return -EAGAIN;
1757
1758 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1759 if (!fixup)
1760 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001761
Chris Mason247e7432008-07-17 12:53:51 -04001762 SetPageChecked(page);
1763 page_cache_get(page);
1764 fixup->work.func = btrfs_writepage_fixup_worker;
1765 fixup->page = page;
1766 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001767 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001768}
1769
Yan Zhengd899e052008-10-30 14:25:28 -04001770static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1771 struct inode *inode, u64 file_pos,
1772 u64 disk_bytenr, u64 disk_num_bytes,
1773 u64 num_bytes, u64 ram_bytes,
1774 u8 compression, u8 encryption,
1775 u16 other_encoding, int extent_type)
1776{
1777 struct btrfs_root *root = BTRFS_I(inode)->root;
1778 struct btrfs_file_extent_item *fi;
1779 struct btrfs_path *path;
1780 struct extent_buffer *leaf;
1781 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001782 int ret;
1783
1784 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001785 if (!path)
1786 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001787
Chris Masonb9473432009-03-13 11:00:37 -04001788 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001789
1790 /*
1791 * we may be replacing one extent in the tree with another.
1792 * The new extent is pinned in the extent map, and we don't want
1793 * to drop it from the cache until it is completely in the btree.
1794 *
1795 * So, tell btrfs_drop_extents to leave this extent in the cache.
1796 * the caller is expected to unpin it and allow it to be merged
1797 * with the others.
1798 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001799 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001800 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001801 if (ret)
1802 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001803
Li Zefan33345d012011-04-20 10:31:50 +08001804 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001805 ins.offset = file_pos;
1806 ins.type = BTRFS_EXTENT_DATA_KEY;
1807 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001808 if (ret)
1809 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001810 leaf = path->nodes[0];
1811 fi = btrfs_item_ptr(leaf, path->slots[0],
1812 struct btrfs_file_extent_item);
1813 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1814 btrfs_set_file_extent_type(leaf, fi, extent_type);
1815 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1816 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1817 btrfs_set_file_extent_offset(leaf, fi, 0);
1818 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1819 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1820 btrfs_set_file_extent_compression(leaf, fi, compression);
1821 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1822 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001823
Yan Zhengd899e052008-10-30 14:25:28 -04001824 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001825 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001826
1827 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001828
1829 ins.objectid = disk_bytenr;
1830 ins.offset = disk_num_bytes;
1831 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001832 ret = btrfs_alloc_reserved_file_extent(trans, root,
1833 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001834 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001835out:
Yan Zhengd899e052008-10-30 14:25:28 -04001836 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001837
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001838 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001839}
1840
Chris Mason5d13a982009-03-13 11:41:46 -04001841/*
1842 * helper function for btrfs_finish_ordered_io, this
1843 * just reads in some of the csum leaves to prime them into ram
1844 * before we start the transaction. It limits the amount of btree
1845 * reads required while inside the transaction.
1846 */
Chris Masond352ac62008-09-29 15:18:18 -04001847/* as ordered data IO finishes, this gets called so we can finish
1848 * an ordered extent if the range of bytes in the file it covers are
1849 * fully written.
1850 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001851static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001852{
Josef Bacik5fd02042012-05-02 14:00:54 -04001853 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001854 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001855 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001856 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001857 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001858 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001859 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001860 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001861
Liu Bo83eea1f2012-07-10 05:28:39 -06001862 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001863
Josef Bacik5fd02042012-05-02 14:00:54 -04001864 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1865 ret = -EIO;
1866 goto out;
1867 }
1868
Yan, Zhengc2167752009-11-12 09:34:21 +00001869 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001870 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Yan, Zhengc2167752009-11-12 09:34:21 +00001871 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1872 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001873 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001874 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001875 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001876 trans = btrfs_join_transaction(root);
Liu Bod280e5b2012-08-21 21:13:25 -06001877 if (IS_ERR(trans)) {
1878 ret = PTR_ERR(trans);
1879 trans = NULL;
1880 goto out;
1881 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001882 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason21151332011-11-10 20:39:08 -05001883 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001884 if (ret) /* -ENOMEM or corruption */
1885 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001886 }
1887 goto out;
1888 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001889
Josef Bacik2ac55d42010-02-03 19:33:23 +00001890 lock_extent_bits(io_tree, ordered_extent->file_offset,
1891 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001892 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001893
Josef Bacik0cb59c92010-07-02 12:14:14 -04001894 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001895 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001896 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001897 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001898 if (IS_ERR(trans)) {
1899 ret = PTR_ERR(trans);
1900 trans = NULL;
1901 goto out_unlock;
1902 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001903 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001904
Chris Masonc8b97812008-10-29 14:49:59 -04001905 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001906 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001907 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001908 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001909 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001910 ordered_extent->file_offset,
1911 ordered_extent->file_offset +
1912 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001913 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001914 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001915 ret = insert_reserved_file_extent(trans, inode,
1916 ordered_extent->file_offset,
1917 ordered_extent->start,
1918 ordered_extent->disk_len,
1919 ordered_extent->len,
1920 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001921 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001922 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04001923 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04001924 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1925 ordered_extent->file_offset, ordered_extent->len,
1926 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001927 if (ret < 0) {
1928 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001929 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001930 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001931
Chris Masone6dcd2d2008-07-17 12:53:50 -04001932 add_pending_csums(trans, inode, ordered_extent->file_offset,
1933 &ordered_extent->list);
1934
Josef Bacik1ef30be2011-04-05 19:25:36 -04001935 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Miao Xiea39f7522011-09-11 10:52:25 -04001936 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Chris Mason21151332011-11-10 20:39:08 -05001937 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001938 if (ret) { /* -ENOMEM or corruption */
1939 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001940 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001941 }
Josef Bacik7c735312012-08-13 15:43:26 -04001942 } else {
1943 btrfs_set_inode_last_trans(trans, inode);
Josef Bacik1ef30be2011-04-05 19:25:36 -04001944 }
1945 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04001946out_unlock:
1947 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1948 ordered_extent->file_offset +
1949 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00001950out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001951 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001952 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06001953 if (trans)
1954 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001955
Josef Bacik5fd02042012-05-02 14:00:54 -04001956 if (ret)
1957 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
1958 ordered_extent->file_offset +
1959 ordered_extent->len - 1, NULL, GFP_NOFS);
1960
1961 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08001962 * This needs to be done to make sure anybody waiting knows we are done
1963 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04001964 */
1965 btrfs_remove_ordered_extent(inode, ordered_extent);
1966
Chris Masone6dcd2d2008-07-17 12:53:50 -04001967 /* once for us */
1968 btrfs_put_ordered_extent(ordered_extent);
1969 /* once for the tree */
1970 btrfs_put_ordered_extent(ordered_extent);
1971
Josef Bacik5fd02042012-05-02 14:00:54 -04001972 return ret;
1973}
1974
1975static void finish_ordered_fn(struct btrfs_work *work)
1976{
1977 struct btrfs_ordered_extent *ordered_extent;
1978 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
1979 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001980}
1981
Christoph Hellwigb2950862008-12-02 09:54:17 -05001982static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001983 struct extent_state *state, int uptodate)
1984{
Josef Bacik5fd02042012-05-02 14:00:54 -04001985 struct inode *inode = page->mapping->host;
1986 struct btrfs_root *root = BTRFS_I(inode)->root;
1987 struct btrfs_ordered_extent *ordered_extent = NULL;
1988 struct btrfs_workers *workers;
1989
liubo1abe9b82011-03-24 11:18:59 +00001990 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1991
Chris Mason8b62b722009-09-02 16:53:46 -04001992 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04001993 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1994 end - start + 1, uptodate))
1995 return 0;
1996
1997 ordered_extent->work.func = finish_ordered_fn;
1998 ordered_extent->work.flags = 0;
1999
Liu Bo83eea1f2012-07-10 05:28:39 -06002000 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002001 workers = &root->fs_info->endio_freespace_worker;
2002 else
2003 workers = &root->fs_info->endio_write_workers;
2004 btrfs_queue_worker(workers, &ordered_extent->work);
2005
2006 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002007}
2008
Chris Masond352ac62008-09-29 15:18:18 -04002009/*
Chris Masond352ac62008-09-29 15:18:18 -04002010 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002011 * if there's a match, we allow the bio to finish. If not, the code in
2012 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002013 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002014static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002015 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002016{
Chris Mason35ebb932007-10-30 16:56:53 -04002017 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04002018 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002019 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002020 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002021 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002022 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002023 struct btrfs_root *root = BTRFS_I(inode)->root;
2024 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002025
Chris Masond20f7042008-12-08 16:58:54 -05002026 if (PageChecked(page)) {
2027 ClearPageChecked(page);
2028 goto good;
2029 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002030
2031 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002032 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002033
Yan Zheng17d217f2008-12-12 10:03:38 -05002034 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002035 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002036 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2037 GFP_NOFS);
2038 return 0;
2039 }
2040
Yanc2e639f2008-02-04 08:57:25 -05002041 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002042 private = state->private;
2043 ret = 0;
2044 } else {
2045 ret = get_state_private(io_tree, start, &private);
2046 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002047 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002048 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002049 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002050
Chris Masonff79f812007-10-15 16:22:25 -04002051 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2052 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002053 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002054 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002055
Cong Wang7ac687d2011-11-25 23:14:28 +08002056 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002057good:
Chris Mason07157aa2007-08-30 08:50:51 -04002058 return 0;
2059
2060zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002061 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002062 "private %llu\n",
2063 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002064 (unsigned long long)start, csum,
2065 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002066 memset(kaddr + offset, 1, end - start + 1);
2067 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002068 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002069 if (private == 0)
2070 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002071 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002072}
Chris Masonb888db22007-08-27 16:49:44 -04002073
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002074struct delayed_iput {
2075 struct list_head list;
2076 struct inode *inode;
2077};
2078
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002079/* JDM: If this is fs-wide, why can't we add a pointer to
2080 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002081void btrfs_add_delayed_iput(struct inode *inode)
2082{
2083 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2084 struct delayed_iput *delayed;
2085
2086 if (atomic_add_unless(&inode->i_count, -1, 1))
2087 return;
2088
2089 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2090 delayed->inode = inode;
2091
2092 spin_lock(&fs_info->delayed_iput_lock);
2093 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2094 spin_unlock(&fs_info->delayed_iput_lock);
2095}
2096
2097void btrfs_run_delayed_iputs(struct btrfs_root *root)
2098{
2099 LIST_HEAD(list);
2100 struct btrfs_fs_info *fs_info = root->fs_info;
2101 struct delayed_iput *delayed;
2102 int empty;
2103
2104 spin_lock(&fs_info->delayed_iput_lock);
2105 empty = list_empty(&fs_info->delayed_iputs);
2106 spin_unlock(&fs_info->delayed_iput_lock);
2107 if (empty)
2108 return;
2109
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002110 spin_lock(&fs_info->delayed_iput_lock);
2111 list_splice_init(&fs_info->delayed_iputs, &list);
2112 spin_unlock(&fs_info->delayed_iput_lock);
2113
2114 while (!list_empty(&list)) {
2115 delayed = list_entry(list.next, struct delayed_iput, list);
2116 list_del(&delayed->list);
2117 iput(delayed->inode);
2118 kfree(delayed);
2119 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002120}
2121
Yan, Zhengd68fc572010-05-16 10:49:58 -04002122enum btrfs_orphan_cleanup_state {
2123 ORPHAN_CLEANUP_STARTED = 1,
2124 ORPHAN_CLEANUP_DONE = 2,
2125};
2126
2127/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002128 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002129 * files in the subvolume, it removes orphan item and frees block_rsv
2130 * structure.
2131 */
2132void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2133 struct btrfs_root *root)
2134{
Josef Bacik90290e12011-12-02 15:44:12 -05002135 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002136 int ret;
2137
Josef Bacik8a35d952012-05-23 14:26:42 -04002138 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002139 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2140 return;
2141
Josef Bacik90290e12011-12-02 15:44:12 -05002142 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002143 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002144 spin_unlock(&root->orphan_lock);
2145 return;
2146 }
2147
2148 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2149 spin_unlock(&root->orphan_lock);
2150 return;
2151 }
2152
2153 block_rsv = root->orphan_block_rsv;
2154 root->orphan_block_rsv = NULL;
2155 spin_unlock(&root->orphan_lock);
2156
Yan, Zhengd68fc572010-05-16 10:49:58 -04002157 if (root->orphan_item_inserted &&
2158 btrfs_root_refs(&root->root_item) > 0) {
2159 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2160 root->root_key.objectid);
2161 BUG_ON(ret);
2162 root->orphan_item_inserted = 0;
2163 }
2164
Josef Bacik90290e12011-12-02 15:44:12 -05002165 if (block_rsv) {
2166 WARN_ON(block_rsv->size > 0);
2167 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002168 }
2169}
2170
2171/*
Josef Bacik7b128762008-07-24 12:17:14 -04002172 * This creates an orphan entry for the given inode in case something goes
2173 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002174 *
2175 * NOTE: caller of this function should reserve 5 units of metadata for
2176 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002177 */
2178int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2179{
2180 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002181 struct btrfs_block_rsv *block_rsv = NULL;
2182 int reserve = 0;
2183 int insert = 0;
2184 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002185
Yan, Zhengd68fc572010-05-16 10:49:58 -04002186 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002187 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002188 if (!block_rsv)
2189 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002190 }
2191
Yan, Zhengd68fc572010-05-16 10:49:58 -04002192 spin_lock(&root->orphan_lock);
2193 if (!root->orphan_block_rsv) {
2194 root->orphan_block_rsv = block_rsv;
2195 } else if (block_rsv) {
2196 btrfs_free_block_rsv(root, block_rsv);
2197 block_rsv = NULL;
2198 }
Josef Bacik7b128762008-07-24 12:17:14 -04002199
Josef Bacik8a35d952012-05-23 14:26:42 -04002200 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2201 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002202#if 0
2203 /*
2204 * For proper ENOSPC handling, we should do orphan
2205 * cleanup when mounting. But this introduces backward
2206 * compatibility issue.
2207 */
2208 if (!xchg(&root->orphan_item_inserted, 1))
2209 insert = 2;
2210 else
2211 insert = 1;
2212#endif
2213 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002214 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002215 }
Josef Bacik7b128762008-07-24 12:17:14 -04002216
Josef Bacik72ac3c02012-05-23 14:13:11 -04002217 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2218 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002219 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002220 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002221
Yan, Zhengd68fc572010-05-16 10:49:58 -04002222 /* grab metadata reservation from transaction handle */
2223 if (reserve) {
2224 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002225 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002226 }
2227
2228 /* insert an orphan item to track this unlinked/truncated file */
2229 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002230 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002231 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002232 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2233 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002234 btrfs_abort_transaction(trans, root, ret);
2235 return ret;
2236 }
2237 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002238 }
2239
2240 /* insert an orphan item to track subvolume contains orphan files */
2241 if (insert >= 2) {
2242 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2243 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002244 if (ret && ret != -EEXIST) {
2245 btrfs_abort_transaction(trans, root, ret);
2246 return ret;
2247 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002248 }
2249 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002250}
2251
2252/*
2253 * We have done the truncate/delete so we can go ahead and remove the orphan
2254 * item for this particular inode.
2255 */
2256int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2257{
2258 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002259 int delete_item = 0;
2260 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002261 int ret = 0;
2262
Yan, Zhengd68fc572010-05-16 10:49:58 -04002263 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002264 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2265 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002266 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002267
Josef Bacik72ac3c02012-05-23 14:13:11 -04002268 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2269 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002270 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002271 spin_unlock(&root->orphan_lock);
2272
2273 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002274 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002275 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002276 }
2277
Josef Bacik8a35d952012-05-23 14:26:42 -04002278 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002279 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002280 atomic_dec(&root->orphan_inodes);
2281 }
Josef Bacik7b128762008-07-24 12:17:14 -04002282
Yan, Zhengd68fc572010-05-16 10:49:58 -04002283 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002284}
2285
2286/*
2287 * this cleans up any orphans that may be left on the list from the last use
2288 * of this root.
2289 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002290int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002291{
2292 struct btrfs_path *path;
2293 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002294 struct btrfs_key key, found_key;
2295 struct btrfs_trans_handle *trans;
2296 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002297 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002298 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2299
Yan, Zhengd68fc572010-05-16 10:49:58 -04002300 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002301 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002302
2303 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002304 if (!path) {
2305 ret = -ENOMEM;
2306 goto out;
2307 }
Josef Bacik7b128762008-07-24 12:17:14 -04002308 path->reada = -1;
2309
2310 key.objectid = BTRFS_ORPHAN_OBJECTID;
2311 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2312 key.offset = (u64)-1;
2313
Josef Bacik7b128762008-07-24 12:17:14 -04002314 while (1) {
2315 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002316 if (ret < 0)
2317 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002318
2319 /*
2320 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002321 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002322 * find the key and see if we have stuff that matches
2323 */
2324 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002325 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002326 if (path->slots[0] == 0)
2327 break;
2328 path->slots[0]--;
2329 }
2330
2331 /* pull out the item */
2332 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002333 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2334
2335 /* make sure the item matches what we want */
2336 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2337 break;
2338 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2339 break;
2340
2341 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002342 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002343
2344 /*
2345 * this is where we are basically btrfs_lookup, without the
2346 * crossing root thing. we store the inode number in the
2347 * offset of the orphan item.
2348 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002349
2350 if (found_key.offset == last_objectid) {
2351 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2352 "stopping orphan cleanup\n");
2353 ret = -EINVAL;
2354 goto out;
2355 }
2356
2357 last_objectid = found_key.offset;
2358
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002359 found_key.objectid = found_key.offset;
2360 found_key.type = BTRFS_INODE_ITEM_KEY;
2361 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002362 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002363 ret = PTR_RET(inode);
2364 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002365 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002366
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002367 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2368 struct btrfs_root *dead_root;
2369 struct btrfs_fs_info *fs_info = root->fs_info;
2370 int is_dead_root = 0;
2371
2372 /*
2373 * this is an orphan in the tree root. Currently these
2374 * could come from 2 sources:
2375 * a) a snapshot deletion in progress
2376 * b) a free space cache inode
2377 * We need to distinguish those two, as the snapshot
2378 * orphan must not get deleted.
2379 * find_dead_roots already ran before us, so if this
2380 * is a snapshot deletion, we should find the root
2381 * in the dead_roots list
2382 */
2383 spin_lock(&fs_info->trans_lock);
2384 list_for_each_entry(dead_root, &fs_info->dead_roots,
2385 root_list) {
2386 if (dead_root->root_key.objectid ==
2387 found_key.objectid) {
2388 is_dead_root = 1;
2389 break;
2390 }
2391 }
2392 spin_unlock(&fs_info->trans_lock);
2393 if (is_dead_root) {
2394 /* prevent this orphan from being found again */
2395 key.offset = found_key.objectid - 1;
2396 continue;
2397 }
2398 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002399 /*
2400 * Inode is already gone but the orphan item is still there,
2401 * kill the orphan item.
2402 */
2403 if (ret == -ESTALE) {
2404 trans = btrfs_start_transaction(root, 1);
2405 if (IS_ERR(trans)) {
2406 ret = PTR_ERR(trans);
2407 goto out;
2408 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002409 printk(KERN_ERR "auto deleting %Lu\n",
2410 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002411 ret = btrfs_del_orphan_item(trans, root,
2412 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002413 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002414 btrfs_end_transaction(trans, root);
2415 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002416 }
Josef Bacik7b128762008-07-24 12:17:14 -04002417
Josef Bacik7b128762008-07-24 12:17:14 -04002418 /*
2419 * add this inode to the orphan list so btrfs_orphan_del does
2420 * the proper thing when we hit it
2421 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002422 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2423 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002424
Josef Bacik7b128762008-07-24 12:17:14 -04002425 /* if we have links, this was a truncate, lets do that */
2426 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002427 if (!S_ISREG(inode->i_mode)) {
2428 WARN_ON(1);
2429 iput(inode);
2430 continue;
2431 }
Josef Bacik7b128762008-07-24 12:17:14 -04002432 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002433 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002434 } else {
2435 nr_unlink++;
2436 }
2437
2438 /* this will do delete_inode and everything for us */
2439 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002440 if (ret)
2441 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002442 }
Miao Xie3254c872011-11-10 20:45:05 -05002443 /* release the path since we're done with it */
2444 btrfs_release_path(path);
2445
Yan, Zhengd68fc572010-05-16 10:49:58 -04002446 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2447
2448 if (root->orphan_block_rsv)
2449 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2450 (u64)-1);
2451
2452 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002453 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002454 if (!IS_ERR(trans))
2455 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002456 }
Josef Bacik7b128762008-07-24 12:17:14 -04002457
2458 if (nr_unlink)
2459 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2460 if (nr_truncate)
2461 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002462
2463out:
2464 if (ret)
2465 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2466 btrfs_free_path(path);
2467 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002468}
2469
Chris Masond352ac62008-09-29 15:18:18 -04002470/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002471 * very simple check to peek ahead in the leaf looking for xattrs. If we
2472 * don't find any xattrs, we know there can't be any acls.
2473 *
2474 * slot is the slot the inode is in, objectid is the objectid of the inode
2475 */
2476static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2477 int slot, u64 objectid)
2478{
2479 u32 nritems = btrfs_header_nritems(leaf);
2480 struct btrfs_key found_key;
2481 int scanned = 0;
2482
2483 slot++;
2484 while (slot < nritems) {
2485 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2486
2487 /* we found a different objectid, there must not be acls */
2488 if (found_key.objectid != objectid)
2489 return 0;
2490
2491 /* we found an xattr, assume we've got an acl */
2492 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2493 return 1;
2494
2495 /*
2496 * we found a key greater than an xattr key, there can't
2497 * be any acls later on
2498 */
2499 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2500 return 0;
2501
2502 slot++;
2503 scanned++;
2504
2505 /*
2506 * it goes inode, inode backrefs, xattrs, extents,
2507 * so if there are a ton of hard links to an inode there can
2508 * be a lot of backrefs. Don't waste time searching too hard,
2509 * this is just an optimization
2510 */
2511 if (scanned >= 8)
2512 break;
2513 }
2514 /* we hit the end of the leaf before we found an xattr or
2515 * something larger than an xattr. We have to assume the inode
2516 * has acls
2517 */
2518 return 1;
2519}
2520
2521/*
Chris Masond352ac62008-09-29 15:18:18 -04002522 * read an inode from the btree into the in-memory inode
2523 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002524static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002525{
2526 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002527 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002528 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002529 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002530 struct btrfs_root *root = BTRFS_I(inode)->root;
2531 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002532 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002533 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002534 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002535 bool filled = false;
2536
2537 ret = btrfs_fill_inode(inode, &rdev);
2538 if (!ret)
2539 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002540
2541 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002542 if (!path)
2543 goto make_bad;
2544
Josef Bacikd90c7322011-05-17 09:50:54 -04002545 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002546 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002547
Chris Mason39279cc2007-06-12 06:35:45 -04002548 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002549 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002550 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002551
Chris Mason5f39d392007-10-15 16:14:19 -04002552 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002553
2554 if (filled)
2555 goto cache_acl;
2556
Chris Mason5f39d392007-10-15 16:14:19 -04002557 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2558 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002559 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002560 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002561 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2562 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002563 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002564
2565 tspec = btrfs_inode_atime(inode_item);
2566 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2567 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2568
2569 tspec = btrfs_inode_mtime(inode_item);
2570 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2571 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2572
2573 tspec = btrfs_inode_ctime(inode_item);
2574 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2575 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2576
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002577 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002578 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002579 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2580
2581 /*
2582 * If we were modified in the current generation and evicted from memory
2583 * and then re-read we need to do a full sync since we don't have any
2584 * idea about which extents were modified before we were evicted from
2585 * cache.
2586 */
2587 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2588 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2589 &BTRFS_I(inode)->runtime_flags);
2590
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002591 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002592 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002593 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002594 rdev = btrfs_inode_rdev(leaf, inode_item);
2595
Josef Bacikaec74772008-07-24 12:12:38 -04002596 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002597 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002598cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002599 /*
2600 * try to precache a NULL acl entry for files that don't have
2601 * any xattrs or acls
2602 */
Li Zefan33345d012011-04-20 10:31:50 +08002603 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2604 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002605 if (!maybe_acls)
2606 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002607
Chris Mason39279cc2007-06-12 06:35:45 -04002608 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002609
Chris Mason39279cc2007-06-12 06:35:45 -04002610 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002611 case S_IFREG:
2612 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002613 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002614 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002615 inode->i_fop = &btrfs_file_operations;
2616 inode->i_op = &btrfs_file_inode_operations;
2617 break;
2618 case S_IFDIR:
2619 inode->i_fop = &btrfs_dir_file_operations;
2620 if (root == root->fs_info->tree_root)
2621 inode->i_op = &btrfs_dir_ro_inode_operations;
2622 else
2623 inode->i_op = &btrfs_dir_inode_operations;
2624 break;
2625 case S_IFLNK:
2626 inode->i_op = &btrfs_symlink_inode_operations;
2627 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002628 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002629 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002630 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002631 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002632 init_special_inode(inode, inode->i_mode, rdev);
2633 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002634 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002635
2636 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002637 return;
2638
2639make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002640 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002641 make_bad_inode(inode);
2642}
2643
Chris Masond352ac62008-09-29 15:18:18 -04002644/*
2645 * given a leaf and an inode, copy the inode fields into the leaf
2646 */
Chris Masone02119d2008-09-05 16:13:11 -04002647static void fill_inode_item(struct btrfs_trans_handle *trans,
2648 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002649 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002650 struct inode *inode)
2651{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002652 btrfs_set_inode_uid(leaf, item, i_uid_read(inode));
2653 btrfs_set_inode_gid(leaf, item, i_gid_read(inode));
Chris Masondbe674a2008-07-17 12:54:05 -04002654 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002655 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2656 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2657
2658 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2659 inode->i_atime.tv_sec);
2660 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2661 inode->i_atime.tv_nsec);
2662
2663 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2664 inode->i_mtime.tv_sec);
2665 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2666 inode->i_mtime.tv_nsec);
2667
2668 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2669 inode->i_ctime.tv_sec);
2670 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2671 inode->i_ctime.tv_nsec);
2672
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002673 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002674 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002675 btrfs_set_inode_sequence(leaf, item, inode->i_version);
Chris Masone02119d2008-09-05 16:13:11 -04002676 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002677 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002678 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04002679 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002680}
2681
Chris Masond352ac62008-09-29 15:18:18 -04002682/*
2683 * copy everything in the in-memory inode into the btree.
2684 */
Chris Mason21151332011-11-10 20:39:08 -05002685static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002686 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002687{
2688 struct btrfs_inode_item *inode_item;
2689 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002690 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002691 int ret;
2692
2693 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002694 if (!path)
2695 return -ENOMEM;
2696
Chris Masonb9473432009-03-13 11:00:37 -04002697 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002698 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2699 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002700 if (ret) {
2701 if (ret > 0)
2702 ret = -ENOENT;
2703 goto failed;
2704 }
2705
Chris Masonb4ce94d2009-02-04 09:25:08 -05002706 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002707 leaf = path->nodes[0];
2708 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002709 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002710
Chris Masone02119d2008-09-05 16:13:11 -04002711 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002712 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002713 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002714 ret = 0;
2715failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002716 btrfs_free_path(path);
2717 return ret;
2718}
2719
Chris Masond352ac62008-09-29 15:18:18 -04002720/*
Chris Mason21151332011-11-10 20:39:08 -05002721 * copy everything in the in-memory inode into the btree.
2722 */
2723noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2724 struct btrfs_root *root, struct inode *inode)
2725{
2726 int ret;
2727
2728 /*
2729 * If the inode is a free space inode, we can deadlock during commit
2730 * if we put it into the delayed code.
2731 *
2732 * The data relocation inode should also be directly updated
2733 * without delay
2734 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002735 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002736 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002737 btrfs_update_root_times(trans, root);
2738
Chris Mason21151332011-11-10 20:39:08 -05002739 ret = btrfs_delayed_update_inode(trans, root, inode);
2740 if (!ret)
2741 btrfs_set_inode_last_trans(trans, inode);
2742 return ret;
2743 }
2744
2745 return btrfs_update_inode_item(trans, root, inode);
2746}
2747
Josef Bacikbe6aef62012-10-22 15:43:12 -04002748noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2749 struct btrfs_root *root,
2750 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002751{
2752 int ret;
2753
2754 ret = btrfs_update_inode(trans, root, inode);
2755 if (ret == -ENOSPC)
2756 return btrfs_update_inode_item(trans, root, inode);
2757 return ret;
2758}
2759
2760/*
Chris Masond352ac62008-09-29 15:18:18 -04002761 * unlink helper that gets used here in inode.c and in the tree logging
2762 * recovery code. It remove a link in a directory with a given name, and
2763 * also drops the back refs in the inode to the directory
2764 */
Al Viro92986792011-03-04 17:14:37 +00002765static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2766 struct btrfs_root *root,
2767 struct inode *dir, struct inode *inode,
2768 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002769{
2770 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002771 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002772 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002773 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002774 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002775 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002776 u64 ino = btrfs_ino(inode);
2777 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002778
2779 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002780 if (!path) {
2781 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002782 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002783 }
2784
Chris Masonb9473432009-03-13 11:00:37 -04002785 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002786 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002787 name, name_len, -1);
2788 if (IS_ERR(di)) {
2789 ret = PTR_ERR(di);
2790 goto err;
2791 }
2792 if (!di) {
2793 ret = -ENOENT;
2794 goto err;
2795 }
Chris Mason5f39d392007-10-15 16:14:19 -04002796 leaf = path->nodes[0];
2797 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002798 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002799 if (ret)
2800 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002801 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002802
Li Zefan33345d012011-04-20 10:31:50 +08002803 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2804 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002805 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002806 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002807 "inode %llu parent %llu\n", name_len, name,
2808 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002809 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002810 goto err;
2811 }
2812
Miao Xie16cdcec2011-04-22 18:12:22 +08002813 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002814 if (ret) {
2815 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002816 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002817 }
Chris Mason39279cc2007-06-12 06:35:45 -04002818
Chris Masone02119d2008-09-05 16:13:11 -04002819 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002820 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002821 if (ret != 0 && ret != -ENOENT) {
2822 btrfs_abort_transaction(trans, root, ret);
2823 goto err;
2824 }
Chris Masone02119d2008-09-05 16:13:11 -04002825
2826 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2827 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002828 if (ret == -ENOENT)
2829 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002830err:
2831 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002832 if (ret)
2833 goto out;
2834
2835 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002836 inode_inc_iversion(inode);
2837 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002838 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002839 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002840out:
Chris Mason39279cc2007-06-12 06:35:45 -04002841 return ret;
2842}
2843
Al Viro92986792011-03-04 17:14:37 +00002844int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2845 struct btrfs_root *root,
2846 struct inode *dir, struct inode *inode,
2847 const char *name, int name_len)
2848{
2849 int ret;
2850 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2851 if (!ret) {
2852 btrfs_drop_nlink(inode);
2853 ret = btrfs_update_inode(trans, root, inode);
2854 }
2855 return ret;
2856}
2857
2858
Yan, Zhenga22285a2010-05-16 10:48:46 -04002859/* helper to check if there is any shared block in the path */
2860static int check_path_shared(struct btrfs_root *root,
2861 struct btrfs_path *path)
2862{
2863 struct extent_buffer *eb;
2864 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002865 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002866
2867 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002868 int ret;
2869
Yan, Zhenga22285a2010-05-16 10:48:46 -04002870 if (!path->nodes[level])
2871 break;
2872 eb = path->nodes[level];
2873 if (!btrfs_block_can_be_shared(root, eb))
2874 continue;
2875 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2876 &refs, NULL);
2877 if (refs > 1)
2878 return 1;
2879 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002880 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002881}
2882
2883/*
2884 * helper to start transaction for unlink and rmdir.
2885 *
2886 * unlink and rmdir are special in btrfs, they do not always free space.
2887 * so in enospc case, we should make sure they will free space before
2888 * allowing them to use the global metadata reservation.
2889 */
2890static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2891 struct dentry *dentry)
2892{
2893 struct btrfs_trans_handle *trans;
2894 struct btrfs_root *root = BTRFS_I(dir)->root;
2895 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002896 struct btrfs_dir_item *di;
2897 struct inode *inode = dentry->d_inode;
2898 u64 index;
2899 int check_link = 1;
2900 int err = -ENOSPC;
2901 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002902 u64 ino = btrfs_ino(inode);
2903 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002904
Josef Bacike70bea52011-10-11 14:18:24 -04002905 /*
2906 * 1 for the possible orphan item
2907 * 1 for the dir item
2908 * 1 for the dir index
2909 * 1 for the inode ref
2910 * 1 for the inode ref in the tree log
2911 * 2 for the dir entries in the log
2912 * 1 for the inode
2913 */
2914 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002915 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2916 return trans;
2917
Li Zefan33345d012011-04-20 10:31:50 +08002918 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002919 return ERR_PTR(-ENOSPC);
2920
2921 /* check if there is someone else holds reference */
2922 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2923 return ERR_PTR(-ENOSPC);
2924
2925 if (atomic_read(&inode->i_count) > 2)
2926 return ERR_PTR(-ENOSPC);
2927
2928 if (xchg(&root->fs_info->enospc_unlink, 1))
2929 return ERR_PTR(-ENOSPC);
2930
2931 path = btrfs_alloc_path();
2932 if (!path) {
2933 root->fs_info->enospc_unlink = 0;
2934 return ERR_PTR(-ENOMEM);
2935 }
2936
Josef Bacik3880a1b2011-10-14 14:46:51 -04002937 /* 1 for the orphan item */
2938 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002939 if (IS_ERR(trans)) {
2940 btrfs_free_path(path);
2941 root->fs_info->enospc_unlink = 0;
2942 return trans;
2943 }
2944
2945 path->skip_locking = 1;
2946 path->search_commit_root = 1;
2947
2948 ret = btrfs_lookup_inode(trans, root, path,
2949 &BTRFS_I(dir)->location, 0);
2950 if (ret < 0) {
2951 err = ret;
2952 goto out;
2953 }
2954 if (ret == 0) {
2955 if (check_path_shared(root, path))
2956 goto out;
2957 } else {
2958 check_link = 0;
2959 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002960 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002961
2962 ret = btrfs_lookup_inode(trans, root, path,
2963 &BTRFS_I(inode)->location, 0);
2964 if (ret < 0) {
2965 err = ret;
2966 goto out;
2967 }
2968 if (ret == 0) {
2969 if (check_path_shared(root, path))
2970 goto out;
2971 } else {
2972 check_link = 0;
2973 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002974 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002975
2976 if (ret == 0 && S_ISREG(inode->i_mode)) {
2977 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002978 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002979 if (ret < 0) {
2980 err = ret;
2981 goto out;
2982 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002983 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002984 if (check_path_shared(root, path))
2985 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02002986 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002987 }
2988
2989 if (!check_link) {
2990 err = 0;
2991 goto out;
2992 }
2993
Li Zefan33345d012011-04-20 10:31:50 +08002994 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002995 dentry->d_name.name, dentry->d_name.len, 0);
2996 if (IS_ERR(di)) {
2997 err = PTR_ERR(di);
2998 goto out;
2999 }
3000 if (di) {
3001 if (check_path_shared(root, path))
3002 goto out;
3003 } else {
3004 err = 0;
3005 goto out;
3006 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003007 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003008
Mark Fashehf1863732012-08-08 11:32:27 -07003009 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3010 dentry->d_name.len, ino, dir_ino, 0,
3011 &index);
3012 if (ret) {
3013 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003014 goto out;
3015 }
Mark Fashehf1863732012-08-08 11:32:27 -07003016
Yan, Zhenga22285a2010-05-16 10:48:46 -04003017 if (check_path_shared(root, path))
3018 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003019
David Sterbab3b4aa72011-04-21 01:20:15 +02003020 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003021
Miao Xie16cdcec2011-04-22 18:12:22 +08003022 /*
3023 * This is a commit root search, if we can lookup inode item and other
3024 * relative items in the commit root, it means the transaction of
3025 * dir/file creation has been committed, and the dir index item that we
3026 * delay to insert has also been inserted into the commit root. So
3027 * we needn't worry about the delayed insertion of the dir index item
3028 * here.
3029 */
Li Zefan33345d012011-04-20 10:31:50 +08003030 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003031 dentry->d_name.name, dentry->d_name.len, 0);
3032 if (IS_ERR(di)) {
3033 err = PTR_ERR(di);
3034 goto out;
3035 }
3036 BUG_ON(ret == -ENOENT);
3037 if (check_path_shared(root, path))
3038 goto out;
3039
3040 err = 0;
3041out:
3042 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003043 /* Migrate the orphan reservation over */
3044 if (!err)
3045 err = btrfs_block_rsv_migrate(trans->block_rsv,
3046 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003047 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003048
Yan, Zhenga22285a2010-05-16 10:48:46 -04003049 if (err) {
3050 btrfs_end_transaction(trans, root);
3051 root->fs_info->enospc_unlink = 0;
3052 return ERR_PTR(err);
3053 }
3054
3055 trans->block_rsv = &root->fs_info->global_block_rsv;
3056 return trans;
3057}
3058
3059static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3060 struct btrfs_root *root)
3061{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003062 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003063 btrfs_block_rsv_release(root, trans->block_rsv,
3064 trans->bytes_reserved);
3065 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003066 BUG_ON(!root->fs_info->enospc_unlink);
3067 root->fs_info->enospc_unlink = 0;
3068 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003069 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003070}
3071
Chris Mason39279cc2007-06-12 06:35:45 -04003072static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3073{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003074 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003075 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003076 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003077 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05003078 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003079
Yan, Zhenga22285a2010-05-16 10:48:46 -04003080 trans = __unlink_start_trans(dir, dentry);
3081 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003082 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003083
Chris Mason12fcfd22009-03-24 10:24:20 -04003084 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3085
Chris Masone02119d2008-09-05 16:13:11 -04003086 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3087 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003088 if (ret)
3089 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003090
Yan, Zhenga22285a2010-05-16 10:48:46 -04003091 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003092 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003093 if (ret)
3094 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003095 }
Josef Bacik7b128762008-07-24 12:17:14 -04003096
Tsutomu Itohb5324022011-07-19 07:27:20 +00003097out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003098 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003099 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003100 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003101 return ret;
3102}
3103
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003104int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3105 struct btrfs_root *root,
3106 struct inode *dir, u64 objectid,
3107 const char *name, int name_len)
3108{
3109 struct btrfs_path *path;
3110 struct extent_buffer *leaf;
3111 struct btrfs_dir_item *di;
3112 struct btrfs_key key;
3113 u64 index;
3114 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003115 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003116
3117 path = btrfs_alloc_path();
3118 if (!path)
3119 return -ENOMEM;
3120
Li Zefan33345d012011-04-20 10:31:50 +08003121 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003122 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003123 if (IS_ERR_OR_NULL(di)) {
3124 if (!di)
3125 ret = -ENOENT;
3126 else
3127 ret = PTR_ERR(di);
3128 goto out;
3129 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003130
3131 leaf = path->nodes[0];
3132 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3133 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3134 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003135 if (ret) {
3136 btrfs_abort_transaction(trans, root, ret);
3137 goto out;
3138 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003139 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003140
3141 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3142 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003143 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003144 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003145 if (ret != -ENOENT) {
3146 btrfs_abort_transaction(trans, root, ret);
3147 goto out;
3148 }
Li Zefan33345d012011-04-20 10:31:50 +08003149 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003150 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003151 if (IS_ERR_OR_NULL(di)) {
3152 if (!di)
3153 ret = -ENOENT;
3154 else
3155 ret = PTR_ERR(di);
3156 btrfs_abort_transaction(trans, root, ret);
3157 goto out;
3158 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003159
3160 leaf = path->nodes[0];
3161 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003162 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003163 index = key.offset;
3164 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003165 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003166
Miao Xie16cdcec2011-04-22 18:12:22 +08003167 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003168 if (ret) {
3169 btrfs_abort_transaction(trans, root, ret);
3170 goto out;
3171 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003172
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003173 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003174 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003175 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003176 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003177 if (ret)
3178 btrfs_abort_transaction(trans, root, ret);
3179out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003180 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003181 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003182}
3183
Chris Mason39279cc2007-06-12 06:35:45 -04003184static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3185{
3186 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003187 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003188 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003189 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003190 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003191
David Sterbab3ae2442012-09-13 16:04:34 -06003192 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003193 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003194 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3195 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003196
Yan, Zhenga22285a2010-05-16 10:48:46 -04003197 trans = __unlink_start_trans(dir, dentry);
3198 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003199 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003200
Li Zefan33345d012011-04-20 10:31:50 +08003201 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003202 err = btrfs_unlink_subvol(trans, root, dir,
3203 BTRFS_I(inode)->location.objectid,
3204 dentry->d_name.name,
3205 dentry->d_name.len);
3206 goto out;
3207 }
3208
Josef Bacik7b128762008-07-24 12:17:14 -04003209 err = btrfs_orphan_add(trans, inode);
3210 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003211 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003212
Chris Mason39279cc2007-06-12 06:35:45 -04003213 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003214 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3215 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003216 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003217 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003218out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003219 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003220 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003221 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003222
Chris Mason39279cc2007-06-12 06:35:45 -04003223 return err;
3224}
3225
Chris Mason323ac952008-10-01 19:05:46 -04003226/*
Chris Mason39279cc2007-06-12 06:35:45 -04003227 * this can truncate away extent items, csum items and directory items.
3228 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003229 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003230 *
3231 * csum items that cross the new i_size are truncated to the new size
3232 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003233 *
3234 * min_type is the minimum key type to truncate down to. If set to 0, this
3235 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003236 */
Yan, Zheng80825102009-11-12 09:35:36 +00003237int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3238 struct btrfs_root *root,
3239 struct inode *inode,
3240 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003241{
Chris Mason39279cc2007-06-12 06:35:45 -04003242 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003243 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003244 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003245 struct btrfs_key key;
3246 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003247 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003248 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003249 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003250 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003251 u64 mask = root->sectorsize - 1;
3252 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003253 int found_extent;
3254 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003255 int pending_del_nr = 0;
3256 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003257 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003258 int ret;
3259 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003260 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003261
3262 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003263
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003264 path = btrfs_alloc_path();
3265 if (!path)
3266 return -ENOMEM;
3267 path->reada = -1;
3268
Josef Bacik5dc562c2012-08-17 13:14:17 -04003269 /*
3270 * We want to drop from the next block forward in case this new size is
3271 * not block aligned since we will be keeping the last block of the
3272 * extent just the way it is.
3273 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003274 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003275 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003276
Miao Xie16cdcec2011-04-22 18:12:22 +08003277 /*
3278 * This function is also used to drop the items in the log tree before
3279 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3280 * it is used to drop the loged items. So we shouldn't kill the delayed
3281 * items.
3282 */
3283 if (min_type == 0 && root == BTRFS_I(inode)->root)
3284 btrfs_kill_delayed_inode_items(inode);
3285
Li Zefan33345d012011-04-20 10:31:50 +08003286 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003287 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003288 key.type = (u8)-1;
3289
Chris Mason85e21ba2008-01-29 15:11:36 -05003290search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003291 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003292 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003293 if (ret < 0) {
3294 err = ret;
3295 goto out;
3296 }
Chris Masond3977122009-01-05 21:25:51 -05003297
Chris Mason85e21ba2008-01-29 15:11:36 -05003298 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003299 /* there are no items in the tree for us to truncate, we're
3300 * done
3301 */
Yan, Zheng80825102009-11-12 09:35:36 +00003302 if (path->slots[0] == 0)
3303 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003304 path->slots[0]--;
3305 }
3306
Chris Masond3977122009-01-05 21:25:51 -05003307 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003308 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003309 leaf = path->nodes[0];
3310 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3311 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003312
Li Zefan33345d012011-04-20 10:31:50 +08003313 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003314 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003315
Chris Mason85e21ba2008-01-29 15:11:36 -05003316 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003317 break;
3318
Chris Mason5f39d392007-10-15 16:14:19 -04003319 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003320 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003321 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003322 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003323 extent_type = btrfs_file_extent_type(leaf, fi);
3324 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003325 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003326 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003327 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003328 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003329 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003330 }
Yan008630c2007-11-07 13:31:09 -05003331 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003332 }
Yan, Zheng80825102009-11-12 09:35:36 +00003333 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003334 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003335 } else {
3336 if (item_end < new_size)
3337 break;
3338 if (found_key.offset >= new_size)
3339 del_item = 1;
3340 else
3341 del_item = 0;
3342 }
Chris Mason39279cc2007-06-12 06:35:45 -04003343 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003344 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003345 if (found_type != BTRFS_EXTENT_DATA_KEY)
3346 goto delete;
3347
3348 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003349 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003350 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003351 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003352 u64 orig_num_bytes =
3353 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003354 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003355 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003356 extent_num_bytes = extent_num_bytes &
3357 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003358 btrfs_set_file_extent_num_bytes(leaf, fi,
3359 extent_num_bytes);
3360 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003361 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003362 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003363 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003364 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003365 } else {
Chris Masondb945352007-10-15 16:15:53 -04003366 extent_num_bytes =
3367 btrfs_file_extent_disk_num_bytes(leaf,
3368 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003369 extent_offset = found_key.offset -
3370 btrfs_file_extent_offset(leaf, fi);
3371
Chris Mason39279cc2007-06-12 06:35:45 -04003372 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003373 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003374 if (extent_start != 0) {
3375 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003376 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003377 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003378 }
3379 }
Chris Mason90692182008-02-08 13:49:28 -05003380 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003381 /*
3382 * we can't truncate inline items that have had
3383 * special encodings
3384 */
3385 if (!del_item &&
3386 btrfs_file_extent_compression(leaf, fi) == 0 &&
3387 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3388 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003389 u32 size = new_size - found_key.offset;
3390
3391 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003392 inode_sub_bytes(inode, item_end + 1 -
3393 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003394 }
3395 size =
3396 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003397 btrfs_truncate_item(trans, root, path,
3398 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003399 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003400 inode_sub_bytes(inode, item_end + 1 -
3401 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003402 }
Chris Mason39279cc2007-06-12 06:35:45 -04003403 }
Chris Mason179e29e2007-11-01 11:28:41 -04003404delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003405 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003406 if (!pending_del_nr) {
3407 /* no pending yet, add ourselves */
3408 pending_del_slot = path->slots[0];
3409 pending_del_nr = 1;
3410 } else if (pending_del_nr &&
3411 path->slots[0] + 1 == pending_del_slot) {
3412 /* hop on the pending chunk */
3413 pending_del_nr++;
3414 pending_del_slot = path->slots[0];
3415 } else {
Chris Masond3977122009-01-05 21:25:51 -05003416 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003417 }
Chris Mason39279cc2007-06-12 06:35:45 -04003418 } else {
3419 break;
3420 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003421 if (found_extent && (root->ref_cows ||
3422 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003423 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003424 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003425 extent_num_bytes, 0,
3426 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003427 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003428 BUG_ON(ret);
3429 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003430
Yan, Zheng80825102009-11-12 09:35:36 +00003431 if (found_type == BTRFS_INODE_ITEM_KEY)
3432 break;
3433
3434 if (path->slots[0] == 0 ||
3435 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003436 if (pending_del_nr) {
3437 ret = btrfs_del_items(trans, root, path,
3438 pending_del_slot,
3439 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003440 if (ret) {
3441 btrfs_abort_transaction(trans,
3442 root, ret);
3443 goto error;
3444 }
Yan, Zheng80825102009-11-12 09:35:36 +00003445 pending_del_nr = 0;
3446 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003447 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003448 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003449 } else {
3450 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003451 }
Chris Mason39279cc2007-06-12 06:35:45 -04003452 }
Yan, Zheng80825102009-11-12 09:35:36 +00003453out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003454 if (pending_del_nr) {
3455 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3456 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003457 if (ret)
3458 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003459 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003460error:
Chris Mason39279cc2007-06-12 06:35:45 -04003461 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003462 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003463}
3464
Chris Masona52d9a82007-08-27 16:49:44 -04003465/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003466 * btrfs_truncate_page - read, zero a chunk and write a page
3467 * @inode - inode that we're zeroing
3468 * @from - the offset to start zeroing
3469 * @len - the length to zero, 0 to zero the entire range respective to the
3470 * offset
3471 * @front - zero up to the offset instead of from the offset on
3472 *
3473 * This will find the page for the "from" offset and cow the page and zero the
3474 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003475 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003476int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3477 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003478{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003479 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003480 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003481 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3482 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003483 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003484 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003485 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003486 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3487 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3488 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003489 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003490 int ret = 0;
3491 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003492 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003493
Josef Bacik2aaa6652012-08-29 14:27:18 -04003494 if ((offset & (blocksize - 1)) == 0 &&
3495 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003496 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003497 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003498 if (ret)
3499 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003500
3501 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003502again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003503 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003504 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003505 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003506 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003507 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003508
3509 page_start = page_offset(page);
3510 page_end = page_start + PAGE_CACHE_SIZE - 1;
3511
Chris Masona52d9a82007-08-27 16:49:44 -04003512 if (!PageUptodate(page)) {
3513 ret = btrfs_readpage(NULL, page);
3514 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003515 if (page->mapping != mapping) {
3516 unlock_page(page);
3517 page_cache_release(page);
3518 goto again;
3519 }
Chris Masona52d9a82007-08-27 16:49:44 -04003520 if (!PageUptodate(page)) {
3521 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003522 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003523 }
3524 }
Chris Mason211c17f2008-05-15 09:13:45 -04003525 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003526
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003527 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003528 set_page_extent_mapped(page);
3529
3530 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3531 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003532 unlock_extent_cached(io_tree, page_start, page_end,
3533 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003534 unlock_page(page);
3535 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003536 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003537 btrfs_put_ordered_extent(ordered);
3538 goto again;
3539 }
3540
Josef Bacik2ac55d42010-02-03 19:33:23 +00003541 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003542 EXTENT_DIRTY | EXTENT_DELALLOC |
3543 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003544 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003545
Josef Bacik2ac55d42010-02-03 19:33:23 +00003546 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3547 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003548 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003549 unlock_extent_cached(io_tree, page_start, page_end,
3550 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003551 goto out_unlock;
3552 }
3553
Chris Masone6dcd2d2008-07-17 12:53:50 -04003554 ret = 0;
3555 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003556 if (!len)
3557 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003558 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003559 if (front)
3560 memset(kaddr, 0, offset);
3561 else
3562 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003563 flush_dcache_page(page);
3564 kunmap(page);
3565 }
Chris Mason247e7432008-07-17 12:53:51 -04003566 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003567 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003568 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3569 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003570
Chris Mason89642222008-07-24 09:41:53 -04003571out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003572 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003573 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003574 unlock_page(page);
3575 page_cache_release(page);
3576out:
3577 return ret;
3578}
3579
Josef Bacik695a0d02011-03-04 15:46:53 -05003580/*
3581 * This function puts in dummy file extents for the area we're creating a hole
3582 * for. So if we are truncating this file to a larger size we need to insert
3583 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3584 * the range between oldsize and size
3585 */
Josef Bacika41ad392011-01-31 15:30:16 -05003586int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003587{
3588 struct btrfs_trans_handle *trans;
3589 struct btrfs_root *root = BTRFS_I(inode)->root;
3590 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003591 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003592 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003593 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003594 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003595 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003596 u64 block_end = (size + mask) & ~mask;
3597 u64 last_byte;
3598 u64 cur_offset;
3599 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003600 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003601
3602 if (size <= hole_start)
3603 return 0;
3604
Yan Zheng9036c102008-10-30 14:19:41 -04003605 while (1) {
3606 struct btrfs_ordered_extent *ordered;
3607 btrfs_wait_ordered_range(inode, hole_start,
3608 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003609 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003610 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003611 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3612 if (!ordered)
3613 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003614 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3615 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003616 btrfs_put_ordered_extent(ordered);
3617 }
3618
Yan Zheng9036c102008-10-30 14:19:41 -04003619 cur_offset = hole_start;
3620 while (1) {
3621 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3622 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003623 if (IS_ERR(em)) {
3624 err = PTR_ERR(em);
3625 break;
3626 }
Yan Zheng9036c102008-10-30 14:19:41 -04003627 last_byte = min(extent_map_end(em), block_end);
3628 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003629 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003630 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003631 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003632
Miao Xie36423202011-12-14 20:12:02 -05003633 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003634 if (IS_ERR(trans)) {
3635 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003636 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003637 }
Yan, Zheng80825102009-11-12 09:35:36 +00003638
Josef Bacik5dc562c2012-08-17 13:14:17 -04003639 err = btrfs_drop_extents(trans, root, inode,
3640 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003641 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003642 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003643 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003644 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003645 break;
Miao Xie5b397372011-09-11 10:52:24 -04003646 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003647
Yan Zheng9036c102008-10-30 14:19:41 -04003648 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003649 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003650 0, hole_size, 0, hole_size,
3651 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003652 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003653 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003654 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003655 break;
Miao Xie5b397372011-09-11 10:52:24 -04003656 }
Yan, Zheng80825102009-11-12 09:35:36 +00003657
Josef Bacik5dc562c2012-08-17 13:14:17 -04003658 btrfs_drop_extent_cache(inode, cur_offset,
3659 cur_offset + hole_size - 1, 0);
3660 hole_em = alloc_extent_map();
3661 if (!hole_em) {
3662 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3663 &BTRFS_I(inode)->runtime_flags);
3664 goto next;
3665 }
3666 hole_em->start = cur_offset;
3667 hole_em->len = hole_size;
3668 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003669
Josef Bacik5dc562c2012-08-17 13:14:17 -04003670 hole_em->block_start = EXTENT_MAP_HOLE;
3671 hole_em->block_len = 0;
3672 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3673 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3674 hole_em->generation = trans->transid;
3675
3676 while (1) {
3677 write_lock(&em_tree->lock);
3678 err = add_extent_mapping(em_tree, hole_em);
3679 if (!err)
3680 list_move(&hole_em->list,
3681 &em_tree->modified_extents);
3682 write_unlock(&em_tree->lock);
3683 if (err != -EEXIST)
3684 break;
3685 btrfs_drop_extent_cache(inode, cur_offset,
3686 cur_offset +
3687 hole_size - 1, 0);
3688 }
3689 free_extent_map(hole_em);
3690next:
Miao Xie36423202011-12-14 20:12:02 -05003691 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003692 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003693 }
3694 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003695 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003696 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003697 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003698 break;
3699 }
3700
Yan, Zhenga22285a2010-05-16 10:48:46 -04003701 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003702 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3703 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003704 return err;
3705}
3706
Josef Bacika41ad392011-01-31 15:30:16 -05003707static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003708{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003709 struct btrfs_root *root = BTRFS_I(inode)->root;
3710 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003711 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003712 int ret;
3713
Josef Bacika41ad392011-01-31 15:30:16 -05003714 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003715 return 0;
3716
Josef Bacika41ad392011-01-31 15:30:16 -05003717 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003718 truncate_pagecache(inode, oldsize, newsize);
3719 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003720 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003721 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003722
Miao Xief4a2f4c2011-12-14 20:12:01 -05003723 trans = btrfs_start_transaction(root, 1);
3724 if (IS_ERR(trans))
3725 return PTR_ERR(trans);
3726
3727 i_size_write(inode, newsize);
3728 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3729 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003730 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003731 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003732
Josef Bacika41ad392011-01-31 15:30:16 -05003733 /*
3734 * We're truncating a file that used to have good data down to
3735 * zero. Make sure it gets into the ordered flush list so that
3736 * any new writes get down to disk quickly.
3737 */
3738 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003739 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3740 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003741
Josef Bacika41ad392011-01-31 15:30:16 -05003742 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3743 truncate_setsize(inode, newsize);
3744 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003745 }
3746
Josef Bacika41ad392011-01-31 15:30:16 -05003747 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003748}
3749
Chris Mason39279cc2007-06-12 06:35:45 -04003750static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3751{
3752 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003753 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003754 int err;
3755
Li Zefanb83cc962010-12-20 16:04:08 +08003756 if (btrfs_root_readonly(root))
3757 return -EROFS;
3758
Chris Mason39279cc2007-06-12 06:35:45 -04003759 err = inode_change_ok(inode, attr);
3760 if (err)
3761 return err;
3762
Chris Mason5a3f23d2009-03-31 13:27:11 -04003763 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003764 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003765 if (err)
3766 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003767 }
Yan Zheng9036c102008-10-30 14:19:41 -04003768
Christoph Hellwig10257742010-06-04 11:30:02 +02003769 if (attr->ia_valid) {
3770 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003771 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003772 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003773
Josef Bacik22c44fe2011-11-30 10:45:38 -05003774 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003775 err = btrfs_acl_chmod(inode);
3776 }
3777
Chris Mason39279cc2007-06-12 06:35:45 -04003778 return err;
3779}
Chris Mason61295eb2008-01-14 16:24:38 -05003780
Al Virobd555972010-06-07 11:35:40 -04003781void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003782{
3783 struct btrfs_trans_handle *trans;
3784 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003785 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003786 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003787 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003788 int ret;
3789
liubo1abe9b82011-03-24 11:18:59 +00003790 trace_btrfs_inode_evict(inode);
3791
Chris Mason39279cc2007-06-12 06:35:45 -04003792 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003793 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003794 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003795 goto no_delete;
3796
Chris Mason39279cc2007-06-12 06:35:45 -04003797 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003798 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003799 goto no_delete;
3800 }
Al Virobd555972010-06-07 11:35:40 -04003801 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003802 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003803
Yan, Zhengc71bf092009-11-12 09:34:40 +00003804 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003805 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003806 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003807 goto no_delete;
3808 }
3809
Yan, Zheng76dda932009-09-21 16:00:26 -04003810 if (inode->i_nlink > 0) {
3811 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3812 goto no_delete;
3813 }
3814
Miao Xie66d8f3d2012-09-06 04:02:28 -06003815 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003816 if (!rsv) {
3817 btrfs_orphan_del(NULL, inode);
3818 goto no_delete;
3819 }
Josef Bacik4a338542011-08-29 11:01:31 -04003820 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003821 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003822 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003823
Chris Masondbe674a2008-07-17 12:54:05 -04003824 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003825
Josef Bacik4289a662011-08-05 13:22:24 -04003826 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003827 * This is a bit simpler than btrfs_truncate since we've already
3828 * reserved our space for our orphan item in the unlink, so we just
3829 * need to reserve some slack space in case we add bytes and update
3830 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003831 */
Yan, Zheng80825102009-11-12 09:35:36 +00003832 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00003833 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3834 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00003835
Josef Bacik726c35f2011-09-26 15:46:06 -04003836 /*
3837 * Try and steal from the global reserve since we will
3838 * likely not use this space anyway, we want to try as
3839 * hard as possible to get this to work.
3840 */
3841 if (ret)
3842 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3843
Yan, Zhengd68fc572010-05-16 10:49:58 -04003844 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003845 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003846 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003847 btrfs_orphan_del(NULL, inode);
3848 btrfs_free_block_rsv(root, rsv);
3849 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003850 }
3851
Miao Xie08e007d2012-10-16 11:33:38 +00003852 trans = btrfs_start_transaction_lflush(root, 1);
Josef Bacik4289a662011-08-05 13:22:24 -04003853 if (IS_ERR(trans)) {
3854 btrfs_orphan_del(NULL, inode);
3855 btrfs_free_block_rsv(root, rsv);
3856 goto no_delete;
3857 }
3858
3859 trans->block_rsv = rsv;
3860
Yan, Zhengd68fc572010-05-16 10:49:58 -04003861 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04003862 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00003863 break;
3864
Miao Xie8407aa42012-09-07 01:43:32 -06003865 trans->block_rsv = &root->fs_info->trans_block_rsv;
3866 ret = btrfs_update_inode(trans, root, inode);
3867 BUG_ON(ret);
3868
Yan, Zheng80825102009-11-12 09:35:36 +00003869 nr = trans->blocks_used;
3870 btrfs_end_transaction(trans, root);
3871 trans = NULL;
3872 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003873 }
3874
Josef Bacik4289a662011-08-05 13:22:24 -04003875 btrfs_free_block_rsv(root, rsv);
3876
Yan, Zheng80825102009-11-12 09:35:36 +00003877 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003878 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003879 ret = btrfs_orphan_del(trans, inode);
3880 BUG_ON(ret);
3881 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003882
Josef Bacik4289a662011-08-05 13:22:24 -04003883 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003884 if (!(root == root->fs_info->tree_root ||
3885 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003886 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003887
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003888 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003889 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003890 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003891no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02003892 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003893 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003894}
3895
3896/*
3897 * this returns the key found in the dir entry in the location pointer.
3898 * If no dir entries were found, location->objectid is 0.
3899 */
3900static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3901 struct btrfs_key *location)
3902{
3903 const char *name = dentry->d_name.name;
3904 int namelen = dentry->d_name.len;
3905 struct btrfs_dir_item *di;
3906 struct btrfs_path *path;
3907 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003908 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003909
3910 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003911 if (!path)
3912 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003913
Li Zefan33345d012011-04-20 10:31:50 +08003914 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003915 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003916 if (IS_ERR(di))
3917 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003918
David Sterbac7040052011-04-19 18:00:01 +02003919 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003920 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003921
Chris Mason5f39d392007-10-15 16:14:19 -04003922 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003923out:
Chris Mason39279cc2007-06-12 06:35:45 -04003924 btrfs_free_path(path);
3925 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003926out_err:
3927 location->objectid = 0;
3928 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003929}
3930
3931/*
3932 * when we hit a tree root in a directory, the btrfs part of the inode
3933 * needs to be changed to reflect the root directory of the tree root. This
3934 * is kind of like crossing a mount point.
3935 */
3936static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003937 struct inode *dir,
3938 struct dentry *dentry,
3939 struct btrfs_key *location,
3940 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003941{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003942 struct btrfs_path *path;
3943 struct btrfs_root *new_root;
3944 struct btrfs_root_ref *ref;
3945 struct extent_buffer *leaf;
3946 int ret;
3947 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003948
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003949 path = btrfs_alloc_path();
3950 if (!path) {
3951 err = -ENOMEM;
3952 goto out;
3953 }
Chris Mason39279cc2007-06-12 06:35:45 -04003954
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003955 err = -ENOENT;
3956 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3957 BTRFS_I(dir)->root->root_key.objectid,
3958 location->objectid);
3959 if (ret) {
3960 if (ret < 0)
3961 err = ret;
3962 goto out;
3963 }
Chris Mason39279cc2007-06-12 06:35:45 -04003964
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003965 leaf = path->nodes[0];
3966 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003967 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003968 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3969 goto out;
3970
3971 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3972 (unsigned long)(ref + 1),
3973 dentry->d_name.len);
3974 if (ret)
3975 goto out;
3976
David Sterbab3b4aa72011-04-21 01:20:15 +02003977 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003978
3979 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3980 if (IS_ERR(new_root)) {
3981 err = PTR_ERR(new_root);
3982 goto out;
3983 }
3984
3985 if (btrfs_root_refs(&new_root->root_item) == 0) {
3986 err = -ENOENT;
3987 goto out;
3988 }
3989
3990 *sub_root = new_root;
3991 location->objectid = btrfs_root_dirid(&new_root->root_item);
3992 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003993 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003994 err = 0;
3995out:
3996 btrfs_free_path(path);
3997 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003998}
3999
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004000static void inode_tree_add(struct inode *inode)
4001{
4002 struct btrfs_root *root = BTRFS_I(inode)->root;
4003 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004004 struct rb_node **p;
4005 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004006 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004007again:
4008 p = &root->inode_tree.rb_node;
4009 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004010
Al Viro1d3382cb2010-10-23 15:19:20 -04004011 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004012 return;
4013
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004014 spin_lock(&root->inode_lock);
4015 while (*p) {
4016 parent = *p;
4017 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4018
Li Zefan33345d012011-04-20 10:31:50 +08004019 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004020 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004021 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004022 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004023 else {
4024 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004025 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004026 rb_erase(parent, &root->inode_tree);
4027 RB_CLEAR_NODE(parent);
4028 spin_unlock(&root->inode_lock);
4029 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004030 }
4031 }
4032 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4033 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4034 spin_unlock(&root->inode_lock);
4035}
4036
4037static void inode_tree_del(struct inode *inode)
4038{
4039 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004040 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004041
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004042 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004043 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004044 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004045 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004046 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004047 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004048 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004049
Josef Bacik0af3d002010-06-21 14:48:16 -04004050 /*
4051 * Free space cache has inodes in the tree root, but the tree root has a
4052 * root_refs of 0, so this could end up dropping the tree root as a
4053 * snapshot, so we need the extra !root->fs_info->tree_root check to
4054 * make sure we don't drop it.
4055 */
4056 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4057 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004058 synchronize_srcu(&root->fs_info->subvol_srcu);
4059 spin_lock(&root->inode_lock);
4060 empty = RB_EMPTY_ROOT(&root->inode_tree);
4061 spin_unlock(&root->inode_lock);
4062 if (empty)
4063 btrfs_add_dead_root(root);
4064 }
4065}
4066
Jeff Mahoney143bede2012-03-01 14:56:26 +01004067void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004068{
4069 struct rb_node *node;
4070 struct rb_node *prev;
4071 struct btrfs_inode *entry;
4072 struct inode *inode;
4073 u64 objectid = 0;
4074
4075 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4076
4077 spin_lock(&root->inode_lock);
4078again:
4079 node = root->inode_tree.rb_node;
4080 prev = NULL;
4081 while (node) {
4082 prev = node;
4083 entry = rb_entry(node, struct btrfs_inode, rb_node);
4084
Li Zefan33345d012011-04-20 10:31:50 +08004085 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004086 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004087 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004088 node = node->rb_right;
4089 else
4090 break;
4091 }
4092 if (!node) {
4093 while (prev) {
4094 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004095 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004096 node = prev;
4097 break;
4098 }
4099 prev = rb_next(prev);
4100 }
4101 }
4102 while (node) {
4103 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004104 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004105 inode = igrab(&entry->vfs_inode);
4106 if (inode) {
4107 spin_unlock(&root->inode_lock);
4108 if (atomic_read(&inode->i_count) > 1)
4109 d_prune_aliases(inode);
4110 /*
Al Viro45321ac2010-06-07 13:43:19 -04004111 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004112 * the inode cache when its usage count
4113 * hits zero.
4114 */
4115 iput(inode);
4116 cond_resched();
4117 spin_lock(&root->inode_lock);
4118 goto again;
4119 }
4120
4121 if (cond_resched_lock(&root->inode_lock))
4122 goto again;
4123
4124 node = rb_next(node);
4125 }
4126 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004127}
4128
Chris Masone02119d2008-09-05 16:13:11 -04004129static int btrfs_init_locked_inode(struct inode *inode, void *p)
4130{
4131 struct btrfs_iget_args *args = p;
4132 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004133 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004134 return 0;
4135}
4136
4137static int btrfs_find_actor(struct inode *inode, void *opaque)
4138{
4139 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004140 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004141 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004142}
4143
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004144static struct inode *btrfs_iget_locked(struct super_block *s,
4145 u64 objectid,
4146 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004147{
4148 struct inode *inode;
4149 struct btrfs_iget_args args;
4150 args.ino = objectid;
4151 args.root = root;
4152
4153 inode = iget5_locked(s, objectid, btrfs_find_actor,
4154 btrfs_init_locked_inode,
4155 (void *)&args);
4156 return inode;
4157}
4158
Balaji Rao1a54ef82008-07-21 02:01:04 +05304159/* Get an inode object given its location and corresponding root.
4160 * Returns in *is_new if the inode was read from disk
4161 */
4162struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004163 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304164{
4165 struct inode *inode;
4166
4167 inode = btrfs_iget_locked(s, location->objectid, root);
4168 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004169 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304170
4171 if (inode->i_state & I_NEW) {
4172 BTRFS_I(inode)->root = root;
4173 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4174 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004175 if (!is_bad_inode(inode)) {
4176 inode_tree_add(inode);
4177 unlock_new_inode(inode);
4178 if (new)
4179 *new = 1;
4180 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004181 unlock_new_inode(inode);
4182 iput(inode);
4183 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004184 }
4185 }
4186
Balaji Rao1a54ef82008-07-21 02:01:04 +05304187 return inode;
4188}
4189
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004190static struct inode *new_simple_dir(struct super_block *s,
4191 struct btrfs_key *key,
4192 struct btrfs_root *root)
4193{
4194 struct inode *inode = new_inode(s);
4195
4196 if (!inode)
4197 return ERR_PTR(-ENOMEM);
4198
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004199 BTRFS_I(inode)->root = root;
4200 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004201 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004202
4203 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004204 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004205 inode->i_fop = &simple_dir_operations;
4206 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4207 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4208
4209 return inode;
4210}
4211
Chris Mason3de45862008-11-17 21:02:50 -05004212struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004213{
Chris Masond3977122009-01-05 21:25:51 -05004214 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004215 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004216 struct btrfs_root *sub_root = root;
4217 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004218 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004219 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004220
4221 if (dentry->d_name.len > BTRFS_NAME_LEN)
4222 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004223
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004224 if (unlikely(d_need_lookup(dentry))) {
4225 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4226 kfree(dentry->d_fsdata);
4227 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004228 /* This thing is hashed, drop it for now */
4229 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004230 } else {
4231 ret = btrfs_inode_by_name(dir, dentry, &location);
4232 }
Chris Mason5f39d392007-10-15 16:14:19 -04004233
Chris Mason39279cc2007-06-12 06:35:45 -04004234 if (ret < 0)
4235 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004236
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004237 if (location.objectid == 0)
4238 return NULL;
4239
4240 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004241 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004242 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004243 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004244
4245 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4246
Yan, Zheng76dda932009-09-21 16:00:26 -04004247 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004248 ret = fixup_tree_root_location(root, dir, dentry,
4249 &location, &sub_root);
4250 if (ret < 0) {
4251 if (ret != -ENOENT)
4252 inode = ERR_PTR(ret);
4253 else
4254 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4255 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004256 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004257 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004258 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4259
Julia Lawall34d19ba2011-01-24 19:55:19 +00004260 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004261 down_read(&root->fs_info->cleanup_work_sem);
4262 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004263 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004264 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004265 if (ret)
4266 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004267 }
4268
Chris Mason3de45862008-11-17 21:02:50 -05004269 return inode;
4270}
4271
Nick Pigginfe15ce42011-01-07 17:49:23 +11004272static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004273{
4274 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004275 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004276
Li Zefan848cce02012-02-21 17:04:28 +08004277 if (!inode && !IS_ROOT(dentry))
4278 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004279
Li Zefan848cce02012-02-21 17:04:28 +08004280 if (inode) {
4281 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004282 if (btrfs_root_refs(&root->root_item) == 0)
4283 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004284
4285 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4286 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004287 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004288 return 0;
4289}
4290
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004291static void btrfs_dentry_release(struct dentry *dentry)
4292{
4293 if (dentry->d_fsdata)
4294 kfree(dentry->d_fsdata);
4295}
4296
Chris Mason3de45862008-11-17 21:02:50 -05004297static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004298 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004299{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004300 struct dentry *ret;
4301
4302 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4303 if (unlikely(d_need_lookup(dentry))) {
4304 spin_lock(&dentry->d_lock);
4305 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4306 spin_unlock(&dentry->d_lock);
4307 }
4308 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004309}
4310
Miao Xie16cdcec2011-04-22 18:12:22 +08004311unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004312 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4313};
4314
David Woodhousecbdf5a22008-08-06 19:42:33 +01004315static int btrfs_real_readdir(struct file *filp, void *dirent,
4316 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004317{
Chris Mason6da6aba2007-12-18 16:15:09 -05004318 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004319 struct btrfs_root *root = BTRFS_I(inode)->root;
4320 struct btrfs_item *item;
4321 struct btrfs_dir_item *di;
4322 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004323 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004324 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004325 struct list_head ins_list;
4326 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004327 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004328 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004329 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004330 unsigned char d_type;
4331 int over = 0;
4332 u32 di_cur;
4333 u32 di_total;
4334 u32 di_len;
4335 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004336 char tmp_name[32];
4337 char *name_ptr;
4338 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004339 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004340
4341 /* FIXME, use a real flag for deciding about the key type */
4342 if (root->fs_info->tree_root == root)
4343 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004344
Chris Mason39544012007-12-12 14:38:19 -05004345 /* special case for "." */
4346 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004347 over = filldir(dirent, ".", 1,
4348 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004349 if (over)
4350 return 0;
4351 filp->f_pos = 1;
4352 }
Chris Mason39544012007-12-12 14:38:19 -05004353 /* special case for .., just use the back ref */
4354 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004355 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004356 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004357 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004358 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004359 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004360 filp->f_pos = 2;
4361 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004362 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004363 if (!path)
4364 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004365
Josef Bacik026fd312011-05-13 10:32:11 -04004366 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004367
Miao Xie16cdcec2011-04-22 18:12:22 +08004368 if (key_type == BTRFS_DIR_INDEX_KEY) {
4369 INIT_LIST_HEAD(&ins_list);
4370 INIT_LIST_HEAD(&del_list);
4371 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4372 }
4373
Chris Mason39279cc2007-06-12 06:35:45 -04004374 btrfs_set_key_type(&key, key_type);
4375 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004376 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004377
Chris Mason39279cc2007-06-12 06:35:45 -04004378 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4379 if (ret < 0)
4380 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004381
4382 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004383 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004384 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004385 if (slot >= btrfs_header_nritems(leaf)) {
4386 ret = btrfs_next_leaf(root, path);
4387 if (ret < 0)
4388 goto err;
4389 else if (ret > 0)
4390 break;
4391 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004392 }
Chris Mason3de45862008-11-17 21:02:50 -05004393
Chris Mason5f39d392007-10-15 16:14:19 -04004394 item = btrfs_item_nr(leaf, slot);
4395 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4396
4397 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004398 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004399 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004400 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004401 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004402 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004403 if (key_type == BTRFS_DIR_INDEX_KEY &&
4404 btrfs_should_delete_dir_index(&del_list,
4405 found_key.offset))
4406 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004407
4408 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004409 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004410
Chris Mason39279cc2007-06-12 06:35:45 -04004411 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4412 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004413 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004414
4415 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004416 struct btrfs_key location;
4417
Josef Bacik22a94d42011-03-16 16:47:17 -04004418 if (verify_dir_item(root, leaf, di))
4419 break;
4420
Chris Mason5f39d392007-10-15 16:14:19 -04004421 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004422 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004423 name_ptr = tmp_name;
4424 } else {
4425 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004426 if (!name_ptr) {
4427 ret = -ENOMEM;
4428 goto err;
4429 }
Chris Mason5f39d392007-10-15 16:14:19 -04004430 }
4431 read_extent_buffer(leaf, name_ptr,
4432 (unsigned long)(di + 1), name_len);
4433
4434 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4435 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004436
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004437
Chris Mason3de45862008-11-17 21:02:50 -05004438 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004439 * skip it.
4440 *
4441 * In contrast to old kernels, we insert the snapshot's
4442 * dir item and dir index after it has been created, so
4443 * we won't find a reference to our own snapshot. We
4444 * still keep the following code for backward
4445 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004446 */
4447 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4448 location.objectid == root->root_key.objectid) {
4449 over = 0;
4450 goto skip;
4451 }
Chris Mason5f39d392007-10-15 16:14:19 -04004452 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004453 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004454 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004455
Chris Mason3de45862008-11-17 21:02:50 -05004456skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004457 if (name_ptr != tmp_name)
4458 kfree(name_ptr);
4459
Chris Mason39279cc2007-06-12 06:35:45 -04004460 if (over)
4461 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004462 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004463 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004464 di_cur += di_len;
4465 di = (struct btrfs_dir_item *)((char *)di + di_len);
4466 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004467next:
4468 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004469 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004470
Miao Xie16cdcec2011-04-22 18:12:22 +08004471 if (key_type == BTRFS_DIR_INDEX_KEY) {
4472 if (is_curr)
4473 filp->f_pos++;
4474 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4475 &ins_list);
4476 if (ret)
4477 goto nopos;
4478 }
4479
David Woodhouse49593bf2008-08-17 17:08:36 +01004480 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004481 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004482 /*
4483 * 32-bit glibc will use getdents64, but then strtol -
4484 * so the last number we can serve is this.
4485 */
4486 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004487 else
4488 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004489nopos:
4490 ret = 0;
4491err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004492 if (key_type == BTRFS_DIR_INDEX_KEY)
4493 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004494 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004495 return ret;
4496}
4497
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004498int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004499{
4500 struct btrfs_root *root = BTRFS_I(inode)->root;
4501 struct btrfs_trans_handle *trans;
4502 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004503 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004504
Josef Bacik72ac3c02012-05-23 14:13:11 -04004505 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004506 return 0;
4507
Liu Bo83eea1f2012-07-10 05:28:39 -06004508 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004509 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004510
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004511 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004512 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004513 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004514 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004515 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004516 if (IS_ERR(trans))
4517 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004518 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004519 }
4520 return ret;
4521}
4522
4523/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004524 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004525 * inode changes. But, it is most likely to find the inode in cache.
4526 * FIXME, needs more benchmarking...there are no reasons other than performance
4527 * to keep or drop this code.
4528 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004529int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004530{
4531 struct btrfs_root *root = BTRFS_I(inode)->root;
4532 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004533 int ret;
4534
Josef Bacik72ac3c02012-05-23 14:13:11 -04004535 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004536 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004537
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004538 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004539 if (IS_ERR(trans))
4540 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004541
4542 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004543 if (ret && ret == -ENOSPC) {
4544 /* whoops, lets try again with the full transaction */
4545 btrfs_end_transaction(trans, root);
4546 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004547 if (IS_ERR(trans))
4548 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004549
Chris Mason94b60442010-05-26 11:02:00 -04004550 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004551 }
Chris Mason39279cc2007-06-12 06:35:45 -04004552 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004553 if (BTRFS_I(inode)->delayed_node)
4554 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004555
4556 return ret;
4557}
4558
4559/*
4560 * This is a copy of file_update_time. We need this so we can return error on
4561 * ENOSPC for updating the inode in the case of file write and mmap writes.
4562 */
Josef Bacike41f9412012-03-26 09:46:47 -04004563static int btrfs_update_time(struct inode *inode, struct timespec *now,
4564 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004565{
Alexander Block2bc5565282012-06-15 09:49:33 +02004566 struct btrfs_root *root = BTRFS_I(inode)->root;
4567
4568 if (btrfs_root_readonly(root))
4569 return -EROFS;
4570
Josef Bacike41f9412012-03-26 09:46:47 -04004571 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004572 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004573 if (flags & S_CTIME)
4574 inode->i_ctime = *now;
4575 if (flags & S_MTIME)
4576 inode->i_mtime = *now;
4577 if (flags & S_ATIME)
4578 inode->i_atime = *now;
4579 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004580}
4581
Chris Masond352ac62008-09-29 15:18:18 -04004582/*
4583 * find the highest existing sequence number in a directory
4584 * and then set the in-memory index_cnt variable to reflect
4585 * free sequence numbers
4586 */
Josef Bacikaec74772008-07-24 12:12:38 -04004587static int btrfs_set_inode_index_count(struct inode *inode)
4588{
4589 struct btrfs_root *root = BTRFS_I(inode)->root;
4590 struct btrfs_key key, found_key;
4591 struct btrfs_path *path;
4592 struct extent_buffer *leaf;
4593 int ret;
4594
Li Zefan33345d012011-04-20 10:31:50 +08004595 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004596 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4597 key.offset = (u64)-1;
4598
4599 path = btrfs_alloc_path();
4600 if (!path)
4601 return -ENOMEM;
4602
4603 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4604 if (ret < 0)
4605 goto out;
4606 /* FIXME: we should be able to handle this */
4607 if (ret == 0)
4608 goto out;
4609 ret = 0;
4610
4611 /*
4612 * MAGIC NUMBER EXPLANATION:
4613 * since we search a directory based on f_pos we have to start at 2
4614 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4615 * else has to start at 2
4616 */
4617 if (path->slots[0] == 0) {
4618 BTRFS_I(inode)->index_cnt = 2;
4619 goto out;
4620 }
4621
4622 path->slots[0]--;
4623
4624 leaf = path->nodes[0];
4625 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4626
Li Zefan33345d012011-04-20 10:31:50 +08004627 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004628 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4629 BTRFS_I(inode)->index_cnt = 2;
4630 goto out;
4631 }
4632
4633 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4634out:
4635 btrfs_free_path(path);
4636 return ret;
4637}
4638
Chris Masond352ac62008-09-29 15:18:18 -04004639/*
4640 * helper to find a free sequence number in a given directory. This current
4641 * code is very simple, later versions will do smarter things in the btree
4642 */
Chris Mason3de45862008-11-17 21:02:50 -05004643int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004644{
4645 int ret = 0;
4646
4647 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004648 ret = btrfs_inode_delayed_dir_index_count(dir);
4649 if (ret) {
4650 ret = btrfs_set_inode_index_count(dir);
4651 if (ret)
4652 return ret;
4653 }
Josef Bacikaec74772008-07-24 12:12:38 -04004654 }
4655
Chris Mason00e4e6b2008-08-05 11:18:09 -04004656 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004657 BTRFS_I(dir)->index_cnt++;
4658
4659 return ret;
4660}
4661
Chris Mason39279cc2007-06-12 06:35:45 -04004662static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4663 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004664 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004665 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004666 u64 ref_objectid, u64 objectid,
4667 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004668{
4669 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004670 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004671 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004672 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004673 struct btrfs_inode_ref *ref;
4674 struct btrfs_key key[2];
4675 u32 sizes[2];
4676 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004677 int ret;
4678 int owner;
4679
Chris Mason5f39d392007-10-15 16:14:19 -04004680 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004681 if (!path)
4682 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004683
Chris Mason39279cc2007-06-12 06:35:45 -04004684 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004685 if (!inode) {
4686 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004687 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004688 }
Chris Mason39279cc2007-06-12 06:35:45 -04004689
Li Zefan581bb052011-04-20 10:06:11 +08004690 /*
4691 * we have to initialize this early, so we can reclaim the inode
4692 * number if we fail afterwards in this function.
4693 */
4694 inode->i_ino = objectid;
4695
Josef Bacikaec74772008-07-24 12:12:38 -04004696 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004697 trace_btrfs_inode_request(dir);
4698
Chris Mason3de45862008-11-17 21:02:50 -05004699 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004700 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004701 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004702 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004703 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004704 }
Josef Bacikaec74772008-07-24 12:12:38 -04004705 }
4706 /*
4707 * index_cnt is ignored for everything but a dir,
4708 * btrfs_get_inode_index_count has an explanation for the magic
4709 * number
4710 */
4711 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004712 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004713 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004714 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04004715
Josef Bacik5dc562c2012-08-17 13:14:17 -04004716 /*
4717 * We could have gotten an inode number from somebody who was fsynced
4718 * and then removed in this same transaction, so let's just set full
4719 * sync since it will be a full sync anyway and this will blow away the
4720 * old info in the log.
4721 */
4722 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4723
Al Viro569254b2011-07-24 17:08:40 -04004724 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004725 owner = 0;
4726 else
4727 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004728
4729 key[0].objectid = objectid;
4730 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4731 key[0].offset = 0;
4732
Mark Fashehf1863732012-08-08 11:32:27 -07004733 /*
4734 * Start new inodes with an inode_ref. This is slightly more
4735 * efficient for small numbers of hard links since they will
4736 * be packed into one item. Extended refs will kick in if we
4737 * add more hard links than can fit in the ref item.
4738 */
Chris Mason9c583092008-01-29 15:15:18 -05004739 key[1].objectid = objectid;
4740 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4741 key[1].offset = ref_objectid;
4742
4743 sizes[0] = sizeof(struct btrfs_inode_item);
4744 sizes[1] = name_len + sizeof(*ref);
4745
Chris Masonb9473432009-03-13 11:00:37 -04004746 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004747 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4748 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004749 goto fail;
4750
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004751 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004752 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004753 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004754 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4755 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004756 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4757 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004758 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004759
4760 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4761 struct btrfs_inode_ref);
4762 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004763 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004764 ptr = (unsigned long)(ref + 1);
4765 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4766
Chris Mason5f39d392007-10-15 16:14:19 -04004767 btrfs_mark_buffer_dirty(path->nodes[0]);
4768 btrfs_free_path(path);
4769
Chris Mason39279cc2007-06-12 06:35:45 -04004770 location = &BTRFS_I(inode)->location;
4771 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004772 location->offset = 0;
4773 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4774
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004775 btrfs_inherit_iflags(inode, dir);
4776
Al Viro569254b2011-07-24 17:08:40 -04004777 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004778 if (btrfs_test_opt(root, NODATASUM))
4779 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004780 if (btrfs_test_opt(root, NODATACOW) ||
4781 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004782 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4783 }
4784
Chris Mason39279cc2007-06-12 06:35:45 -04004785 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004786 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004787
4788 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004789 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004790
Alexander Block8ea05e32012-07-25 17:35:53 +02004791 btrfs_update_root_times(trans, root);
4792
Chris Mason39279cc2007-06-12 06:35:45 -04004793 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004794fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004795 if (dir)
4796 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004797 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004798 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004799 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004800}
4801
4802static inline u8 btrfs_inode_type(struct inode *inode)
4803{
4804 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4805}
4806
Chris Masond352ac62008-09-29 15:18:18 -04004807/*
4808 * utility function to add 'inode' into 'parent_inode' with
4809 * a give name and a given sequence number.
4810 * if 'add_backref' is true, also insert a backref from the
4811 * inode to the parent directory.
4812 */
Chris Masone02119d2008-09-05 16:13:11 -04004813int btrfs_add_link(struct btrfs_trans_handle *trans,
4814 struct inode *parent_inode, struct inode *inode,
4815 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004816{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004817 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004818 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004819 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004820 u64 ino = btrfs_ino(inode);
4821 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004822
Li Zefan33345d012011-04-20 10:31:50 +08004823 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004824 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4825 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004826 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004827 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4828 key.offset = 0;
4829 }
Chris Mason39279cc2007-06-12 06:35:45 -04004830
Li Zefan33345d012011-04-20 10:31:50 +08004831 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004832 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4833 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004834 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004835 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004836 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4837 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004838 }
4839
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004840 /* Nothing to clean up yet */
4841 if (ret)
4842 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004843
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004844 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4845 parent_inode, &key,
4846 btrfs_inode_type(inode), index);
4847 if (ret == -EEXIST)
4848 goto fail_dir_item;
4849 else if (ret) {
4850 btrfs_abort_transaction(trans, root, ret);
4851 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004852 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004853
4854 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4855 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004856 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004857 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4858 ret = btrfs_update_inode(trans, root, parent_inode);
4859 if (ret)
4860 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004861 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004862
4863fail_dir_item:
4864 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4865 u64 local_index;
4866 int err;
4867 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4868 key.objectid, root->root_key.objectid,
4869 parent_ino, &local_index, name, name_len);
4870
4871 } else if (add_backref) {
4872 u64 local_index;
4873 int err;
4874
4875 err = btrfs_del_inode_ref(trans, root, name, name_len,
4876 ino, parent_ino, &local_index);
4877 }
4878 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004879}
4880
4881static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004882 struct inode *dir, struct dentry *dentry,
4883 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004884{
Josef Bacika1b075d2010-11-19 20:36:11 +00004885 int err = btrfs_add_link(trans, dir, inode,
4886 dentry->d_name.name, dentry->d_name.len,
4887 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004888 if (err > 0)
4889 err = -EEXIST;
4890 return err;
4891}
4892
Josef Bacik618e21d2007-07-11 10:18:17 -04004893static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004894 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004895{
4896 struct btrfs_trans_handle *trans;
4897 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004898 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004899 int err;
4900 int drop_inode = 0;
4901 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004902 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004903 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004904
4905 if (!new_valid_dev(rdev))
4906 return -EINVAL;
4907
Josef Bacik9ed74f22009-09-11 16:12:44 -04004908 /*
4909 * 2 for inode item and ref
4910 * 2 for dir items
4911 * 1 for xattr if selinux is on
4912 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004913 trans = btrfs_start_transaction(root, 5);
4914 if (IS_ERR(trans))
4915 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004916
Li Zefan581bb052011-04-20 10:06:11 +08004917 err = btrfs_find_free_ino(root, &objectid);
4918 if (err)
4919 goto out_unlock;
4920
Josef Bacikaec74772008-07-24 12:12:38 -04004921 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004922 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04004923 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004924 if (IS_ERR(inode)) {
4925 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004926 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004927 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004928
Eric Paris2a7dba32011-02-01 11:05:39 -05004929 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004930 if (err) {
4931 drop_inode = 1;
4932 goto out_unlock;
4933 }
4934
Casey Schauflerad19db72011-12-15 10:09:07 -05004935 /*
4936 * If the active LSM wants to access the inode during
4937 * d_instantiate it needs these. Smack checks to see
4938 * if the filesystem supports xattrs by looking at the
4939 * ops vector.
4940 */
4941
4942 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004943 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004944 if (err)
4945 drop_inode = 1;
4946 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004947 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004948 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05004949 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004950 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004951out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004952 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004953 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004954 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004955 if (drop_inode) {
4956 inode_dec_link_count(inode);
4957 iput(inode);
4958 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004959 return err;
4960}
4961
Chris Mason39279cc2007-06-12 06:35:45 -04004962static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04004963 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04004964{
4965 struct btrfs_trans_handle *trans;
4966 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004967 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004968 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004969 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004970 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004971 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004972 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004973
Josef Bacik9ed74f22009-09-11 16:12:44 -04004974 /*
4975 * 2 for inode item and ref
4976 * 2 for dir items
4977 * 1 for xattr if selinux is on
4978 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004979 trans = btrfs_start_transaction(root, 5);
4980 if (IS_ERR(trans))
4981 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004982
Li Zefan581bb052011-04-20 10:06:11 +08004983 err = btrfs_find_free_ino(root, &objectid);
4984 if (err)
4985 goto out_unlock;
4986
Josef Bacikaec74772008-07-24 12:12:38 -04004987 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004988 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04004989 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004990 if (IS_ERR(inode)) {
4991 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004992 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004993 }
Chris Mason39279cc2007-06-12 06:35:45 -04004994
Eric Paris2a7dba32011-02-01 11:05:39 -05004995 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004996 if (err) {
4997 drop_inode = 1;
4998 goto out_unlock;
4999 }
5000
Casey Schauflerad19db72011-12-15 10:09:07 -05005001 /*
5002 * If the active LSM wants to access the inode during
5003 * d_instantiate it needs these. Smack checks to see
5004 * if the filesystem supports xattrs by looking at the
5005 * ops vector.
5006 */
5007 inode->i_fop = &btrfs_file_operations;
5008 inode->i_op = &btrfs_file_inode_operations;
5009
Josef Bacika1b075d2010-11-19 20:36:11 +00005010 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005011 if (err)
5012 drop_inode = 1;
5013 else {
5014 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005015 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05005016 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro08c422c2011-12-23 07:58:13 -05005017 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005018 }
Chris Mason39279cc2007-06-12 06:35:45 -04005019out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005020 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005021 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005022 if (drop_inode) {
5023 inode_dec_link_count(inode);
5024 iput(inode);
5025 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005026 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005027 return err;
5028}
5029
5030static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5031 struct dentry *dentry)
5032{
5033 struct btrfs_trans_handle *trans;
5034 struct btrfs_root *root = BTRFS_I(dir)->root;
5035 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005036 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05005037 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005038 int err;
5039 int drop_inode = 0;
5040
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005041 /* do not allow sys_link's with other subvols of the same device */
5042 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005043 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005044
Mark Fashehf1863732012-08-08 11:32:27 -07005045 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005046 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005047
Chris Mason3de45862008-11-17 21:02:50 -05005048 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005049 if (err)
5050 goto fail;
5051
Yan, Zhenga22285a2010-05-16 10:48:46 -04005052 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005053 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005054 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005055 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005056 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005057 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005058 if (IS_ERR(trans)) {
5059 err = PTR_ERR(trans);
5060 goto fail;
5061 }
Chris Mason5f39d392007-10-15 16:14:19 -04005062
Miao Xie31534952011-04-13 13:19:21 +08005063 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005064 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005065 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005066 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005067
Josef Bacika1b075d2010-11-19 20:36:11 +00005068 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005069
Yan, Zhenga5719522009-09-24 09:17:31 -04005070 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005071 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005072 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005073 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005074 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005075 if (err)
5076 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005077 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005078 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005079 }
Chris Mason39279cc2007-06-12 06:35:45 -04005080
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005081 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005082 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005083fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005084 if (drop_inode) {
5085 inode_dec_link_count(inode);
5086 iput(inode);
5087 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005088 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005089 return err;
5090}
5091
Al Viro18bb1db2011-07-26 01:41:39 -04005092static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005093{
Chris Masonb9d86662008-05-02 16:13:49 -04005094 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005095 struct btrfs_trans_handle *trans;
5096 struct btrfs_root *root = BTRFS_I(dir)->root;
5097 int err = 0;
5098 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005099 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005100 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005101 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005102
Josef Bacik9ed74f22009-09-11 16:12:44 -04005103 /*
5104 * 2 items for inode and ref
5105 * 2 items for dir items
5106 * 1 for xattr if selinux is on
5107 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005108 trans = btrfs_start_transaction(root, 5);
5109 if (IS_ERR(trans))
5110 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005111
Li Zefan581bb052011-04-20 10:06:11 +08005112 err = btrfs_find_free_ino(root, &objectid);
5113 if (err)
5114 goto out_fail;
5115
Josef Bacikaec74772008-07-24 12:12:38 -04005116 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005117 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005118 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005119 if (IS_ERR(inode)) {
5120 err = PTR_ERR(inode);
5121 goto out_fail;
5122 }
Chris Mason5f39d392007-10-15 16:14:19 -04005123
Chris Mason39279cc2007-06-12 06:35:45 -04005124 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005125
Eric Paris2a7dba32011-02-01 11:05:39 -05005126 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005127 if (err)
5128 goto out_fail;
5129
Chris Mason39279cc2007-06-12 06:35:45 -04005130 inode->i_op = &btrfs_dir_inode_operations;
5131 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005132
Chris Masondbe674a2008-07-17 12:54:05 -04005133 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005134 err = btrfs_update_inode(trans, root, inode);
5135 if (err)
5136 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005137
Josef Bacika1b075d2010-11-19 20:36:11 +00005138 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5139 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005140 if (err)
5141 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005142
Chris Mason39279cc2007-06-12 06:35:45 -04005143 d_instantiate(dentry, inode);
5144 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005145
5146out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005147 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005148 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005149 if (drop_on_err)
5150 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005151 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005152 return err;
5153}
5154
Chris Masond352ac62008-09-29 15:18:18 -04005155/* helper for btfs_get_extent. Given an existing extent in the tree,
5156 * and an extent that you want to insert, deal with overlap and insert
5157 * the new extent into the tree.
5158 */
Chris Mason3b951512008-04-17 11:29:12 -04005159static int merge_extent_mapping(struct extent_map_tree *em_tree,
5160 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005161 struct extent_map *em,
5162 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005163{
5164 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005165
Chris Masone6dcd2d2008-07-17 12:53:50 -04005166 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5167 start_diff = map_start - em->start;
5168 em->start = map_start;
5169 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005170 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5171 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005172 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005173 em->block_len -= start_diff;
5174 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005175 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005176}
5177
Chris Masonc8b97812008-10-29 14:49:59 -04005178static noinline int uncompress_inline(struct btrfs_path *path,
5179 struct inode *inode, struct page *page,
5180 size_t pg_offset, u64 extent_offset,
5181 struct btrfs_file_extent_item *item)
5182{
5183 int ret;
5184 struct extent_buffer *leaf = path->nodes[0];
5185 char *tmp;
5186 size_t max_size;
5187 unsigned long inline_size;
5188 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005189 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005190
5191 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005192 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005193 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5194 inline_size = btrfs_file_extent_inline_item_len(leaf,
5195 btrfs_item_nr(leaf, path->slots[0]));
5196 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005197 if (!tmp)
5198 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005199 ptr = btrfs_file_extent_inline_start(item);
5200
5201 read_extent_buffer(leaf, tmp, ptr, inline_size);
5202
Chris Mason5b050f02008-11-11 09:34:41 -05005203 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005204 ret = btrfs_decompress(compress_type, tmp, page,
5205 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005206 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005207 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005208 unsigned long copy_size = min_t(u64,
5209 PAGE_CACHE_SIZE - pg_offset,
5210 max_size - extent_offset);
5211 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005212 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005213 }
5214 kfree(tmp);
5215 return 0;
5216}
5217
Chris Masond352ac62008-09-29 15:18:18 -04005218/*
5219 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005220 * the ugly parts come from merging extents from the disk with the in-ram
5221 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005222 * where the in-ram extents might be locked pending data=ordered completion.
5223 *
5224 * This also copies inline extents directly into the page.
5225 */
Chris Masond3977122009-01-05 21:25:51 -05005226
Chris Masona52d9a82007-08-27 16:49:44 -04005227struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005228 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005229 int create)
5230{
5231 int ret;
5232 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005233 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005234 u64 extent_start = 0;
5235 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005236 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005237 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005238 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005239 struct btrfs_root *root = BTRFS_I(inode)->root;
5240 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005241 struct extent_buffer *leaf;
5242 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005243 struct extent_map *em = NULL;
5244 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005245 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005246 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005247 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005248
Chris Masona52d9a82007-08-27 16:49:44 -04005249again:
Chris Mason890871b2009-09-02 16:24:52 -04005250 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005251 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005252 if (em)
5253 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005254 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005255
Chris Masona52d9a82007-08-27 16:49:44 -04005256 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005257 if (em->start > start || em->start + em->len <= start)
5258 free_extent_map(em);
5259 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005260 free_extent_map(em);
5261 else
5262 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005263 }
David Sterba172ddd62011-04-21 00:48:27 +02005264 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005265 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005266 err = -ENOMEM;
5267 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005268 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005269 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005270 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005271 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005272 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005273 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005274
5275 if (!path) {
5276 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005277 if (!path) {
5278 err = -ENOMEM;
5279 goto out;
5280 }
5281 /*
5282 * Chances are we'll be called again, so go ahead and do
5283 * readahead
5284 */
5285 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005286 }
5287
Chris Mason179e29e2007-11-01 11:28:41 -04005288 ret = btrfs_lookup_file_extent(trans, root, path,
5289 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005290 if (ret < 0) {
5291 err = ret;
5292 goto out;
5293 }
5294
5295 if (ret != 0) {
5296 if (path->slots[0] == 0)
5297 goto not_found;
5298 path->slots[0]--;
5299 }
5300
Chris Mason5f39d392007-10-15 16:14:19 -04005301 leaf = path->nodes[0];
5302 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005303 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005304 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005305 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5306 found_type = btrfs_key_type(&found_key);
5307 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005308 found_type != BTRFS_EXTENT_DATA_KEY) {
5309 goto not_found;
5310 }
5311
Chris Mason5f39d392007-10-15 16:14:19 -04005312 found_type = btrfs_file_extent_type(leaf, item);
5313 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005314 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005315 if (found_type == BTRFS_FILE_EXTENT_REG ||
5316 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005317 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005318 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005319 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5320 size_t size;
5321 size = btrfs_file_extent_inline_len(leaf, item);
5322 extent_end = (extent_start + size + root->sectorsize - 1) &
5323 ~((u64)root->sectorsize - 1);
5324 }
5325
5326 if (start >= extent_end) {
5327 path->slots[0]++;
5328 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5329 ret = btrfs_next_leaf(root, path);
5330 if (ret < 0) {
5331 err = ret;
5332 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005333 }
Yan Zheng9036c102008-10-30 14:19:41 -04005334 if (ret > 0)
5335 goto not_found;
5336 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005337 }
Yan Zheng9036c102008-10-30 14:19:41 -04005338 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5339 if (found_key.objectid != objectid ||
5340 found_key.type != BTRFS_EXTENT_DATA_KEY)
5341 goto not_found;
5342 if (start + len <= found_key.offset)
5343 goto not_found;
5344 em->start = start;
5345 em->len = found_key.offset - start;
5346 goto not_found_em;
5347 }
5348
Yan Zhengd899e052008-10-30 14:25:28 -04005349 if (found_type == BTRFS_FILE_EXTENT_REG ||
5350 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005351 em->start = extent_start;
5352 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005353 em->orig_start = extent_start -
5354 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005355 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5356 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005357 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005358 goto insert;
5359 }
Li Zefan261507a02010-12-17 14:21:50 +08005360 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005361 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005362 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005363 em->block_start = bytenr;
5364 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5365 item);
5366 } else {
5367 bytenr += btrfs_file_extent_offset(leaf, item);
5368 em->block_start = bytenr;
5369 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005370 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5371 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005372 }
Chris Masona52d9a82007-08-27 16:49:44 -04005373 goto insert;
5374 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005375 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005376 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005377 size_t size;
5378 size_t extent_offset;
5379 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005380
Chris Masona52d9a82007-08-27 16:49:44 -04005381 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005382 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005383 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005384 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005385 goto out;
5386 }
5387
Yan Zheng9036c102008-10-30 14:19:41 -04005388 size = btrfs_file_extent_inline_len(leaf, item);
5389 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005390 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005391 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005392 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005393 em->len = (copy_size + root->sectorsize - 1) &
5394 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005395 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005396 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005397 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005398 em->compress_type = compress_type;
5399 }
Yan689f9342007-10-29 11:41:07 -04005400 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005401 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005402 if (btrfs_file_extent_compression(leaf, item) !=
5403 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005404 ret = uncompress_inline(path, inode, page,
5405 pg_offset,
5406 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005407 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005408 } else {
5409 map = kmap(page);
5410 read_extent_buffer(leaf, map + pg_offset, ptr,
5411 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005412 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5413 memset(map + pg_offset + copy_size, 0,
5414 PAGE_CACHE_SIZE - pg_offset -
5415 copy_size);
5416 }
Chris Masonc8b97812008-10-29 14:49:59 -04005417 kunmap(page);
5418 }
Chris Mason179e29e2007-11-01 11:28:41 -04005419 flush_dcache_page(page);
5420 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005421 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005422 if (!trans) {
5423 kunmap(page);
5424 free_extent_map(em);
5425 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005426
David Sterbab3b4aa72011-04-21 01:20:15 +02005427 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005428 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005429
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005430 if (IS_ERR(trans))
5431 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005432 goto again;
5433 }
Chris Masonc8b97812008-10-29 14:49:59 -04005434 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005435 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005436 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005437 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005438 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005439 }
Chris Masond1310b22008-01-24 16:13:08 -05005440 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005441 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005442 goto insert;
5443 } else {
Chris Masond3977122009-01-05 21:25:51 -05005444 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005445 WARN_ON(1);
5446 }
5447not_found:
5448 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005449 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005450not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005451 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005452 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005453insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005454 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005455 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005456 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5457 "[%llu %llu]\n", (unsigned long long)em->start,
5458 (unsigned long long)em->len,
5459 (unsigned long long)start,
5460 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005461 err = -EIO;
5462 goto out;
5463 }
Chris Masond1310b22008-01-24 16:13:08 -05005464
5465 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005466 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005467 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005468 /* it is possible that someone inserted the extent into the tree
5469 * while we had the lock dropped. It is also possible that
5470 * an overlapping map exists in the tree
5471 */
Chris Masona52d9a82007-08-27 16:49:44 -04005472 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005473 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005474
5475 ret = 0;
5476
Chris Mason3b951512008-04-17 11:29:12 -04005477 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005478 if (existing && (existing->start > start ||
5479 existing->start + existing->len <= start)) {
5480 free_extent_map(existing);
5481 existing = NULL;
5482 }
Chris Mason3b951512008-04-17 11:29:12 -04005483 if (!existing) {
5484 existing = lookup_extent_mapping(em_tree, em->start,
5485 em->len);
5486 if (existing) {
5487 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005488 em, start,
5489 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005490 free_extent_map(existing);
5491 if (err) {
5492 free_extent_map(em);
5493 em = NULL;
5494 }
5495 } else {
5496 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005497 free_extent_map(em);
5498 em = NULL;
5499 }
5500 } else {
5501 free_extent_map(em);
5502 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005503 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005504 }
Chris Masona52d9a82007-08-27 16:49:44 -04005505 }
Chris Mason890871b2009-09-02 16:24:52 -04005506 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005507out:
liubo1abe9b82011-03-24 11:18:59 +00005508
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005509 if (em)
5510 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005511
Chris Masonf4219502008-07-22 11:18:09 -04005512 if (path)
5513 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005514 if (trans) {
5515 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005516 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005517 err = ret;
5518 }
Chris Masona52d9a82007-08-27 16:49:44 -04005519 if (err) {
5520 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005521 return ERR_PTR(err);
5522 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005523 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005524 return em;
5525}
5526
Chris Masonec29ed52011-02-23 16:23:20 -05005527struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5528 size_t pg_offset, u64 start, u64 len,
5529 int create)
5530{
5531 struct extent_map *em;
5532 struct extent_map *hole_em = NULL;
5533 u64 range_start = start;
5534 u64 end;
5535 u64 found;
5536 u64 found_end;
5537 int err = 0;
5538
5539 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5540 if (IS_ERR(em))
5541 return em;
5542 if (em) {
5543 /*
5544 * if our em maps to a hole, there might
5545 * actually be delalloc bytes behind it
5546 */
5547 if (em->block_start != EXTENT_MAP_HOLE)
5548 return em;
5549 else
5550 hole_em = em;
5551 }
5552
5553 /* check to see if we've wrapped (len == -1 or similar) */
5554 end = start + len;
5555 if (end < start)
5556 end = (u64)-1;
5557 else
5558 end -= 1;
5559
5560 em = NULL;
5561
5562 /* ok, we didn't find anything, lets look for delalloc */
5563 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5564 end, len, EXTENT_DELALLOC, 1);
5565 found_end = range_start + found;
5566 if (found_end < range_start)
5567 found_end = (u64)-1;
5568
5569 /*
5570 * we didn't find anything useful, return
5571 * the original results from get_extent()
5572 */
5573 if (range_start > end || found_end <= start) {
5574 em = hole_em;
5575 hole_em = NULL;
5576 goto out;
5577 }
5578
5579 /* adjust the range_start to make sure it doesn't
5580 * go backwards from the start they passed in
5581 */
5582 range_start = max(start,range_start);
5583 found = found_end - range_start;
5584
5585 if (found > 0) {
5586 u64 hole_start = start;
5587 u64 hole_len = len;
5588
David Sterba172ddd62011-04-21 00:48:27 +02005589 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005590 if (!em) {
5591 err = -ENOMEM;
5592 goto out;
5593 }
5594 /*
5595 * when btrfs_get_extent can't find anything it
5596 * returns one huge hole
5597 *
5598 * make sure what it found really fits our range, and
5599 * adjust to make sure it is based on the start from
5600 * the caller
5601 */
5602 if (hole_em) {
5603 u64 calc_end = extent_map_end(hole_em);
5604
5605 if (calc_end <= start || (hole_em->start > end)) {
5606 free_extent_map(hole_em);
5607 hole_em = NULL;
5608 } else {
5609 hole_start = max(hole_em->start, start);
5610 hole_len = calc_end - hole_start;
5611 }
5612 }
5613 em->bdev = NULL;
5614 if (hole_em && range_start > hole_start) {
5615 /* our hole starts before our delalloc, so we
5616 * have to return just the parts of the hole
5617 * that go until the delalloc starts
5618 */
5619 em->len = min(hole_len,
5620 range_start - hole_start);
5621 em->start = hole_start;
5622 em->orig_start = hole_start;
5623 /*
5624 * don't adjust block start at all,
5625 * it is fixed at EXTENT_MAP_HOLE
5626 */
5627 em->block_start = hole_em->block_start;
5628 em->block_len = hole_len;
5629 } else {
5630 em->start = range_start;
5631 em->len = found;
5632 em->orig_start = range_start;
5633 em->block_start = EXTENT_MAP_DELALLOC;
5634 em->block_len = found;
5635 }
5636 } else if (hole_em) {
5637 return hole_em;
5638 }
5639out:
5640
5641 free_extent_map(hole_em);
5642 if (err) {
5643 free_extent_map(em);
5644 return ERR_PTR(err);
5645 }
5646 return em;
5647}
5648
Josef Bacik4b46fce2010-05-23 11:00:55 -04005649static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005650 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005651 u64 start, u64 len)
5652{
5653 struct btrfs_root *root = BTRFS_I(inode)->root;
5654 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005655 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5656 struct btrfs_key ins;
5657 u64 alloc_hint;
5658 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005659 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005660
Josef Bacik16d299a2011-04-06 14:53:07 -04005661 /*
5662 * Ok if the extent map we looked up is a hole and is for the exact
5663 * range we want, there is no reason to allocate a new one, however if
5664 * it is not right then we need to free this one and drop the cache for
5665 * our range.
5666 */
5667 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5668 em->len != len) {
5669 free_extent_map(em);
5670 em = NULL;
5671 insert = true;
5672 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5673 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005674
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005675 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005676 if (IS_ERR(trans))
5677 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005678
Chris Mason4cb53002011-05-24 15:35:30 -04005679 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5680 btrfs_add_inode_defrag(trans, inode);
5681
Josef Bacik4b46fce2010-05-23 11:00:55 -04005682 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5683
5684 alloc_hint = get_extent_allocation_hint(inode, start, len);
5685 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005686 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005687 if (ret) {
5688 em = ERR_PTR(ret);
5689 goto out;
5690 }
5691
Josef Bacik4b46fce2010-05-23 11:00:55 -04005692 if (!em) {
David Sterba172ddd62011-04-21 00:48:27 +02005693 em = alloc_extent_map();
Josef Bacik16d299a2011-04-06 14:53:07 -04005694 if (!em) {
5695 em = ERR_PTR(-ENOMEM);
5696 goto out;
5697 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005698 }
5699
5700 em->start = start;
5701 em->orig_start = em->start;
5702 em->len = ins.offset;
5703
5704 em->block_start = ins.objectid;
5705 em->block_len = ins.offset;
5706 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005707
5708 /*
5709 * We need to do this because if we're using the original em we searched
5710 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5711 */
5712 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005713 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5714
Josef Bacik16d299a2011-04-06 14:53:07 -04005715 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005716 write_lock(&em_tree->lock);
5717 ret = add_extent_mapping(em_tree, em);
5718 write_unlock(&em_tree->lock);
5719 if (ret != -EEXIST)
5720 break;
5721 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5722 }
5723
5724 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5725 ins.offset, ins.offset, 0);
5726 if (ret) {
5727 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5728 em = ERR_PTR(ret);
5729 }
5730out:
5731 btrfs_end_transaction(trans, root);
5732 return em;
5733}
5734
Chris Mason46bfbb52010-05-26 11:04:10 -04005735/*
5736 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5737 * block must be cow'd
5738 */
5739static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5740 struct inode *inode, u64 offset, u64 len)
5741{
5742 struct btrfs_path *path;
5743 int ret;
5744 struct extent_buffer *leaf;
5745 struct btrfs_root *root = BTRFS_I(inode)->root;
5746 struct btrfs_file_extent_item *fi;
5747 struct btrfs_key key;
5748 u64 disk_bytenr;
5749 u64 backref_offset;
5750 u64 extent_end;
5751 u64 num_bytes;
5752 int slot;
5753 int found_type;
5754
5755 path = btrfs_alloc_path();
5756 if (!path)
5757 return -ENOMEM;
5758
Li Zefan33345d012011-04-20 10:31:50 +08005759 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005760 offset, 0);
5761 if (ret < 0)
5762 goto out;
5763
5764 slot = path->slots[0];
5765 if (ret == 1) {
5766 if (slot == 0) {
5767 /* can't find the item, must cow */
5768 ret = 0;
5769 goto out;
5770 }
5771 slot--;
5772 }
5773 ret = 0;
5774 leaf = path->nodes[0];
5775 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005776 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005777 key.type != BTRFS_EXTENT_DATA_KEY) {
5778 /* not our file or wrong item type, must cow */
5779 goto out;
5780 }
5781
5782 if (key.offset > offset) {
5783 /* Wrong offset, must cow */
5784 goto out;
5785 }
5786
5787 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5788 found_type = btrfs_file_extent_type(leaf, fi);
5789 if (found_type != BTRFS_FILE_EXTENT_REG &&
5790 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5791 /* not a regular extent, must cow */
5792 goto out;
5793 }
5794 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5795 backref_offset = btrfs_file_extent_offset(leaf, fi);
5796
5797 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5798 if (extent_end < offset + len) {
5799 /* extent doesn't include our full range, must cow */
5800 goto out;
5801 }
5802
5803 if (btrfs_extent_readonly(root, disk_bytenr))
5804 goto out;
5805
5806 /*
5807 * look for other files referencing this extent, if we
5808 * find any we must cow
5809 */
Li Zefan33345d012011-04-20 10:31:50 +08005810 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005811 key.offset - backref_offset, disk_bytenr))
5812 goto out;
5813
5814 /*
5815 * adjust disk_bytenr and num_bytes to cover just the bytes
5816 * in this extent we are about to write. If there
5817 * are any csums in that range we have to cow in order
5818 * to keep the csums correct
5819 */
5820 disk_bytenr += backref_offset;
5821 disk_bytenr += offset - key.offset;
5822 num_bytes = min(offset + len, extent_end) - offset;
5823 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5824 goto out;
5825 /*
5826 * all of the above have passed, it is safe to overwrite this extent
5827 * without cow
5828 */
5829 ret = 1;
5830out:
5831 btrfs_free_path(path);
5832 return ret;
5833}
5834
Josef Bacikeb838e72012-07-31 16:28:48 -04005835static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5836 struct extent_state **cached_state, int writing)
5837{
5838 struct btrfs_ordered_extent *ordered;
5839 int ret = 0;
5840
5841 while (1) {
5842 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5843 0, cached_state);
5844 /*
5845 * We're concerned with the entire range that we're going to be
5846 * doing DIO to, so we need to make sure theres no ordered
5847 * extents in this range.
5848 */
5849 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5850 lockend - lockstart + 1);
5851
5852 /*
5853 * We need to make sure there are no buffered pages in this
5854 * range either, we could have raced between the invalidate in
5855 * generic_file_direct_write and locking the extent. The
5856 * invalidate needs to happen so that reads after a write do not
5857 * get stale data.
5858 */
5859 if (!ordered && (!writing ||
5860 !test_range_bit(&BTRFS_I(inode)->io_tree,
5861 lockstart, lockend, EXTENT_UPTODATE, 0,
5862 *cached_state)))
5863 break;
5864
5865 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5866 cached_state, GFP_NOFS);
5867
5868 if (ordered) {
5869 btrfs_start_ordered_extent(inode, ordered, 1);
5870 btrfs_put_ordered_extent(ordered);
5871 } else {
5872 /* Screw you mmap */
5873 ret = filemap_write_and_wait_range(inode->i_mapping,
5874 lockstart,
5875 lockend);
5876 if (ret)
5877 break;
5878
5879 /*
5880 * If we found a page that couldn't be invalidated just
5881 * fall back to buffered.
5882 */
5883 ret = invalidate_inode_pages2_range(inode->i_mapping,
5884 lockstart >> PAGE_CACHE_SHIFT,
5885 lockend >> PAGE_CACHE_SHIFT);
5886 if (ret)
5887 break;
5888 }
5889
5890 cond_resched();
5891 }
5892
5893 return ret;
5894}
5895
Josef Bacik69ffb542012-09-11 15:40:07 -04005896static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5897 u64 len, u64 orig_start,
5898 u64 block_start, u64 block_len,
5899 int type)
5900{
5901 struct extent_map_tree *em_tree;
5902 struct extent_map *em;
5903 struct btrfs_root *root = BTRFS_I(inode)->root;
5904 int ret;
5905
5906 em_tree = &BTRFS_I(inode)->extent_tree;
5907 em = alloc_extent_map();
5908 if (!em)
5909 return ERR_PTR(-ENOMEM);
5910
5911 em->start = start;
5912 em->orig_start = orig_start;
5913 em->len = len;
5914 em->block_len = block_len;
5915 em->block_start = block_start;
5916 em->bdev = root->fs_info->fs_devices->latest_bdev;
5917 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5918 if (type == BTRFS_ORDERED_PREALLOC)
5919 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
5920
5921 do {
5922 btrfs_drop_extent_cache(inode, em->start,
5923 em->start + em->len - 1, 0);
5924 write_lock(&em_tree->lock);
5925 ret = add_extent_mapping(em_tree, em);
5926 write_unlock(&em_tree->lock);
5927 } while (ret == -EEXIST);
5928
5929 if (ret) {
5930 free_extent_map(em);
5931 return ERR_PTR(ret);
5932 }
5933
5934 return em;
5935}
5936
5937
Josef Bacik4b46fce2010-05-23 11:00:55 -04005938static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5939 struct buffer_head *bh_result, int create)
5940{
5941 struct extent_map *em;
5942 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04005943 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005944 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04005945 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005946 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005947 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04005948 int unlock_bits = EXTENT_LOCKED;
5949 int ret;
5950
Josef Bacikeb838e72012-07-31 16:28:48 -04005951 if (create) {
5952 ret = btrfs_delalloc_reserve_space(inode, len);
5953 if (ret)
5954 return ret;
5955 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04005956 } else {
5957 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04005958 }
5959
Josef Bacikc3298612012-08-03 16:49:19 -04005960 lockstart = start;
5961 lockend = start + len - 1;
5962
Josef Bacikeb838e72012-07-31 16:28:48 -04005963 /*
5964 * If this errors out it's because we couldn't invalidate pagecache for
5965 * this range and we need to fallback to buffered.
5966 */
5967 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
5968 return -ENOTBLK;
5969
5970 if (create) {
5971 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
5972 lockend, EXTENT_DELALLOC, NULL,
5973 &cached_state, GFP_NOFS);
5974 if (ret)
5975 goto unlock_err;
5976 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005977
5978 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04005979 if (IS_ERR(em)) {
5980 ret = PTR_ERR(em);
5981 goto unlock_err;
5982 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005983
5984 /*
5985 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5986 * io. INLINE is special, and we could probably kludge it in here, but
5987 * it's still buffered so for safety lets just fall back to the generic
5988 * buffered path.
5989 *
5990 * For COMPRESSED we _have_ to read the entire extent in so we can
5991 * decompress it, so there will be buffering required no matter what we
5992 * do, so go ahead and fallback to buffered.
5993 *
5994 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5995 * to buffered IO. Don't blame me, this is the price we pay for using
5996 * the generic code.
5997 */
5998 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5999 em->block_start == EXTENT_MAP_INLINE) {
6000 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006001 ret = -ENOTBLK;
6002 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006003 }
6004
6005 /* Just a good old fashioned hole, return */
6006 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6007 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6008 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006009 ret = 0;
6010 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006011 }
6012
6013 /*
6014 * We don't allocate a new extent in the following cases
6015 *
6016 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6017 * existing extent.
6018 * 2) The extent is marked as PREALLOC. We're good to go here and can
6019 * just use the extent.
6020 *
6021 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006022 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006023 len = min(len, em->len - (start - em->start));
6024 lockstart = start + len;
6025 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006026 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006027
6028 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6029 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6030 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006031 int type;
6032 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006033 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006034
6035 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6036 type = BTRFS_ORDERED_PREALLOC;
6037 else
6038 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006039 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006040 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006041
6042 /*
6043 * we're not going to log anything, but we do need
6044 * to make sure the current transaction stays open
6045 * while we look for nocow cross refs
6046 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006047 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006048 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006049 goto must_cow;
6050
6051 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006052 u64 orig_start = em->start;
6053
6054 if (type == BTRFS_ORDERED_PREALLOC) {
6055 free_extent_map(em);
6056 em = create_pinned_em(inode, start, len,
6057 orig_start,
6058 block_start, len, type);
6059 if (IS_ERR(em)) {
6060 btrfs_end_transaction(trans, root);
6061 goto unlock_err;
6062 }
6063 }
6064
Chris Mason46bfbb52010-05-26 11:04:10 -04006065 ret = btrfs_add_ordered_extent_dio(inode, start,
6066 block_start, len, len, type);
6067 btrfs_end_transaction(trans, root);
6068 if (ret) {
6069 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006070 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006071 }
6072 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006073 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006074 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006075 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006076must_cow:
6077 /*
6078 * this will cow the extent, reset the len in case we changed
6079 * it above
6080 */
6081 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04006082 em = btrfs_new_extent_direct(inode, em, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006083 if (IS_ERR(em)) {
6084 ret = PTR_ERR(em);
6085 goto unlock_err;
6086 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006087 len = min(len, em->len - (start - em->start));
6088unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006089 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6090 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006091 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006092 bh_result->b_bdev = em->bdev;
6093 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006094 if (create) {
6095 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6096 set_buffer_new(bh_result);
6097
6098 /*
6099 * Need to update the i_size under the extent lock so buffered
6100 * readers will get the updated i_size when we unlock.
6101 */
6102 if (start + len > i_size_read(inode))
6103 i_size_write(inode, start + len);
6104 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006105
Josef Bacikeb838e72012-07-31 16:28:48 -04006106 /*
6107 * In the case of write we need to clear and unlock the entire range,
6108 * in the case of read we need to unlock only the end area that we
6109 * aren't using if there is any left over space.
6110 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006111 if (lockstart < lockend) {
6112 if (create && len < lockend - lockstart) {
6113 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006114 lockstart + len - 1,
6115 unlock_bits | EXTENT_DEFRAG, 1, 0,
Liu Bo24c03fa2012-08-22 20:10:38 -06006116 &cached_state, GFP_NOFS);
6117 /*
6118 * Beside unlock, we also need to cleanup reserved space
6119 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6120 */
6121 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6122 lockstart + len, lockend,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006123 unlock_bits | EXTENT_DO_ACCOUNTING |
6124 EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006125 } else {
6126 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6127 lockend, unlock_bits, 1, 0,
6128 &cached_state, GFP_NOFS);
6129 }
6130 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006131 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006132 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006133
Josef Bacik4b46fce2010-05-23 11:00:55 -04006134 free_extent_map(em);
6135
6136 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006137
6138unlock_err:
6139 if (create)
6140 unlock_bits |= EXTENT_DO_ACCOUNTING;
6141
6142 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6143 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6144 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006145}
6146
6147struct btrfs_dio_private {
6148 struct inode *inode;
6149 u64 logical_offset;
6150 u64 disk_bytenr;
6151 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006152 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006153
6154 /* number of bios pending for this dio */
6155 atomic_t pending_bios;
6156
6157 /* IO errors */
6158 int errors;
6159
6160 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006161};
6162
6163static void btrfs_endio_direct_read(struct bio *bio, int err)
6164{
Miao Xiee65e1532010-11-22 03:04:43 +00006165 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006166 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6167 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006168 struct inode *inode = dip->inode;
6169 struct btrfs_root *root = BTRFS_I(inode)->root;
6170 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006171
6172 start = dip->logical_offset;
6173 do {
6174 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6175 struct page *page = bvec->bv_page;
6176 char *kaddr;
6177 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006178 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006179 unsigned long flags;
6180
Josef Bacikc3298612012-08-03 16:49:19 -04006181 if (get_state_private(&BTRFS_I(inode)->io_tree,
6182 start, &private))
6183 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006184 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006185 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006186 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6187 csum, bvec->bv_len);
6188 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006189 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006190 local_irq_restore(flags);
6191
6192 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006193 if (csum != private) {
6194failed:
Li Zefan33345d012011-04-20 10:31:50 +08006195 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006196 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006197 (unsigned long long)btrfs_ino(inode),
6198 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006199 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006200 err = -EIO;
6201 }
6202 }
6203
6204 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006205 bvec++;
6206 } while (bvec <= bvec_end);
6207
6208 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006209 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006210 bio->bi_private = dip->private;
6211
Josef Bacik4b46fce2010-05-23 11:00:55 -04006212 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006213
6214 /* If we had a csum failure make sure to clear the uptodate flag */
6215 if (err)
6216 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006217 dio_end_io(bio, err);
6218}
6219
6220static void btrfs_endio_direct_write(struct bio *bio, int err)
6221{
6222 struct btrfs_dio_private *dip = bio->bi_private;
6223 struct inode *inode = dip->inode;
6224 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006225 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006226 u64 ordered_offset = dip->logical_offset;
6227 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006228 int ret;
6229
6230 if (err)
6231 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006232again:
6233 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6234 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006235 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006236 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006237 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006238
Josef Bacik5fd02042012-05-02 14:00:54 -04006239 ordered->work.func = finish_ordered_fn;
6240 ordered->work.flags = 0;
6241 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6242 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006243out_test:
6244 /*
6245 * our bio might span multiple ordered extents. If we haven't
6246 * completed the accounting for the whole dio, go back and try again
6247 */
6248 if (ordered_offset < dip->logical_offset + dip->bytes) {
6249 ordered_bytes = dip->logical_offset + dip->bytes -
6250 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006251 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006252 goto again;
6253 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006254out_done:
6255 bio->bi_private = dip->private;
6256
Josef Bacik4b46fce2010-05-23 11:00:55 -04006257 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006258
6259 /* If we had an error make sure to clear the uptodate flag */
6260 if (err)
6261 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006262 dio_end_io(bio, err);
6263}
6264
Chris Masoneaf25d92010-05-25 09:48:28 -04006265static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6266 struct bio *bio, int mirror_num,
6267 unsigned long bio_flags, u64 offset)
6268{
6269 int ret;
6270 struct btrfs_root *root = BTRFS_I(inode)->root;
6271 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006272 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006273 return 0;
6274}
6275
Miao Xiee65e1532010-11-22 03:04:43 +00006276static void btrfs_end_dio_bio(struct bio *bio, int err)
6277{
6278 struct btrfs_dio_private *dip = bio->bi_private;
6279
6280 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006281 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006282 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006283 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006284 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006285 dip->errors = 1;
6286
6287 /*
6288 * before atomic variable goto zero, we must make sure
6289 * dip->errors is perceived to be set.
6290 */
6291 smp_mb__before_atomic_dec();
6292 }
6293
6294 /* if there are more bios still pending for this dio, just exit */
6295 if (!atomic_dec_and_test(&dip->pending_bios))
6296 goto out;
6297
6298 if (dip->errors)
6299 bio_io_error(dip->orig_bio);
6300 else {
6301 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6302 bio_endio(dip->orig_bio, 0);
6303 }
6304out:
6305 bio_put(bio);
6306}
6307
6308static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6309 u64 first_sector, gfp_t gfp_flags)
6310{
6311 int nr_vecs = bio_get_nr_vecs(bdev);
6312 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6313}
6314
6315static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6316 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006317 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006318{
6319 int write = rw & REQ_WRITE;
6320 struct btrfs_root *root = BTRFS_I(inode)->root;
6321 int ret;
6322
6323 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006324
6325 if (!write) {
6326 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6327 if (ret)
6328 goto err;
6329 }
Miao Xiee65e1532010-11-22 03:04:43 +00006330
Josef Bacik1ae39932011-04-06 14:41:34 -04006331 if (skip_sum)
6332 goto map;
6333
6334 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006335 ret = btrfs_wq_submit_bio(root->fs_info,
6336 inode, rw, bio, 0, 0,
6337 file_offset,
6338 __btrfs_submit_bio_start_direct_io,
6339 __btrfs_submit_bio_done);
6340 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006341 } else if (write) {
6342 /*
6343 * If we aren't doing async submit, calculate the csum of the
6344 * bio now.
6345 */
6346 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6347 if (ret)
6348 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006349 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006350 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006351 if (ret)
6352 goto err;
6353 }
Miao Xiee65e1532010-11-22 03:04:43 +00006354
Josef Bacik1ae39932011-04-06 14:41:34 -04006355map:
6356 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006357err:
6358 bio_put(bio);
6359 return ret;
6360}
6361
6362static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6363 int skip_sum)
6364{
6365 struct inode *inode = dip->inode;
6366 struct btrfs_root *root = BTRFS_I(inode)->root;
6367 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6368 struct bio *bio;
6369 struct bio *orig_bio = dip->orig_bio;
6370 struct bio_vec *bvec = orig_bio->bi_io_vec;
6371 u64 start_sector = orig_bio->bi_sector;
6372 u64 file_offset = dip->logical_offset;
6373 u64 submit_len = 0;
6374 u64 map_length;
6375 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006376 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006377 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006378
Miao Xiee65e1532010-11-22 03:04:43 +00006379 map_length = orig_bio->bi_size;
6380 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6381 &map_length, NULL, 0);
6382 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006383 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006384 return -EIO;
6385 }
6386
Josef Bacik02f57c72011-04-06 14:25:44 -04006387 if (map_length >= orig_bio->bi_size) {
6388 bio = orig_bio;
6389 goto submit;
6390 }
6391
Josef Bacik1ae39932011-04-06 14:41:34 -04006392 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006393 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6394 if (!bio)
6395 return -ENOMEM;
6396 bio->bi_private = dip;
6397 bio->bi_end_io = btrfs_end_dio_bio;
6398 atomic_inc(&dip->pending_bios);
6399
Miao Xiee65e1532010-11-22 03:04:43 +00006400 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6401 if (unlikely(map_length < submit_len + bvec->bv_len ||
6402 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6403 bvec->bv_offset) < bvec->bv_len)) {
6404 /*
6405 * inc the count before we submit the bio so
6406 * we know the end IO handler won't happen before
6407 * we inc the count. Otherwise, the dip might get freed
6408 * before we're done setting it up
6409 */
6410 atomic_inc(&dip->pending_bios);
6411 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6412 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006413 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006414 if (ret) {
6415 bio_put(bio);
6416 atomic_dec(&dip->pending_bios);
6417 goto out_err;
6418 }
6419
Miao Xiee65e1532010-11-22 03:04:43 +00006420 start_sector += submit_len >> 9;
6421 file_offset += submit_len;
6422
6423 submit_len = 0;
6424 nr_pages = 0;
6425
6426 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6427 start_sector, GFP_NOFS);
6428 if (!bio)
6429 goto out_err;
6430 bio->bi_private = dip;
6431 bio->bi_end_io = btrfs_end_dio_bio;
6432
6433 map_length = orig_bio->bi_size;
6434 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6435 &map_length, NULL, 0);
6436 if (ret) {
6437 bio_put(bio);
6438 goto out_err;
6439 }
6440 } else {
6441 submit_len += bvec->bv_len;
6442 nr_pages ++;
6443 bvec++;
6444 }
6445 }
6446
Josef Bacik02f57c72011-04-06 14:25:44 -04006447submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006448 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006449 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006450 if (!ret)
6451 return 0;
6452
6453 bio_put(bio);
6454out_err:
6455 dip->errors = 1;
6456 /*
6457 * before atomic variable goto zero, we must
6458 * make sure dip->errors is perceived to be set.
6459 */
6460 smp_mb__before_atomic_dec();
6461 if (atomic_dec_and_test(&dip->pending_bios))
6462 bio_io_error(dip->orig_bio);
6463
6464 /* bio_end_io() will handle error, so we needn't return it */
6465 return 0;
6466}
6467
Josef Bacik4b46fce2010-05-23 11:00:55 -04006468static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6469 loff_t file_offset)
6470{
6471 struct btrfs_root *root = BTRFS_I(inode)->root;
6472 struct btrfs_dio_private *dip;
6473 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006474 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006475 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006476 int ret = 0;
6477
6478 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6479
6480 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6481 if (!dip) {
6482 ret = -ENOMEM;
6483 goto free_ordered;
6484 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006485
6486 dip->private = bio->bi_private;
6487 dip->inode = inode;
6488 dip->logical_offset = file_offset;
6489
Josef Bacik4b46fce2010-05-23 11:00:55 -04006490 dip->bytes = 0;
6491 do {
6492 dip->bytes += bvec->bv_len;
6493 bvec++;
6494 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6495
Chris Mason46bfbb52010-05-26 11:04:10 -04006496 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006497 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006498 dip->errors = 0;
6499 dip->orig_bio = bio;
6500 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006501
6502 if (write)
6503 bio->bi_end_io = btrfs_endio_direct_write;
6504 else
6505 bio->bi_end_io = btrfs_endio_direct_read;
6506
Miao Xiee65e1532010-11-22 03:04:43 +00006507 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6508 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006509 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006510free_ordered:
6511 /*
6512 * If this is a write, we need to clean up the reserved space and kill
6513 * the ordered extent.
6514 */
6515 if (write) {
6516 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006517 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006518 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6519 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6520 btrfs_free_reserved_extent(root, ordered->start,
6521 ordered->disk_len);
6522 btrfs_put_ordered_extent(ordered);
6523 btrfs_put_ordered_extent(ordered);
6524 }
6525 bio_endio(bio, ret);
6526}
6527
Chris Mason5a5f79b2010-05-26 21:33:37 -04006528static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6529 const struct iovec *iov, loff_t offset,
6530 unsigned long nr_segs)
6531{
6532 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006533 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006534 size_t size;
6535 unsigned long addr;
6536 unsigned blocksize_mask = root->sectorsize - 1;
6537 ssize_t retval = -EINVAL;
6538 loff_t end = offset;
6539
6540 if (offset & blocksize_mask)
6541 goto out;
6542
6543 /* Check the memory alignment. Blocks cannot straddle pages */
6544 for (seg = 0; seg < nr_segs; seg++) {
6545 addr = (unsigned long)iov[seg].iov_base;
6546 size = iov[seg].iov_len;
6547 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006548 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006549 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006550
6551 /* If this is a write we don't need to check anymore */
6552 if (rw & WRITE)
6553 continue;
6554
6555 /*
6556 * Check to make sure we don't have duplicate iov_base's in this
6557 * iovec, if so return EINVAL, otherwise we'll get csum errors
6558 * when reading back.
6559 */
6560 for (i = seg + 1; i < nr_segs; i++) {
6561 if (iov[seg].iov_base == iov[i].iov_base)
6562 goto out;
6563 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006564 }
6565 retval = 0;
6566out:
6567 return retval;
6568}
Josef Bacikeb838e72012-07-31 16:28:48 -04006569
Chris Mason16432982008-04-10 10:23:21 -04006570static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6571 const struct iovec *iov, loff_t offset,
6572 unsigned long nr_segs)
6573{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006574 struct file *file = iocb->ki_filp;
6575 struct inode *inode = file->f_mapping->host;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006576
Chris Mason5a5f79b2010-05-26 21:33:37 -04006577 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006578 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006579 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006580
Josef Bacikeb838e72012-07-31 16:28:48 -04006581 return __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006582 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6583 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6584 btrfs_submit_direct, 0);
Chris Mason16432982008-04-10 10:23:21 -04006585}
6586
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006587static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6588 __u64 start, __u64 len)
6589{
Chris Masonec29ed52011-02-23 16:23:20 -05006590 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006591}
6592
Chris Mason9ebefb182007-06-15 13:50:00 -04006593int btrfs_readpage(struct file *file, struct page *page)
6594{
Chris Masond1310b22008-01-24 16:13:08 -05006595 struct extent_io_tree *tree;
6596 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006597 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006598}
Chris Mason1832a6d2007-12-21 16:27:21 -05006599
Chris Mason39279cc2007-06-12 06:35:45 -04006600static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6601{
Chris Masond1310b22008-01-24 16:13:08 -05006602 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006603
6604
6605 if (current->flags & PF_MEMALLOC) {
6606 redirty_page_for_writepage(wbc, page);
6607 unlock_page(page);
6608 return 0;
6609 }
Chris Masond1310b22008-01-24 16:13:08 -05006610 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006611 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6612}
Chris Mason39279cc2007-06-12 06:35:45 -04006613
Chris Masonf4219502008-07-22 11:18:09 -04006614int btrfs_writepages(struct address_space *mapping,
6615 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04006616{
Chris Masond1310b22008-01-24 16:13:08 -05006617 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006618
Chris Masond1310b22008-01-24 16:13:08 -05006619 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04006620 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6621}
6622
Chris Mason3ab2fb52007-11-08 10:59:22 -05006623static int
6624btrfs_readpages(struct file *file, struct address_space *mapping,
6625 struct list_head *pages, unsigned nr_pages)
6626{
Chris Masond1310b22008-01-24 16:13:08 -05006627 struct extent_io_tree *tree;
6628 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006629 return extent_readpages(tree, mapping, pages, nr_pages,
6630 btrfs_get_extent);
6631}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006632static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006633{
Chris Masond1310b22008-01-24 16:13:08 -05006634 struct extent_io_tree *tree;
6635 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006636 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006637
Chris Masond1310b22008-01-24 16:13:08 -05006638 tree = &BTRFS_I(page->mapping->host)->io_tree;
6639 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006640 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006641 if (ret == 1) {
6642 ClearPagePrivate(page);
6643 set_page_private(page, 0);
6644 page_cache_release(page);
6645 }
6646 return ret;
6647}
Chris Mason39279cc2007-06-12 06:35:45 -04006648
Chris Masone6dcd2d2008-07-17 12:53:50 -04006649static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6650{
Chris Mason98509cf2008-09-11 15:51:43 -04006651 if (PageWriteback(page) || PageDirty(page))
6652 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006653 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006654}
6655
Chris Masona52d9a82007-08-27 16:49:44 -04006656static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6657{
Josef Bacik5fd02042012-05-02 14:00:54 -04006658 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006659 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006660 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006661 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006662 u64 page_start = page_offset(page);
6663 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006664
Chris Mason8b62b722009-09-02 16:53:46 -04006665 /*
6666 * we have the page locked, so new writeback can't start,
6667 * and the dirty bit won't be cleared while we are here.
6668 *
6669 * Wait for IO on this page so that we can safely clear
6670 * the PagePrivate2 bit and do ordered accounting
6671 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006672 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006673
Josef Bacik5fd02042012-05-02 14:00:54 -04006674 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006675 if (offset) {
6676 btrfs_releasepage(page, GFP_NOFS);
6677 return;
6678 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006679 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Josef Bacik5fd02042012-05-02 14:00:54 -04006680 ordered = btrfs_lookup_ordered_extent(inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006681 page_offset(page));
6682 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006683 /*
6684 * IO on this page will never be started, so we need
6685 * to account for any ordered extents now
6686 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006687 clear_extent_bit(tree, page_start, page_end,
6688 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006689 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6690 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006691 /*
6692 * whoever cleared the private bit is responsible
6693 * for the finish_ordered_io
6694 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006695 if (TestClearPagePrivate2(page) &&
6696 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6697 PAGE_CACHE_SIZE, 1)) {
6698 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006699 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006700 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006701 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006702 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006703 }
6704 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006705 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006706 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6707 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006708 __btrfs_releasepage(page, GFP_NOFS);
6709
Chris Mason4a096752008-07-21 10:29:44 -04006710 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04006711 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04006712 ClearPagePrivate(page);
6713 set_page_private(page, 0);
6714 page_cache_release(page);
6715 }
Chris Mason39279cc2007-06-12 06:35:45 -04006716}
6717
Chris Mason9ebefb182007-06-15 13:50:00 -04006718/*
6719 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6720 * called from a page fault handler when a page is first dirtied. Hence we must
6721 * be careful to check for EOF conditions here. We set the page up correctly
6722 * for a written page which means we get ENOSPC checking when writing into
6723 * holes and correct delalloc and unwritten extent mapping on filesystems that
6724 * support these features.
6725 *
6726 * We are not allowed to take the i_mutex here so we have to play games to
6727 * protect against truncate races as the page could now be beyond EOF. Because
6728 * vmtruncate() writes the inode size before removing pages, once we have the
6729 * page lock we can determine safely if the page is beyond EOF. If it is not
6730 * beyond EOF, then the page is guaranteed safe against truncation until we
6731 * unlock the page.
6732 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006733int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006734{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006735 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006736 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006737 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006738 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6739 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006740 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006741 char *kaddr;
6742 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006743 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006744 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006745 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006746 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006747 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006748
Jan Karab2b5ef52012-06-12 16:20:45 +02006749 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006750 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006751 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006752 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006753 reserved = 1;
6754 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006755 if (ret) {
6756 if (ret == -ENOMEM)
6757 ret = VM_FAULT_OOM;
6758 else /* -ENOSPC, -EIO, etc */
6759 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006760 if (reserved)
6761 goto out;
6762 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006763 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006764
Nick Piggin56a76f82009-03-31 15:23:23 -07006765 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006766again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006767 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006768 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006769 page_start = page_offset(page);
6770 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006771
Chris Mason9ebefb182007-06-15 13:50:00 -04006772 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006773 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006774 /* page got truncated out from underneath us */
6775 goto out_unlock;
6776 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006777 wait_on_page_writeback(page);
6778
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006779 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006780 set_page_extent_mapped(page);
6781
Chris Masoneb84ae02008-07-17 13:53:27 -04006782 /*
6783 * we can't set the delalloc bits if there are pending ordered
6784 * extents. Drop our locks and wait for them to finish
6785 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006786 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6787 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006788 unlock_extent_cached(io_tree, page_start, page_end,
6789 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006790 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006791 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006792 btrfs_put_ordered_extent(ordered);
6793 goto again;
6794 }
6795
Josef Bacikfbf19082009-10-01 17:10:23 -04006796 /*
6797 * XXX - page_mkwrite gets called every time the page is dirtied, even
6798 * if it was already dirty, so for space accounting reasons we need to
6799 * clear any delalloc bits for the range we are fixing to save. There
6800 * is probably a better way to do this, but for now keep consistent with
6801 * prepare_pages in the normal write path.
6802 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006803 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006804 EXTENT_DIRTY | EXTENT_DELALLOC |
6805 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006806 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006807
Josef Bacik2ac55d42010-02-03 19:33:23 +00006808 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6809 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006810 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006811 unlock_extent_cached(io_tree, page_start, page_end,
6812 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006813 ret = VM_FAULT_SIGBUS;
6814 goto out_unlock;
6815 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006816 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006817
6818 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006819 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006820 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006821 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006822 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006823
Chris Masone6dcd2d2008-07-17 12:53:50 -04006824 if (zero_start != PAGE_CACHE_SIZE) {
6825 kaddr = kmap(page);
6826 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6827 flush_dcache_page(page);
6828 kunmap(page);
6829 }
Chris Mason247e7432008-07-17 12:53:51 -04006830 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006831 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006832 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006833
Chris Mason257c62e2009-10-13 13:21:08 -04006834 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6835 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006836 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006837
Josef Bacik2ac55d42010-02-03 19:33:23 +00006838 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006839
6840out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006841 if (!ret) {
6842 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006843 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006844 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006845 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006846out:
Josef Bacikec39e182012-01-12 19:10:12 -05006847 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006848out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006849 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006850 return ret;
6851}
6852
Josef Bacika41ad392011-01-31 15:30:16 -05006853static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006854{
6855 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006856 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006857 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006858 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006859 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006860 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006861 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006862 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006863
Josef Bacik2aaa6652012-08-29 14:27:18 -04006864 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04006865 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006866 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006867
Chris Mason4a096752008-07-21 10:29:44 -04006868 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006869 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006870
Josef Bacikfcb80c22011-05-03 10:40:22 -04006871 /*
6872 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6873 * 3 things going on here
6874 *
6875 * 1) We need to reserve space for our orphan item and the space to
6876 * delete our orphan item. Lord knows we don't want to have a dangling
6877 * orphan item because we didn't reserve space to remove it.
6878 *
6879 * 2) We need to reserve space to update our inode.
6880 *
6881 * 3) We need to have something to cache all the space that is going to
6882 * be free'd up by the truncate operation, but also have some slack
6883 * space reserved in case it uses space during the truncate (thank you
6884 * very much snapshotting).
6885 *
6886 * And we need these to all be seperate. The fact is we can use alot of
6887 * space doing the truncate, and we have no earthly idea how much space
6888 * we will use, so we need the truncate reservation to be seperate so it
6889 * doesn't end up using space reserved for updating the inode or
6890 * removing the orphan item. We also need to be able to stop the
6891 * transaction and start a new one, which means we need to be able to
6892 * update the inode several times, and we have no idea of knowing how
6893 * many times that will be, so we can't just reserve 1 item for the
6894 * entirety of the opration, so that has to be done seperately as well.
6895 * Then there is the orphan item, which does indeed need to be held on
6896 * to for the whole operation, and we need nobody to touch this reserved
6897 * space except the orphan code.
6898 *
6899 * So that leaves us with
6900 *
6901 * 1) root->orphan_block_rsv - for the orphan deletion.
6902 * 2) rsv - for the truncate reservation, which we will steal from the
6903 * transaction reservation.
6904 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6905 * updating the inode.
6906 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06006907 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006908 if (!rsv)
6909 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006910 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006911 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006912
Josef Bacik907cbce2011-08-08 13:46:15 -04006913 /*
Josef Bacik07127182011-08-19 10:29:59 -04006914 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006915 * 1 for the orphan item we're going to add
6916 * 1 for the orphan item deletion
6917 * 1 for updating the inode.
6918 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04006919 trans = btrfs_start_transaction(root, 4);
6920 if (IS_ERR(trans)) {
6921 err = PTR_ERR(trans);
6922 goto out;
6923 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006924
Josef Bacik907cbce2011-08-08 13:46:15 -04006925 /* Migrate the slack space for the truncate to our reserve */
6926 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6927 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006928 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006929
6930 ret = btrfs_orphan_add(trans, inode);
6931 if (ret) {
6932 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006933 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006934 }
6935
Chris Mason5a3f23d2009-03-31 13:27:11 -04006936 /*
6937 * setattr is responsible for setting the ordered_data_close flag,
6938 * but that is only tested during the last file release. That
6939 * could happen well after the next commit, leaving a great big
6940 * window where new writes may get lost if someone chooses to write
6941 * to this file after truncating to zero
6942 *
6943 * The inode doesn't have any dirty data here, and so if we commit
6944 * this is a noop. If someone immediately starts writing to the inode
6945 * it is very likely we'll catch some of their writes in this
6946 * transaction, and the commit will find this file on the ordered
6947 * data list with good things to send down.
6948 *
6949 * This is a best effort solution, there is still a window where
6950 * using truncate to replace the contents of the file will
6951 * end up with a zero length file after a crash.
6952 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04006953 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
6954 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04006955 btrfs_add_ordered_operation(trans, root, inode);
6956
Josef Bacik5dc562c2012-08-17 13:14:17 -04006957 /*
6958 * So if we truncate and then write and fsync we normally would just
6959 * write the extents that changed, which is a problem if we need to
6960 * first truncate that entire inode. So set this flag so we write out
6961 * all of the extents in the inode to the sync log so we're completely
6962 * safe.
6963 */
6964 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006965 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04006966
Yan, Zheng80825102009-11-12 09:35:36 +00006967 while (1) {
6968 ret = btrfs_truncate_inode_items(trans, root, inode,
6969 inode->i_size,
6970 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006971 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05006972 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006973 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006974 }
Chris Mason39279cc2007-06-12 06:35:45 -04006975
Josef Bacikfcb80c22011-05-03 10:40:22 -04006976 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006977 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006978 if (ret) {
6979 err = ret;
6980 break;
6981 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04006982
Yan, Zheng80825102009-11-12 09:35:36 +00006983 nr = trans->blocks_used;
6984 btrfs_end_transaction(trans, root);
6985 btrfs_btree_balance_dirty(root, nr);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006986
6987 trans = btrfs_start_transaction(root, 2);
6988 if (IS_ERR(trans)) {
6989 ret = err = PTR_ERR(trans);
6990 trans = NULL;
6991 break;
6992 }
6993
6994 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
6995 rsv, min_size);
6996 BUG_ON(ret); /* shouldn't happen */
6997 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006998 }
6999
7000 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007001 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007002 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007003 if (ret)
7004 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05007005 } else if (ret && inode->i_nlink > 0) {
7006 /*
7007 * Failed to do the truncate, remove us from the in memory
7008 * orphan list.
7009 */
7010 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00007011 }
7012
Chris Mason917c16b2011-11-08 14:49:59 -05007013 if (trans) {
7014 trans->block_rsv = &root->fs_info->trans_block_rsv;
7015 ret = btrfs_update_inode(trans, root, inode);
7016 if (ret && !err)
7017 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007018
Chris Mason917c16b2011-11-08 14:49:59 -05007019 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007020 ret = btrfs_end_transaction(trans, root);
Chris Mason917c16b2011-11-08 14:49:59 -05007021 btrfs_btree_balance_dirty(root, nr);
7022 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007023
7024out:
7025 btrfs_free_block_rsv(root, rsv);
7026
Josef Bacik3893e332011-01-31 16:03:11 -05007027 if (ret && !err)
7028 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007029
Josef Bacik3893e332011-01-31 16:03:11 -05007030 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007031}
7032
Sven Wegener3b963622008-06-09 21:57:42 -04007033/*
Chris Masond352ac62008-09-29 15:18:18 -04007034 * create a new subvolume directory/inode (helper for the ioctl).
7035 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007036int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007037 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007038{
Chris Mason39279cc2007-06-12 06:35:45 -04007039 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007040 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007041 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007042
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007043 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7044 new_dirid, new_dirid,
7045 S_IFDIR | (~current_umask() & S_IRWXUGO),
7046 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007047 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007048 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007049 inode->i_op = &btrfs_dir_inode_operations;
7050 inode->i_fop = &btrfs_dir_file_operations;
7051
Miklos Szeredibfe86842011-10-28 14:13:29 +02007052 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007053 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007054
Yan, Zheng76dda932009-09-21 16:00:26 -04007055 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007056
Yan, Zheng76dda932009-09-21 16:00:26 -04007057 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007058 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007059}
7060
Chris Mason39279cc2007-06-12 06:35:45 -04007061struct inode *btrfs_alloc_inode(struct super_block *sb)
7062{
7063 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007064 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007065
7066 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7067 if (!ei)
7068 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007069
7070 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007071 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007072 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007073 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007074 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007075 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007076 ei->disk_i_size = 0;
7077 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007078 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007079 ei->index_cnt = (u64)-1;
7080 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007081 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007082
Josef Bacik9e0baf62011-07-15 15:16:44 +00007083 spin_lock_init(&ei->lock);
7084 ei->outstanding_extents = 0;
7085 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007086
Josef Bacik72ac3c02012-05-23 14:13:11 -04007087 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007088 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007089
Miao Xie16cdcec2011-04-22 18:12:22 +08007090 ei->delayed_node = NULL;
7091
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007092 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007093 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007094 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7095 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007096 ei->io_tree.track_uptodate = 1;
7097 ei->io_failure_tree.track_uptodate = 1;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007098 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007099 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007100 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007101 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007102 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007103 RB_CLEAR_NODE(&ei->rb_node);
7104
7105 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007106}
7107
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007108static void btrfs_i_callback(struct rcu_head *head)
7109{
7110 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007111 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7112}
7113
Chris Mason39279cc2007-06-12 06:35:45 -04007114void btrfs_destroy_inode(struct inode *inode)
7115{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007116 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007117 struct btrfs_root *root = BTRFS_I(inode)->root;
7118
Al Virob3d9b7a2012-06-09 13:51:19 -04007119 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007120 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007121 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7122 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007123 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7124 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007125
Chris Mason5a3f23d2009-03-31 13:27:11 -04007126 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007127 * This can happen where we create an inode, but somebody else also
7128 * created the same inode and we need to destroy the one we already
7129 * created.
7130 */
7131 if (!root)
7132 goto free;
7133
7134 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007135 * Make sure we're properly removed from the ordered operation
7136 * lists.
7137 */
7138 smp_mb();
7139 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7140 spin_lock(&root->fs_info->ordered_extent_lock);
7141 list_del_init(&BTRFS_I(inode)->ordered_operations);
7142 spin_unlock(&root->fs_info->ordered_extent_lock);
7143 }
7144
Josef Bacik8a35d952012-05-23 14:26:42 -04007145 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7146 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007147 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7148 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007149 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007150 }
Josef Bacik7b128762008-07-24 12:17:14 -04007151
Chris Masond3977122009-01-05 21:25:51 -05007152 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007153 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7154 if (!ordered)
7155 break;
7156 else {
Chris Masond3977122009-01-05 21:25:51 -05007157 printk(KERN_ERR "btrfs found ordered "
7158 "extent %llu %llu on inode cleanup\n",
7159 (unsigned long long)ordered->file_offset,
7160 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007161 btrfs_remove_ordered_extent(inode, ordered);
7162 btrfs_put_ordered_extent(ordered);
7163 btrfs_put_ordered_extent(ordered);
7164 }
7165 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007166 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007167 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007168free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007169 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007170 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007171}
7172
Al Viro45321ac2010-06-07 13:43:19 -04007173int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007174{
7175 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007176
Josef Bacik0af3d002010-06-21 14:48:16 -04007177 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007178 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007179 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007180 else
Al Viro45321ac2010-06-07 13:43:19 -04007181 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007182}
7183
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007184static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007185{
7186 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7187
7188 inode_init_once(&ei->vfs_inode);
7189}
7190
7191void btrfs_destroy_cachep(void)
7192{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007193 /*
7194 * Make sure all delayed rcu free inodes are flushed before we
7195 * destroy cache.
7196 */
7197 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007198 if (btrfs_inode_cachep)
7199 kmem_cache_destroy(btrfs_inode_cachep);
7200 if (btrfs_trans_handle_cachep)
7201 kmem_cache_destroy(btrfs_trans_handle_cachep);
7202 if (btrfs_transaction_cachep)
7203 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007204 if (btrfs_path_cachep)
7205 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007206 if (btrfs_free_space_cachep)
7207 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007208 if (btrfs_delalloc_work_cachep)
7209 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007210}
7211
7212int btrfs_init_cachep(void)
7213{
David Sterba837e1972012-09-07 03:00:48 -06007214 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007215 sizeof(struct btrfs_inode), 0,
7216 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007217 if (!btrfs_inode_cachep)
7218 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007219
David Sterba837e1972012-09-07 03:00:48 -06007220 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007221 sizeof(struct btrfs_trans_handle), 0,
7222 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007223 if (!btrfs_trans_handle_cachep)
7224 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007225
David Sterba837e1972012-09-07 03:00:48 -06007226 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007227 sizeof(struct btrfs_transaction), 0,
7228 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007229 if (!btrfs_transaction_cachep)
7230 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007231
David Sterba837e1972012-09-07 03:00:48 -06007232 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007233 sizeof(struct btrfs_path), 0,
7234 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007235 if (!btrfs_path_cachep)
7236 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007237
David Sterba837e1972012-09-07 03:00:48 -06007238 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007239 sizeof(struct btrfs_free_space), 0,
7240 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7241 if (!btrfs_free_space_cachep)
7242 goto fail;
7243
Miao Xie8ccf6f192012-10-25 09:28:04 +00007244 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7245 sizeof(struct btrfs_delalloc_work), 0,
7246 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7247 NULL);
7248 if (!btrfs_delalloc_work_cachep)
7249 goto fail;
7250
Chris Mason39279cc2007-06-12 06:35:45 -04007251 return 0;
7252fail:
7253 btrfs_destroy_cachep();
7254 return -ENOMEM;
7255}
7256
7257static int btrfs_getattr(struct vfsmount *mnt,
7258 struct dentry *dentry, struct kstat *stat)
7259{
7260 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007261 u32 blocksize = inode->i_sb->s_blocksize;
7262
Chris Mason39279cc2007-06-12 06:35:45 -04007263 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007264 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007265 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007266 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7267 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007268 return 0;
7269}
7270
Liu Bo75e7cb72011-03-22 10:12:20 +00007271/*
7272 * If a file is moved, it will inherit the cow and compression flags of the new
7273 * directory.
7274 */
7275static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7276{
7277 struct btrfs_inode *b_dir = BTRFS_I(dir);
7278 struct btrfs_inode *b_inode = BTRFS_I(inode);
7279
7280 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7281 b_inode->flags |= BTRFS_INODE_NODATACOW;
7282 else
7283 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7284
Liu Bobc178232012-06-14 02:23:18 -06007285 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007286 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007287 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7288 } else {
7289 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7290 BTRFS_INODE_NOCOMPRESS);
7291 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007292}
7293
Chris Masond3977122009-01-05 21:25:51 -05007294static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7295 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007296{
7297 struct btrfs_trans_handle *trans;
7298 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007299 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007300 struct inode *new_inode = new_dentry->d_inode;
7301 struct inode *old_inode = old_dentry->d_inode;
7302 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007303 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007304 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007305 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007306 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007307
Li Zefan33345d012011-04-20 10:31:50 +08007308 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007309 return -EPERM;
7310
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007311 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007312 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007313 return -EXDEV;
7314
Li Zefan33345d012011-04-20 10:31:50 +08007315 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7316 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007317 return -ENOTEMPTY;
7318
Chris Mason39279cc2007-06-12 06:35:45 -04007319 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007320 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007321 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007322 /*
7323 * we're using rename to replace one file with another.
7324 * and the replacement file is large. Start IO on it now so
7325 * we don't add too much work to the end of the transaction
7326 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007327 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007328 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7329 filemap_flush(old_inode->i_mapping);
7330
Yan, Zheng76dda932009-09-21 16:00:26 -04007331 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007332 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007333 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007334 /*
7335 * We want to reserve the absolute worst case amount of items. So if
7336 * both inodes are subvols and we need to unlink them then that would
7337 * require 4 item modifications, but if they are both normal inodes it
7338 * would require 5 item modifications, so we'll assume their normal
7339 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7340 * should cover the worst case number of items we'll modify.
7341 */
7342 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007343 if (IS_ERR(trans)) {
7344 ret = PTR_ERR(trans);
7345 goto out_notrans;
7346 }
Chris Mason5f39d392007-10-15 16:14:19 -04007347
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007348 if (dest != root)
7349 btrfs_record_root_in_trans(trans, dest);
7350
Yan, Zhenga5719522009-09-24 09:17:31 -04007351 ret = btrfs_set_inode_index(new_dir, &index);
7352 if (ret)
7353 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007354
Li Zefan33345d012011-04-20 10:31:50 +08007355 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007356 /* force full log commit if subvolume involved. */
7357 root->fs_info->last_trans_log_full_commit = trans->transid;
7358 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007359 ret = btrfs_insert_inode_ref(trans, dest,
7360 new_dentry->d_name.name,
7361 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007362 old_ino,
7363 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007364 if (ret)
7365 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007366 /*
7367 * this is an ugly little race, but the rename is required
7368 * to make sure that if we crash, the inode is either at the
7369 * old name or the new one. pinning the log transaction lets
7370 * us make sure we don't allow a log commit to come in after
7371 * we unlink the name but before we add the new name back in.
7372 */
7373 btrfs_pin_log_trans(root);
7374 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007375 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007376 * make sure the inode gets flushed if it is replacing
7377 * something.
7378 */
Li Zefan33345d012011-04-20 10:31:50 +08007379 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007380 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007381
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007382 inode_inc_iversion(old_dir);
7383 inode_inc_iversion(new_dir);
7384 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007385 old_dir->i_ctime = old_dir->i_mtime = ctime;
7386 new_dir->i_ctime = new_dir->i_mtime = ctime;
7387 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007388
Chris Mason12fcfd22009-03-24 10:24:20 -04007389 if (old_dentry->d_parent != new_dentry->d_parent)
7390 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7391
Li Zefan33345d012011-04-20 10:31:50 +08007392 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007393 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7394 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7395 old_dentry->d_name.name,
7396 old_dentry->d_name.len);
7397 } else {
Al Viro92986792011-03-04 17:14:37 +00007398 ret = __btrfs_unlink_inode(trans, root, old_dir,
7399 old_dentry->d_inode,
7400 old_dentry->d_name.name,
7401 old_dentry->d_name.len);
7402 if (!ret)
7403 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007404 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007405 if (ret) {
7406 btrfs_abort_transaction(trans, root, ret);
7407 goto out_fail;
7408 }
Chris Mason39279cc2007-06-12 06:35:45 -04007409
7410 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007411 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007412 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007413 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007414 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7415 root_objectid = BTRFS_I(new_inode)->location.objectid;
7416 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7417 root_objectid,
7418 new_dentry->d_name.name,
7419 new_dentry->d_name.len);
7420 BUG_ON(new_inode->i_nlink == 0);
7421 } else {
7422 ret = btrfs_unlink_inode(trans, dest, new_dir,
7423 new_dentry->d_inode,
7424 new_dentry->d_name.name,
7425 new_dentry->d_name.len);
7426 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007427 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007428 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007429 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007430 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007431 if (ret) {
7432 btrfs_abort_transaction(trans, root, ret);
7433 goto out_fail;
7434 }
Chris Mason39279cc2007-06-12 06:35:45 -04007435 }
Josef Bacikaec74772008-07-24 12:12:38 -04007436
Liu Bo75e7cb72011-03-22 10:12:20 +00007437 fixup_inode_flags(new_dir, old_inode);
7438
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007439 ret = btrfs_add_link(trans, new_dir, old_inode,
7440 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007441 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007442 if (ret) {
7443 btrfs_abort_transaction(trans, root, ret);
7444 goto out_fail;
7445 }
Chris Mason39279cc2007-06-12 06:35:45 -04007446
Li Zefan33345d012011-04-20 10:31:50 +08007447 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007448 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007449 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007450 btrfs_end_log_trans(root);
7451 }
Chris Mason39279cc2007-06-12 06:35:45 -04007452out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007453 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007454out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007455 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007456 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007457
Chris Mason39279cc2007-06-12 06:35:45 -04007458 return ret;
7459}
7460
Miao Xie8ccf6f192012-10-25 09:28:04 +00007461static void btrfs_run_delalloc_work(struct btrfs_work *work)
7462{
7463 struct btrfs_delalloc_work *delalloc_work;
7464
7465 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7466 work);
7467 if (delalloc_work->wait)
7468 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7469 else
7470 filemap_flush(delalloc_work->inode->i_mapping);
7471
7472 if (delalloc_work->delay_iput)
7473 btrfs_add_delayed_iput(delalloc_work->inode);
7474 else
7475 iput(delalloc_work->inode);
7476 complete(&delalloc_work->completion);
7477}
7478
7479struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7480 int wait, int delay_iput)
7481{
7482 struct btrfs_delalloc_work *work;
7483
7484 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7485 if (!work)
7486 return NULL;
7487
7488 init_completion(&work->completion);
7489 INIT_LIST_HEAD(&work->list);
7490 work->inode = inode;
7491 work->wait = wait;
7492 work->delay_iput = delay_iput;
7493 work->work.func = btrfs_run_delalloc_work;
7494
7495 return work;
7496}
7497
7498void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7499{
7500 wait_for_completion(&work->completion);
7501 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7502}
7503
Chris Masond352ac62008-09-29 15:18:18 -04007504/*
7505 * some fairly slow code that needs optimization. This walks the list
7506 * of all the inodes with pending delalloc and forces them to disk.
7507 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007508int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007509{
7510 struct list_head *head = &root->fs_info->delalloc_inodes;
7511 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007512 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007513 struct btrfs_delalloc_work *work, *next;
7514 struct list_head works;
7515 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04007516
Yan Zhengc146afa2008-11-12 14:34:12 -05007517 if (root->fs_info->sb->s_flags & MS_RDONLY)
7518 return -EROFS;
7519
Miao Xie8ccf6f192012-10-25 09:28:04 +00007520 INIT_LIST_HEAD(&works);
7521
Chris Mason75eff682008-12-15 15:54:40 -05007522 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007523 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007524 binode = list_entry(head->next, struct btrfs_inode,
7525 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007526 inode = igrab(&binode->vfs_inode);
7527 if (!inode)
7528 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007529 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007530 if (inode) {
Miao Xie8ccf6f192012-10-25 09:28:04 +00007531 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7532 if (!work) {
7533 ret = -ENOMEM;
7534 goto out;
7535 }
7536 list_add_tail(&work->list, &works);
7537 btrfs_queue_worker(&root->fs_info->flush_workers,
7538 &work->work);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007539 }
7540 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007541 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007542 }
Chris Mason75eff682008-12-15 15:54:40 -05007543 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007544
7545 /* the filemap_flush will queue IO into the worker threads, but
7546 * we have to make sure the IO is actually started and that
7547 * ordered extents get created before we return
7548 */
7549 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007550 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007551 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007552 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007553 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7554 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007555 }
7556 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007557out:
7558 list_for_each_entry_safe(work, next, &works, list) {
7559 list_del_init(&work->list);
7560 btrfs_wait_and_free_delalloc_work(work);
7561 }
7562 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04007563}
7564
Chris Mason39279cc2007-06-12 06:35:45 -04007565static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7566 const char *symname)
7567{
7568 struct btrfs_trans_handle *trans;
7569 struct btrfs_root *root = BTRFS_I(dir)->root;
7570 struct btrfs_path *path;
7571 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007572 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007573 int err;
7574 int drop_inode = 0;
7575 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007576 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007577 int name_len;
7578 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007579 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007580 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007581 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007582 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007583
7584 name_len = strlen(symname) + 1;
7585 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7586 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007587
Josef Bacik9ed74f22009-09-11 16:12:44 -04007588 /*
7589 * 2 items for inode item and ref
7590 * 2 items for dir items
7591 * 1 item for xattr if selinux is on
7592 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007593 trans = btrfs_start_transaction(root, 5);
7594 if (IS_ERR(trans))
7595 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007596
Li Zefan581bb052011-04-20 10:06:11 +08007597 err = btrfs_find_free_ino(root, &objectid);
7598 if (err)
7599 goto out_unlock;
7600
Josef Bacikaec74772008-07-24 12:12:38 -04007601 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007602 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007603 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007604 if (IS_ERR(inode)) {
7605 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007606 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007607 }
Chris Mason39279cc2007-06-12 06:35:45 -04007608
Eric Paris2a7dba32011-02-01 11:05:39 -05007609 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007610 if (err) {
7611 drop_inode = 1;
7612 goto out_unlock;
7613 }
7614
Casey Schauflerad19db72011-12-15 10:09:07 -05007615 /*
7616 * If the active LSM wants to access the inode during
7617 * d_instantiate it needs these. Smack checks to see
7618 * if the filesystem supports xattrs by looking at the
7619 * ops vector.
7620 */
7621 inode->i_fop = &btrfs_file_operations;
7622 inode->i_op = &btrfs_file_inode_operations;
7623
Josef Bacika1b075d2010-11-19 20:36:11 +00007624 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007625 if (err)
7626 drop_inode = 1;
7627 else {
7628 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007629 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007630 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007631 }
Chris Mason39279cc2007-06-12 06:35:45 -04007632 if (drop_inode)
7633 goto out_unlock;
7634
7635 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007636 if (!path) {
7637 err = -ENOMEM;
7638 drop_inode = 1;
7639 goto out_unlock;
7640 }
Li Zefan33345d012011-04-20 10:31:50 +08007641 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007642 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007643 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7644 datasize = btrfs_file_extent_calc_inline_size(name_len);
7645 err = btrfs_insert_empty_item(trans, root, path, &key,
7646 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007647 if (err) {
7648 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007649 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007650 goto out_unlock;
7651 }
Chris Mason5f39d392007-10-15 16:14:19 -04007652 leaf = path->nodes[0];
7653 ei = btrfs_item_ptr(leaf, path->slots[0],
7654 struct btrfs_file_extent_item);
7655 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7656 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007657 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007658 btrfs_set_file_extent_encryption(leaf, ei, 0);
7659 btrfs_set_file_extent_compression(leaf, ei, 0);
7660 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7661 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7662
Chris Mason39279cc2007-06-12 06:35:45 -04007663 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007664 write_extent_buffer(leaf, symname, ptr, name_len);
7665 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007666 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007667
Chris Mason39279cc2007-06-12 06:35:45 -04007668 inode->i_op = &btrfs_symlink_inode_operations;
7669 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007670 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007671 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007672 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007673 err = btrfs_update_inode(trans, root, inode);
7674 if (err)
7675 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007676
7677out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007678 if (!err)
7679 d_instantiate(dentry, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007680 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007681 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007682 if (drop_inode) {
7683 inode_dec_link_count(inode);
7684 iput(inode);
7685 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007686 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007687 return err;
7688}
Chris Mason16432982008-04-10 10:23:21 -04007689
Josef Bacik0af3d002010-06-21 14:48:16 -04007690static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7691 u64 start, u64 num_bytes, u64 min_size,
7692 loff_t actual_len, u64 *alloc_hint,
7693 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007694{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007695 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7696 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007697 struct btrfs_root *root = BTRFS_I(inode)->root;
7698 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007699 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007700 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007701 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007702 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007703
Josef Bacik0af3d002010-06-21 14:48:16 -04007704 if (trans)
7705 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007706 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007707 if (own_trans) {
7708 trans = btrfs_start_transaction(root, 3);
7709 if (IS_ERR(trans)) {
7710 ret = PTR_ERR(trans);
7711 break;
7712 }
Yan Zhengd899e052008-10-30 14:25:28 -04007713 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007714
Yan, Zhengefa56462010-05-16 10:49:59 -04007715 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007716 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007717 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007718 if (own_trans)
7719 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007720 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007721 }
7722
Yan Zhengd899e052008-10-30 14:25:28 -04007723 ret = insert_reserved_file_extent(trans, inode,
7724 cur_offset, ins.objectid,
7725 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007726 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007727 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007728 if (ret) {
7729 btrfs_abort_transaction(trans, root, ret);
7730 if (own_trans)
7731 btrfs_end_transaction(trans, root);
7732 break;
7733 }
Chris Masona1ed8352009-09-11 12:27:37 -04007734 btrfs_drop_extent_cache(inode, cur_offset,
7735 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007736
Josef Bacik5dc562c2012-08-17 13:14:17 -04007737 em = alloc_extent_map();
7738 if (!em) {
7739 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7740 &BTRFS_I(inode)->runtime_flags);
7741 goto next;
7742 }
7743
7744 em->start = cur_offset;
7745 em->orig_start = cur_offset;
7746 em->len = ins.offset;
7747 em->block_start = ins.objectid;
7748 em->block_len = ins.offset;
7749 em->bdev = root->fs_info->fs_devices->latest_bdev;
7750 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7751 em->generation = trans->transid;
7752
7753 while (1) {
7754 write_lock(&em_tree->lock);
7755 ret = add_extent_mapping(em_tree, em);
7756 if (!ret)
7757 list_move(&em->list,
7758 &em_tree->modified_extents);
7759 write_unlock(&em_tree->lock);
7760 if (ret != -EEXIST)
7761 break;
7762 btrfs_drop_extent_cache(inode, cur_offset,
7763 cur_offset + ins.offset - 1,
7764 0);
7765 }
7766 free_extent_map(em);
7767next:
Yan Zhengd899e052008-10-30 14:25:28 -04007768 num_bytes -= ins.offset;
7769 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007770 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007771
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007772 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007773 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007774 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007775 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007776 (actual_len > inode->i_size) &&
7777 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007778 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007779 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007780 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007781 i_size = cur_offset;
7782 i_size_write(inode, i_size);
7783 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007784 }
7785
Yan Zhengd899e052008-10-30 14:25:28 -04007786 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007787
7788 if (ret) {
7789 btrfs_abort_transaction(trans, root, ret);
7790 if (own_trans)
7791 btrfs_end_transaction(trans, root);
7792 break;
7793 }
Yan Zhengd899e052008-10-30 14:25:28 -04007794
Josef Bacik0af3d002010-06-21 14:48:16 -04007795 if (own_trans)
7796 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007797 }
Yan Zhengd899e052008-10-30 14:25:28 -04007798 return ret;
7799}
7800
Josef Bacik0af3d002010-06-21 14:48:16 -04007801int btrfs_prealloc_file_range(struct inode *inode, int mode,
7802 u64 start, u64 num_bytes, u64 min_size,
7803 loff_t actual_len, u64 *alloc_hint)
7804{
7805 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7806 min_size, actual_len, alloc_hint,
7807 NULL);
7808}
7809
7810int btrfs_prealloc_file_range_trans(struct inode *inode,
7811 struct btrfs_trans_handle *trans, int mode,
7812 u64 start, u64 num_bytes, u64 min_size,
7813 loff_t actual_len, u64 *alloc_hint)
7814{
7815 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7816 min_size, actual_len, alloc_hint, trans);
7817}
7818
Chris Masone6dcd2d2008-07-17 12:53:50 -04007819static int btrfs_set_page_dirty(struct page *page)
7820{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007821 return __set_page_dirty_nobuffers(page);
7822}
7823
Al Viro10556cb22011-06-20 19:28:19 -04007824static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007825{
Li Zefanb83cc962010-12-20 16:04:08 +08007826 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007827 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007828
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007829 if (mask & MAY_WRITE &&
7830 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7831 if (btrfs_root_readonly(root))
7832 return -EROFS;
7833 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7834 return -EACCES;
7835 }
Al Viro2830ba72011-06-20 19:16:29 -04007836 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007837}
Chris Mason39279cc2007-06-12 06:35:45 -04007838
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007839static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007840 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007841 .lookup = btrfs_lookup,
7842 .create = btrfs_create,
7843 .unlink = btrfs_unlink,
7844 .link = btrfs_link,
7845 .mkdir = btrfs_mkdir,
7846 .rmdir = btrfs_rmdir,
7847 .rename = btrfs_rename,
7848 .symlink = btrfs_symlink,
7849 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007850 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007851 .setxattr = btrfs_setxattr,
7852 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007853 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007854 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007855 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007856 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007857};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007858static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007859 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007860 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007861 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007862};
Yan, Zheng76dda932009-09-21 16:00:26 -04007863
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007864static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007865 .llseek = generic_file_llseek,
7866 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007867 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007868 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007869#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007870 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007871#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007872 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007873 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007874};
7875
Chris Masond1310b22008-01-24 16:13:08 -05007876static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007877 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007878 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007879 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007880 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007881 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007882 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007883 .set_bit_hook = btrfs_set_bit_hook,
7884 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007885 .merge_extent_hook = btrfs_merge_extent_hook,
7886 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007887};
7888
Chris Mason35054392009-01-21 13:11:13 -05007889/*
7890 * btrfs doesn't support the bmap operation because swapfiles
7891 * use bmap to make a mapping of extents in the file. They assume
7892 * these extents won't change over the life of the file and they
7893 * use the bmap result to do IO directly to the drive.
7894 *
7895 * the btrfs bmap call would return logical addresses that aren't
7896 * suitable for IO and they also will change frequently as COW
7897 * operations happen. So, swapfile + btrfs == corruption.
7898 *
7899 * For now we're avoiding this by dropping bmap.
7900 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007901static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007902 .readpage = btrfs_readpage,
7903 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04007904 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007905 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007906 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007907 .invalidatepage = btrfs_invalidatepage,
7908 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007909 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007910 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007911};
7912
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007913static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007914 .readpage = btrfs_readpage,
7915 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007916 .invalidatepage = btrfs_invalidatepage,
7917 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007918};
7919
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007920static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007921 .getattr = btrfs_getattr,
7922 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007923 .setxattr = btrfs_setxattr,
7924 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007925 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007926 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007927 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007928 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007929 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007930 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007931};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007932static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007933 .getattr = btrfs_getattr,
7934 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007935 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007936 .setxattr = btrfs_setxattr,
7937 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007938 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007939 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007940 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007941 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04007942};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007943static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007944 .readlink = generic_readlink,
7945 .follow_link = page_follow_link_light,
7946 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007947 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05007948 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007949 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007950 .setxattr = btrfs_setxattr,
7951 .getxattr = btrfs_getxattr,
7952 .listxattr = btrfs_listxattr,
7953 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007954 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007955 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007956};
Yan, Zheng76dda932009-09-21 16:00:26 -04007957
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007958const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007959 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007960 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04007961};