blob: 456667b04fe6e09fb63f15643eb7f69a6985c9b3 [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>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Chris Mason39279cc2007-06-12 06:35:45 -040059
60struct btrfs_iget_args {
61 u64 ino;
62 struct btrfs_root *root;
63};
64
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070065static const struct inode_operations btrfs_dir_inode_operations;
66static const struct inode_operations btrfs_symlink_inode_operations;
67static const struct inode_operations btrfs_dir_ro_inode_operations;
68static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
71static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050073static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040074
75static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000076static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_trans_handle_cachep;
78struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050080struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081
82#define S_SHIFT 12
83static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
84 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
85 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
86 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
87 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
88 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
89 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
90 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
91};
92
Eric Sandeen3972f262013-01-12 02:57:22 +000093static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050094static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040095static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050096static noinline int cow_file_range(struct inode *inode,
97 struct page *locked_page,
98 u64 start, u64 end, int *page_started,
99 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400100static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
101 u64 len, u64 orig_start,
102 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400103 u64 orig_block_len, u64 ram_bytes,
104 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400105
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000106static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500107 struct inode *inode, struct inode *dir,
108 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500109{
110 int err;
111
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000112 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500113 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500114 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500115 return err;
116}
117
Chris Masond352ac62008-09-29 15:18:18 -0400118/*
Chris Masonc8b97812008-10-29 14:49:59 -0400119 * this does all the hard work for inserting an inline extent into
120 * the btree. The caller should have done a btrfs_drop_extents so that
121 * no overlapping inline items exist in the btree
122 */
Chris Masond3977122009-01-05 21:25:51 -0500123static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400124 struct btrfs_root *root, struct inode *inode,
125 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000126 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400127 struct page **compressed_pages)
128{
129 struct btrfs_key key;
130 struct btrfs_path *path;
131 struct extent_buffer *leaf;
132 struct page *page = NULL;
133 char *kaddr;
134 unsigned long ptr;
135 struct btrfs_file_extent_item *ei;
136 int err = 0;
137 int ret;
138 size_t cur_size = size;
139 size_t datasize;
140 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400141
Li Zefanfe3f5662011-03-28 08:30:38 +0000142 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400143 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Chris Masond3977122009-01-05 21:25:51 -0500145 path = btrfs_alloc_path();
146 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400147 return -ENOMEM;
148
Chris Masonb9473432009-03-13 11:00:37 -0400149 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400150
Li Zefan33345d012011-04-20 10:31:50 +0800151 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400152 key.offset = start;
153 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400154 datasize = btrfs_file_extent_calc_inline_size(cur_size);
155
156 inode_add_bytes(inode, size);
157 ret = btrfs_insert_empty_item(trans, root, path, &key,
158 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400159 if (ret) {
160 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400161 goto fail;
162 }
163 leaf = path->nodes[0];
164 ei = btrfs_item_ptr(leaf, path->slots[0],
165 struct btrfs_file_extent_item);
166 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
167 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
168 btrfs_set_file_extent_encryption(leaf, ei, 0);
169 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
170 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
171 ptr = btrfs_file_extent_inline_start(ei);
172
Li Zefan261507a02010-12-17 14:21:50 +0800173 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400174 struct page *cpage;
175 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500176 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400177 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500178 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400179 PAGE_CACHE_SIZE);
180
Cong Wang7ac687d2011-11-25 23:14:28 +0800181 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400182 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800183 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400184
185 i++;
186 ptr += cur_size;
187 compressed_size -= cur_size;
188 }
189 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800190 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400191 } else {
192 page = find_get_page(inode->i_mapping,
193 start >> PAGE_CACHE_SHIFT);
194 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800195 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400196 offset = start & (PAGE_CACHE_SIZE - 1);
197 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800198 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400199 page_cache_release(page);
200 }
201 btrfs_mark_buffer_dirty(leaf);
202 btrfs_free_path(path);
203
Yan, Zhengc2167752009-11-12 09:34:21 +0000204 /*
205 * we're an inline extent, so nobody can
206 * extend the file past i_size without locking
207 * a page we already have locked.
208 *
209 * We must do any isize and inode updates
210 * before we unlock the pages. Otherwise we
211 * could end up racing with unlink.
212 */
Chris Masonc8b97812008-10-29 14:49:59 -0400213 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100214 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000215
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100216 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400217fail:
218 btrfs_free_path(path);
219 return err;
220}
221
222
223/*
224 * conditionally insert an inline extent into the file. This
225 * does the checks required to make sure the data is small enough
226 * to fit as an inline extent.
227 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400228static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400229 struct btrfs_root *root,
230 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000231 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400232 struct page **compressed_pages)
233{
234 u64 isize = i_size_read(inode);
235 u64 actual_end = min(end + 1, isize);
236 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000237 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400238 u64 data_len = inline_len;
239 int ret;
240
241 if (compressed_size)
242 data_len = compressed_size;
243
244 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400245 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400246 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
247 (!compressed_size &&
248 (actual_end & (root->sectorsize - 1)) == 0) ||
249 end + 1 < isize ||
250 data_len > root->fs_info->max_inline) {
251 return 1;
252 }
253
Josef Bacik26714852012-08-29 12:24:27 -0400254 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100255 if (ret)
256 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400257
258 if (isize > actual_end)
259 inline_len = min_t(u64, isize, actual_end);
260 ret = insert_inline_extent(trans, root, inode, start,
261 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000262 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400263 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100264 btrfs_abort_transaction(trans, root, ret);
265 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400266 } else if (ret == -ENOSPC) {
267 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100268 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400269
Josef Bacikbdc20e62013-02-28 13:23:38 -0500270 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400271 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400272 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400273 return 0;
274}
275
Chris Mason771ed682008-11-06 22:02:51 -0500276struct async_extent {
277 u64 start;
278 u64 ram_size;
279 u64 compressed_size;
280 struct page **pages;
281 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800282 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500283 struct list_head list;
284};
285
286struct async_cow {
287 struct inode *inode;
288 struct btrfs_root *root;
289 struct page *locked_page;
290 u64 start;
291 u64 end;
292 struct list_head extents;
293 struct btrfs_work work;
294};
295
296static noinline int add_async_extent(struct async_cow *cow,
297 u64 start, u64 ram_size,
298 u64 compressed_size,
299 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800300 unsigned long nr_pages,
301 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500302{
303 struct async_extent *async_extent;
304
305 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100306 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500307 async_extent->start = start;
308 async_extent->ram_size = ram_size;
309 async_extent->compressed_size = compressed_size;
310 async_extent->pages = pages;
311 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800312 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500313 list_add_tail(&async_extent->list, &cow->extents);
314 return 0;
315}
316
Chris Masonc8b97812008-10-29 14:49:59 -0400317/*
Chris Mason771ed682008-11-06 22:02:51 -0500318 * we create compressed extents in two phases. The first
319 * phase compresses a range of pages that have already been
320 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400321 *
Chris Mason771ed682008-11-06 22:02:51 -0500322 * This is done inside an ordered work queue, and the compression
323 * is spread across many cpus. The actual IO submission is step
324 * two, and the ordered work queue takes care of making sure that
325 * happens in the same order things were put onto the queue by
326 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400327 *
Chris Mason771ed682008-11-06 22:02:51 -0500328 * If this code finds it can't get good compression, it puts an
329 * entry onto the work queue to write the uncompressed bytes. This
330 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300331 * are written in the same order that the flusher thread sent them
332 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400333 */
Chris Mason771ed682008-11-06 22:02:51 -0500334static noinline int compress_file_range(struct inode *inode,
335 struct page *locked_page,
336 u64 start, u64 end,
337 struct async_cow *async_cow,
338 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400339{
340 struct btrfs_root *root = BTRFS_I(inode)->root;
341 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400342 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400343 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400344 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500345 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400346 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400347 struct page **pages = NULL;
348 unsigned long nr_pages;
349 unsigned long nr_pages_ret = 0;
350 unsigned long total_compressed = 0;
351 unsigned long total_in = 0;
352 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500353 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400354 int i;
355 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800356 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400357 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400358
Liu Bo4cb13e52012-03-29 09:57:45 -0400359 /* if this is a small write inside eof, kick off a defrag */
360 if ((end - start + 1) < 16 * 1024 &&
361 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400362 btrfs_add_inode_defrag(NULL, inode);
363
Chris Mason42dc7ba2008-12-15 11:44:56 -0500364 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400365again:
366 will_compress = 0;
367 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
368 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
369
Chris Masonf03d9301f2009-02-04 09:31:06 -0500370 /*
371 * we don't want to send crud past the end of i_size through
372 * compression, that's just a waste of CPU time. So, if the
373 * end of the file is before the start of our current
374 * requested range of bytes, we bail out to the uncompressed
375 * cleanup code that can deal with all of this.
376 *
377 * It isn't really the fastest way to fix things, but this is a
378 * very uncommon corner.
379 */
380 if (actual_end <= start)
381 goto cleanup_and_bail_uncompressed;
382
Chris Masonc8b97812008-10-29 14:49:59 -0400383 total_compressed = actual_end - start;
384
385 /* we want to make sure that amount of ram required to uncompress
386 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500387 * of a compressed extent to 128k. This is a crucial number
388 * because it also controls how easily we can spread reads across
389 * cpus for decompression.
390 *
391 * We also want to make sure the amount of IO required to do
392 * a random read is reasonably small, so we limit the size of
393 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400394 */
395 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000396 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500397 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400398 total_in = 0;
399 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400400
Chris Mason771ed682008-11-06 22:02:51 -0500401 /*
402 * we do compression for mount -o compress and when the
403 * inode has not been flagged as nocompress. This flag can
404 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400405 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200406 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500407 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000408 (BTRFS_I(inode)->force_compress) ||
409 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400410 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400411 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800412 if (!pages) {
413 /* just bail out to the uncompressed code */
414 goto cont;
415 }
Chris Mason179e29e2007-11-01 11:28:41 -0400416
Li Zefan261507a02010-12-17 14:21:50 +0800417 if (BTRFS_I(inode)->force_compress)
418 compress_type = BTRFS_I(inode)->force_compress;
419
Chris Mason4adaa612013-03-26 13:07:00 -0400420 /*
421 * we need to call clear_page_dirty_for_io on each
422 * page in the range. Otherwise applications with the file
423 * mmap'd can wander in and change the page contents while
424 * we are compressing them.
425 *
426 * If the compression fails for any reason, we set the pages
427 * dirty again later on.
428 */
429 extent_range_clear_dirty_for_io(inode, start, end);
430 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800431 ret = btrfs_compress_pages(compress_type,
432 inode->i_mapping, start,
433 total_compressed, pages,
434 nr_pages, &nr_pages_ret,
435 &total_in,
436 &total_compressed,
437 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400438
439 if (!ret) {
440 unsigned long offset = total_compressed &
441 (PAGE_CACHE_SIZE - 1);
442 struct page *page = pages[nr_pages_ret - 1];
443 char *kaddr;
444
445 /* zero the tail end of the last page, we might be
446 * sending it down to disk
447 */
448 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800449 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400450 memset(kaddr + offset, 0,
451 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800452 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400453 }
454 will_compress = 1;
455 }
456 }
Li Zefan560f7d72011-09-08 10:22:01 +0800457cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400458 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400459 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100460 if (IS_ERR(trans)) {
461 ret = PTR_ERR(trans);
462 trans = NULL;
463 goto cleanup_and_out;
464 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400465 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500466
Chris Masonc8b97812008-10-29 14:49:59 -0400467 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500468 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400469 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500470 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400471 */
472 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000473 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400474 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500475 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400476 ret = cow_file_range_inline(trans, root, inode,
477 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000478 total_compressed,
479 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400480 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100481 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500482 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100483 * inline extent creation worked or returned error,
484 * we don't need to create any more async work items.
485 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500486 */
Chris Masonc8b97812008-10-29 14:49:59 -0400487 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400488 &BTRFS_I(inode)->io_tree,
489 start, end, NULL,
490 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400491 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400492 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000493
494 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400495 goto free_pages_out;
496 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000497 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400498 }
499
500 if (will_compress) {
501 /*
502 * we aren't doing an inline extent round the compressed size
503 * up to a block size boundary so the allocator does sane
504 * things
505 */
Qu Wenruofda28322013-02-26 08:10:22 +0000506 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400507
508 /*
509 * one last check to make sure the compression is really a
510 * win, compare the page count read with the blocks on disk
511 */
Qu Wenruofda28322013-02-26 08:10:22 +0000512 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400513 if (total_compressed >= total_in) {
514 will_compress = 0;
515 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400516 num_bytes = total_in;
517 }
518 }
519 if (!will_compress && pages) {
520 /*
521 * the compression code ran but failed to make things smaller,
522 * free any pages it allocated and our page pointer array
523 */
524 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400525 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400526 page_cache_release(pages[i]);
527 }
528 kfree(pages);
529 pages = NULL;
530 total_compressed = 0;
531 nr_pages_ret = 0;
532
533 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500534 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
535 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500536 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500537 }
Chris Masonc8b97812008-10-29 14:49:59 -0400538 }
Chris Mason771ed682008-11-06 22:02:51 -0500539 if (will_compress) {
540 *num_added += 1;
541
542 /* the async work queues will take care of doing actual
543 * allocation on disk for these compressed pages,
544 * and will submit them to the elevator.
545 */
546 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800547 total_compressed, pages, nr_pages_ret,
548 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500549
Yan, Zheng24ae6362010-12-06 07:02:36 +0000550 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500551 start += num_bytes;
552 pages = NULL;
553 cond_resched();
554 goto again;
555 }
556 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500557cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500558 /*
559 * No compression, but we still need to write the pages in
560 * the file we've been given so far. redirty the locked
561 * page if it corresponds to our extent and set things up
562 * for the async work queue to run cow_file_range to do
563 * the normal delalloc dance
564 */
565 if (page_offset(locked_page) >= start &&
566 page_offset(locked_page) <= end) {
567 __set_page_dirty_nobuffers(locked_page);
568 /* unlocked later on in the async handlers */
569 }
Chris Mason4adaa612013-03-26 13:07:00 -0400570 if (redirty)
571 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800572 add_async_extent(async_cow, start, end - start + 1,
573 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500574 *num_added += 1;
575 }
576
577out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100578 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500579
580free_pages_out:
581 for (i = 0; i < nr_pages_ret; i++) {
582 WARN_ON(pages[i]->mapping);
583 page_cache_release(pages[i]);
584 }
Chris Masond3977122009-01-05 21:25:51 -0500585 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500586
587 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100588
589cleanup_and_out:
590 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
591 start, end, NULL,
592 EXTENT_CLEAR_UNLOCK_PAGE |
593 EXTENT_CLEAR_DIRTY |
594 EXTENT_CLEAR_DELALLOC |
595 EXTENT_SET_WRITEBACK |
596 EXTENT_END_WRITEBACK);
597 if (!trans || IS_ERR(trans))
598 btrfs_error(root->fs_info, ret, "Failed to join transaction");
599 else
600 btrfs_abort_transaction(trans, root, ret);
601 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500602}
603
604/*
605 * phase two of compressed writeback. This is the ordered portion
606 * of the code, which only gets called in the order the work was
607 * queued. We walk all the async extents created by compress_file_range
608 * and send them down to the disk.
609 */
610static noinline int submit_compressed_extents(struct inode *inode,
611 struct async_cow *async_cow)
612{
613 struct async_extent *async_extent;
614 u64 alloc_hint = 0;
615 struct btrfs_trans_handle *trans;
616 struct btrfs_key ins;
617 struct extent_map *em;
618 struct btrfs_root *root = BTRFS_I(inode)->root;
619 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
620 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500621 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500622
623 if (list_empty(&async_cow->extents))
624 return 0;
625
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500626again:
Chris Masond3977122009-01-05 21:25:51 -0500627 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500628 async_extent = list_entry(async_cow->extents.next,
629 struct async_extent, list);
630 list_del(&async_extent->list);
631
632 io_tree = &BTRFS_I(inode)->io_tree;
633
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500634retry:
Chris Mason771ed682008-11-06 22:02:51 -0500635 /* did the compression code fall back to uncompressed IO? */
636 if (!async_extent->pages) {
637 int page_started = 0;
638 unsigned long nr_written = 0;
639
640 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000641 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100642 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500643
644 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500645 ret = cow_file_range(inode, async_cow->locked_page,
646 async_extent->start,
647 async_extent->start +
648 async_extent->ram_size - 1,
649 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500650
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100651 /* JDM XXX */
652
Chris Mason771ed682008-11-06 22:02:51 -0500653 /*
654 * if page_started, cow_file_range inserted an
655 * inline extent and took care of all the unlocking
656 * and IO for us. Otherwise, we need to submit
657 * all those pages down to the drive.
658 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500659 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500660 extent_write_locked_range(io_tree,
661 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500662 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500663 async_extent->ram_size - 1,
664 btrfs_get_extent,
665 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500666 else if (ret)
667 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500668 kfree(async_extent);
669 cond_resched();
670 continue;
671 }
672
673 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100674 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500675
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400676 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100677 if (IS_ERR(trans)) {
678 ret = PTR_ERR(trans);
679 } else {
680 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
681 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500682 async_extent->compressed_size,
683 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500684 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600685 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100686 btrfs_abort_transaction(trans, root, ret);
687 btrfs_end_transaction(trans, root);
688 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000689
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500690 if (ret) {
691 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500692
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500693 for (i = 0; i < async_extent->nr_pages; i++) {
694 WARN_ON(async_extent->pages[i]->mapping);
695 page_cache_release(async_extent->pages[i]);
696 }
697 kfree(async_extent->pages);
698 async_extent->nr_pages = 0;
699 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500700
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100701 if (ret == -ENOSPC)
702 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500703 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500704 }
705
Yan, Zhengc2167752009-11-12 09:34:21 +0000706 /*
707 * here we're doing allocation and writeback of the
708 * compressed pages
709 */
710 btrfs_drop_extent_cache(inode, async_extent->start,
711 async_extent->start +
712 async_extent->ram_size - 1, 0);
713
David Sterba172ddd62011-04-21 00:48:27 +0200714 em = alloc_extent_map();
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500715 if (!em)
716 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500717 em->start = async_extent->start;
718 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500719 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400720 em->mod_start = em->start;
721 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500722
723 em->block_start = ins.objectid;
724 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500725 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400726 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500727 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800728 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500729 set_bit(EXTENT_FLAG_PINNED, &em->flags);
730 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400731 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500732
Chris Masond3977122009-01-05 21:25:51 -0500733 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400734 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500735 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400736 if (!ret)
737 list_move(&em->list,
738 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400739 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500740 if (ret != -EEXIST) {
741 free_extent_map(em);
742 break;
743 }
744 btrfs_drop_extent_cache(inode, async_extent->start,
745 async_extent->start +
746 async_extent->ram_size - 1, 0);
747 }
748
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500749 if (ret)
750 goto out_free_reserve;
751
Li Zefan261507a02010-12-17 14:21:50 +0800752 ret = btrfs_add_ordered_extent_compress(inode,
753 async_extent->start,
754 ins.objectid,
755 async_extent->ram_size,
756 ins.offset,
757 BTRFS_ORDERED_COMPRESSED,
758 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500759 if (ret)
760 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500761
Chris Mason771ed682008-11-06 22:02:51 -0500762 /*
763 * clear dirty, set writeback and unlock the pages.
764 */
765 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400766 &BTRFS_I(inode)->io_tree,
767 async_extent->start,
768 async_extent->start +
769 async_extent->ram_size - 1,
770 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
771 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400772 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400773 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500774
775 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500776 async_extent->start,
777 async_extent->ram_size,
778 ins.objectid,
779 ins.offset, async_extent->pages,
780 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500781 alloc_hint = ins.objectid + ins.offset;
782 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500783 if (ret)
784 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500785 cond_resched();
786 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100787 ret = 0;
788out:
789 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500790out_free_reserve:
791 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100792out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500793 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
794 async_extent->start,
795 async_extent->start +
796 async_extent->ram_size - 1,
797 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
798 EXTENT_CLEAR_UNLOCK |
799 EXTENT_CLEAR_DELALLOC |
800 EXTENT_CLEAR_DIRTY |
801 EXTENT_SET_WRITEBACK |
802 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100803 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500804 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500805}
806
Josef Bacik4b46fce2010-05-23 11:00:55 -0400807static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
808 u64 num_bytes)
809{
810 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
811 struct extent_map *em;
812 u64 alloc_hint = 0;
813
814 read_lock(&em_tree->lock);
815 em = search_extent_mapping(em_tree, start, num_bytes);
816 if (em) {
817 /*
818 * if block start isn't an actual block number then find the
819 * first block in this inode and use that as a hint. If that
820 * block is also bogus then just don't worry about it.
821 */
822 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
823 free_extent_map(em);
824 em = search_extent_mapping(em_tree, 0, 0);
825 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
826 alloc_hint = em->block_start;
827 if (em)
828 free_extent_map(em);
829 } else {
830 alloc_hint = em->block_start;
831 free_extent_map(em);
832 }
833 }
834 read_unlock(&em_tree->lock);
835
836 return alloc_hint;
837}
838
Chris Mason771ed682008-11-06 22:02:51 -0500839/*
840 * when extent_io.c finds a delayed allocation range in the file,
841 * the call backs end up in this code. The basic idea is to
842 * allocate extents on disk for the range, and create ordered data structs
843 * in ram to track those extents.
844 *
845 * locked_page is the page that writepage had locked already. We use
846 * it to make sure we don't do extra locks or unlocks.
847 *
848 * *page_started is set to one if we unlock locked_page and do everything
849 * required to start IO on it. It may be clean and already done with
850 * IO when we return.
851 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000852static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
853 struct inode *inode,
854 struct btrfs_root *root,
855 struct page *locked_page,
856 u64 start, u64 end, int *page_started,
857 unsigned long *nr_written,
858 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500859{
Chris Mason771ed682008-11-06 22:02:51 -0500860 u64 alloc_hint = 0;
861 u64 num_bytes;
862 unsigned long ram_size;
863 u64 disk_num_bytes;
864 u64 cur_alloc_size;
865 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500866 struct btrfs_key ins;
867 struct extent_map *em;
868 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
869 int ret = 0;
870
Liu Bo83eea1f2012-07-10 05:28:39 -0600871 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500872
Qu Wenruofda28322013-02-26 08:10:22 +0000873 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500874 num_bytes = max(blocksize, num_bytes);
875 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500876
Chris Mason4cb53002011-05-24 15:35:30 -0400877 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400878 if (num_bytes < 64 * 1024 &&
879 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400880 btrfs_add_inode_defrag(trans, inode);
881
Chris Mason771ed682008-11-06 22:02:51 -0500882 if (start == 0) {
883 /* lets try to make an inline extent */
884 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000885 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500886 if (ret == 0) {
887 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400888 &BTRFS_I(inode)->io_tree,
889 start, end, NULL,
890 EXTENT_CLEAR_UNLOCK_PAGE |
891 EXTENT_CLEAR_UNLOCK |
892 EXTENT_CLEAR_DELALLOC |
893 EXTENT_CLEAR_DIRTY |
894 EXTENT_SET_WRITEBACK |
895 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000896
Chris Mason771ed682008-11-06 22:02:51 -0500897 *nr_written = *nr_written +
898 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
899 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500900 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100901 } else if (ret < 0) {
902 btrfs_abort_transaction(trans, root, ret);
903 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500904 }
905 }
Chris Masonc8b97812008-10-29 14:49:59 -0400906
907 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200908 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400909
Josef Bacik4b46fce2010-05-23 11:00:55 -0400910 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400911 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400912
Chris Masond3977122009-01-05 21:25:51 -0500913 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400914 unsigned long op;
915
Josef Bacik287a0ab2010-03-19 18:07:23 +0000916 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400917 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500918 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500919 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100920 if (ret < 0) {
921 btrfs_abort_transaction(trans, root, ret);
922 goto out_unlock;
923 }
Chris Masond3977122009-01-05 21:25:51 -0500924
David Sterba172ddd62011-04-21 00:48:27 +0200925 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100926 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400927 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500928 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500929 ram_size = ins.offset;
930 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400931 em->mod_start = em->start;
932 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400933
Chris Masone6dcd2d2008-07-17 12:53:50 -0400934 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400935 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500936 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400937 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400938 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400939 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400940 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400941
Chris Masond3977122009-01-05 21:25:51 -0500942 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400943 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400944 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400945 if (!ret)
946 list_move(&em->list,
947 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400948 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400949 if (ret != -EEXIST) {
950 free_extent_map(em);
951 break;
952 }
953 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400954 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400955 }
956
Chris Mason98d20f62008-04-14 09:46:10 -0400957 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400958 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500959 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100960 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400961
Yan Zheng17d217f2008-12-12 10:03:38 -0500962 if (root->root_key.objectid ==
963 BTRFS_DATA_RELOC_TREE_OBJECTID) {
964 ret = btrfs_reloc_clone_csums(inode, start,
965 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966 if (ret) {
967 btrfs_abort_transaction(trans, root, ret);
968 goto out_unlock;
969 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500970 }
971
Chris Masond3977122009-01-05 21:25:51 -0500972 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400973 break;
Chris Masond3977122009-01-05 21:25:51 -0500974
Chris Masonc8b97812008-10-29 14:49:59 -0400975 /* we're not doing compressed IO, don't unlock the first
976 * page (which the caller expects to stay locked), don't
977 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400978 *
979 * Do set the Private2 bit so we know this page was properly
980 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400981 */
Chris Masona791e352009-10-08 11:27:10 -0400982 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
983 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
984 EXTENT_SET_PRIVATE2;
985
Chris Masonc8b97812008-10-29 14:49:59 -0400986 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
987 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400988 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400989 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500990 num_bytes -= cur_alloc_size;
991 alloc_hint = ins.objectid + ins.offset;
992 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400993 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500995 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000996
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100997out_unlock:
998 extent_clear_unlock_delalloc(inode,
999 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -04001000 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001001 EXTENT_CLEAR_UNLOCK_PAGE |
1002 EXTENT_CLEAR_UNLOCK |
1003 EXTENT_CLEAR_DELALLOC |
1004 EXTENT_CLEAR_DIRTY |
1005 EXTENT_SET_WRITEBACK |
1006 EXTENT_END_WRITEBACK);
1007
1008 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001009}
Chris Masonc8b97812008-10-29 14:49:59 -04001010
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001011static noinline int cow_file_range(struct inode *inode,
1012 struct page *locked_page,
1013 u64 start, u64 end, int *page_started,
1014 unsigned long *nr_written,
1015 int unlock)
1016{
1017 struct btrfs_trans_handle *trans;
1018 struct btrfs_root *root = BTRFS_I(inode)->root;
1019 int ret;
1020
1021 trans = btrfs_join_transaction(root);
1022 if (IS_ERR(trans)) {
1023 extent_clear_unlock_delalloc(inode,
1024 &BTRFS_I(inode)->io_tree,
1025 start, end, locked_page,
1026 EXTENT_CLEAR_UNLOCK_PAGE |
1027 EXTENT_CLEAR_UNLOCK |
1028 EXTENT_CLEAR_DELALLOC |
1029 EXTENT_CLEAR_DIRTY |
1030 EXTENT_SET_WRITEBACK |
1031 EXTENT_END_WRITEBACK);
1032 return PTR_ERR(trans);
1033 }
1034 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1035
1036 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1037 page_started, nr_written, unlock);
1038
1039 btrfs_end_transaction(trans, root);
1040
1041 return ret;
1042}
1043
Chris Mason771ed682008-11-06 22:02:51 -05001044/*
1045 * work queue call back to started compression on a file and pages
1046 */
1047static noinline void async_cow_start(struct btrfs_work *work)
1048{
1049 struct async_cow *async_cow;
1050 int num_added = 0;
1051 async_cow = container_of(work, struct async_cow, work);
1052
1053 compress_file_range(async_cow->inode, async_cow->locked_page,
1054 async_cow->start, async_cow->end, async_cow,
1055 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001056 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001057 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001058 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001059 }
Chris Mason771ed682008-11-06 22:02:51 -05001060}
1061
1062/*
1063 * work queue call back to submit previously compressed pages
1064 */
1065static noinline void async_cow_submit(struct btrfs_work *work)
1066{
1067 struct async_cow *async_cow;
1068 struct btrfs_root *root;
1069 unsigned long nr_pages;
1070
1071 async_cow = container_of(work, struct async_cow, work);
1072
1073 root = async_cow->root;
1074 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1075 PAGE_CACHE_SHIFT;
1076
Josef Bacik66657b32012-08-01 15:36:24 -04001077 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001078 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001079 waitqueue_active(&root->fs_info->async_submit_wait))
1080 wake_up(&root->fs_info->async_submit_wait);
1081
Chris Masond3977122009-01-05 21:25:51 -05001082 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001083 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001084}
Chris Masonc8b97812008-10-29 14:49:59 -04001085
Chris Mason771ed682008-11-06 22:02:51 -05001086static noinline void async_cow_free(struct btrfs_work *work)
1087{
1088 struct async_cow *async_cow;
1089 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001090 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001091 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001092 kfree(async_cow);
1093}
1094
1095static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1096 u64 start, u64 end, int *page_started,
1097 unsigned long *nr_written)
1098{
1099 struct async_cow *async_cow;
1100 struct btrfs_root *root = BTRFS_I(inode)->root;
1101 unsigned long nr_pages;
1102 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001103 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001104
Chris Masona3429ab2009-10-08 12:30:20 -04001105 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1106 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001107 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001108 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001109 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001110 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001111 async_cow->root = root;
1112 async_cow->locked_page = locked_page;
1113 async_cow->start = start;
1114
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001115 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001116 cur_end = end;
1117 else
1118 cur_end = min(end, start + 512 * 1024 - 1);
1119
1120 async_cow->end = cur_end;
1121 INIT_LIST_HEAD(&async_cow->extents);
1122
1123 async_cow->work.func = async_cow_start;
1124 async_cow->work.ordered_func = async_cow_submit;
1125 async_cow->work.ordered_free = async_cow_free;
1126 async_cow->work.flags = 0;
1127
Chris Mason771ed682008-11-06 22:02:51 -05001128 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1129 PAGE_CACHE_SHIFT;
1130 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1131
1132 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1133 &async_cow->work);
1134
1135 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1136 wait_event(root->fs_info->async_submit_wait,
1137 (atomic_read(&root->fs_info->async_delalloc_pages) <
1138 limit));
1139 }
1140
Chris Masond3977122009-01-05 21:25:51 -05001141 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001142 atomic_read(&root->fs_info->async_delalloc_pages)) {
1143 wait_event(root->fs_info->async_submit_wait,
1144 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1145 0));
1146 }
1147
1148 *nr_written += nr_pages;
1149 start = cur_end + 1;
1150 }
1151 *page_started = 1;
1152 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001153}
1154
Chris Masond3977122009-01-05 21:25:51 -05001155static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001156 u64 bytenr, u64 num_bytes)
1157{
1158 int ret;
1159 struct btrfs_ordered_sum *sums;
1160 LIST_HEAD(list);
1161
Yan Zheng07d400a2009-01-06 11:42:00 -05001162 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001163 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001164 if (ret == 0 && list_empty(&list))
1165 return 0;
1166
1167 while (!list_empty(&list)) {
1168 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1169 list_del(&sums->list);
1170 kfree(sums);
1171 }
1172 return 1;
1173}
1174
Chris Masond352ac62008-09-29 15:18:18 -04001175/*
1176 * when nowcow writeback call back. This checks for snapshots or COW copies
1177 * of the extents that exist in the file, and COWs the file as required.
1178 *
1179 * If no cow copies or snapshots exist, we write directly to the existing
1180 * blocks on disk
1181 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001182static noinline int run_delalloc_nocow(struct inode *inode,
1183 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001184 u64 start, u64 end, int *page_started, int force,
1185 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001186{
Chris Masonbe20aa92007-12-17 20:14:01 -05001187 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001188 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001189 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001190 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001191 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001192 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001193 u64 cow_start;
1194 u64 cur_offset;
1195 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001196 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001197 u64 disk_bytenr;
1198 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001199 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001200 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001202 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001203 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001204 int nocow;
1205 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001206 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001207 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001208
1209 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001210 if (!path) {
1211 extent_clear_unlock_delalloc(inode,
1212 &BTRFS_I(inode)->io_tree,
1213 start, end, locked_page,
1214 EXTENT_CLEAR_UNLOCK_PAGE |
1215 EXTENT_CLEAR_UNLOCK |
1216 EXTENT_CLEAR_DELALLOC |
1217 EXTENT_CLEAR_DIRTY |
1218 EXTENT_SET_WRITEBACK |
1219 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001220 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001221 }
Li Zefan82d59022011-04-20 10:33:24 +08001222
Liu Bo83eea1f2012-07-10 05:28:39 -06001223 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001224
1225 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001226 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001227 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001228 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001229
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001230 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001231 extent_clear_unlock_delalloc(inode,
1232 &BTRFS_I(inode)->io_tree,
1233 start, end, locked_page,
1234 EXTENT_CLEAR_UNLOCK_PAGE |
1235 EXTENT_CLEAR_UNLOCK |
1236 EXTENT_CLEAR_DELALLOC |
1237 EXTENT_CLEAR_DIRTY |
1238 EXTENT_SET_WRITEBACK |
1239 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001240 btrfs_free_path(path);
1241 return PTR_ERR(trans);
1242 }
1243
Josef Bacik74b21072011-04-13 12:02:53 -04001244 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001245
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 cow_start = (u64)-1;
1247 cur_offset = start;
1248 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001249 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001250 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001251 if (ret < 0) {
1252 btrfs_abort_transaction(trans, root, ret);
1253 goto error;
1254 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001255 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1256 leaf = path->nodes[0];
1257 btrfs_item_key_to_cpu(leaf, &found_key,
1258 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001259 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001260 found_key.type == BTRFS_EXTENT_DATA_KEY)
1261 path->slots[0]--;
1262 }
1263 check_prev = 0;
1264next_slot:
1265 leaf = path->nodes[0];
1266 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1267 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001268 if (ret < 0) {
1269 btrfs_abort_transaction(trans, root, ret);
1270 goto error;
1271 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 if (ret > 0)
1273 break;
1274 leaf = path->nodes[0];
1275 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001276
Yan Zheng80ff3852008-10-30 14:20:02 -04001277 nocow = 0;
1278 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001279 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001281
Li Zefan33345d012011-04-20 10:31:50 +08001282 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1284 found_key.offset > end)
1285 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001286
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 if (found_key.offset > cur_offset) {
1288 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001289 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 goto out_check;
1291 }
Chris Masonc31f8832008-01-08 15:46:31 -05001292
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 fi = btrfs_item_ptr(leaf, path->slots[0],
1294 struct btrfs_file_extent_item);
1295 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001296
Josef Bacikcc95bef2013-04-04 14:31:27 -04001297 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001298 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1299 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001301 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 extent_end = found_key.offset +
1303 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001304 disk_num_bytes =
1305 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 if (extent_end <= start) {
1307 path->slots[0]++;
1308 goto next_slot;
1309 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001310 if (disk_bytenr == 0)
1311 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 if (btrfs_file_extent_compression(leaf, fi) ||
1313 btrfs_file_extent_encryption(leaf, fi) ||
1314 btrfs_file_extent_other_encoding(leaf, fi))
1315 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001316 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1317 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001318 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001320 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001321 found_key.offset -
1322 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001323 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001324 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001325 disk_bytenr += cur_offset - found_key.offset;
1326 num_bytes = min(end + 1, extent_end) - cur_offset;
1327 /*
1328 * force cow if csum exists in the range.
1329 * this ensure that csum for a given extent are
1330 * either valid or do not exist.
1331 */
1332 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1333 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001334 nocow = 1;
1335 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1336 extent_end = found_key.offset +
1337 btrfs_file_extent_inline_len(leaf, fi);
1338 extent_end = ALIGN(extent_end, root->sectorsize);
1339 } else {
1340 BUG_ON(1);
1341 }
1342out_check:
1343 if (extent_end <= start) {
1344 path->slots[0]++;
1345 goto next_slot;
1346 }
1347 if (!nocow) {
1348 if (cow_start == (u64)-1)
1349 cow_start = cur_offset;
1350 cur_offset = extent_end;
1351 if (cur_offset > end)
1352 break;
1353 path->slots[0]++;
1354 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001355 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001356
David Sterbab3b4aa72011-04-21 01:20:15 +02001357 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001359 ret = __cow_file_range(trans, inode, root, locked_page,
1360 cow_start, found_key.offset - 1,
1361 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001362 if (ret) {
1363 btrfs_abort_transaction(trans, root, ret);
1364 goto error;
1365 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001367 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001368
Yan Zhengd899e052008-10-30 14:25:28 -04001369 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1370 struct extent_map *em;
1371 struct extent_map_tree *em_tree;
1372 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001373 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001374 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001375 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001376 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001377 em->len = num_bytes;
1378 em->block_len = num_bytes;
1379 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001380 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001381 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001382 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001383 em->mod_start = em->start;
1384 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001385 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001386 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001387 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001388 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001389 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001390 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001391 if (!ret)
1392 list_move(&em->list,
1393 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001394 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001395 if (ret != -EEXIST) {
1396 free_extent_map(em);
1397 break;
1398 }
1399 btrfs_drop_extent_cache(inode, em->start,
1400 em->start + em->len - 1, 0);
1401 }
1402 type = BTRFS_ORDERED_PREALLOC;
1403 } else {
1404 type = BTRFS_ORDERED_NOCOW;
1405 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001406
1407 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001408 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001409 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001410
Yan, Zhengefa56462010-05-16 10:49:59 -04001411 if (root->root_key.objectid ==
1412 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1413 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1414 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001415 if (ret) {
1416 btrfs_abort_transaction(trans, root, ret);
1417 goto error;
1418 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001419 }
1420
Yan Zhengd899e052008-10-30 14:25:28 -04001421 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001422 cur_offset, cur_offset + num_bytes - 1,
1423 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1424 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1425 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001426 cur_offset = extent_end;
1427 if (cur_offset > end)
1428 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001429 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001430 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001431
Josef Bacik17ca04a2012-05-31 15:58:55 -04001432 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001433 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001434 cur_offset = end;
1435 }
1436
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001438 ret = __cow_file_range(trans, inode, root, locked_page,
1439 cow_start, end,
1440 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001441 if (ret) {
1442 btrfs_abort_transaction(trans, root, ret);
1443 goto error;
1444 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001445 }
1446
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001447error:
Miao Xiea698d0752012-09-20 01:51:59 -06001448 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001449 if (!ret)
1450 ret = err;
1451
Josef Bacik17ca04a2012-05-31 15:58:55 -04001452 if (ret && cur_offset < end)
1453 extent_clear_unlock_delalloc(inode,
1454 &BTRFS_I(inode)->io_tree,
1455 cur_offset, end, locked_page,
1456 EXTENT_CLEAR_UNLOCK_PAGE |
1457 EXTENT_CLEAR_UNLOCK |
1458 EXTENT_CLEAR_DELALLOC |
1459 EXTENT_CLEAR_DIRTY |
1460 EXTENT_SET_WRITEBACK |
1461 EXTENT_END_WRITEBACK);
1462
Yan Zheng7ea394f2008-08-05 13:05:02 -04001463 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001464 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001465}
1466
Chris Masond352ac62008-09-29 15:18:18 -04001467/*
1468 * extent_io.c call back to do delayed allocation processing
1469 */
Chris Masonc8b97812008-10-29 14:49:59 -04001470static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001471 u64 start, u64 end, int *page_started,
1472 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001473{
Chris Masonbe20aa92007-12-17 20:14:01 -05001474 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001475 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001476
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001477 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001478 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001479 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001480 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001481 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001482 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001483 } else if (!btrfs_test_opt(root, COMPRESS) &&
1484 !(BTRFS_I(inode)->force_compress) &&
1485 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001486 ret = cow_file_range(inode, locked_page, start, end,
1487 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001488 } else {
1489 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1490 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001491 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001492 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001493 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001494 return ret;
1495}
1496
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001497static void btrfs_split_extent_hook(struct inode *inode,
1498 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001499{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001500 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001501 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001502 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001503
Josef Bacik9e0baf62011-07-15 15:16:44 +00001504 spin_lock(&BTRFS_I(inode)->lock);
1505 BTRFS_I(inode)->outstanding_extents++;
1506 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001507}
1508
1509/*
1510 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1511 * extents so we can keep track of new extents that are just merged onto old
1512 * extents, such as when we are doing sequential writes, so we can properly
1513 * account for the metadata space we'll need.
1514 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001515static void btrfs_merge_extent_hook(struct inode *inode,
1516 struct extent_state *new,
1517 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001518{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001519 /* not delalloc, ignore it */
1520 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001521 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001522
Josef Bacik9e0baf62011-07-15 15:16:44 +00001523 spin_lock(&BTRFS_I(inode)->lock);
1524 BTRFS_I(inode)->outstanding_extents--;
1525 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001526}
1527
Chris Masond352ac62008-09-29 15:18:18 -04001528/*
1529 * extent_io.c set_bit_hook, used to track delayed allocation
1530 * bytes in this file, and to maintain the list of inodes that
1531 * have pending delalloc work to be done.
1532 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001533static void btrfs_set_bit_hook(struct inode *inode,
1534 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001535{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001536
Chris Mason75eff682008-12-15 15:54:40 -05001537 /*
1538 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001539 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001540 * bit, which is only set or cleared with irqs on
1541 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001542 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001543 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001544 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001545 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001546
Josef Bacik9e0baf62011-07-15 15:16:44 +00001547 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001548 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001549 } else {
1550 spin_lock(&BTRFS_I(inode)->lock);
1551 BTRFS_I(inode)->outstanding_extents++;
1552 spin_unlock(&BTRFS_I(inode)->lock);
1553 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001554
Miao Xie963d6782013-01-29 10:10:51 +00001555 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1556 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001557 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001558 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001559 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1560 &BTRFS_I(inode)->runtime_flags)) {
1561 spin_lock(&root->fs_info->delalloc_lock);
1562 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1563 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1564 &root->fs_info->delalloc_inodes);
1565 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1566 &BTRFS_I(inode)->runtime_flags);
1567 }
1568 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001569 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001570 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001571 }
Chris Mason291d6732008-01-29 15:55:23 -05001572}
1573
Chris Masond352ac62008-09-29 15:18:18 -04001574/*
1575 * extent_io.c clear_bit_hook, see set_bit_hook for why
1576 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001577static void btrfs_clear_bit_hook(struct inode *inode,
1578 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001579{
Chris Mason75eff682008-12-15 15:54:40 -05001580 /*
1581 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001582 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001583 * bit, which is only set or cleared with irqs on
1584 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001585 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001586 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001587 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001588 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001589
Josef Bacik9e0baf62011-07-15 15:16:44 +00001590 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001591 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001592 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1593 spin_lock(&BTRFS_I(inode)->lock);
1594 BTRFS_I(inode)->outstanding_extents--;
1595 spin_unlock(&BTRFS_I(inode)->lock);
1596 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001597
1598 if (*bits & EXTENT_DO_ACCOUNTING)
1599 btrfs_delalloc_release_metadata(inode, len);
1600
Josef Bacik0cb59c92010-07-02 12:14:14 -04001601 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1602 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001603 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001604
Miao Xie963d6782013-01-29 10:10:51 +00001605 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1606 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001607 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001608 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001609 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001610 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1611 &BTRFS_I(inode)->runtime_flags)) {
1612 spin_lock(&root->fs_info->delalloc_lock);
1613 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1614 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1615 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1616 &BTRFS_I(inode)->runtime_flags);
1617 }
1618 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001619 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001620 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001621 }
Chris Mason291d6732008-01-29 15:55:23 -05001622}
1623
Chris Masond352ac62008-09-29 15:18:18 -04001624/*
1625 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1626 * we don't create bios that span stripes or chunks
1627 */
David Woodhouse64a16702009-07-15 23:29:37 +01001628int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001629 size_t size, struct bio *bio,
1630 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001631{
1632 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001633 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001634 u64 length = 0;
1635 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001636 int ret;
1637
Chris Mason771ed682008-11-06 22:02:51 -05001638 if (bio_flags & EXTENT_BIO_COMPRESSED)
1639 return 0;
1640
Chris Masonf2d8d742008-04-21 10:03:05 -04001641 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001642 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001643 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001644 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001645 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001646 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001647 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001648 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001649 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001650}
1651
Chris Masond352ac62008-09-29 15:18:18 -04001652/*
1653 * in order to insert checksums into the metadata in large chunks,
1654 * we wait until bio submission time. All the pages in the bio are
1655 * checksummed and sums are attached onto the ordered extent record.
1656 *
1657 * At IO completion time the cums attached on the ordered extent record
1658 * are inserted into the btree
1659 */
Chris Masond3977122009-01-05 21:25:51 -05001660static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1661 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001662 unsigned long bio_flags,
1663 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001664{
Chris Mason065631f2008-02-20 12:07:25 -05001665 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001666 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001667
Chris Masond20f7042008-12-08 16:58:54 -05001668 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001669 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001670 return 0;
1671}
Chris Masone0156402008-04-16 11:15:20 -04001672
Chris Mason4a69a412008-11-06 22:03:00 -05001673/*
1674 * in order to insert checksums into the metadata in large chunks,
1675 * we wait until bio submission time. All the pages in the bio are
1676 * checksummed and sums are attached onto the ordered extent record.
1677 *
1678 * At IO completion time the cums attached on the ordered extent record
1679 * are inserted into the btree
1680 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001681static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001682 int mirror_num, unsigned long bio_flags,
1683 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001684{
1685 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001686 int ret;
1687
1688 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1689 if (ret)
1690 bio_endio(bio, ret);
1691 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001692}
1693
Chris Masond352ac62008-09-29 15:18:18 -04001694/*
Chris Masoncad321a2008-12-17 14:51:42 -05001695 * extent_io.c submission hook. This does the right thing for csum calculation
1696 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001697 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001698static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001699 int mirror_num, unsigned long bio_flags,
1700 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001701{
1702 struct btrfs_root *root = BTRFS_I(inode)->root;
1703 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001704 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001705 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001706 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001707
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001708 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001709
Liu Bo83eea1f2012-07-10 05:28:39 -06001710 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001711 metadata = 2;
1712
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001713 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001714 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1715 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001716 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001717
Chris Masond20f7042008-12-08 16:58:54 -05001718 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001719 ret = btrfs_submit_compressed_read(inode, bio,
1720 mirror_num,
1721 bio_flags);
1722 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001723 } else if (!skip_sum) {
1724 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1725 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001726 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001727 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001728 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001729 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001730 /* csum items have already been cloned */
1731 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1732 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001733 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001734 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001735 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001736 bio_flags, bio_offset,
1737 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001738 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001739 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001740 } else if (!skip_sum) {
1741 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1742 if (ret)
1743 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001744 }
1745
Chris Mason0b86a832008-03-24 15:01:56 -04001746mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001747 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1748
1749out:
1750 if (ret < 0)
1751 bio_endio(bio, ret);
1752 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001753}
Chris Mason6885f302008-02-20 16:11:05 -05001754
Chris Masond352ac62008-09-29 15:18:18 -04001755/*
1756 * given a list of ordered sums record them in the inode. This happens
1757 * at IO completion time based on sums calculated at bio submission time.
1758 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001759static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001760 struct inode *inode, u64 file_offset,
1761 struct list_head *list)
1762{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001763 struct btrfs_ordered_sum *sum;
1764
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001765 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001766 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001767 btrfs_csum_file_blocks(trans,
1768 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001769 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001770 }
1771 return 0;
1772}
1773
Josef Bacik2ac55d42010-02-03 19:33:23 +00001774int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1775 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001776{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001777 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001778 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001779 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001780}
1781
Chris Masond352ac62008-09-29 15:18:18 -04001782/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001783struct btrfs_writepage_fixup {
1784 struct page *page;
1785 struct btrfs_work work;
1786};
1787
Christoph Hellwigb2950862008-12-02 09:54:17 -05001788static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001789{
1790 struct btrfs_writepage_fixup *fixup;
1791 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001792 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001793 struct page *page;
1794 struct inode *inode;
1795 u64 page_start;
1796 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001797 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001798
1799 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1800 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001801again:
Chris Mason247e7432008-07-17 12:53:51 -04001802 lock_page(page);
1803 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1804 ClearPageChecked(page);
1805 goto out_page;
1806 }
1807
1808 inode = page->mapping->host;
1809 page_start = page_offset(page);
1810 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1811
Josef Bacik2ac55d42010-02-03 19:33:23 +00001812 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001813 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001814
1815 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001816 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001817 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001818
1819 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1820 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001821 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1822 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001823 unlock_page(page);
1824 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001825 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001826 goto again;
1827 }
Chris Mason247e7432008-07-17 12:53:51 -04001828
Jeff Mahoney87826df2012-02-15 16:23:57 +01001829 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1830 if (ret) {
1831 mapping_set_error(page->mapping, ret);
1832 end_extent_writepage(page, ret, page_start, page_end);
1833 ClearPageChecked(page);
1834 goto out;
1835 }
1836
Josef Bacik2ac55d42010-02-03 19:33:23 +00001837 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001838 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001839 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001840out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001841 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1842 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001843out_page:
1844 unlock_page(page);
1845 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001846 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001847}
1848
1849/*
1850 * There are a few paths in the higher layers of the kernel that directly
1851 * set the page dirty bit without asking the filesystem if it is a
1852 * good idea. This causes problems because we want to make sure COW
1853 * properly happens and the data=ordered rules are followed.
1854 *
Chris Masonc8b97812008-10-29 14:49:59 -04001855 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001856 * hasn't been properly setup for IO. We kick off an async process
1857 * to fix it up. The async helper will wait for ordered extents, set
1858 * the delalloc bit and make it safe to write the page.
1859 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001860static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001861{
1862 struct inode *inode = page->mapping->host;
1863 struct btrfs_writepage_fixup *fixup;
1864 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001865
Chris Mason8b62b722009-09-02 16:53:46 -04001866 /* this page is properly in the ordered list */
1867 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001868 return 0;
1869
1870 if (PageChecked(page))
1871 return -EAGAIN;
1872
1873 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1874 if (!fixup)
1875 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001876
Chris Mason247e7432008-07-17 12:53:51 -04001877 SetPageChecked(page);
1878 page_cache_get(page);
1879 fixup->work.func = btrfs_writepage_fixup_worker;
1880 fixup->page = page;
1881 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001882 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001883}
1884
Yan Zhengd899e052008-10-30 14:25:28 -04001885static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1886 struct inode *inode, u64 file_pos,
1887 u64 disk_bytenr, u64 disk_num_bytes,
1888 u64 num_bytes, u64 ram_bytes,
1889 u8 compression, u8 encryption,
1890 u16 other_encoding, int extent_type)
1891{
1892 struct btrfs_root *root = BTRFS_I(inode)->root;
1893 struct btrfs_file_extent_item *fi;
1894 struct btrfs_path *path;
1895 struct extent_buffer *leaf;
1896 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001897 int ret;
1898
1899 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001900 if (!path)
1901 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001902
Chris Masonb9473432009-03-13 11:00:37 -04001903 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001904
1905 /*
1906 * we may be replacing one extent in the tree with another.
1907 * The new extent is pinned in the extent map, and we don't want
1908 * to drop it from the cache until it is completely in the btree.
1909 *
1910 * So, tell btrfs_drop_extents to leave this extent in the cache.
1911 * the caller is expected to unpin it and allow it to be merged
1912 * with the others.
1913 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001914 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001915 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001916 if (ret)
1917 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001918
Li Zefan33345d012011-04-20 10:31:50 +08001919 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001920 ins.offset = file_pos;
1921 ins.type = BTRFS_EXTENT_DATA_KEY;
1922 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001923 if (ret)
1924 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001925 leaf = path->nodes[0];
1926 fi = btrfs_item_ptr(leaf, path->slots[0],
1927 struct btrfs_file_extent_item);
1928 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1929 btrfs_set_file_extent_type(leaf, fi, extent_type);
1930 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1931 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1932 btrfs_set_file_extent_offset(leaf, fi, 0);
1933 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1934 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1935 btrfs_set_file_extent_compression(leaf, fi, compression);
1936 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1937 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001938
Yan Zhengd899e052008-10-30 14:25:28 -04001939 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001940 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001941
1942 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001943
1944 ins.objectid = disk_bytenr;
1945 ins.offset = disk_num_bytes;
1946 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001947 ret = btrfs_alloc_reserved_file_extent(trans, root,
1948 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001949 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001950out:
Yan Zhengd899e052008-10-30 14:25:28 -04001951 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001952
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001953 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001954}
1955
Liu Bo38c227d2013-01-29 03:18:40 +00001956/* snapshot-aware defrag */
1957struct sa_defrag_extent_backref {
1958 struct rb_node node;
1959 struct old_sa_defrag_extent *old;
1960 u64 root_id;
1961 u64 inum;
1962 u64 file_pos;
1963 u64 extent_offset;
1964 u64 num_bytes;
1965 u64 generation;
1966};
1967
1968struct old_sa_defrag_extent {
1969 struct list_head list;
1970 struct new_sa_defrag_extent *new;
1971
1972 u64 extent_offset;
1973 u64 bytenr;
1974 u64 offset;
1975 u64 len;
1976 int count;
1977};
1978
1979struct new_sa_defrag_extent {
1980 struct rb_root root;
1981 struct list_head head;
1982 struct btrfs_path *path;
1983 struct inode *inode;
1984 u64 file_pos;
1985 u64 len;
1986 u64 bytenr;
1987 u64 disk_len;
1988 u8 compress_type;
1989};
1990
1991static int backref_comp(struct sa_defrag_extent_backref *b1,
1992 struct sa_defrag_extent_backref *b2)
1993{
1994 if (b1->root_id < b2->root_id)
1995 return -1;
1996 else if (b1->root_id > b2->root_id)
1997 return 1;
1998
1999 if (b1->inum < b2->inum)
2000 return -1;
2001 else if (b1->inum > b2->inum)
2002 return 1;
2003
2004 if (b1->file_pos < b2->file_pos)
2005 return -1;
2006 else if (b1->file_pos > b2->file_pos)
2007 return 1;
2008
2009 /*
2010 * [------------------------------] ===> (a range of space)
2011 * |<--->| |<---->| =============> (fs/file tree A)
2012 * |<---------------------------->| ===> (fs/file tree B)
2013 *
2014 * A range of space can refer to two file extents in one tree while
2015 * refer to only one file extent in another tree.
2016 *
2017 * So we may process a disk offset more than one time(two extents in A)
2018 * and locate at the same extent(one extent in B), then insert two same
2019 * backrefs(both refer to the extent in B).
2020 */
2021 return 0;
2022}
2023
2024static void backref_insert(struct rb_root *root,
2025 struct sa_defrag_extent_backref *backref)
2026{
2027 struct rb_node **p = &root->rb_node;
2028 struct rb_node *parent = NULL;
2029 struct sa_defrag_extent_backref *entry;
2030 int ret;
2031
2032 while (*p) {
2033 parent = *p;
2034 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2035
2036 ret = backref_comp(backref, entry);
2037 if (ret < 0)
2038 p = &(*p)->rb_left;
2039 else
2040 p = &(*p)->rb_right;
2041 }
2042
2043 rb_link_node(&backref->node, parent, p);
2044 rb_insert_color(&backref->node, root);
2045}
2046
2047/*
2048 * Note the backref might has changed, and in this case we just return 0.
2049 */
2050static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2051 void *ctx)
2052{
2053 struct btrfs_file_extent_item *extent;
2054 struct btrfs_fs_info *fs_info;
2055 struct old_sa_defrag_extent *old = ctx;
2056 struct new_sa_defrag_extent *new = old->new;
2057 struct btrfs_path *path = new->path;
2058 struct btrfs_key key;
2059 struct btrfs_root *root;
2060 struct sa_defrag_extent_backref *backref;
2061 struct extent_buffer *leaf;
2062 struct inode *inode = new->inode;
2063 int slot;
2064 int ret;
2065 u64 extent_offset;
2066 u64 num_bytes;
2067
2068 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2069 inum == btrfs_ino(inode))
2070 return 0;
2071
2072 key.objectid = root_id;
2073 key.type = BTRFS_ROOT_ITEM_KEY;
2074 key.offset = (u64)-1;
2075
2076 fs_info = BTRFS_I(inode)->root->fs_info;
2077 root = btrfs_read_fs_root_no_name(fs_info, &key);
2078 if (IS_ERR(root)) {
2079 if (PTR_ERR(root) == -ENOENT)
2080 return 0;
2081 WARN_ON(1);
2082 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2083 inum, offset, root_id);
2084 return PTR_ERR(root);
2085 }
2086
2087 key.objectid = inum;
2088 key.type = BTRFS_EXTENT_DATA_KEY;
2089 if (offset > (u64)-1 << 32)
2090 key.offset = 0;
2091 else
2092 key.offset = offset;
2093
2094 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2095 if (ret < 0) {
2096 WARN_ON(1);
2097 return ret;
2098 }
2099
2100 while (1) {
2101 cond_resched();
2102
2103 leaf = path->nodes[0];
2104 slot = path->slots[0];
2105
2106 if (slot >= btrfs_header_nritems(leaf)) {
2107 ret = btrfs_next_leaf(root, path);
2108 if (ret < 0) {
2109 goto out;
2110 } else if (ret > 0) {
2111 ret = 0;
2112 goto out;
2113 }
2114 continue;
2115 }
2116
2117 path->slots[0]++;
2118
2119 btrfs_item_key_to_cpu(leaf, &key, slot);
2120
2121 if (key.objectid > inum)
2122 goto out;
2123
2124 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2125 continue;
2126
2127 extent = btrfs_item_ptr(leaf, slot,
2128 struct btrfs_file_extent_item);
2129
2130 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2131 continue;
2132
2133 extent_offset = btrfs_file_extent_offset(leaf, extent);
2134 if (key.offset - extent_offset != offset)
2135 continue;
2136
2137 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2138 if (extent_offset >= old->extent_offset + old->offset +
2139 old->len || extent_offset + num_bytes <=
2140 old->extent_offset + old->offset)
2141 continue;
2142
2143 break;
2144 }
2145
2146 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2147 if (!backref) {
2148 ret = -ENOENT;
2149 goto out;
2150 }
2151
2152 backref->root_id = root_id;
2153 backref->inum = inum;
2154 backref->file_pos = offset + extent_offset;
2155 backref->num_bytes = num_bytes;
2156 backref->extent_offset = extent_offset;
2157 backref->generation = btrfs_file_extent_generation(leaf, extent);
2158 backref->old = old;
2159 backref_insert(&new->root, backref);
2160 old->count++;
2161out:
2162 btrfs_release_path(path);
2163 WARN_ON(ret);
2164 return ret;
2165}
2166
2167static noinline bool record_extent_backrefs(struct btrfs_path *path,
2168 struct new_sa_defrag_extent *new)
2169{
2170 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2171 struct old_sa_defrag_extent *old, *tmp;
2172 int ret;
2173
2174 new->path = path;
2175
2176 list_for_each_entry_safe(old, tmp, &new->head, list) {
2177 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2178 path, record_one_backref,
2179 old);
2180 BUG_ON(ret < 0 && ret != -ENOENT);
2181
2182 /* no backref to be processed for this extent */
2183 if (!old->count) {
2184 list_del(&old->list);
2185 kfree(old);
2186 }
2187 }
2188
2189 if (list_empty(&new->head))
2190 return false;
2191
2192 return true;
2193}
2194
2195static int relink_is_mergable(struct extent_buffer *leaf,
2196 struct btrfs_file_extent_item *fi,
2197 u64 disk_bytenr)
2198{
2199 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2200 return 0;
2201
2202 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2203 return 0;
2204
2205 if (btrfs_file_extent_compression(leaf, fi) ||
2206 btrfs_file_extent_encryption(leaf, fi) ||
2207 btrfs_file_extent_other_encoding(leaf, fi))
2208 return 0;
2209
2210 return 1;
2211}
2212
2213/*
2214 * Note the backref might has changed, and in this case we just return 0.
2215 */
2216static noinline int relink_extent_backref(struct btrfs_path *path,
2217 struct sa_defrag_extent_backref *prev,
2218 struct sa_defrag_extent_backref *backref)
2219{
2220 struct btrfs_file_extent_item *extent;
2221 struct btrfs_file_extent_item *item;
2222 struct btrfs_ordered_extent *ordered;
2223 struct btrfs_trans_handle *trans;
2224 struct btrfs_fs_info *fs_info;
2225 struct btrfs_root *root;
2226 struct btrfs_key key;
2227 struct extent_buffer *leaf;
2228 struct old_sa_defrag_extent *old = backref->old;
2229 struct new_sa_defrag_extent *new = old->new;
2230 struct inode *src_inode = new->inode;
2231 struct inode *inode;
2232 struct extent_state *cached = NULL;
2233 int ret = 0;
2234 u64 start;
2235 u64 len;
2236 u64 lock_start;
2237 u64 lock_end;
2238 bool merge = false;
2239 int index;
2240
2241 if (prev && prev->root_id == backref->root_id &&
2242 prev->inum == backref->inum &&
2243 prev->file_pos + prev->num_bytes == backref->file_pos)
2244 merge = true;
2245
2246 /* step 1: get root */
2247 key.objectid = backref->root_id;
2248 key.type = BTRFS_ROOT_ITEM_KEY;
2249 key.offset = (u64)-1;
2250
2251 fs_info = BTRFS_I(src_inode)->root->fs_info;
2252 index = srcu_read_lock(&fs_info->subvol_srcu);
2253
2254 root = btrfs_read_fs_root_no_name(fs_info, &key);
2255 if (IS_ERR(root)) {
2256 srcu_read_unlock(&fs_info->subvol_srcu, index);
2257 if (PTR_ERR(root) == -ENOENT)
2258 return 0;
2259 return PTR_ERR(root);
2260 }
2261 if (btrfs_root_refs(&root->root_item) == 0) {
2262 srcu_read_unlock(&fs_info->subvol_srcu, index);
2263 /* parse ENOENT to 0 */
2264 return 0;
2265 }
2266
2267 /* step 2: get inode */
2268 key.objectid = backref->inum;
2269 key.type = BTRFS_INODE_ITEM_KEY;
2270 key.offset = 0;
2271
2272 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2273 if (IS_ERR(inode)) {
2274 srcu_read_unlock(&fs_info->subvol_srcu, index);
2275 return 0;
2276 }
2277
2278 srcu_read_unlock(&fs_info->subvol_srcu, index);
2279
2280 /* step 3: relink backref */
2281 lock_start = backref->file_pos;
2282 lock_end = backref->file_pos + backref->num_bytes - 1;
2283 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2284 0, &cached);
2285
2286 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2287 if (ordered) {
2288 btrfs_put_ordered_extent(ordered);
2289 goto out_unlock;
2290 }
2291
2292 trans = btrfs_join_transaction(root);
2293 if (IS_ERR(trans)) {
2294 ret = PTR_ERR(trans);
2295 goto out_unlock;
2296 }
2297
2298 key.objectid = backref->inum;
2299 key.type = BTRFS_EXTENT_DATA_KEY;
2300 key.offset = backref->file_pos;
2301
2302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2303 if (ret < 0) {
2304 goto out_free_path;
2305 } else if (ret > 0) {
2306 ret = 0;
2307 goto out_free_path;
2308 }
2309
2310 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2311 struct btrfs_file_extent_item);
2312
2313 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2314 backref->generation)
2315 goto out_free_path;
2316
2317 btrfs_release_path(path);
2318
2319 start = backref->file_pos;
2320 if (backref->extent_offset < old->extent_offset + old->offset)
2321 start += old->extent_offset + old->offset -
2322 backref->extent_offset;
2323
2324 len = min(backref->extent_offset + backref->num_bytes,
2325 old->extent_offset + old->offset + old->len);
2326 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2327
2328 ret = btrfs_drop_extents(trans, root, inode, start,
2329 start + len, 1);
2330 if (ret)
2331 goto out_free_path;
2332again:
2333 key.objectid = btrfs_ino(inode);
2334 key.type = BTRFS_EXTENT_DATA_KEY;
2335 key.offset = start;
2336
Liu Boa09a0a72013-03-11 09:20:58 +00002337 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002338 if (merge) {
2339 struct btrfs_file_extent_item *fi;
2340 u64 extent_len;
2341 struct btrfs_key found_key;
2342
2343 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2344 if (ret < 0)
2345 goto out_free_path;
2346
2347 path->slots[0]--;
2348 leaf = path->nodes[0];
2349 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2350
2351 fi = btrfs_item_ptr(leaf, path->slots[0],
2352 struct btrfs_file_extent_item);
2353 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2354
2355 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2356 extent_len + found_key.offset == start) {
2357 btrfs_set_file_extent_num_bytes(leaf, fi,
2358 extent_len + len);
2359 btrfs_mark_buffer_dirty(leaf);
2360 inode_add_bytes(inode, len);
2361
2362 ret = 1;
2363 goto out_free_path;
2364 } else {
2365 merge = false;
2366 btrfs_release_path(path);
2367 goto again;
2368 }
2369 }
2370
2371 ret = btrfs_insert_empty_item(trans, root, path, &key,
2372 sizeof(*extent));
2373 if (ret) {
2374 btrfs_abort_transaction(trans, root, ret);
2375 goto out_free_path;
2376 }
2377
2378 leaf = path->nodes[0];
2379 item = btrfs_item_ptr(leaf, path->slots[0],
2380 struct btrfs_file_extent_item);
2381 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2382 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2383 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2384 btrfs_set_file_extent_num_bytes(leaf, item, len);
2385 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2386 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2387 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2388 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2389 btrfs_set_file_extent_encryption(leaf, item, 0);
2390 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2391
2392 btrfs_mark_buffer_dirty(leaf);
2393 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002394 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002395
2396 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2397 new->disk_len, 0,
2398 backref->root_id, backref->inum,
2399 new->file_pos, 0); /* start - extent_offset */
2400 if (ret) {
2401 btrfs_abort_transaction(trans, root, ret);
2402 goto out_free_path;
2403 }
2404
2405 ret = 1;
2406out_free_path:
2407 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002408 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002409 btrfs_end_transaction(trans, root);
2410out_unlock:
2411 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2412 &cached, GFP_NOFS);
2413 iput(inode);
2414 return ret;
2415}
2416
2417static void relink_file_extents(struct new_sa_defrag_extent *new)
2418{
2419 struct btrfs_path *path;
2420 struct old_sa_defrag_extent *old, *tmp;
2421 struct sa_defrag_extent_backref *backref;
2422 struct sa_defrag_extent_backref *prev = NULL;
2423 struct inode *inode;
2424 struct btrfs_root *root;
2425 struct rb_node *node;
2426 int ret;
2427
2428 inode = new->inode;
2429 root = BTRFS_I(inode)->root;
2430
2431 path = btrfs_alloc_path();
2432 if (!path)
2433 return;
2434
2435 if (!record_extent_backrefs(path, new)) {
2436 btrfs_free_path(path);
2437 goto out;
2438 }
2439 btrfs_release_path(path);
2440
2441 while (1) {
2442 node = rb_first(&new->root);
2443 if (!node)
2444 break;
2445 rb_erase(node, &new->root);
2446
2447 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2448
2449 ret = relink_extent_backref(path, prev, backref);
2450 WARN_ON(ret < 0);
2451
2452 kfree(prev);
2453
2454 if (ret == 1)
2455 prev = backref;
2456 else
2457 prev = NULL;
2458 cond_resched();
2459 }
2460 kfree(prev);
2461
2462 btrfs_free_path(path);
2463
2464 list_for_each_entry_safe(old, tmp, &new->head, list) {
2465 list_del(&old->list);
2466 kfree(old);
2467 }
2468out:
2469 atomic_dec(&root->fs_info->defrag_running);
2470 wake_up(&root->fs_info->transaction_wait);
2471
2472 kfree(new);
2473}
2474
2475static struct new_sa_defrag_extent *
2476record_old_file_extents(struct inode *inode,
2477 struct btrfs_ordered_extent *ordered)
2478{
2479 struct btrfs_root *root = BTRFS_I(inode)->root;
2480 struct btrfs_path *path;
2481 struct btrfs_key key;
2482 struct old_sa_defrag_extent *old, *tmp;
2483 struct new_sa_defrag_extent *new;
2484 int ret;
2485
2486 new = kmalloc(sizeof(*new), GFP_NOFS);
2487 if (!new)
2488 return NULL;
2489
2490 new->inode = inode;
2491 new->file_pos = ordered->file_offset;
2492 new->len = ordered->len;
2493 new->bytenr = ordered->start;
2494 new->disk_len = ordered->disk_len;
2495 new->compress_type = ordered->compress_type;
2496 new->root = RB_ROOT;
2497 INIT_LIST_HEAD(&new->head);
2498
2499 path = btrfs_alloc_path();
2500 if (!path)
2501 goto out_kfree;
2502
2503 key.objectid = btrfs_ino(inode);
2504 key.type = BTRFS_EXTENT_DATA_KEY;
2505 key.offset = new->file_pos;
2506
2507 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2508 if (ret < 0)
2509 goto out_free_path;
2510 if (ret > 0 && path->slots[0] > 0)
2511 path->slots[0]--;
2512
2513 /* find out all the old extents for the file range */
2514 while (1) {
2515 struct btrfs_file_extent_item *extent;
2516 struct extent_buffer *l;
2517 int slot;
2518 u64 num_bytes;
2519 u64 offset;
2520 u64 end;
2521 u64 disk_bytenr;
2522 u64 extent_offset;
2523
2524 l = path->nodes[0];
2525 slot = path->slots[0];
2526
2527 if (slot >= btrfs_header_nritems(l)) {
2528 ret = btrfs_next_leaf(root, path);
2529 if (ret < 0)
2530 goto out_free_list;
2531 else if (ret > 0)
2532 break;
2533 continue;
2534 }
2535
2536 btrfs_item_key_to_cpu(l, &key, slot);
2537
2538 if (key.objectid != btrfs_ino(inode))
2539 break;
2540 if (key.type != BTRFS_EXTENT_DATA_KEY)
2541 break;
2542 if (key.offset >= new->file_pos + new->len)
2543 break;
2544
2545 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2546
2547 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2548 if (key.offset + num_bytes < new->file_pos)
2549 goto next;
2550
2551 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2552 if (!disk_bytenr)
2553 goto next;
2554
2555 extent_offset = btrfs_file_extent_offset(l, extent);
2556
2557 old = kmalloc(sizeof(*old), GFP_NOFS);
2558 if (!old)
2559 goto out_free_list;
2560
2561 offset = max(new->file_pos, key.offset);
2562 end = min(new->file_pos + new->len, key.offset + num_bytes);
2563
2564 old->bytenr = disk_bytenr;
2565 old->extent_offset = extent_offset;
2566 old->offset = offset - key.offset;
2567 old->len = end - offset;
2568 old->new = new;
2569 old->count = 0;
2570 list_add_tail(&old->list, &new->head);
2571next:
2572 path->slots[0]++;
2573 cond_resched();
2574 }
2575
2576 btrfs_free_path(path);
2577 atomic_inc(&root->fs_info->defrag_running);
2578
2579 return new;
2580
2581out_free_list:
2582 list_for_each_entry_safe(old, tmp, &new->head, list) {
2583 list_del(&old->list);
2584 kfree(old);
2585 }
2586out_free_path:
2587 btrfs_free_path(path);
2588out_kfree:
2589 kfree(new);
2590 return NULL;
2591}
2592
Chris Mason5d13a982009-03-13 11:41:46 -04002593/*
2594 * helper function for btrfs_finish_ordered_io, this
2595 * just reads in some of the csum leaves to prime them into ram
2596 * before we start the transaction. It limits the amount of btree
2597 * reads required while inside the transaction.
2598 */
Chris Masond352ac62008-09-29 15:18:18 -04002599/* as ordered data IO finishes, this gets called so we can finish
2600 * an ordered extent if the range of bytes in the file it covers are
2601 * fully written.
2602 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002603static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002604{
Josef Bacik5fd02042012-05-02 14:00:54 -04002605 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002606 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002607 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002608 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002609 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002610 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002611 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002612 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002613 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002614
Liu Bo83eea1f2012-07-10 05:28:39 -06002615 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002616
Josef Bacik5fd02042012-05-02 14:00:54 -04002617 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2618 ret = -EIO;
2619 goto out;
2620 }
2621
Yan, Zhengc2167752009-11-12 09:34:21 +00002622 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002623 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002624 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2625 if (nolock)
2626 trans = btrfs_join_transaction_nolock(root);
2627 else
2628 trans = btrfs_join_transaction(root);
2629 if (IS_ERR(trans)) {
2630 ret = PTR_ERR(trans);
2631 trans = NULL;
2632 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002633 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002634 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2635 ret = btrfs_update_inode_fallback(trans, root, inode);
2636 if (ret) /* -ENOMEM or corruption */
2637 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002638 goto out;
2639 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002640
Josef Bacik2ac55d42010-02-03 19:33:23 +00002641 lock_extent_bits(io_tree, ordered_extent->file_offset,
2642 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002643 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002644
Liu Bo38c227d2013-01-29 03:18:40 +00002645 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2646 ordered_extent->file_offset + ordered_extent->len - 1,
2647 EXTENT_DEFRAG, 1, cached_state);
2648 if (ret) {
2649 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2650 if (last_snapshot >= BTRFS_I(inode)->generation)
2651 /* the inode is shared */
2652 new = record_old_file_extents(inode, ordered_extent);
2653
2654 clear_extent_bit(io_tree, ordered_extent->file_offset,
2655 ordered_extent->file_offset + ordered_extent->len - 1,
2656 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2657 }
2658
Josef Bacik0cb59c92010-07-02 12:14:14 -04002659 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002660 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002661 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002662 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002663 if (IS_ERR(trans)) {
2664 ret = PTR_ERR(trans);
2665 trans = NULL;
2666 goto out_unlock;
2667 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002668 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002669
Chris Masonc8b97812008-10-29 14:49:59 -04002670 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002671 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002672 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002673 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002674 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002675 ordered_extent->file_offset,
2676 ordered_extent->file_offset +
2677 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002678 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002679 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002680 ret = insert_reserved_file_extent(trans, inode,
2681 ordered_extent->file_offset,
2682 ordered_extent->start,
2683 ordered_extent->disk_len,
2684 ordered_extent->len,
2685 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002686 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002687 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002688 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002689 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2690 ordered_extent->file_offset, ordered_extent->len,
2691 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002692 if (ret < 0) {
2693 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002694 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002695 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002696
Chris Masone6dcd2d2008-07-17 12:53:50 -04002697 add_pending_csums(trans, inode, ordered_extent->file_offset,
2698 &ordered_extent->list);
2699
Josef Bacik6c760c02012-11-09 10:53:21 -05002700 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2701 ret = btrfs_update_inode_fallback(trans, root, inode);
2702 if (ret) { /* -ENOMEM or corruption */
2703 btrfs_abort_transaction(trans, root, ret);
2704 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002705 }
2706 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002707out_unlock:
2708 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2709 ordered_extent->file_offset +
2710 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002711out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002712 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002713 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002714 if (trans)
2715 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002716
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002717 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002718 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2719 ordered_extent->file_offset +
2720 ordered_extent->len - 1, NULL, GFP_NOFS);
2721
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002722 /*
2723 * If the ordered extent had an IOERR or something else went
2724 * wrong we need to return the space for this ordered extent
2725 * back to the allocator.
2726 */
2727 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2728 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2729 btrfs_free_reserved_extent(root, ordered_extent->start,
2730 ordered_extent->disk_len);
2731 }
2732
2733
Josef Bacik5fd02042012-05-02 14:00:54 -04002734 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002735 * This needs to be done to make sure anybody waiting knows we are done
2736 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002737 */
2738 btrfs_remove_ordered_extent(inode, ordered_extent);
2739
Liu Bo38c227d2013-01-29 03:18:40 +00002740 /* for snapshot-aware defrag */
2741 if (new)
2742 relink_file_extents(new);
2743
Chris Masone6dcd2d2008-07-17 12:53:50 -04002744 /* once for us */
2745 btrfs_put_ordered_extent(ordered_extent);
2746 /* once for the tree */
2747 btrfs_put_ordered_extent(ordered_extent);
2748
Josef Bacik5fd02042012-05-02 14:00:54 -04002749 return ret;
2750}
2751
2752static void finish_ordered_fn(struct btrfs_work *work)
2753{
2754 struct btrfs_ordered_extent *ordered_extent;
2755 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2756 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002757}
2758
Christoph Hellwigb2950862008-12-02 09:54:17 -05002759static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002760 struct extent_state *state, int uptodate)
2761{
Josef Bacik5fd02042012-05-02 14:00:54 -04002762 struct inode *inode = page->mapping->host;
2763 struct btrfs_root *root = BTRFS_I(inode)->root;
2764 struct btrfs_ordered_extent *ordered_extent = NULL;
2765 struct btrfs_workers *workers;
2766
liubo1abe9b82011-03-24 11:18:59 +00002767 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2768
Chris Mason8b62b722009-09-02 16:53:46 -04002769 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002770 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2771 end - start + 1, uptodate))
2772 return 0;
2773
2774 ordered_extent->work.func = finish_ordered_fn;
2775 ordered_extent->work.flags = 0;
2776
Liu Bo83eea1f2012-07-10 05:28:39 -06002777 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002778 workers = &root->fs_info->endio_freespace_worker;
2779 else
2780 workers = &root->fs_info->endio_write_workers;
2781 btrfs_queue_worker(workers, &ordered_extent->work);
2782
2783 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002784}
2785
Chris Masond352ac62008-09-29 15:18:18 -04002786/*
Chris Masond352ac62008-09-29 15:18:18 -04002787 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002788 * if there's a match, we allow the bio to finish. If not, the code in
2789 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002790 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002791static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002792 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002793{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002794 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002795 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002796 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002797 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002798 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002799 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002800 struct btrfs_root *root = BTRFS_I(inode)->root;
2801 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002802 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2803 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002804
Chris Masond20f7042008-12-08 16:58:54 -05002805 if (PageChecked(page)) {
2806 ClearPageChecked(page);
2807 goto good;
2808 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002809
2810 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002811 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002812
Yan Zheng17d217f2008-12-12 10:03:38 -05002813 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002814 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002815 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2816 GFP_NOFS);
2817 return 0;
2818 }
2819
Yanc2e639f2008-02-04 08:57:25 -05002820 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002821 private = state->private;
2822 ret = 0;
2823 } else {
2824 ret = get_state_private(io_tree, start, &private);
2825 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002826 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002827 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002828 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002829
Liu Bob0496682013-03-14 14:57:45 +00002830 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002831 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002832 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002833 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002834
Cong Wang7ac687d2011-11-25 23:14:28 +08002835 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002836good:
Chris Mason07157aa2007-08-30 08:50:51 -04002837 return 0;
2838
2839zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002840 if (__ratelimit(&_rs))
2841 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2842 (unsigned long long)btrfs_ino(page->mapping->host),
2843 (unsigned long long)start, csum,
2844 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002845 memset(kaddr + offset, 1, end - start + 1);
2846 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002847 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002848 if (private == 0)
2849 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002850 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002851}
Chris Masonb888db2b2007-08-27 16:49:44 -04002852
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002853struct delayed_iput {
2854 struct list_head list;
2855 struct inode *inode;
2856};
2857
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002858/* JDM: If this is fs-wide, why can't we add a pointer to
2859 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002860void btrfs_add_delayed_iput(struct inode *inode)
2861{
2862 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2863 struct delayed_iput *delayed;
2864
2865 if (atomic_add_unless(&inode->i_count, -1, 1))
2866 return;
2867
2868 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2869 delayed->inode = inode;
2870
2871 spin_lock(&fs_info->delayed_iput_lock);
2872 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2873 spin_unlock(&fs_info->delayed_iput_lock);
2874}
2875
2876void btrfs_run_delayed_iputs(struct btrfs_root *root)
2877{
2878 LIST_HEAD(list);
2879 struct btrfs_fs_info *fs_info = root->fs_info;
2880 struct delayed_iput *delayed;
2881 int empty;
2882
2883 spin_lock(&fs_info->delayed_iput_lock);
2884 empty = list_empty(&fs_info->delayed_iputs);
2885 spin_unlock(&fs_info->delayed_iput_lock);
2886 if (empty)
2887 return;
2888
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002889 spin_lock(&fs_info->delayed_iput_lock);
2890 list_splice_init(&fs_info->delayed_iputs, &list);
2891 spin_unlock(&fs_info->delayed_iput_lock);
2892
2893 while (!list_empty(&list)) {
2894 delayed = list_entry(list.next, struct delayed_iput, list);
2895 list_del(&delayed->list);
2896 iput(delayed->inode);
2897 kfree(delayed);
2898 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002899}
2900
Yan, Zhengd68fc572010-05-16 10:49:58 -04002901/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002902 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002903 * files in the subvolume, it removes orphan item and frees block_rsv
2904 * structure.
2905 */
2906void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2907 struct btrfs_root *root)
2908{
Josef Bacik90290e12011-12-02 15:44:12 -05002909 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002910 int ret;
2911
Josef Bacik8a35d952012-05-23 14:26:42 -04002912 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002913 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2914 return;
2915
Josef Bacik90290e12011-12-02 15:44:12 -05002916 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002917 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002918 spin_unlock(&root->orphan_lock);
2919 return;
2920 }
2921
2922 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2923 spin_unlock(&root->orphan_lock);
2924 return;
2925 }
2926
2927 block_rsv = root->orphan_block_rsv;
2928 root->orphan_block_rsv = NULL;
2929 spin_unlock(&root->orphan_lock);
2930
Yan, Zhengd68fc572010-05-16 10:49:58 -04002931 if (root->orphan_item_inserted &&
2932 btrfs_root_refs(&root->root_item) > 0) {
2933 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2934 root->root_key.objectid);
2935 BUG_ON(ret);
2936 root->orphan_item_inserted = 0;
2937 }
2938
Josef Bacik90290e12011-12-02 15:44:12 -05002939 if (block_rsv) {
2940 WARN_ON(block_rsv->size > 0);
2941 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002942 }
2943}
2944
2945/*
Josef Bacik7b128762008-07-24 12:17:14 -04002946 * This creates an orphan entry for the given inode in case something goes
2947 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002948 *
2949 * NOTE: caller of this function should reserve 5 units of metadata for
2950 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002951 */
2952int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2953{
2954 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002955 struct btrfs_block_rsv *block_rsv = NULL;
2956 int reserve = 0;
2957 int insert = 0;
2958 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002959
Yan, Zhengd68fc572010-05-16 10:49:58 -04002960 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002961 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002962 if (!block_rsv)
2963 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002964 }
2965
Yan, Zhengd68fc572010-05-16 10:49:58 -04002966 spin_lock(&root->orphan_lock);
2967 if (!root->orphan_block_rsv) {
2968 root->orphan_block_rsv = block_rsv;
2969 } else if (block_rsv) {
2970 btrfs_free_block_rsv(root, block_rsv);
2971 block_rsv = NULL;
2972 }
Josef Bacik7b128762008-07-24 12:17:14 -04002973
Josef Bacik8a35d952012-05-23 14:26:42 -04002974 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2975 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002976#if 0
2977 /*
2978 * For proper ENOSPC handling, we should do orphan
2979 * cleanup when mounting. But this introduces backward
2980 * compatibility issue.
2981 */
2982 if (!xchg(&root->orphan_item_inserted, 1))
2983 insert = 2;
2984 else
2985 insert = 1;
2986#endif
2987 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002988 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002989 }
Josef Bacik7b128762008-07-24 12:17:14 -04002990
Josef Bacik72ac3c02012-05-23 14:13:11 -04002991 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2992 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002993 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002994 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002995
Yan, Zhengd68fc572010-05-16 10:49:58 -04002996 /* grab metadata reservation from transaction handle */
2997 if (reserve) {
2998 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002999 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003000 }
3001
3002 /* insert an orphan item to track this unlinked/truncated file */
3003 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003004 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003005 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003006 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3007 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003008 btrfs_abort_transaction(trans, root, ret);
3009 return ret;
3010 }
3011 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003012 }
3013
3014 /* insert an orphan item to track subvolume contains orphan files */
3015 if (insert >= 2) {
3016 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3017 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003018 if (ret && ret != -EEXIST) {
3019 btrfs_abort_transaction(trans, root, ret);
3020 return ret;
3021 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003022 }
3023 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003024}
3025
3026/*
3027 * We have done the truncate/delete so we can go ahead and remove the orphan
3028 * item for this particular inode.
3029 */
3030int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
3031{
3032 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003033 int delete_item = 0;
3034 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003035 int ret = 0;
3036
Yan, Zhengd68fc572010-05-16 10:49:58 -04003037 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003038 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3039 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003040 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003041
Josef Bacik72ac3c02012-05-23 14:13:11 -04003042 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3043 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003044 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003045 spin_unlock(&root->orphan_lock);
3046
3047 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003048 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003050 }
3051
Josef Bacik8a35d952012-05-23 14:26:42 -04003052 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003053 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003054 atomic_dec(&root->orphan_inodes);
3055 }
Josef Bacik7b128762008-07-24 12:17:14 -04003056
Yan, Zhengd68fc572010-05-16 10:49:58 -04003057 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003058}
3059
3060/*
3061 * this cleans up any orphans that may be left on the list from the last use
3062 * of this root.
3063 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003064int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003065{
3066 struct btrfs_path *path;
3067 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003068 struct btrfs_key key, found_key;
3069 struct btrfs_trans_handle *trans;
3070 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003071 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003072 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3073
Yan, Zhengd68fc572010-05-16 10:49:58 -04003074 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003075 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003076
3077 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003078 if (!path) {
3079 ret = -ENOMEM;
3080 goto out;
3081 }
Josef Bacik7b128762008-07-24 12:17:14 -04003082 path->reada = -1;
3083
3084 key.objectid = BTRFS_ORPHAN_OBJECTID;
3085 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3086 key.offset = (u64)-1;
3087
Josef Bacik7b128762008-07-24 12:17:14 -04003088 while (1) {
3089 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003090 if (ret < 0)
3091 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003092
3093 /*
3094 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003095 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003096 * find the key and see if we have stuff that matches
3097 */
3098 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003099 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003100 if (path->slots[0] == 0)
3101 break;
3102 path->slots[0]--;
3103 }
3104
3105 /* pull out the item */
3106 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003107 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3108
3109 /* make sure the item matches what we want */
3110 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3111 break;
3112 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3113 break;
3114
3115 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003116 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003117
3118 /*
3119 * this is where we are basically btrfs_lookup, without the
3120 * crossing root thing. we store the inode number in the
3121 * offset of the orphan item.
3122 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003123
3124 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003125 btrfs_err(root->fs_info,
3126 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003127 ret = -EINVAL;
3128 goto out;
3129 }
3130
3131 last_objectid = found_key.offset;
3132
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003133 found_key.objectid = found_key.offset;
3134 found_key.type = BTRFS_INODE_ITEM_KEY;
3135 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003136 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003137 ret = PTR_RET(inode);
3138 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003139 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003140
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003141 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3142 struct btrfs_root *dead_root;
3143 struct btrfs_fs_info *fs_info = root->fs_info;
3144 int is_dead_root = 0;
3145
3146 /*
3147 * this is an orphan in the tree root. Currently these
3148 * could come from 2 sources:
3149 * a) a snapshot deletion in progress
3150 * b) a free space cache inode
3151 * We need to distinguish those two, as the snapshot
3152 * orphan must not get deleted.
3153 * find_dead_roots already ran before us, so if this
3154 * is a snapshot deletion, we should find the root
3155 * in the dead_roots list
3156 */
3157 spin_lock(&fs_info->trans_lock);
3158 list_for_each_entry(dead_root, &fs_info->dead_roots,
3159 root_list) {
3160 if (dead_root->root_key.objectid ==
3161 found_key.objectid) {
3162 is_dead_root = 1;
3163 break;
3164 }
3165 }
3166 spin_unlock(&fs_info->trans_lock);
3167 if (is_dead_root) {
3168 /* prevent this orphan from being found again */
3169 key.offset = found_key.objectid - 1;
3170 continue;
3171 }
3172 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003173 /*
3174 * Inode is already gone but the orphan item is still there,
3175 * kill the orphan item.
3176 */
3177 if (ret == -ESTALE) {
3178 trans = btrfs_start_transaction(root, 1);
3179 if (IS_ERR(trans)) {
3180 ret = PTR_ERR(trans);
3181 goto out;
3182 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003183 btrfs_debug(root->fs_info, "auto deleting %Lu",
3184 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003185 ret = btrfs_del_orphan_item(trans, root,
3186 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003187 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003188 btrfs_end_transaction(trans, root);
3189 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003190 }
Josef Bacik7b128762008-07-24 12:17:14 -04003191
Josef Bacik7b128762008-07-24 12:17:14 -04003192 /*
3193 * add this inode to the orphan list so btrfs_orphan_del does
3194 * the proper thing when we hit it
3195 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003196 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3197 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003198 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003199
Josef Bacik7b128762008-07-24 12:17:14 -04003200 /* if we have links, this was a truncate, lets do that */
3201 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003202 if (!S_ISREG(inode->i_mode)) {
3203 WARN_ON(1);
3204 iput(inode);
3205 continue;
3206 }
Josef Bacik7b128762008-07-24 12:17:14 -04003207 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003208
3209 /* 1 for the orphan item deletion. */
3210 trans = btrfs_start_transaction(root, 1);
3211 if (IS_ERR(trans)) {
3212 ret = PTR_ERR(trans);
3213 goto out;
3214 }
3215 ret = btrfs_orphan_add(trans, inode);
3216 btrfs_end_transaction(trans, root);
3217 if (ret)
3218 goto out;
3219
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003220 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003221 if (ret)
3222 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003223 } else {
3224 nr_unlink++;
3225 }
3226
3227 /* this will do delete_inode and everything for us */
3228 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003229 if (ret)
3230 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003231 }
Miao Xie3254c872011-11-10 20:45:05 -05003232 /* release the path since we're done with it */
3233 btrfs_release_path(path);
3234
Yan, Zhengd68fc572010-05-16 10:49:58 -04003235 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3236
3237 if (root->orphan_block_rsv)
3238 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3239 (u64)-1);
3240
3241 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003242 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003243 if (!IS_ERR(trans))
3244 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245 }
Josef Bacik7b128762008-07-24 12:17:14 -04003246
3247 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003248 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003249 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003250 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003251
3252out:
3253 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003254 btrfs_crit(root->fs_info,
3255 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003256 btrfs_free_path(path);
3257 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003258}
3259
Chris Masond352ac62008-09-29 15:18:18 -04003260/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003261 * very simple check to peek ahead in the leaf looking for xattrs. If we
3262 * don't find any xattrs, we know there can't be any acls.
3263 *
3264 * slot is the slot the inode is in, objectid is the objectid of the inode
3265 */
3266static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3267 int slot, u64 objectid)
3268{
3269 u32 nritems = btrfs_header_nritems(leaf);
3270 struct btrfs_key found_key;
3271 int scanned = 0;
3272
3273 slot++;
3274 while (slot < nritems) {
3275 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3276
3277 /* we found a different objectid, there must not be acls */
3278 if (found_key.objectid != objectid)
3279 return 0;
3280
3281 /* we found an xattr, assume we've got an acl */
3282 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3283 return 1;
3284
3285 /*
3286 * we found a key greater than an xattr key, there can't
3287 * be any acls later on
3288 */
3289 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3290 return 0;
3291
3292 slot++;
3293 scanned++;
3294
3295 /*
3296 * it goes inode, inode backrefs, xattrs, extents,
3297 * so if there are a ton of hard links to an inode there can
3298 * be a lot of backrefs. Don't waste time searching too hard,
3299 * this is just an optimization
3300 */
3301 if (scanned >= 8)
3302 break;
3303 }
3304 /* we hit the end of the leaf before we found an xattr or
3305 * something larger than an xattr. We have to assume the inode
3306 * has acls
3307 */
3308 return 1;
3309}
3310
3311/*
Chris Masond352ac62008-09-29 15:18:18 -04003312 * read an inode from the btree into the in-memory inode
3313 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003314static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003315{
3316 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003317 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003318 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003319 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003320 struct btrfs_root *root = BTRFS_I(inode)->root;
3321 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003322 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003323 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003324 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003325 bool filled = false;
3326
3327 ret = btrfs_fill_inode(inode, &rdev);
3328 if (!ret)
3329 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003330
3331 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003332 if (!path)
3333 goto make_bad;
3334
Josef Bacikd90c7322011-05-17 09:50:54 -04003335 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003336 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003337
Chris Mason39279cc2007-06-12 06:35:45 -04003338 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003339 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003340 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003341
Chris Mason5f39d392007-10-15 16:14:19 -04003342 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003343
3344 if (filled)
3345 goto cache_acl;
3346
Chris Mason5f39d392007-10-15 16:14:19 -04003347 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3348 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003349 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003350 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003351 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3352 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003353 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003354
3355 tspec = btrfs_inode_atime(inode_item);
3356 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3357 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3358
3359 tspec = btrfs_inode_mtime(inode_item);
3360 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3361 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3362
3363 tspec = btrfs_inode_ctime(inode_item);
3364 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3365 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3366
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003367 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003368 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003369 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3370
3371 /*
3372 * If we were modified in the current generation and evicted from memory
3373 * and then re-read we need to do a full sync since we don't have any
3374 * idea about which extents were modified before we were evicted from
3375 * cache.
3376 */
3377 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3378 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3379 &BTRFS_I(inode)->runtime_flags);
3380
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003381 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003382 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003383 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003384 rdev = btrfs_inode_rdev(leaf, inode_item);
3385
Josef Bacikaec74772008-07-24 12:12:38 -04003386 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003387 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003388cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003389 /*
3390 * try to precache a NULL acl entry for files that don't have
3391 * any xattrs or acls
3392 */
Li Zefan33345d012011-04-20 10:31:50 +08003393 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3394 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003395 if (!maybe_acls)
3396 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003397
Chris Mason39279cc2007-06-12 06:35:45 -04003398 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003399
Chris Mason39279cc2007-06-12 06:35:45 -04003400 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003401 case S_IFREG:
3402 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003403 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003404 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003405 inode->i_fop = &btrfs_file_operations;
3406 inode->i_op = &btrfs_file_inode_operations;
3407 break;
3408 case S_IFDIR:
3409 inode->i_fop = &btrfs_dir_file_operations;
3410 if (root == root->fs_info->tree_root)
3411 inode->i_op = &btrfs_dir_ro_inode_operations;
3412 else
3413 inode->i_op = &btrfs_dir_inode_operations;
3414 break;
3415 case S_IFLNK:
3416 inode->i_op = &btrfs_symlink_inode_operations;
3417 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003418 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003419 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003420 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003421 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003422 init_special_inode(inode, inode->i_mode, rdev);
3423 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003424 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003425
3426 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003427 return;
3428
3429make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003430 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003431 make_bad_inode(inode);
3432}
3433
Chris Masond352ac62008-09-29 15:18:18 -04003434/*
3435 * given a leaf and an inode, copy the inode fields into the leaf
3436 */
Chris Masone02119d2008-09-05 16:13:11 -04003437static void fill_inode_item(struct btrfs_trans_handle *trans,
3438 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003439 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003440 struct inode *inode)
3441{
Liu Bo51fab692012-12-27 09:01:21 +00003442 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003443
Liu Bo51fab692012-12-27 09:01:21 +00003444 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003445
Liu Bo51fab692012-12-27 09:01:21 +00003446 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3447 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3448 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3449 &token);
3450 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3451 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003452
Liu Bo51fab692012-12-27 09:01:21 +00003453 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3454 inode->i_atime.tv_sec, &token);
3455 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3456 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003457
Liu Bo51fab692012-12-27 09:01:21 +00003458 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3459 inode->i_mtime.tv_sec, &token);
3460 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3461 inode->i_mtime.tv_nsec, &token);
3462
3463 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3464 inode->i_ctime.tv_sec, &token);
3465 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3466 inode->i_ctime.tv_nsec, &token);
3467
3468 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3469 &token);
3470 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3471 &token);
3472 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3473 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3474 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3475 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3476 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003477}
3478
Chris Masond352ac62008-09-29 15:18:18 -04003479/*
3480 * copy everything in the in-memory inode into the btree.
3481 */
Chris Mason21151332011-11-10 20:39:08 -05003482static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003483 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003484{
3485 struct btrfs_inode_item *inode_item;
3486 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003487 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003488 int ret;
3489
3490 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003491 if (!path)
3492 return -ENOMEM;
3493
Chris Masonb9473432009-03-13 11:00:37 -04003494 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003495 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3496 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003497 if (ret) {
3498 if (ret > 0)
3499 ret = -ENOENT;
3500 goto failed;
3501 }
3502
Chris Masonb4ce94d2009-02-04 09:25:08 -05003503 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003504 leaf = path->nodes[0];
3505 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003506 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003507
Chris Masone02119d2008-09-05 16:13:11 -04003508 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003509 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003510 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003511 ret = 0;
3512failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003513 btrfs_free_path(path);
3514 return ret;
3515}
3516
Chris Masond352ac62008-09-29 15:18:18 -04003517/*
Chris Mason21151332011-11-10 20:39:08 -05003518 * copy everything in the in-memory inode into the btree.
3519 */
3520noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3521 struct btrfs_root *root, struct inode *inode)
3522{
3523 int ret;
3524
3525 /*
3526 * If the inode is a free space inode, we can deadlock during commit
3527 * if we put it into the delayed code.
3528 *
3529 * The data relocation inode should also be directly updated
3530 * without delay
3531 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003532 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003533 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003534 btrfs_update_root_times(trans, root);
3535
Chris Mason21151332011-11-10 20:39:08 -05003536 ret = btrfs_delayed_update_inode(trans, root, inode);
3537 if (!ret)
3538 btrfs_set_inode_last_trans(trans, inode);
3539 return ret;
3540 }
3541
3542 return btrfs_update_inode_item(trans, root, inode);
3543}
3544
Josef Bacikbe6aef62012-10-22 15:43:12 -04003545noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3546 struct btrfs_root *root,
3547 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003548{
3549 int ret;
3550
3551 ret = btrfs_update_inode(trans, root, inode);
3552 if (ret == -ENOSPC)
3553 return btrfs_update_inode_item(trans, root, inode);
3554 return ret;
3555}
3556
3557/*
Chris Masond352ac62008-09-29 15:18:18 -04003558 * unlink helper that gets used here in inode.c and in the tree logging
3559 * recovery code. It remove a link in a directory with a given name, and
3560 * also drops the back refs in the inode to the directory
3561 */
Al Viro92986792011-03-04 17:14:37 +00003562static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3563 struct btrfs_root *root,
3564 struct inode *dir, struct inode *inode,
3565 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003566{
3567 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003568 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003569 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003570 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003571 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003572 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003573 u64 ino = btrfs_ino(inode);
3574 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003575
3576 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003577 if (!path) {
3578 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003579 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003580 }
3581
Chris Masonb9473432009-03-13 11:00:37 -04003582 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003583 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003584 name, name_len, -1);
3585 if (IS_ERR(di)) {
3586 ret = PTR_ERR(di);
3587 goto err;
3588 }
3589 if (!di) {
3590 ret = -ENOENT;
3591 goto err;
3592 }
Chris Mason5f39d392007-10-15 16:14:19 -04003593 leaf = path->nodes[0];
3594 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003595 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003596 if (ret)
3597 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003598 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003599
Li Zefan33345d012011-04-20 10:31:50 +08003600 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3601 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003602 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003603 btrfs_info(root->fs_info,
3604 "failed to delete reference to %.*s, inode %llu parent %llu",
3605 name_len, name,
3606 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003607 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003608 goto err;
3609 }
3610
Miao Xie16cdcec2011-04-22 18:12:22 +08003611 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003612 if (ret) {
3613 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003614 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003615 }
Chris Mason39279cc2007-06-12 06:35:45 -04003616
Chris Masone02119d2008-09-05 16:13:11 -04003617 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003618 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003619 if (ret != 0 && ret != -ENOENT) {
3620 btrfs_abort_transaction(trans, root, ret);
3621 goto err;
3622 }
Chris Masone02119d2008-09-05 16:13:11 -04003623
3624 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3625 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003626 if (ret == -ENOENT)
3627 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003628err:
3629 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003630 if (ret)
3631 goto out;
3632
3633 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003634 inode_inc_iversion(inode);
3635 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003636 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003637 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003638out:
Chris Mason39279cc2007-06-12 06:35:45 -04003639 return ret;
3640}
3641
Al Viro92986792011-03-04 17:14:37 +00003642int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3643 struct btrfs_root *root,
3644 struct inode *dir, struct inode *inode,
3645 const char *name, int name_len)
3646{
3647 int ret;
3648 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3649 if (!ret) {
3650 btrfs_drop_nlink(inode);
3651 ret = btrfs_update_inode(trans, root, inode);
3652 }
3653 return ret;
3654}
3655
3656
Yan, Zhenga22285a2010-05-16 10:48:46 -04003657/* helper to check if there is any shared block in the path */
3658static int check_path_shared(struct btrfs_root *root,
3659 struct btrfs_path *path)
3660{
3661 struct extent_buffer *eb;
3662 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00003663 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003664
3665 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003666 int ret;
3667
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668 if (!path->nodes[level])
3669 break;
3670 eb = path->nodes[level];
3671 if (!btrfs_block_can_be_shared(root, eb))
3672 continue;
Josef Bacik3173a182013-03-07 14:22:04 -05003673 ret = btrfs_lookup_extent_info(NULL, root, eb->start, level, 1,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003674 &refs, NULL);
3675 if (refs > 1)
3676 return 1;
3677 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003678 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003679}
3680
3681/*
3682 * helper to start transaction for unlink and rmdir.
3683 *
3684 * unlink and rmdir are special in btrfs, they do not always free space.
3685 * so in enospc case, we should make sure they will free space before
3686 * allowing them to use the global metadata reservation.
3687 */
3688static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3689 struct dentry *dentry)
3690{
3691 struct btrfs_trans_handle *trans;
3692 struct btrfs_root *root = BTRFS_I(dir)->root;
3693 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003694 struct btrfs_dir_item *di;
3695 struct inode *inode = dentry->d_inode;
3696 u64 index;
3697 int check_link = 1;
3698 int err = -ENOSPC;
3699 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003700 u64 ino = btrfs_ino(inode);
3701 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702
Josef Bacike70bea52011-10-11 14:18:24 -04003703 /*
3704 * 1 for the possible orphan item
3705 * 1 for the dir item
3706 * 1 for the dir index
3707 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003708 * 1 for the inode
3709 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003710 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003711 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3712 return trans;
3713
Li Zefan33345d012011-04-20 10:31:50 +08003714 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003715 return ERR_PTR(-ENOSPC);
3716
3717 /* check if there is someone else holds reference */
3718 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3719 return ERR_PTR(-ENOSPC);
3720
3721 if (atomic_read(&inode->i_count) > 2)
3722 return ERR_PTR(-ENOSPC);
3723
3724 if (xchg(&root->fs_info->enospc_unlink, 1))
3725 return ERR_PTR(-ENOSPC);
3726
3727 path = btrfs_alloc_path();
3728 if (!path) {
3729 root->fs_info->enospc_unlink = 0;
3730 return ERR_PTR(-ENOMEM);
3731 }
3732
Josef Bacik3880a1b2011-10-14 14:46:51 -04003733 /* 1 for the orphan item */
3734 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003735 if (IS_ERR(trans)) {
3736 btrfs_free_path(path);
3737 root->fs_info->enospc_unlink = 0;
3738 return trans;
3739 }
3740
3741 path->skip_locking = 1;
3742 path->search_commit_root = 1;
3743
3744 ret = btrfs_lookup_inode(trans, root, path,
3745 &BTRFS_I(dir)->location, 0);
3746 if (ret < 0) {
3747 err = ret;
3748 goto out;
3749 }
3750 if (ret == 0) {
3751 if (check_path_shared(root, path))
3752 goto out;
3753 } else {
3754 check_link = 0;
3755 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003756 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003757
3758 ret = btrfs_lookup_inode(trans, root, path,
3759 &BTRFS_I(inode)->location, 0);
3760 if (ret < 0) {
3761 err = ret;
3762 goto out;
3763 }
3764 if (ret == 0) {
3765 if (check_path_shared(root, path))
3766 goto out;
3767 } else {
3768 check_link = 0;
3769 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003770 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003771
3772 if (ret == 0 && S_ISREG(inode->i_mode)) {
3773 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003774 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003775 if (ret < 0) {
3776 err = ret;
3777 goto out;
3778 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003779 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003780 if (check_path_shared(root, path))
3781 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003782 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003783 }
3784
3785 if (!check_link) {
3786 err = 0;
3787 goto out;
3788 }
3789
Li Zefan33345d012011-04-20 10:31:50 +08003790 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003791 dentry->d_name.name, dentry->d_name.len, 0);
3792 if (IS_ERR(di)) {
3793 err = PTR_ERR(di);
3794 goto out;
3795 }
3796 if (di) {
3797 if (check_path_shared(root, path))
3798 goto out;
3799 } else {
3800 err = 0;
3801 goto out;
3802 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003803 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003804
Mark Fashehf1863732012-08-08 11:32:27 -07003805 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3806 dentry->d_name.len, ino, dir_ino, 0,
3807 &index);
3808 if (ret) {
3809 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003810 goto out;
3811 }
Mark Fashehf1863732012-08-08 11:32:27 -07003812
Yan, Zhenga22285a2010-05-16 10:48:46 -04003813 if (check_path_shared(root, path))
3814 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003815
David Sterbab3b4aa72011-04-21 01:20:15 +02003816 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003817
Miao Xie16cdcec2011-04-22 18:12:22 +08003818 /*
3819 * This is a commit root search, if we can lookup inode item and other
3820 * relative items in the commit root, it means the transaction of
3821 * dir/file creation has been committed, and the dir index item that we
3822 * delay to insert has also been inserted into the commit root. So
3823 * we needn't worry about the delayed insertion of the dir index item
3824 * here.
3825 */
Li Zefan33345d012011-04-20 10:31:50 +08003826 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003827 dentry->d_name.name, dentry->d_name.len, 0);
3828 if (IS_ERR(di)) {
3829 err = PTR_ERR(di);
3830 goto out;
3831 }
3832 BUG_ON(ret == -ENOENT);
3833 if (check_path_shared(root, path))
3834 goto out;
3835
3836 err = 0;
3837out:
3838 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003839 /* Migrate the orphan reservation over */
3840 if (!err)
3841 err = btrfs_block_rsv_migrate(trans->block_rsv,
3842 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003843 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003844
Yan, Zhenga22285a2010-05-16 10:48:46 -04003845 if (err) {
3846 btrfs_end_transaction(trans, root);
3847 root->fs_info->enospc_unlink = 0;
3848 return ERR_PTR(err);
3849 }
3850
3851 trans->block_rsv = &root->fs_info->global_block_rsv;
3852 return trans;
3853}
3854
3855static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3856 struct btrfs_root *root)
3857{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003858 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003859 btrfs_block_rsv_release(root, trans->block_rsv,
3860 trans->bytes_reserved);
3861 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003862 BUG_ON(!root->fs_info->enospc_unlink);
3863 root->fs_info->enospc_unlink = 0;
3864 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003865 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003866}
3867
Chris Mason39279cc2007-06-12 06:35:45 -04003868static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3869{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003870 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003871 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003872 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003873 int ret;
3874
Yan, Zhenga22285a2010-05-16 10:48:46 -04003875 trans = __unlink_start_trans(dir, dentry);
3876 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003877 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003878
Chris Mason12fcfd22009-03-24 10:24:20 -04003879 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3880
Chris Masone02119d2008-09-05 16:13:11 -04003881 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3882 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003883 if (ret)
3884 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003885
Yan, Zhenga22285a2010-05-16 10:48:46 -04003886 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003887 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003888 if (ret)
3889 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003890 }
Josef Bacik7b128762008-07-24 12:17:14 -04003891
Tsutomu Itohb5324022011-07-19 07:27:20 +00003892out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003893 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003894 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003895 return ret;
3896}
3897
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003898int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3899 struct btrfs_root *root,
3900 struct inode *dir, u64 objectid,
3901 const char *name, int name_len)
3902{
3903 struct btrfs_path *path;
3904 struct extent_buffer *leaf;
3905 struct btrfs_dir_item *di;
3906 struct btrfs_key key;
3907 u64 index;
3908 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003909 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003910
3911 path = btrfs_alloc_path();
3912 if (!path)
3913 return -ENOMEM;
3914
Li Zefan33345d012011-04-20 10:31:50 +08003915 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003916 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003917 if (IS_ERR_OR_NULL(di)) {
3918 if (!di)
3919 ret = -ENOENT;
3920 else
3921 ret = PTR_ERR(di);
3922 goto out;
3923 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003924
3925 leaf = path->nodes[0];
3926 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3927 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3928 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003929 if (ret) {
3930 btrfs_abort_transaction(trans, root, ret);
3931 goto out;
3932 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003933 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003934
3935 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3936 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003937 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003938 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003939 if (ret != -ENOENT) {
3940 btrfs_abort_transaction(trans, root, ret);
3941 goto out;
3942 }
Li Zefan33345d012011-04-20 10:31:50 +08003943 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003944 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003945 if (IS_ERR_OR_NULL(di)) {
3946 if (!di)
3947 ret = -ENOENT;
3948 else
3949 ret = PTR_ERR(di);
3950 btrfs_abort_transaction(trans, root, ret);
3951 goto out;
3952 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003953
3954 leaf = path->nodes[0];
3955 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003956 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003957 index = key.offset;
3958 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003959 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003960
Miao Xie16cdcec2011-04-22 18:12:22 +08003961 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003962 if (ret) {
3963 btrfs_abort_transaction(trans, root, ret);
3964 goto out;
3965 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003966
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003967 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003968 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003969 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003970 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003971 if (ret)
3972 btrfs_abort_transaction(trans, root, ret);
3973out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003974 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003975 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003976}
3977
Chris Mason39279cc2007-06-12 06:35:45 -04003978static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3979{
3980 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003981 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003982 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003983 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003984
David Sterbab3ae2442012-09-13 16:04:34 -06003985 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003986 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003987 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3988 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003989
Yan, Zhenga22285a2010-05-16 10:48:46 -04003990 trans = __unlink_start_trans(dir, dentry);
3991 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003992 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003993
Li Zefan33345d012011-04-20 10:31:50 +08003994 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003995 err = btrfs_unlink_subvol(trans, root, dir,
3996 BTRFS_I(inode)->location.objectid,
3997 dentry->d_name.name,
3998 dentry->d_name.len);
3999 goto out;
4000 }
4001
Josef Bacik7b128762008-07-24 12:17:14 -04004002 err = btrfs_orphan_add(trans, inode);
4003 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004004 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004005
Chris Mason39279cc2007-06-12 06:35:45 -04004006 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004007 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4008 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004009 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004010 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004011out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04004012 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004013 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004014
Chris Mason39279cc2007-06-12 06:35:45 -04004015 return err;
4016}
4017
Chris Mason323ac952008-10-01 19:05:46 -04004018/*
Chris Mason39279cc2007-06-12 06:35:45 -04004019 * this can truncate away extent items, csum items and directory items.
4020 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004021 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004022 *
4023 * csum items that cross the new i_size are truncated to the new size
4024 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004025 *
4026 * min_type is the minimum key type to truncate down to. If set to 0, this
4027 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004028 */
Yan, Zheng80825102009-11-12 09:35:36 +00004029int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4030 struct btrfs_root *root,
4031 struct inode *inode,
4032 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004033{
Chris Mason39279cc2007-06-12 06:35:45 -04004034 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004035 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004036 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004037 struct btrfs_key key;
4038 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004039 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004040 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004041 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004042 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004043 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004044 int found_extent;
4045 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004046 int pending_del_nr = 0;
4047 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004048 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004049 int ret;
4050 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004051 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004052
4053 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004054
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004055 path = btrfs_alloc_path();
4056 if (!path)
4057 return -ENOMEM;
4058 path->reada = -1;
4059
Josef Bacik5dc562c2012-08-17 13:14:17 -04004060 /*
4061 * We want to drop from the next block forward in case this new size is
4062 * not block aligned since we will be keeping the last block of the
4063 * extent just the way it is.
4064 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004065 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004066 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4067 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004068
Miao Xie16cdcec2011-04-22 18:12:22 +08004069 /*
4070 * This function is also used to drop the items in the log tree before
4071 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4072 * it is used to drop the loged items. So we shouldn't kill the delayed
4073 * items.
4074 */
4075 if (min_type == 0 && root == BTRFS_I(inode)->root)
4076 btrfs_kill_delayed_inode_items(inode);
4077
Li Zefan33345d012011-04-20 10:31:50 +08004078 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004079 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004080 key.type = (u8)-1;
4081
Chris Mason85e21ba2008-01-29 15:11:36 -05004082search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004083 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004084 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004085 if (ret < 0) {
4086 err = ret;
4087 goto out;
4088 }
Chris Masond3977122009-01-05 21:25:51 -05004089
Chris Mason85e21ba2008-01-29 15:11:36 -05004090 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004091 /* there are no items in the tree for us to truncate, we're
4092 * done
4093 */
Yan, Zheng80825102009-11-12 09:35:36 +00004094 if (path->slots[0] == 0)
4095 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004096 path->slots[0]--;
4097 }
4098
Chris Masond3977122009-01-05 21:25:51 -05004099 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004100 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004101 leaf = path->nodes[0];
4102 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4103 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004104
Li Zefan33345d012011-04-20 10:31:50 +08004105 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004106 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004107
Chris Mason85e21ba2008-01-29 15:11:36 -05004108 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004109 break;
4110
Chris Mason5f39d392007-10-15 16:14:19 -04004111 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004112 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004113 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004114 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004115 extent_type = btrfs_file_extent_type(leaf, fi);
4116 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004117 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004118 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004119 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004120 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004121 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004122 }
Yan008630c2007-11-07 13:31:09 -05004123 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004124 }
Yan, Zheng80825102009-11-12 09:35:36 +00004125 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004126 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004127 } else {
4128 if (item_end < new_size)
4129 break;
4130 if (found_key.offset >= new_size)
4131 del_item = 1;
4132 else
4133 del_item = 0;
4134 }
Chris Mason39279cc2007-06-12 06:35:45 -04004135 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004136 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004137 if (found_type != BTRFS_EXTENT_DATA_KEY)
4138 goto delete;
4139
4140 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004141 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004142 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004143 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004144 u64 orig_num_bytes =
4145 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004146 extent_num_bytes = ALIGN(new_size -
4147 found_key.offset,
4148 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004149 btrfs_set_file_extent_num_bytes(leaf, fi,
4150 extent_num_bytes);
4151 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004152 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004153 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004154 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004155 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004156 } else {
Chris Masondb945352007-10-15 16:15:53 -04004157 extent_num_bytes =
4158 btrfs_file_extent_disk_num_bytes(leaf,
4159 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004160 extent_offset = found_key.offset -
4161 btrfs_file_extent_offset(leaf, fi);
4162
Chris Mason39279cc2007-06-12 06:35:45 -04004163 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004164 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004165 if (extent_start != 0) {
4166 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004167 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004168 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004169 }
4170 }
Chris Mason90692182008-02-08 13:49:28 -05004171 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004172 /*
4173 * we can't truncate inline items that have had
4174 * special encodings
4175 */
4176 if (!del_item &&
4177 btrfs_file_extent_compression(leaf, fi) == 0 &&
4178 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4179 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004180 u32 size = new_size - found_key.offset;
4181
4182 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004183 inode_sub_bytes(inode, item_end + 1 -
4184 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004185 }
4186 size =
4187 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004188 btrfs_truncate_item(trans, root, path,
4189 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004190 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004191 inode_sub_bytes(inode, item_end + 1 -
4192 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004193 }
Chris Mason39279cc2007-06-12 06:35:45 -04004194 }
Chris Mason179e29e2007-11-01 11:28:41 -04004195delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004196 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004197 if (!pending_del_nr) {
4198 /* no pending yet, add ourselves */
4199 pending_del_slot = path->slots[0];
4200 pending_del_nr = 1;
4201 } else if (pending_del_nr &&
4202 path->slots[0] + 1 == pending_del_slot) {
4203 /* hop on the pending chunk */
4204 pending_del_nr++;
4205 pending_del_slot = path->slots[0];
4206 } else {
Chris Masond3977122009-01-05 21:25:51 -05004207 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004208 }
Chris Mason39279cc2007-06-12 06:35:45 -04004209 } else {
4210 break;
4211 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004212 if (found_extent && (root->ref_cows ||
4213 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004214 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004215 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004216 extent_num_bytes, 0,
4217 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004218 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004219 BUG_ON(ret);
4220 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004221
Yan, Zheng80825102009-11-12 09:35:36 +00004222 if (found_type == BTRFS_INODE_ITEM_KEY)
4223 break;
4224
4225 if (path->slots[0] == 0 ||
4226 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004227 if (pending_del_nr) {
4228 ret = btrfs_del_items(trans, root, path,
4229 pending_del_slot,
4230 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004231 if (ret) {
4232 btrfs_abort_transaction(trans,
4233 root, ret);
4234 goto error;
4235 }
Yan, Zheng80825102009-11-12 09:35:36 +00004236 pending_del_nr = 0;
4237 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004238 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004239 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004240 } else {
4241 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004242 }
Chris Mason39279cc2007-06-12 06:35:45 -04004243 }
Yan, Zheng80825102009-11-12 09:35:36 +00004244out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004245 if (pending_del_nr) {
4246 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4247 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004248 if (ret)
4249 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004250 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004251error:
Chris Mason39279cc2007-06-12 06:35:45 -04004252 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004253 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004254}
4255
Chris Masona52d9a82007-08-27 16:49:44 -04004256/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004257 * btrfs_truncate_page - read, zero a chunk and write a page
4258 * @inode - inode that we're zeroing
4259 * @from - the offset to start zeroing
4260 * @len - the length to zero, 0 to zero the entire range respective to the
4261 * offset
4262 * @front - zero up to the offset instead of from the offset on
4263 *
4264 * This will find the page for the "from" offset and cow the page and zero the
4265 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004266 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004267int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4268 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004269{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004270 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004271 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004272 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4273 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004274 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004275 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004276 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004277 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4278 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4279 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004280 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004281 int ret = 0;
4282 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004283 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004284
Josef Bacik2aaa6652012-08-29 14:27:18 -04004285 if ((offset & (blocksize - 1)) == 0 &&
4286 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004287 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004288 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004289 if (ret)
4290 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004291
Chris Mason211c17f2008-05-15 09:13:45 -04004292again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004293 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004294 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004295 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004296 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004297 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004298 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004299
4300 page_start = page_offset(page);
4301 page_end = page_start + PAGE_CACHE_SIZE - 1;
4302
Chris Masona52d9a82007-08-27 16:49:44 -04004303 if (!PageUptodate(page)) {
4304 ret = btrfs_readpage(NULL, page);
4305 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004306 if (page->mapping != mapping) {
4307 unlock_page(page);
4308 page_cache_release(page);
4309 goto again;
4310 }
Chris Masona52d9a82007-08-27 16:49:44 -04004311 if (!PageUptodate(page)) {
4312 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004313 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004314 }
4315 }
Chris Mason211c17f2008-05-15 09:13:45 -04004316 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004317
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004318 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004319 set_page_extent_mapped(page);
4320
4321 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4322 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004323 unlock_extent_cached(io_tree, page_start, page_end,
4324 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004325 unlock_page(page);
4326 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004327 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004328 btrfs_put_ordered_extent(ordered);
4329 goto again;
4330 }
4331
Josef Bacik2ac55d42010-02-03 19:33:23 +00004332 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004333 EXTENT_DIRTY | EXTENT_DELALLOC |
4334 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004335 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004336
Josef Bacik2ac55d42010-02-03 19:33:23 +00004337 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4338 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004339 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004340 unlock_extent_cached(io_tree, page_start, page_end,
4341 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004342 goto out_unlock;
4343 }
4344
Chris Masone6dcd2d2008-07-17 12:53:50 -04004345 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004346 if (!len)
4347 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004348 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004349 if (front)
4350 memset(kaddr, 0, offset);
4351 else
4352 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004353 flush_dcache_page(page);
4354 kunmap(page);
4355 }
Chris Mason247e7432008-07-17 12:53:51 -04004356 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004357 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004358 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4359 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004360
Chris Mason89642222008-07-24 09:41:53 -04004361out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004362 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004363 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004364 unlock_page(page);
4365 page_cache_release(page);
4366out:
4367 return ret;
4368}
4369
Josef Bacik695a0d02011-03-04 15:46:53 -05004370/*
4371 * This function puts in dummy file extents for the area we're creating a hole
4372 * for. So if we are truncating this file to a larger size we need to insert
4373 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4374 * the range between oldsize and size
4375 */
Josef Bacika41ad392011-01-31 15:30:16 -05004376int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004377{
4378 struct btrfs_trans_handle *trans;
4379 struct btrfs_root *root = BTRFS_I(inode)->root;
4380 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004381 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004382 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004383 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004384 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4385 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004386 u64 last_byte;
4387 u64 cur_offset;
4388 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004389 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004390
4391 if (size <= hole_start)
4392 return 0;
4393
Yan Zheng9036c102008-10-30 14:19:41 -04004394 while (1) {
4395 struct btrfs_ordered_extent *ordered;
4396 btrfs_wait_ordered_range(inode, hole_start,
4397 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004398 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004399 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004400 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4401 if (!ordered)
4402 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004403 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4404 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004405 btrfs_put_ordered_extent(ordered);
4406 }
4407
Yan Zheng9036c102008-10-30 14:19:41 -04004408 cur_offset = hole_start;
4409 while (1) {
4410 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4411 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004412 if (IS_ERR(em)) {
4413 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004414 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004415 break;
4416 }
Yan Zheng9036c102008-10-30 14:19:41 -04004417 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004418 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004419 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004420 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004421 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004422
Miao Xie36423202011-12-14 20:12:02 -05004423 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004424 if (IS_ERR(trans)) {
4425 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004426 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004427 }
Yan, Zheng80825102009-11-12 09:35:36 +00004428
Josef Bacik5dc562c2012-08-17 13:14:17 -04004429 err = btrfs_drop_extents(trans, root, inode,
4430 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004431 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004432 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004433 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004434 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004435 break;
Miao Xie5b397372011-09-11 10:52:24 -04004436 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004437
Yan Zheng9036c102008-10-30 14:19:41 -04004438 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004439 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004440 0, hole_size, 0, hole_size,
4441 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004442 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004443 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004444 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004445 break;
Miao Xie5b397372011-09-11 10:52:24 -04004446 }
Yan, Zheng80825102009-11-12 09:35:36 +00004447
Josef Bacik5dc562c2012-08-17 13:14:17 -04004448 btrfs_drop_extent_cache(inode, cur_offset,
4449 cur_offset + hole_size - 1, 0);
4450 hole_em = alloc_extent_map();
4451 if (!hole_em) {
4452 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4453 &BTRFS_I(inode)->runtime_flags);
4454 goto next;
4455 }
4456 hole_em->start = cur_offset;
4457 hole_em->len = hole_size;
4458 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004459
Josef Bacik5dc562c2012-08-17 13:14:17 -04004460 hole_em->block_start = EXTENT_MAP_HOLE;
4461 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004462 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004463 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004464 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4465 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4466 hole_em->generation = trans->transid;
4467
4468 while (1) {
4469 write_lock(&em_tree->lock);
4470 err = add_extent_mapping(em_tree, hole_em);
4471 if (!err)
4472 list_move(&hole_em->list,
4473 &em_tree->modified_extents);
4474 write_unlock(&em_tree->lock);
4475 if (err != -EEXIST)
4476 break;
4477 btrfs_drop_extent_cache(inode, cur_offset,
4478 cur_offset +
4479 hole_size - 1, 0);
4480 }
4481 free_extent_map(hole_em);
4482next:
Miao Xie36423202011-12-14 20:12:02 -05004483 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004484 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004485 }
4486 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004487 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004488 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004489 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004490 break;
4491 }
4492
Yan, Zhenga22285a2010-05-16 10:48:46 -04004493 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004494 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4495 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004496 return err;
4497}
4498
Eric Sandeen3972f262013-01-12 02:57:22 +00004499static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004500{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004501 struct btrfs_root *root = BTRFS_I(inode)->root;
4502 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004503 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004504 loff_t newsize = attr->ia_size;
4505 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004506 int ret;
4507
Josef Bacika41ad392011-01-31 15:30:16 -05004508 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004509 return 0;
4510
Eric Sandeen3972f262013-01-12 02:57:22 +00004511 /*
4512 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4513 * special case where we need to update the times despite not having
4514 * these flags set. For all other operations the VFS set these flags
4515 * explicitly if it wants a timestamp update.
4516 */
4517 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4518 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4519
Josef Bacika41ad392011-01-31 15:30:16 -05004520 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004521 truncate_pagecache(inode, oldsize, newsize);
4522 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004523 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004524 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004525
Miao Xief4a2f4c2011-12-14 20:12:01 -05004526 trans = btrfs_start_transaction(root, 1);
4527 if (IS_ERR(trans))
4528 return PTR_ERR(trans);
4529
4530 i_size_write(inode, newsize);
4531 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4532 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004533 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004534 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004535
Josef Bacika41ad392011-01-31 15:30:16 -05004536 /*
4537 * We're truncating a file that used to have good data down to
4538 * zero. Make sure it gets into the ordered flush list so that
4539 * any new writes get down to disk quickly.
4540 */
4541 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004542 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4543 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004544
Josef Bacikf3fe8202013-01-07 17:03:21 -05004545 /*
4546 * 1 for the orphan item we're going to add
4547 * 1 for the orphan item deletion.
4548 */
4549 trans = btrfs_start_transaction(root, 2);
4550 if (IS_ERR(trans))
4551 return PTR_ERR(trans);
4552
4553 /*
4554 * We need to do this in case we fail at _any_ point during the
4555 * actual truncate. Once we do the truncate_setsize we could
4556 * invalidate pages which forces any outstanding ordered io to
4557 * be instantly completed which will give us extents that need
4558 * to be truncated. If we fail to get an orphan inode down we
4559 * could have left over extents that were never meant to live,
4560 * so we need to garuntee from this point on that everything
4561 * will be consistent.
4562 */
4563 ret = btrfs_orphan_add(trans, inode);
4564 btrfs_end_transaction(trans, root);
4565 if (ret)
4566 return ret;
4567
Josef Bacika41ad392011-01-31 15:30:16 -05004568 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4569 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004570
4571 /* Disable nonlocked read DIO to avoid the end less truncate */
4572 btrfs_inode_block_unlocked_dio(inode);
4573 inode_dio_wait(inode);
4574 btrfs_inode_resume_unlocked_dio(inode);
4575
Josef Bacika41ad392011-01-31 15:30:16 -05004576 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004577 if (ret && inode->i_nlink)
4578 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004579 }
4580
Josef Bacika41ad392011-01-31 15:30:16 -05004581 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004582}
4583
Chris Mason39279cc2007-06-12 06:35:45 -04004584static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4585{
4586 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004587 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004588 int err;
4589
Li Zefanb83cc962010-12-20 16:04:08 +08004590 if (btrfs_root_readonly(root))
4591 return -EROFS;
4592
Chris Mason39279cc2007-06-12 06:35:45 -04004593 err = inode_change_ok(inode, attr);
4594 if (err)
4595 return err;
4596
Chris Mason5a3f23d2009-03-31 13:27:11 -04004597 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004598 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004599 if (err)
4600 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004601 }
Yan Zheng9036c102008-10-30 14:19:41 -04004602
Christoph Hellwig10257742010-06-04 11:30:02 +02004603 if (attr->ia_valid) {
4604 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004605 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004606 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004607
Josef Bacik22c44fe2011-11-30 10:45:38 -05004608 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004609 err = btrfs_acl_chmod(inode);
4610 }
4611
Chris Mason39279cc2007-06-12 06:35:45 -04004612 return err;
4613}
Chris Mason61295eb2008-01-14 16:24:38 -05004614
Al Virobd555972010-06-07 11:35:40 -04004615void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004616{
4617 struct btrfs_trans_handle *trans;
4618 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004619 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004620 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004621 int ret;
4622
liubo1abe9b82011-03-24 11:18:59 +00004623 trace_btrfs_inode_evict(inode);
4624
Chris Mason39279cc2007-06-12 06:35:45 -04004625 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004626 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004627 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004628 goto no_delete;
4629
Chris Mason39279cc2007-06-12 06:35:45 -04004630 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004631 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004632 goto no_delete;
4633 }
Al Virobd555972010-06-07 11:35:40 -04004634 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004635 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004636
Yan, Zhengc71bf092009-11-12 09:34:40 +00004637 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004638 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004639 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004640 goto no_delete;
4641 }
4642
Yan, Zheng76dda932009-09-21 16:00:26 -04004643 if (inode->i_nlink > 0) {
4644 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4645 goto no_delete;
4646 }
4647
Miao Xie0e8c36a2012-12-19 06:59:51 +00004648 ret = btrfs_commit_inode_delayed_inode(inode);
4649 if (ret) {
4650 btrfs_orphan_del(NULL, inode);
4651 goto no_delete;
4652 }
4653
Miao Xie66d8f3d2012-09-06 04:02:28 -06004654 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004655 if (!rsv) {
4656 btrfs_orphan_del(NULL, inode);
4657 goto no_delete;
4658 }
Josef Bacik4a338542011-08-29 11:01:31 -04004659 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004660 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004661 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004662
Chris Masondbe674a2008-07-17 12:54:05 -04004663 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004664
Josef Bacik4289a662011-08-05 13:22:24 -04004665 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004666 * This is a bit simpler than btrfs_truncate since we've already
4667 * reserved our space for our orphan item in the unlink, so we just
4668 * need to reserve some slack space in case we add bytes and update
4669 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004670 */
Yan, Zheng80825102009-11-12 09:35:36 +00004671 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004672 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4673 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004674
Josef Bacik726c35f2011-09-26 15:46:06 -04004675 /*
4676 * Try and steal from the global reserve since we will
4677 * likely not use this space anyway, we want to try as
4678 * hard as possible to get this to work.
4679 */
4680 if (ret)
4681 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4682
Yan, Zhengd68fc572010-05-16 10:49:58 -04004683 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004684 btrfs_warn(root->fs_info,
4685 "Could not get space for a delete, will truncate on mount %d",
4686 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004687 btrfs_orphan_del(NULL, inode);
4688 btrfs_free_block_rsv(root, rsv);
4689 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004690 }
4691
Miao Xie0e8c36a2012-12-19 06:59:51 +00004692 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004693 if (IS_ERR(trans)) {
4694 btrfs_orphan_del(NULL, inode);
4695 btrfs_free_block_rsv(root, rsv);
4696 goto no_delete;
4697 }
4698
4699 trans->block_rsv = rsv;
4700
Yan, Zhengd68fc572010-05-16 10:49:58 -04004701 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004702 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004703 break;
4704
Miao Xie8407aa42012-09-07 01:43:32 -06004705 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004706 btrfs_end_transaction(trans, root);
4707 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004708 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004709 }
4710
Josef Bacik4289a662011-08-05 13:22:24 -04004711 btrfs_free_block_rsv(root, rsv);
4712
Yan, Zheng80825102009-11-12 09:35:36 +00004713 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004714 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004715 ret = btrfs_orphan_del(trans, inode);
4716 BUG_ON(ret);
4717 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004718
Josef Bacik4289a662011-08-05 13:22:24 -04004719 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004720 if (!(root == root->fs_info->tree_root ||
4721 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004722 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004723
Chris Mason54aa1f42007-06-22 14:16:25 -04004724 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004725 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004726no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004727 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004728 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004729}
4730
4731/*
4732 * this returns the key found in the dir entry in the location pointer.
4733 * If no dir entries were found, location->objectid is 0.
4734 */
4735static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4736 struct btrfs_key *location)
4737{
4738 const char *name = dentry->d_name.name;
4739 int namelen = dentry->d_name.len;
4740 struct btrfs_dir_item *di;
4741 struct btrfs_path *path;
4742 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004743 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004744
4745 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004746 if (!path)
4747 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004748
Li Zefan33345d012011-04-20 10:31:50 +08004749 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004750 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004751 if (IS_ERR(di))
4752 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004753
David Sterbac7040052011-04-19 18:00:01 +02004754 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004755 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004756
Chris Mason5f39d392007-10-15 16:14:19 -04004757 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004758out:
Chris Mason39279cc2007-06-12 06:35:45 -04004759 btrfs_free_path(path);
4760 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004761out_err:
4762 location->objectid = 0;
4763 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004764}
4765
4766/*
4767 * when we hit a tree root in a directory, the btrfs part of the inode
4768 * needs to be changed to reflect the root directory of the tree root. This
4769 * is kind of like crossing a mount point.
4770 */
4771static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004772 struct inode *dir,
4773 struct dentry *dentry,
4774 struct btrfs_key *location,
4775 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004776{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004777 struct btrfs_path *path;
4778 struct btrfs_root *new_root;
4779 struct btrfs_root_ref *ref;
4780 struct extent_buffer *leaf;
4781 int ret;
4782 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004783
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004784 path = btrfs_alloc_path();
4785 if (!path) {
4786 err = -ENOMEM;
4787 goto out;
4788 }
Chris Mason39279cc2007-06-12 06:35:45 -04004789
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004790 err = -ENOENT;
4791 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4792 BTRFS_I(dir)->root->root_key.objectid,
4793 location->objectid);
4794 if (ret) {
4795 if (ret < 0)
4796 err = ret;
4797 goto out;
4798 }
Chris Mason39279cc2007-06-12 06:35:45 -04004799
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004800 leaf = path->nodes[0];
4801 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004802 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004803 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4804 goto out;
4805
4806 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4807 (unsigned long)(ref + 1),
4808 dentry->d_name.len);
4809 if (ret)
4810 goto out;
4811
David Sterbab3b4aa72011-04-21 01:20:15 +02004812 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004813
4814 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4815 if (IS_ERR(new_root)) {
4816 err = PTR_ERR(new_root);
4817 goto out;
4818 }
4819
4820 if (btrfs_root_refs(&new_root->root_item) == 0) {
4821 err = -ENOENT;
4822 goto out;
4823 }
4824
4825 *sub_root = new_root;
4826 location->objectid = btrfs_root_dirid(&new_root->root_item);
4827 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004828 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004829 err = 0;
4830out:
4831 btrfs_free_path(path);
4832 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004833}
4834
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004835static void inode_tree_add(struct inode *inode)
4836{
4837 struct btrfs_root *root = BTRFS_I(inode)->root;
4838 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004839 struct rb_node **p;
4840 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004841 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004842again:
4843 p = &root->inode_tree.rb_node;
4844 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004845
Al Viro1d3382cb2010-10-23 15:19:20 -04004846 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004847 return;
4848
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004849 spin_lock(&root->inode_lock);
4850 while (*p) {
4851 parent = *p;
4852 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4853
Li Zefan33345d012011-04-20 10:31:50 +08004854 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004855 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004856 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004857 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004858 else {
4859 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004860 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004861 rb_erase(parent, &root->inode_tree);
4862 RB_CLEAR_NODE(parent);
4863 spin_unlock(&root->inode_lock);
4864 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004865 }
4866 }
4867 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4868 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4869 spin_unlock(&root->inode_lock);
4870}
4871
4872static void inode_tree_del(struct inode *inode)
4873{
4874 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004875 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004876
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004877 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004878 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004879 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004880 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004881 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004882 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004883 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004884
Josef Bacik0af3d002010-06-21 14:48:16 -04004885 /*
4886 * Free space cache has inodes in the tree root, but the tree root has a
4887 * root_refs of 0, so this could end up dropping the tree root as a
4888 * snapshot, so we need the extra !root->fs_info->tree_root check to
4889 * make sure we don't drop it.
4890 */
4891 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4892 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004893 synchronize_srcu(&root->fs_info->subvol_srcu);
4894 spin_lock(&root->inode_lock);
4895 empty = RB_EMPTY_ROOT(&root->inode_tree);
4896 spin_unlock(&root->inode_lock);
4897 if (empty)
4898 btrfs_add_dead_root(root);
4899 }
4900}
4901
Jeff Mahoney143bede2012-03-01 14:56:26 +01004902void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004903{
4904 struct rb_node *node;
4905 struct rb_node *prev;
4906 struct btrfs_inode *entry;
4907 struct inode *inode;
4908 u64 objectid = 0;
4909
4910 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4911
4912 spin_lock(&root->inode_lock);
4913again:
4914 node = root->inode_tree.rb_node;
4915 prev = NULL;
4916 while (node) {
4917 prev = node;
4918 entry = rb_entry(node, struct btrfs_inode, rb_node);
4919
Li Zefan33345d012011-04-20 10:31:50 +08004920 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004921 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004922 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004923 node = node->rb_right;
4924 else
4925 break;
4926 }
4927 if (!node) {
4928 while (prev) {
4929 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004930 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004931 node = prev;
4932 break;
4933 }
4934 prev = rb_next(prev);
4935 }
4936 }
4937 while (node) {
4938 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004939 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004940 inode = igrab(&entry->vfs_inode);
4941 if (inode) {
4942 spin_unlock(&root->inode_lock);
4943 if (atomic_read(&inode->i_count) > 1)
4944 d_prune_aliases(inode);
4945 /*
Al Viro45321ac2010-06-07 13:43:19 -04004946 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004947 * the inode cache when its usage count
4948 * hits zero.
4949 */
4950 iput(inode);
4951 cond_resched();
4952 spin_lock(&root->inode_lock);
4953 goto again;
4954 }
4955
4956 if (cond_resched_lock(&root->inode_lock))
4957 goto again;
4958
4959 node = rb_next(node);
4960 }
4961 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004962}
4963
Chris Masone02119d2008-09-05 16:13:11 -04004964static int btrfs_init_locked_inode(struct inode *inode, void *p)
4965{
4966 struct btrfs_iget_args *args = p;
4967 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004968 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004969 return 0;
4970}
4971
4972static int btrfs_find_actor(struct inode *inode, void *opaque)
4973{
4974 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004975 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004976 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004977}
4978
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004979static struct inode *btrfs_iget_locked(struct super_block *s,
4980 u64 objectid,
4981 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004982{
4983 struct inode *inode;
4984 struct btrfs_iget_args args;
4985 args.ino = objectid;
4986 args.root = root;
4987
4988 inode = iget5_locked(s, objectid, btrfs_find_actor,
4989 btrfs_init_locked_inode,
4990 (void *)&args);
4991 return inode;
4992}
4993
Balaji Rao1a54ef82008-07-21 02:01:04 +05304994/* Get an inode object given its location and corresponding root.
4995 * Returns in *is_new if the inode was read from disk
4996 */
4997struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004998 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304999{
5000 struct inode *inode;
5001
5002 inode = btrfs_iget_locked(s, location->objectid, root);
5003 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005004 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305005
5006 if (inode->i_state & I_NEW) {
5007 BTRFS_I(inode)->root = root;
5008 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
5009 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005010 if (!is_bad_inode(inode)) {
5011 inode_tree_add(inode);
5012 unlock_new_inode(inode);
5013 if (new)
5014 *new = 1;
5015 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005016 unlock_new_inode(inode);
5017 iput(inode);
5018 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005019 }
5020 }
5021
Balaji Rao1a54ef82008-07-21 02:01:04 +05305022 return inode;
5023}
5024
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005025static struct inode *new_simple_dir(struct super_block *s,
5026 struct btrfs_key *key,
5027 struct btrfs_root *root)
5028{
5029 struct inode *inode = new_inode(s);
5030
5031 if (!inode)
5032 return ERR_PTR(-ENOMEM);
5033
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005034 BTRFS_I(inode)->root = root;
5035 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005036 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005037
5038 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005039 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005040 inode->i_fop = &simple_dir_operations;
5041 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5042 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5043
5044 return inode;
5045}
5046
Chris Mason3de45862008-11-17 21:02:50 -05005047struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005048{
Chris Masond3977122009-01-05 21:25:51 -05005049 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005050 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005051 struct btrfs_root *sub_root = root;
5052 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005053 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005054 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005055
5056 if (dentry->d_name.len > BTRFS_NAME_LEN)
5057 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005058
Jeff Layton39e3c952012-11-28 11:30:53 -05005059 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005060 if (ret < 0)
5061 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005062
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005063 if (location.objectid == 0)
5064 return NULL;
5065
5066 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005067 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005068 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005069 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005070
5071 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5072
Yan, Zheng76dda932009-09-21 16:00:26 -04005073 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005074 ret = fixup_tree_root_location(root, dir, dentry,
5075 &location, &sub_root);
5076 if (ret < 0) {
5077 if (ret != -ENOENT)
5078 inode = ERR_PTR(ret);
5079 else
5080 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5081 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005082 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005083 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005084 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5085
Julia Lawall34d19ba2011-01-24 19:55:19 +00005086 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005087 down_read(&root->fs_info->cleanup_work_sem);
5088 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005089 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005090 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005091 if (ret)
5092 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005093 }
5094
Chris Mason3de45862008-11-17 21:02:50 -05005095 return inode;
5096}
5097
Nick Pigginfe15ce42011-01-07 17:49:23 +11005098static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005099{
5100 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005101 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005102
Li Zefan848cce02012-02-21 17:04:28 +08005103 if (!inode && !IS_ROOT(dentry))
5104 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005105
Li Zefan848cce02012-02-21 17:04:28 +08005106 if (inode) {
5107 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005108 if (btrfs_root_refs(&root->root_item) == 0)
5109 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005110
5111 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5112 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005113 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005114 return 0;
5115}
5116
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005117static void btrfs_dentry_release(struct dentry *dentry)
5118{
5119 if (dentry->d_fsdata)
5120 kfree(dentry->d_fsdata);
5121}
5122
Chris Mason3de45862008-11-17 21:02:50 -05005123static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005124 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005125{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005126 struct dentry *ret;
5127
5128 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005129 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005130}
5131
Miao Xie16cdcec2011-04-22 18:12:22 +08005132unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005133 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5134};
5135
David Woodhousecbdf5a22008-08-06 19:42:33 +01005136static int btrfs_real_readdir(struct file *filp, void *dirent,
5137 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005138{
Al Viro496ad9a2013-01-23 17:07:38 -05005139 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005140 struct btrfs_root *root = BTRFS_I(inode)->root;
5141 struct btrfs_item *item;
5142 struct btrfs_dir_item *di;
5143 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005144 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005145 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005146 struct list_head ins_list;
5147 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005148 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005149 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005150 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005151 unsigned char d_type;
5152 int over = 0;
5153 u32 di_cur;
5154 u32 di_total;
5155 u32 di_len;
5156 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005157 char tmp_name[32];
5158 char *name_ptr;
5159 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005160 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005161
5162 /* FIXME, use a real flag for deciding about the key type */
5163 if (root->fs_info->tree_root == root)
5164 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005165
Chris Mason39544012007-12-12 14:38:19 -05005166 /* special case for "." */
5167 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005168 over = filldir(dirent, ".", 1,
5169 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005170 if (over)
5171 return 0;
5172 filp->f_pos = 1;
5173 }
Chris Mason39544012007-12-12 14:38:19 -05005174 /* special case for .., just use the back ref */
5175 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005176 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005177 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005178 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005179 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005180 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005181 filp->f_pos = 2;
5182 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005183 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005184 if (!path)
5185 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005186
Josef Bacik026fd312011-05-13 10:32:11 -04005187 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005188
Miao Xie16cdcec2011-04-22 18:12:22 +08005189 if (key_type == BTRFS_DIR_INDEX_KEY) {
5190 INIT_LIST_HEAD(&ins_list);
5191 INIT_LIST_HEAD(&del_list);
5192 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5193 }
5194
Chris Mason39279cc2007-06-12 06:35:45 -04005195 btrfs_set_key_type(&key, key_type);
5196 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005197 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005198
Chris Mason39279cc2007-06-12 06:35:45 -04005199 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5200 if (ret < 0)
5201 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005202
5203 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005204 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005205 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005206 if (slot >= btrfs_header_nritems(leaf)) {
5207 ret = btrfs_next_leaf(root, path);
5208 if (ret < 0)
5209 goto err;
5210 else if (ret > 0)
5211 break;
5212 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005213 }
Chris Mason3de45862008-11-17 21:02:50 -05005214
Chris Mason5f39d392007-10-15 16:14:19 -04005215 item = btrfs_item_nr(leaf, slot);
5216 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5217
5218 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005219 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005220 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005221 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005222 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005223 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005224 if (key_type == BTRFS_DIR_INDEX_KEY &&
5225 btrfs_should_delete_dir_index(&del_list,
5226 found_key.offset))
5227 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005228
5229 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005230 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005231
Chris Mason39279cc2007-06-12 06:35:45 -04005232 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5233 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005234 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005235
5236 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005237 struct btrfs_key location;
5238
Josef Bacik22a94d42011-03-16 16:47:17 -04005239 if (verify_dir_item(root, leaf, di))
5240 break;
5241
Chris Mason5f39d392007-10-15 16:14:19 -04005242 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005243 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005244 name_ptr = tmp_name;
5245 } else {
5246 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005247 if (!name_ptr) {
5248 ret = -ENOMEM;
5249 goto err;
5250 }
Chris Mason5f39d392007-10-15 16:14:19 -04005251 }
5252 read_extent_buffer(leaf, name_ptr,
5253 (unsigned long)(di + 1), name_len);
5254
5255 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5256 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005257
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005258
Chris Mason3de45862008-11-17 21:02:50 -05005259 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005260 * skip it.
5261 *
5262 * In contrast to old kernels, we insert the snapshot's
5263 * dir item and dir index after it has been created, so
5264 * we won't find a reference to our own snapshot. We
5265 * still keep the following code for backward
5266 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005267 */
5268 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5269 location.objectid == root->root_key.objectid) {
5270 over = 0;
5271 goto skip;
5272 }
Chris Mason5f39d392007-10-15 16:14:19 -04005273 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005274 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005275 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005276
Chris Mason3de45862008-11-17 21:02:50 -05005277skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005278 if (name_ptr != tmp_name)
5279 kfree(name_ptr);
5280
Chris Mason39279cc2007-06-12 06:35:45 -04005281 if (over)
5282 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005283 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005284 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005285 di_cur += di_len;
5286 di = (struct btrfs_dir_item *)((char *)di + di_len);
5287 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005288next:
5289 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005290 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005291
Miao Xie16cdcec2011-04-22 18:12:22 +08005292 if (key_type == BTRFS_DIR_INDEX_KEY) {
5293 if (is_curr)
5294 filp->f_pos++;
5295 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5296 &ins_list);
5297 if (ret)
5298 goto nopos;
5299 }
5300
David Woodhouse49593bf2008-08-17 17:08:36 +01005301 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005302 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005303 /*
5304 * 32-bit glibc will use getdents64, but then strtol -
5305 * so the last number we can serve is this.
5306 */
5307 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005308 else
5309 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005310nopos:
5311 ret = 0;
5312err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005313 if (key_type == BTRFS_DIR_INDEX_KEY)
5314 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005315 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005316 return ret;
5317}
5318
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005319int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005320{
5321 struct btrfs_root *root = BTRFS_I(inode)->root;
5322 struct btrfs_trans_handle *trans;
5323 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005324 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005325
Josef Bacik72ac3c02012-05-23 14:13:11 -04005326 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005327 return 0;
5328
Liu Bo83eea1f2012-07-10 05:28:39 -06005329 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005330 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005331
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005332 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005333 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005334 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005335 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005336 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005337 if (IS_ERR(trans))
5338 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005339 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005340 }
5341 return ret;
5342}
5343
5344/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005345 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005346 * inode changes. But, it is most likely to find the inode in cache.
5347 * FIXME, needs more benchmarking...there are no reasons other than performance
5348 * to keep or drop this code.
5349 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05005350int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005351{
5352 struct btrfs_root *root = BTRFS_I(inode)->root;
5353 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005354 int ret;
5355
Josef Bacik72ac3c02012-05-23 14:13:11 -04005356 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005357 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005358
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005359 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005360 if (IS_ERR(trans))
5361 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005362
5363 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005364 if (ret && ret == -ENOSPC) {
5365 /* whoops, lets try again with the full transaction */
5366 btrfs_end_transaction(trans, root);
5367 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005368 if (IS_ERR(trans))
5369 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005370
Chris Mason94b60442010-05-26 11:02:00 -04005371 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005372 }
Chris Mason39279cc2007-06-12 06:35:45 -04005373 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005374 if (BTRFS_I(inode)->delayed_node)
5375 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005376
5377 return ret;
5378}
5379
5380/*
5381 * This is a copy of file_update_time. We need this so we can return error on
5382 * ENOSPC for updating the inode in the case of file write and mmap writes.
5383 */
Josef Bacike41f9412012-03-26 09:46:47 -04005384static int btrfs_update_time(struct inode *inode, struct timespec *now,
5385 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005386{
Alexander Block2bc5565282012-06-15 09:49:33 +02005387 struct btrfs_root *root = BTRFS_I(inode)->root;
5388
5389 if (btrfs_root_readonly(root))
5390 return -EROFS;
5391
Josef Bacike41f9412012-03-26 09:46:47 -04005392 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005393 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005394 if (flags & S_CTIME)
5395 inode->i_ctime = *now;
5396 if (flags & S_MTIME)
5397 inode->i_mtime = *now;
5398 if (flags & S_ATIME)
5399 inode->i_atime = *now;
5400 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005401}
5402
Chris Masond352ac62008-09-29 15:18:18 -04005403/*
5404 * find the highest existing sequence number in a directory
5405 * and then set the in-memory index_cnt variable to reflect
5406 * free sequence numbers
5407 */
Josef Bacikaec74772008-07-24 12:12:38 -04005408static int btrfs_set_inode_index_count(struct inode *inode)
5409{
5410 struct btrfs_root *root = BTRFS_I(inode)->root;
5411 struct btrfs_key key, found_key;
5412 struct btrfs_path *path;
5413 struct extent_buffer *leaf;
5414 int ret;
5415
Li Zefan33345d012011-04-20 10:31:50 +08005416 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005417 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5418 key.offset = (u64)-1;
5419
5420 path = btrfs_alloc_path();
5421 if (!path)
5422 return -ENOMEM;
5423
5424 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5425 if (ret < 0)
5426 goto out;
5427 /* FIXME: we should be able to handle this */
5428 if (ret == 0)
5429 goto out;
5430 ret = 0;
5431
5432 /*
5433 * MAGIC NUMBER EXPLANATION:
5434 * since we search a directory based on f_pos we have to start at 2
5435 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5436 * else has to start at 2
5437 */
5438 if (path->slots[0] == 0) {
5439 BTRFS_I(inode)->index_cnt = 2;
5440 goto out;
5441 }
5442
5443 path->slots[0]--;
5444
5445 leaf = path->nodes[0];
5446 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5447
Li Zefan33345d012011-04-20 10:31:50 +08005448 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005449 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5450 BTRFS_I(inode)->index_cnt = 2;
5451 goto out;
5452 }
5453
5454 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5455out:
5456 btrfs_free_path(path);
5457 return ret;
5458}
5459
Chris Masond352ac62008-09-29 15:18:18 -04005460/*
5461 * helper to find a free sequence number in a given directory. This current
5462 * code is very simple, later versions will do smarter things in the btree
5463 */
Chris Mason3de45862008-11-17 21:02:50 -05005464int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005465{
5466 int ret = 0;
5467
5468 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005469 ret = btrfs_inode_delayed_dir_index_count(dir);
5470 if (ret) {
5471 ret = btrfs_set_inode_index_count(dir);
5472 if (ret)
5473 return ret;
5474 }
Josef Bacikaec74772008-07-24 12:12:38 -04005475 }
5476
Chris Mason00e4e6b2008-08-05 11:18:09 -04005477 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005478 BTRFS_I(dir)->index_cnt++;
5479
5480 return ret;
5481}
5482
Chris Mason39279cc2007-06-12 06:35:45 -04005483static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5484 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005485 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005486 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005487 u64 ref_objectid, u64 objectid,
5488 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005489{
5490 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005491 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005492 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005493 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005494 struct btrfs_inode_ref *ref;
5495 struct btrfs_key key[2];
5496 u32 sizes[2];
5497 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005498 int ret;
5499 int owner;
5500
Chris Mason5f39d392007-10-15 16:14:19 -04005501 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005502 if (!path)
5503 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005504
Chris Mason39279cc2007-06-12 06:35:45 -04005505 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005506 if (!inode) {
5507 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005508 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005509 }
Chris Mason39279cc2007-06-12 06:35:45 -04005510
Li Zefan581bb052011-04-20 10:06:11 +08005511 /*
5512 * we have to initialize this early, so we can reclaim the inode
5513 * number if we fail afterwards in this function.
5514 */
5515 inode->i_ino = objectid;
5516
Josef Bacikaec74772008-07-24 12:12:38 -04005517 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005518 trace_btrfs_inode_request(dir);
5519
Chris Mason3de45862008-11-17 21:02:50 -05005520 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005521 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005522 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005523 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005524 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005525 }
Josef Bacikaec74772008-07-24 12:12:38 -04005526 }
5527 /*
5528 * index_cnt is ignored for everything but a dir,
5529 * btrfs_get_inode_index_count has an explanation for the magic
5530 * number
5531 */
5532 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005533 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005534 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005535 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005536
Josef Bacik5dc562c2012-08-17 13:14:17 -04005537 /*
5538 * We could have gotten an inode number from somebody who was fsynced
5539 * and then removed in this same transaction, so let's just set full
5540 * sync since it will be a full sync anyway and this will blow away the
5541 * old info in the log.
5542 */
5543 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5544
Al Viro569254b2011-07-24 17:08:40 -04005545 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005546 owner = 0;
5547 else
5548 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005549
5550 key[0].objectid = objectid;
5551 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5552 key[0].offset = 0;
5553
Mark Fashehf1863732012-08-08 11:32:27 -07005554 /*
5555 * Start new inodes with an inode_ref. This is slightly more
5556 * efficient for small numbers of hard links since they will
5557 * be packed into one item. Extended refs will kick in if we
5558 * add more hard links than can fit in the ref item.
5559 */
Chris Mason9c583092008-01-29 15:15:18 -05005560 key[1].objectid = objectid;
5561 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5562 key[1].offset = ref_objectid;
5563
5564 sizes[0] = sizeof(struct btrfs_inode_item);
5565 sizes[1] = name_len + sizeof(*ref);
5566
Chris Masonb9473432009-03-13 11:00:37 -04005567 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005568 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5569 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005570 goto fail;
5571
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005572 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005573 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005574 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005575 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5576 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005577 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5578 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005579 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005580
5581 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5582 struct btrfs_inode_ref);
5583 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005584 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005585 ptr = (unsigned long)(ref + 1);
5586 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5587
Chris Mason5f39d392007-10-15 16:14:19 -04005588 btrfs_mark_buffer_dirty(path->nodes[0]);
5589 btrfs_free_path(path);
5590
Chris Mason39279cc2007-06-12 06:35:45 -04005591 location = &BTRFS_I(inode)->location;
5592 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005593 location->offset = 0;
5594 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5595
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005596 btrfs_inherit_iflags(inode, dir);
5597
Al Viro569254b2011-07-24 17:08:40 -04005598 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005599 if (btrfs_test_opt(root, NODATASUM))
5600 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005601 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005602 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5603 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005604 }
5605
Chris Mason39279cc2007-06-12 06:35:45 -04005606 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005608
5609 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005610 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005611
Alexander Block8ea05e32012-07-25 17:35:53 +02005612 btrfs_update_root_times(trans, root);
5613
Chris Mason39279cc2007-06-12 06:35:45 -04005614 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005615fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005616 if (dir)
5617 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005618 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005619 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005620 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005621}
5622
5623static inline u8 btrfs_inode_type(struct inode *inode)
5624{
5625 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5626}
5627
Chris Masond352ac62008-09-29 15:18:18 -04005628/*
5629 * utility function to add 'inode' into 'parent_inode' with
5630 * a give name and a given sequence number.
5631 * if 'add_backref' is true, also insert a backref from the
5632 * inode to the parent directory.
5633 */
Chris Masone02119d2008-09-05 16:13:11 -04005634int btrfs_add_link(struct btrfs_trans_handle *trans,
5635 struct inode *parent_inode, struct inode *inode,
5636 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005637{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005639 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005640 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005641 u64 ino = btrfs_ino(inode);
5642 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005643
Li Zefan33345d012011-04-20 10:31:50 +08005644 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005645 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5646 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005647 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005648 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5649 key.offset = 0;
5650 }
Chris Mason39279cc2007-06-12 06:35:45 -04005651
Li Zefan33345d012011-04-20 10:31:50 +08005652 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005653 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5654 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005655 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005656 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005657 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5658 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005659 }
5660
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005661 /* Nothing to clean up yet */
5662 if (ret)
5663 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005664
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005665 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5666 parent_inode, &key,
5667 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005668 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005669 goto fail_dir_item;
5670 else if (ret) {
5671 btrfs_abort_transaction(trans, root, ret);
5672 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005673 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005674
5675 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5676 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005677 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005678 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5679 ret = btrfs_update_inode(trans, root, parent_inode);
5680 if (ret)
5681 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005682 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005683
5684fail_dir_item:
5685 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5686 u64 local_index;
5687 int err;
5688 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5689 key.objectid, root->root_key.objectid,
5690 parent_ino, &local_index, name, name_len);
5691
5692 } else if (add_backref) {
5693 u64 local_index;
5694 int err;
5695
5696 err = btrfs_del_inode_ref(trans, root, name, name_len,
5697 ino, parent_ino, &local_index);
5698 }
5699 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005700}
5701
5702static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005703 struct inode *dir, struct dentry *dentry,
5704 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005705{
Josef Bacika1b075d2010-11-19 20:36:11 +00005706 int err = btrfs_add_link(trans, dir, inode,
5707 dentry->d_name.name, dentry->d_name.len,
5708 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005709 if (err > 0)
5710 err = -EEXIST;
5711 return err;
5712}
5713
Josef Bacik618e21d2007-07-11 10:18:17 -04005714static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005715 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005716{
5717 struct btrfs_trans_handle *trans;
5718 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005719 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005720 int err;
5721 int drop_inode = 0;
5722 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005723 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005724
5725 if (!new_valid_dev(rdev))
5726 return -EINVAL;
5727
Josef Bacik9ed74f22009-09-11 16:12:44 -04005728 /*
5729 * 2 for inode item and ref
5730 * 2 for dir items
5731 * 1 for xattr if selinux is on
5732 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005733 trans = btrfs_start_transaction(root, 5);
5734 if (IS_ERR(trans))
5735 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005736
Li Zefan581bb052011-04-20 10:06:11 +08005737 err = btrfs_find_free_ino(root, &objectid);
5738 if (err)
5739 goto out_unlock;
5740
Josef Bacikaec74772008-07-24 12:12:38 -04005741 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005742 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005743 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005744 if (IS_ERR(inode)) {
5745 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005746 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005747 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005748
Eric Paris2a7dba32011-02-01 11:05:39 -05005749 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005750 if (err) {
5751 drop_inode = 1;
5752 goto out_unlock;
5753 }
5754
Casey Schauflerad19db72011-12-15 10:09:07 -05005755 /*
5756 * If the active LSM wants to access the inode during
5757 * d_instantiate it needs these. Smack checks to see
5758 * if the filesystem supports xattrs by looking at the
5759 * ops vector.
5760 */
5761
5762 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005763 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005764 if (err)
5765 drop_inode = 1;
5766 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005767 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005768 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005769 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005770 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005771out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005772 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005773 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005774 if (drop_inode) {
5775 inode_dec_link_count(inode);
5776 iput(inode);
5777 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005778 return err;
5779}
5780
Chris Mason39279cc2007-06-12 06:35:45 -04005781static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005782 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005783{
5784 struct btrfs_trans_handle *trans;
5785 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005786 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005787 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005788 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005789 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005790 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005791
Josef Bacik9ed74f22009-09-11 16:12:44 -04005792 /*
5793 * 2 for inode item and ref
5794 * 2 for dir items
5795 * 1 for xattr if selinux is on
5796 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005797 trans = btrfs_start_transaction(root, 5);
5798 if (IS_ERR(trans))
5799 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005800
Li Zefan581bb052011-04-20 10:06:11 +08005801 err = btrfs_find_free_ino(root, &objectid);
5802 if (err)
5803 goto out_unlock;
5804
Josef Bacikaec74772008-07-24 12:12:38 -04005805 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005806 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005807 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005808 if (IS_ERR(inode)) {
5809 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005810 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005811 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005812 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005813
Eric Paris2a7dba32011-02-01 11:05:39 -05005814 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005815 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005816 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005817
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005818 err = btrfs_update_inode(trans, root, inode);
5819 if (err)
5820 goto out_unlock;
5821
Casey Schauflerad19db72011-12-15 10:09:07 -05005822 /*
5823 * If the active LSM wants to access the inode during
5824 * d_instantiate it needs these. Smack checks to see
5825 * if the filesystem supports xattrs by looking at the
5826 * ops vector.
5827 */
5828 inode->i_fop = &btrfs_file_operations;
5829 inode->i_op = &btrfs_file_inode_operations;
5830
Josef Bacika1b075d2010-11-19 20:36:11 +00005831 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005832 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005833 goto out_unlock;
5834
5835 inode->i_mapping->a_ops = &btrfs_aops;
5836 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5837 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5838 d_instantiate(dentry, inode);
5839
Chris Mason39279cc2007-06-12 06:35:45 -04005840out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005841 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005842 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005843 inode_dec_link_count(inode);
5844 iput(inode);
5845 }
Liu Bob53d3f52012-11-14 14:34:34 +00005846 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005847 return err;
5848}
5849
5850static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5851 struct dentry *dentry)
5852{
5853 struct btrfs_trans_handle *trans;
5854 struct btrfs_root *root = BTRFS_I(dir)->root;
5855 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005856 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005857 int err;
5858 int drop_inode = 0;
5859
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005860 /* do not allow sys_link's with other subvols of the same device */
5861 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005862 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005863
Mark Fashehf1863732012-08-08 11:32:27 -07005864 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005865 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005866
Chris Mason3de45862008-11-17 21:02:50 -05005867 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005868 if (err)
5869 goto fail;
5870
Yan, Zhenga22285a2010-05-16 10:48:46 -04005871 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005872 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005873 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005874 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005875 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005876 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005877 if (IS_ERR(trans)) {
5878 err = PTR_ERR(trans);
5879 goto fail;
5880 }
Chris Mason5f39d392007-10-15 16:14:19 -04005881
Miao Xie31534952011-04-13 13:19:21 +08005882 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005883 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005884 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005885 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005886 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005887
Josef Bacika1b075d2010-11-19 20:36:11 +00005888 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005889
Yan, Zhenga5719522009-09-24 09:17:31 -04005890 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005891 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005892 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005893 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005894 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005895 if (err)
5896 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005897 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005898 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005899 }
Chris Mason39279cc2007-06-12 06:35:45 -04005900
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005901 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005902fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005903 if (drop_inode) {
5904 inode_dec_link_count(inode);
5905 iput(inode);
5906 }
Liu Bob53d3f52012-11-14 14:34:34 +00005907 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005908 return err;
5909}
5910
Al Viro18bb1db2011-07-26 01:41:39 -04005911static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005912{
Chris Masonb9d86662008-05-02 16:13:49 -04005913 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005914 struct btrfs_trans_handle *trans;
5915 struct btrfs_root *root = BTRFS_I(dir)->root;
5916 int err = 0;
5917 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005918 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005919 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005920
Josef Bacik9ed74f22009-09-11 16:12:44 -04005921 /*
5922 * 2 items for inode and ref
5923 * 2 items for dir items
5924 * 1 for xattr if selinux is on
5925 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005926 trans = btrfs_start_transaction(root, 5);
5927 if (IS_ERR(trans))
5928 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005929
Li Zefan581bb052011-04-20 10:06:11 +08005930 err = btrfs_find_free_ino(root, &objectid);
5931 if (err)
5932 goto out_fail;
5933
Josef Bacikaec74772008-07-24 12:12:38 -04005934 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005935 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005936 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005937 if (IS_ERR(inode)) {
5938 err = PTR_ERR(inode);
5939 goto out_fail;
5940 }
Chris Mason5f39d392007-10-15 16:14:19 -04005941
Chris Mason39279cc2007-06-12 06:35:45 -04005942 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005943
Eric Paris2a7dba32011-02-01 11:05:39 -05005944 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005945 if (err)
5946 goto out_fail;
5947
Chris Mason39279cc2007-06-12 06:35:45 -04005948 inode->i_op = &btrfs_dir_inode_operations;
5949 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005950
Chris Masondbe674a2008-07-17 12:54:05 -04005951 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005952 err = btrfs_update_inode(trans, root, inode);
5953 if (err)
5954 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005955
Josef Bacika1b075d2010-11-19 20:36:11 +00005956 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5957 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005958 if (err)
5959 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005960
Chris Mason39279cc2007-06-12 06:35:45 -04005961 d_instantiate(dentry, inode);
5962 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005963
5964out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005965 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005966 if (drop_on_err)
5967 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005968 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005969 return err;
5970}
5971
Chris Masond352ac62008-09-29 15:18:18 -04005972/* helper for btfs_get_extent. Given an existing extent in the tree,
5973 * and an extent that you want to insert, deal with overlap and insert
5974 * the new extent into the tree.
5975 */
Chris Mason3b951512008-04-17 11:29:12 -04005976static int merge_extent_mapping(struct extent_map_tree *em_tree,
5977 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005978 struct extent_map *em,
5979 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005980{
5981 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005982
Chris Masone6dcd2d2008-07-17 12:53:50 -04005983 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5984 start_diff = map_start - em->start;
5985 em->start = map_start;
5986 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005987 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5988 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005989 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005990 em->block_len -= start_diff;
5991 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005992 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005993}
5994
Chris Masonc8b97812008-10-29 14:49:59 -04005995static noinline int uncompress_inline(struct btrfs_path *path,
5996 struct inode *inode, struct page *page,
5997 size_t pg_offset, u64 extent_offset,
5998 struct btrfs_file_extent_item *item)
5999{
6000 int ret;
6001 struct extent_buffer *leaf = path->nodes[0];
6002 char *tmp;
6003 size_t max_size;
6004 unsigned long inline_size;
6005 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006006 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006007
6008 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006009 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006010 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6011 inline_size = btrfs_file_extent_inline_item_len(leaf,
6012 btrfs_item_nr(leaf, path->slots[0]));
6013 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006014 if (!tmp)
6015 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006016 ptr = btrfs_file_extent_inline_start(item);
6017
6018 read_extent_buffer(leaf, tmp, ptr, inline_size);
6019
Chris Mason5b050f02008-11-11 09:34:41 -05006020 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006021 ret = btrfs_decompress(compress_type, tmp, page,
6022 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006023 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006024 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006025 unsigned long copy_size = min_t(u64,
6026 PAGE_CACHE_SIZE - pg_offset,
6027 max_size - extent_offset);
6028 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006029 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006030 }
6031 kfree(tmp);
6032 return 0;
6033}
6034
Chris Masond352ac62008-09-29 15:18:18 -04006035/*
6036 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006037 * the ugly parts come from merging extents from the disk with the in-ram
6038 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006039 * where the in-ram extents might be locked pending data=ordered completion.
6040 *
6041 * This also copies inline extents directly into the page.
6042 */
Chris Masond3977122009-01-05 21:25:51 -05006043
Chris Masona52d9a82007-08-27 16:49:44 -04006044struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006045 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006046 int create)
6047{
6048 int ret;
6049 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006050 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006051 u64 extent_start = 0;
6052 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006053 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006054 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006055 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006056 struct btrfs_root *root = BTRFS_I(inode)->root;
6057 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006058 struct extent_buffer *leaf;
6059 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006060 struct extent_map *em = NULL;
6061 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006062 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006063 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006064 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006065
Chris Masona52d9a82007-08-27 16:49:44 -04006066again:
Chris Mason890871b2009-09-02 16:24:52 -04006067 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006068 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006069 if (em)
6070 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006071 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006072
Chris Masona52d9a82007-08-27 16:49:44 -04006073 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006074 if (em->start > start || em->start + em->len <= start)
6075 free_extent_map(em);
6076 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006077 free_extent_map(em);
6078 else
6079 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006080 }
David Sterba172ddd62011-04-21 00:48:27 +02006081 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006082 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006083 err = -ENOMEM;
6084 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006085 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006086 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006087 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006088 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006089 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006090 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006091
6092 if (!path) {
6093 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006094 if (!path) {
6095 err = -ENOMEM;
6096 goto out;
6097 }
6098 /*
6099 * Chances are we'll be called again, so go ahead and do
6100 * readahead
6101 */
6102 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006103 }
6104
Chris Mason179e29e2007-11-01 11:28:41 -04006105 ret = btrfs_lookup_file_extent(trans, root, path,
6106 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006107 if (ret < 0) {
6108 err = ret;
6109 goto out;
6110 }
6111
6112 if (ret != 0) {
6113 if (path->slots[0] == 0)
6114 goto not_found;
6115 path->slots[0]--;
6116 }
6117
Chris Mason5f39d392007-10-15 16:14:19 -04006118 leaf = path->nodes[0];
6119 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006120 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006121 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006122 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6123 found_type = btrfs_key_type(&found_key);
6124 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006125 found_type != BTRFS_EXTENT_DATA_KEY) {
6126 goto not_found;
6127 }
6128
Chris Mason5f39d392007-10-15 16:14:19 -04006129 found_type = btrfs_file_extent_type(leaf, item);
6130 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006131 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006132 if (found_type == BTRFS_FILE_EXTENT_REG ||
6133 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006134 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006135 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006136 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6137 size_t size;
6138 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006139 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006140 }
6141
6142 if (start >= extent_end) {
6143 path->slots[0]++;
6144 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6145 ret = btrfs_next_leaf(root, path);
6146 if (ret < 0) {
6147 err = ret;
6148 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006149 }
Yan Zheng9036c102008-10-30 14:19:41 -04006150 if (ret > 0)
6151 goto not_found;
6152 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006153 }
Yan Zheng9036c102008-10-30 14:19:41 -04006154 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6155 if (found_key.objectid != objectid ||
6156 found_key.type != BTRFS_EXTENT_DATA_KEY)
6157 goto not_found;
6158 if (start + len <= found_key.offset)
6159 goto not_found;
6160 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006161 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006162 em->len = found_key.offset - start;
6163 goto not_found_em;
6164 }
6165
Josef Bacikcc95bef2013-04-04 14:31:27 -04006166 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006167 if (found_type == BTRFS_FILE_EXTENT_REG ||
6168 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006169 em->start = extent_start;
6170 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006171 em->orig_start = extent_start -
6172 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006173 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6174 item);
Chris Masondb945352007-10-15 16:15:53 -04006175 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6176 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006177 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006178 goto insert;
6179 }
Li Zefan261507a02010-12-17 14:21:50 +08006180 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006181 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006182 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006183 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006184 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006185 } else {
6186 bytenr += btrfs_file_extent_offset(leaf, item);
6187 em->block_start = bytenr;
6188 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006189 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6190 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006191 }
Chris Masona52d9a82007-08-27 16:49:44 -04006192 goto insert;
6193 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006194 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006195 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006196 size_t size;
6197 size_t extent_offset;
6198 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006199
Chris Masona52d9a82007-08-27 16:49:44 -04006200 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006201 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006202 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006203 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006204 goto out;
6205 }
6206
Yan Zheng9036c102008-10-30 14:19:41 -04006207 size = btrfs_file_extent_inline_len(leaf, item);
6208 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006209 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006210 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006211 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006212 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006213 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006214 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006215 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006216 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006217 em->compress_type = compress_type;
6218 }
Yan689f9342007-10-29 11:41:07 -04006219 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006220 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006221 if (btrfs_file_extent_compression(leaf, item) !=
6222 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006223 ret = uncompress_inline(path, inode, page,
6224 pg_offset,
6225 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006226 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006227 } else {
6228 map = kmap(page);
6229 read_extent_buffer(leaf, map + pg_offset, ptr,
6230 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006231 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6232 memset(map + pg_offset + copy_size, 0,
6233 PAGE_CACHE_SIZE - pg_offset -
6234 copy_size);
6235 }
Chris Masonc8b97812008-10-29 14:49:59 -04006236 kunmap(page);
6237 }
Chris Mason179e29e2007-11-01 11:28:41 -04006238 flush_dcache_page(page);
6239 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006240 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006241 if (!trans) {
6242 kunmap(page);
6243 free_extent_map(em);
6244 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006245
David Sterbab3b4aa72011-04-21 01:20:15 +02006246 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006247 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006248
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006249 if (IS_ERR(trans))
6250 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006251 goto again;
6252 }
Chris Masonc8b97812008-10-29 14:49:59 -04006253 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006254 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006255 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006256 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006257 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006258 }
Chris Masond1310b22008-01-24 16:13:08 -05006259 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006260 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006261 goto insert;
6262 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006263 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006264 }
6265not_found:
6266 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006267 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006268 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006269not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006270 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006271 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006272insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006273 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006274 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006275 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6276 (unsigned long long)em->start,
6277 (unsigned long long)em->len,
6278 (unsigned long long)start,
6279 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006280 err = -EIO;
6281 goto out;
6282 }
Chris Masond1310b22008-01-24 16:13:08 -05006283
6284 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006285 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006286 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04006287 /* it is possible that someone inserted the extent into the tree
6288 * while we had the lock dropped. It is also possible that
6289 * an overlapping map exists in the tree
6290 */
Chris Masona52d9a82007-08-27 16:49:44 -04006291 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006292 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006293
6294 ret = 0;
6295
Chris Mason3b951512008-04-17 11:29:12 -04006296 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006297 if (existing && (existing->start > start ||
6298 existing->start + existing->len <= start)) {
6299 free_extent_map(existing);
6300 existing = NULL;
6301 }
Chris Mason3b951512008-04-17 11:29:12 -04006302 if (!existing) {
6303 existing = lookup_extent_mapping(em_tree, em->start,
6304 em->len);
6305 if (existing) {
6306 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006307 em, start,
6308 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006309 free_extent_map(existing);
6310 if (err) {
6311 free_extent_map(em);
6312 em = NULL;
6313 }
6314 } else {
6315 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006316 free_extent_map(em);
6317 em = NULL;
6318 }
6319 } else {
6320 free_extent_map(em);
6321 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006322 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006323 }
Chris Masona52d9a82007-08-27 16:49:44 -04006324 }
Chris Mason890871b2009-09-02 16:24:52 -04006325 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006326out:
liubo1abe9b82011-03-24 11:18:59 +00006327
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006328 if (em)
6329 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006330
Chris Masonf4219502008-07-22 11:18:09 -04006331 if (path)
6332 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006333 if (trans) {
6334 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006335 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006336 err = ret;
6337 }
Chris Masona52d9a82007-08-27 16:49:44 -04006338 if (err) {
6339 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006340 return ERR_PTR(err);
6341 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006342 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006343 return em;
6344}
6345
Chris Masonec29ed52011-02-23 16:23:20 -05006346struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6347 size_t pg_offset, u64 start, u64 len,
6348 int create)
6349{
6350 struct extent_map *em;
6351 struct extent_map *hole_em = NULL;
6352 u64 range_start = start;
6353 u64 end;
6354 u64 found;
6355 u64 found_end;
6356 int err = 0;
6357
6358 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6359 if (IS_ERR(em))
6360 return em;
6361 if (em) {
6362 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006363 * if our em maps to
6364 * - a hole or
6365 * - a pre-alloc extent,
6366 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006367 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006368 if (em->block_start != EXTENT_MAP_HOLE &&
6369 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006370 return em;
6371 else
6372 hole_em = em;
6373 }
6374
6375 /* check to see if we've wrapped (len == -1 or similar) */
6376 end = start + len;
6377 if (end < start)
6378 end = (u64)-1;
6379 else
6380 end -= 1;
6381
6382 em = NULL;
6383
6384 /* ok, we didn't find anything, lets look for delalloc */
6385 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6386 end, len, EXTENT_DELALLOC, 1);
6387 found_end = range_start + found;
6388 if (found_end < range_start)
6389 found_end = (u64)-1;
6390
6391 /*
6392 * we didn't find anything useful, return
6393 * the original results from get_extent()
6394 */
6395 if (range_start > end || found_end <= start) {
6396 em = hole_em;
6397 hole_em = NULL;
6398 goto out;
6399 }
6400
6401 /* adjust the range_start to make sure it doesn't
6402 * go backwards from the start they passed in
6403 */
6404 range_start = max(start,range_start);
6405 found = found_end - range_start;
6406
6407 if (found > 0) {
6408 u64 hole_start = start;
6409 u64 hole_len = len;
6410
David Sterba172ddd62011-04-21 00:48:27 +02006411 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006412 if (!em) {
6413 err = -ENOMEM;
6414 goto out;
6415 }
6416 /*
6417 * when btrfs_get_extent can't find anything it
6418 * returns one huge hole
6419 *
6420 * make sure what it found really fits our range, and
6421 * adjust to make sure it is based on the start from
6422 * the caller
6423 */
6424 if (hole_em) {
6425 u64 calc_end = extent_map_end(hole_em);
6426
6427 if (calc_end <= start || (hole_em->start > end)) {
6428 free_extent_map(hole_em);
6429 hole_em = NULL;
6430 } else {
6431 hole_start = max(hole_em->start, start);
6432 hole_len = calc_end - hole_start;
6433 }
6434 }
6435 em->bdev = NULL;
6436 if (hole_em && range_start > hole_start) {
6437 /* our hole starts before our delalloc, so we
6438 * have to return just the parts of the hole
6439 * that go until the delalloc starts
6440 */
6441 em->len = min(hole_len,
6442 range_start - hole_start);
6443 em->start = hole_start;
6444 em->orig_start = hole_start;
6445 /*
6446 * don't adjust block start at all,
6447 * it is fixed at EXTENT_MAP_HOLE
6448 */
6449 em->block_start = hole_em->block_start;
6450 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006451 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6452 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006453 } else {
6454 em->start = range_start;
6455 em->len = found;
6456 em->orig_start = range_start;
6457 em->block_start = EXTENT_MAP_DELALLOC;
6458 em->block_len = found;
6459 }
6460 } else if (hole_em) {
6461 return hole_em;
6462 }
6463out:
6464
6465 free_extent_map(hole_em);
6466 if (err) {
6467 free_extent_map(em);
6468 return ERR_PTR(err);
6469 }
6470 return em;
6471}
6472
Josef Bacik4b46fce2010-05-23 11:00:55 -04006473static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6474 u64 start, u64 len)
6475{
6476 struct btrfs_root *root = BTRFS_I(inode)->root;
6477 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006478 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006479 struct btrfs_key ins;
6480 u64 alloc_hint;
6481 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006482
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006483 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006484 if (IS_ERR(trans))
6485 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006486
6487 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6488
6489 alloc_hint = get_extent_allocation_hint(inode, start, len);
6490 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006491 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006492 if (ret) {
6493 em = ERR_PTR(ret);
6494 goto out;
6495 }
6496
Josef Bacik70c8a912012-10-11 16:54:30 -04006497 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006498 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006499 if (IS_ERR(em))
6500 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006501
6502 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6503 ins.offset, ins.offset, 0);
6504 if (ret) {
6505 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6506 em = ERR_PTR(ret);
6507 }
6508out:
6509 btrfs_end_transaction(trans, root);
6510 return em;
6511}
6512
Chris Mason46bfbb52010-05-26 11:04:10 -04006513/*
6514 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6515 * block must be cow'd
6516 */
6517static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
6518 struct inode *inode, u64 offset, u64 len)
6519{
6520 struct btrfs_path *path;
6521 int ret;
6522 struct extent_buffer *leaf;
6523 struct btrfs_root *root = BTRFS_I(inode)->root;
6524 struct btrfs_file_extent_item *fi;
6525 struct btrfs_key key;
6526 u64 disk_bytenr;
6527 u64 backref_offset;
6528 u64 extent_end;
6529 u64 num_bytes;
6530 int slot;
6531 int found_type;
6532
6533 path = btrfs_alloc_path();
6534 if (!path)
6535 return -ENOMEM;
6536
Li Zefan33345d012011-04-20 10:31:50 +08006537 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006538 offset, 0);
6539 if (ret < 0)
6540 goto out;
6541
6542 slot = path->slots[0];
6543 if (ret == 1) {
6544 if (slot == 0) {
6545 /* can't find the item, must cow */
6546 ret = 0;
6547 goto out;
6548 }
6549 slot--;
6550 }
6551 ret = 0;
6552 leaf = path->nodes[0];
6553 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006554 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006555 key.type != BTRFS_EXTENT_DATA_KEY) {
6556 /* not our file or wrong item type, must cow */
6557 goto out;
6558 }
6559
6560 if (key.offset > offset) {
6561 /* Wrong offset, must cow */
6562 goto out;
6563 }
6564
6565 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6566 found_type = btrfs_file_extent_type(leaf, fi);
6567 if (found_type != BTRFS_FILE_EXTENT_REG &&
6568 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6569 /* not a regular extent, must cow */
6570 goto out;
6571 }
6572 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6573 backref_offset = btrfs_file_extent_offset(leaf, fi);
6574
6575 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6576 if (extent_end < offset + len) {
6577 /* extent doesn't include our full range, must cow */
6578 goto out;
6579 }
6580
6581 if (btrfs_extent_readonly(root, disk_bytenr))
6582 goto out;
6583
6584 /*
6585 * look for other files referencing this extent, if we
6586 * find any we must cow
6587 */
Li Zefan33345d012011-04-20 10:31:50 +08006588 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006589 key.offset - backref_offset, disk_bytenr))
6590 goto out;
6591
6592 /*
6593 * adjust disk_bytenr and num_bytes to cover just the bytes
6594 * in this extent we are about to write. If there
6595 * are any csums in that range we have to cow in order
6596 * to keep the csums correct
6597 */
6598 disk_bytenr += backref_offset;
6599 disk_bytenr += offset - key.offset;
6600 num_bytes = min(offset + len, extent_end) - offset;
6601 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6602 goto out;
6603 /*
6604 * all of the above have passed, it is safe to overwrite this extent
6605 * without cow
6606 */
6607 ret = 1;
6608out:
6609 btrfs_free_path(path);
6610 return ret;
6611}
6612
Josef Bacikeb838e72012-07-31 16:28:48 -04006613static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6614 struct extent_state **cached_state, int writing)
6615{
6616 struct btrfs_ordered_extent *ordered;
6617 int ret = 0;
6618
6619 while (1) {
6620 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6621 0, cached_state);
6622 /*
6623 * We're concerned with the entire range that we're going to be
6624 * doing DIO to, so we need to make sure theres no ordered
6625 * extents in this range.
6626 */
6627 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6628 lockend - lockstart + 1);
6629
6630 /*
6631 * We need to make sure there are no buffered pages in this
6632 * range either, we could have raced between the invalidate in
6633 * generic_file_direct_write and locking the extent. The
6634 * invalidate needs to happen so that reads after a write do not
6635 * get stale data.
6636 */
6637 if (!ordered && (!writing ||
6638 !test_range_bit(&BTRFS_I(inode)->io_tree,
6639 lockstart, lockend, EXTENT_UPTODATE, 0,
6640 *cached_state)))
6641 break;
6642
6643 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6644 cached_state, GFP_NOFS);
6645
6646 if (ordered) {
6647 btrfs_start_ordered_extent(inode, ordered, 1);
6648 btrfs_put_ordered_extent(ordered);
6649 } else {
6650 /* Screw you mmap */
6651 ret = filemap_write_and_wait_range(inode->i_mapping,
6652 lockstart,
6653 lockend);
6654 if (ret)
6655 break;
6656
6657 /*
6658 * If we found a page that couldn't be invalidated just
6659 * fall back to buffered.
6660 */
6661 ret = invalidate_inode_pages2_range(inode->i_mapping,
6662 lockstart >> PAGE_CACHE_SHIFT,
6663 lockend >> PAGE_CACHE_SHIFT);
6664 if (ret)
6665 break;
6666 }
6667
6668 cond_resched();
6669 }
6670
6671 return ret;
6672}
6673
Josef Bacik69ffb542012-09-11 15:40:07 -04006674static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6675 u64 len, u64 orig_start,
6676 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006677 u64 orig_block_len, u64 ram_bytes,
6678 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006679{
6680 struct extent_map_tree *em_tree;
6681 struct extent_map *em;
6682 struct btrfs_root *root = BTRFS_I(inode)->root;
6683 int ret;
6684
6685 em_tree = &BTRFS_I(inode)->extent_tree;
6686 em = alloc_extent_map();
6687 if (!em)
6688 return ERR_PTR(-ENOMEM);
6689
6690 em->start = start;
6691 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006692 em->mod_start = start;
6693 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006694 em->len = len;
6695 em->block_len = block_len;
6696 em->block_start = block_start;
6697 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006698 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006699 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006700 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006701 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6702 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006703 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006704
6705 do {
6706 btrfs_drop_extent_cache(inode, em->start,
6707 em->start + em->len - 1, 0);
6708 write_lock(&em_tree->lock);
6709 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04006710 if (!ret)
6711 list_move(&em->list,
6712 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006713 write_unlock(&em_tree->lock);
6714 } while (ret == -EEXIST);
6715
6716 if (ret) {
6717 free_extent_map(em);
6718 return ERR_PTR(ret);
6719 }
6720
6721 return em;
6722}
6723
6724
Josef Bacik4b46fce2010-05-23 11:00:55 -04006725static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6726 struct buffer_head *bh_result, int create)
6727{
6728 struct extent_map *em;
6729 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006730 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006731 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006732 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006733 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006734 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006735 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006736 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006737
Miao Xie172a5042013-02-21 02:48:22 -07006738 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006739 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006740 else
Josef Bacikc3298612012-08-03 16:49:19 -04006741 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006742
Josef Bacikc3298612012-08-03 16:49:19 -04006743 lockstart = start;
6744 lockend = start + len - 1;
6745
Josef Bacikeb838e72012-07-31 16:28:48 -04006746 /*
6747 * If this errors out it's because we couldn't invalidate pagecache for
6748 * this range and we need to fallback to buffered.
6749 */
6750 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6751 return -ENOTBLK;
6752
Josef Bacik4b46fce2010-05-23 11:00:55 -04006753 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006754 if (IS_ERR(em)) {
6755 ret = PTR_ERR(em);
6756 goto unlock_err;
6757 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006758
6759 /*
6760 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6761 * io. INLINE is special, and we could probably kludge it in here, but
6762 * it's still buffered so for safety lets just fall back to the generic
6763 * buffered path.
6764 *
6765 * For COMPRESSED we _have_ to read the entire extent in so we can
6766 * decompress it, so there will be buffering required no matter what we
6767 * do, so go ahead and fallback to buffered.
6768 *
6769 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6770 * to buffered IO. Don't blame me, this is the price we pay for using
6771 * the generic code.
6772 */
6773 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6774 em->block_start == EXTENT_MAP_INLINE) {
6775 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006776 ret = -ENOTBLK;
6777 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006778 }
6779
6780 /* Just a good old fashioned hole, return */
6781 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6782 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6783 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006784 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006785 }
6786
6787 /*
6788 * We don't allocate a new extent in the following cases
6789 *
6790 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6791 * existing extent.
6792 * 2) The extent is marked as PREALLOC. We're good to go here and can
6793 * just use the extent.
6794 *
6795 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006796 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006797 len = min(len, em->len - (start - em->start));
6798 lockstart = start + len;
6799 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006800 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006801
6802 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6803 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6804 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006805 int type;
6806 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006807 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006808
6809 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6810 type = BTRFS_ORDERED_PREALLOC;
6811 else
6812 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006813 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006814 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006815
6816 /*
6817 * we're not going to log anything, but we do need
6818 * to make sure the current transaction stays open
6819 * while we look for nocow cross refs
6820 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006821 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006822 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006823 goto must_cow;
6824
6825 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006826 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006827 u64 orig_block_len = em->orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006828 u64 ram_bytes = em->ram_bytes;
Josef Bacik69ffb542012-09-11 15:40:07 -04006829
6830 if (type == BTRFS_ORDERED_PREALLOC) {
6831 free_extent_map(em);
6832 em = create_pinned_em(inode, start, len,
6833 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006834 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006835 orig_block_len,
6836 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006837 if (IS_ERR(em)) {
6838 btrfs_end_transaction(trans, root);
6839 goto unlock_err;
6840 }
6841 }
6842
Chris Mason46bfbb52010-05-26 11:04:10 -04006843 ret = btrfs_add_ordered_extent_dio(inode, start,
6844 block_start, len, len, type);
6845 btrfs_end_transaction(trans, root);
6846 if (ret) {
6847 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006848 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006849 }
6850 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006851 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006852 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006853 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006854must_cow:
6855 /*
6856 * this will cow the extent, reset the len in case we changed
6857 * it above
6858 */
6859 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006860 free_extent_map(em);
6861 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006862 if (IS_ERR(em)) {
6863 ret = PTR_ERR(em);
6864 goto unlock_err;
6865 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006866 len = min(len, em->len - (start - em->start));
6867unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006868 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6869 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006870 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006871 bh_result->b_bdev = em->bdev;
6872 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006873 if (create) {
6874 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6875 set_buffer_new(bh_result);
6876
6877 /*
6878 * Need to update the i_size under the extent lock so buffered
6879 * readers will get the updated i_size when we unlock.
6880 */
6881 if (start + len > i_size_read(inode))
6882 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006883
Miao Xie172a5042013-02-21 02:48:22 -07006884 spin_lock(&BTRFS_I(inode)->lock);
6885 BTRFS_I(inode)->outstanding_extents++;
6886 spin_unlock(&BTRFS_I(inode)->lock);
6887
Miao Xie09348562013-02-07 10:12:07 +00006888 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6889 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6890 &cached_state, GFP_NOFS);
6891 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006892 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006893
Josef Bacikeb838e72012-07-31 16:28:48 -04006894 /*
6895 * In the case of write we need to clear and unlock the entire range,
6896 * in the case of read we need to unlock only the end area that we
6897 * aren't using if there is any left over space.
6898 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006899 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006900 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6901 lockend, unlock_bits, 1, 0,
6902 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006903 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006904 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006905 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006906
Josef Bacik4b46fce2010-05-23 11:00:55 -04006907 free_extent_map(em);
6908
6909 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006910
6911unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006912 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6913 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6914 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006915}
6916
6917struct btrfs_dio_private {
6918 struct inode *inode;
6919 u64 logical_offset;
6920 u64 disk_bytenr;
6921 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006922 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006923
6924 /* number of bios pending for this dio */
6925 atomic_t pending_bios;
6926
6927 /* IO errors */
6928 int errors;
6929
6930 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006931};
6932
6933static void btrfs_endio_direct_read(struct bio *bio, int err)
6934{
Miao Xiee65e1532010-11-22 03:04:43 +00006935 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006936 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6937 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006938 struct inode *inode = dip->inode;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006939 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006940 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006941
6942 start = dip->logical_offset;
6943 do {
6944 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6945 struct page *page = bvec->bv_page;
6946 char *kaddr;
6947 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006948 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006949 unsigned long flags;
6950
Josef Bacikc3298612012-08-03 16:49:19 -04006951 if (get_state_private(&BTRFS_I(inode)->io_tree,
6952 start, &private))
6953 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006954 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006955 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006956 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006957 csum, bvec->bv_len);
6958 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006959 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006960 local_irq_restore(flags);
6961
6962 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006963 if (csum != private) {
6964failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006965 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6966 (unsigned long long)btrfs_ino(inode),
6967 (unsigned long long)start,
6968 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006969 err = -EIO;
6970 }
6971 }
6972
6973 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006974 bvec++;
6975 } while (bvec <= bvec_end);
6976
6977 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006978 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006979 bio->bi_private = dip->private;
6980
Josef Bacik4b46fce2010-05-23 11:00:55 -04006981 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006982
6983 /* If we had a csum failure make sure to clear the uptodate flag */
6984 if (err)
6985 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006986 dio_end_io(bio, err);
6987}
6988
6989static void btrfs_endio_direct_write(struct bio *bio, int err)
6990{
6991 struct btrfs_dio_private *dip = bio->bi_private;
6992 struct inode *inode = dip->inode;
6993 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006994 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006995 u64 ordered_offset = dip->logical_offset;
6996 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006997 int ret;
6998
6999 if (err)
7000 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007001again:
7002 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7003 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007004 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007005 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007006 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007007
Josef Bacik5fd02042012-05-02 14:00:54 -04007008 ordered->work.func = finish_ordered_fn;
7009 ordered->work.flags = 0;
7010 btrfs_queue_worker(&root->fs_info->endio_write_workers,
7011 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007012out_test:
7013 /*
7014 * our bio might span multiple ordered extents. If we haven't
7015 * completed the accounting for the whole dio, go back and try again
7016 */
7017 if (ordered_offset < dip->logical_offset + dip->bytes) {
7018 ordered_bytes = dip->logical_offset + dip->bytes -
7019 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007020 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007021 goto again;
7022 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007023out_done:
7024 bio->bi_private = dip->private;
7025
Josef Bacik4b46fce2010-05-23 11:00:55 -04007026 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007027
7028 /* If we had an error make sure to clear the uptodate flag */
7029 if (err)
7030 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007031 dio_end_io(bio, err);
7032}
7033
Chris Masoneaf25d92010-05-25 09:48:28 -04007034static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7035 struct bio *bio, int mirror_num,
7036 unsigned long bio_flags, u64 offset)
7037{
7038 int ret;
7039 struct btrfs_root *root = BTRFS_I(inode)->root;
7040 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007041 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007042 return 0;
7043}
7044
Miao Xiee65e1532010-11-22 03:04:43 +00007045static void btrfs_end_dio_bio(struct bio *bio, int err)
7046{
7047 struct btrfs_dio_private *dip = bio->bi_private;
7048
7049 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007050 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007051 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007052 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007053 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007054 dip->errors = 1;
7055
7056 /*
7057 * before atomic variable goto zero, we must make sure
7058 * dip->errors is perceived to be set.
7059 */
7060 smp_mb__before_atomic_dec();
7061 }
7062
7063 /* if there are more bios still pending for this dio, just exit */
7064 if (!atomic_dec_and_test(&dip->pending_bios))
7065 goto out;
7066
7067 if (dip->errors)
7068 bio_io_error(dip->orig_bio);
7069 else {
7070 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
7071 bio_endio(dip->orig_bio, 0);
7072 }
7073out:
7074 bio_put(bio);
7075}
7076
7077static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7078 u64 first_sector, gfp_t gfp_flags)
7079{
7080 int nr_vecs = bio_get_nr_vecs(bdev);
7081 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7082}
7083
7084static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7085 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007086 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007087{
7088 int write = rw & REQ_WRITE;
7089 struct btrfs_root *root = BTRFS_I(inode)->root;
7090 int ret;
7091
Josef Bacikb812ce22012-11-16 13:56:32 -05007092 if (async_submit)
7093 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7094
Miao Xiee65e1532010-11-22 03:04:43 +00007095 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007096
7097 if (!write) {
7098 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7099 if (ret)
7100 goto err;
7101 }
Miao Xiee65e1532010-11-22 03:04:43 +00007102
Josef Bacik1ae39932011-04-06 14:41:34 -04007103 if (skip_sum)
7104 goto map;
7105
7106 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007107 ret = btrfs_wq_submit_bio(root->fs_info,
7108 inode, rw, bio, 0, 0,
7109 file_offset,
7110 __btrfs_submit_bio_start_direct_io,
7111 __btrfs_submit_bio_done);
7112 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007113 } else if (write) {
7114 /*
7115 * If we aren't doing async submit, calculate the csum of the
7116 * bio now.
7117 */
7118 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7119 if (ret)
7120 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007121 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007122 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007123 if (ret)
7124 goto err;
7125 }
Miao Xiee65e1532010-11-22 03:04:43 +00007126
Josef Bacik1ae39932011-04-06 14:41:34 -04007127map:
7128 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007129err:
7130 bio_put(bio);
7131 return ret;
7132}
7133
7134static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7135 int skip_sum)
7136{
7137 struct inode *inode = dip->inode;
7138 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007139 struct bio *bio;
7140 struct bio *orig_bio = dip->orig_bio;
7141 struct bio_vec *bvec = orig_bio->bi_io_vec;
7142 u64 start_sector = orig_bio->bi_sector;
7143 u64 file_offset = dip->logical_offset;
7144 u64 submit_len = 0;
7145 u64 map_length;
7146 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007147 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007148 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007149
Miao Xiee65e1532010-11-22 03:04:43 +00007150 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007151 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007152 &map_length, NULL, 0);
7153 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007154 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007155 return -EIO;
7156 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007157 if (map_length >= orig_bio->bi_size) {
7158 bio = orig_bio;
7159 goto submit;
7160 }
7161
David Woodhouse53b381b2013-01-29 18:40:14 -05007162 /* async crcs make it difficult to collect full stripe writes. */
7163 if (btrfs_get_alloc_profile(root, 1) &
7164 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7165 async_submit = 0;
7166 else
7167 async_submit = 1;
7168
Josef Bacik02f57c72011-04-06 14:25:44 -04007169 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7170 if (!bio)
7171 return -ENOMEM;
7172 bio->bi_private = dip;
7173 bio->bi_end_io = btrfs_end_dio_bio;
7174 atomic_inc(&dip->pending_bios);
7175
Miao Xiee65e1532010-11-22 03:04:43 +00007176 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7177 if (unlikely(map_length < submit_len + bvec->bv_len ||
7178 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7179 bvec->bv_offset) < bvec->bv_len)) {
7180 /*
7181 * inc the count before we submit the bio so
7182 * we know the end IO handler won't happen before
7183 * we inc the count. Otherwise, the dip might get freed
7184 * before we're done setting it up
7185 */
7186 atomic_inc(&dip->pending_bios);
7187 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7188 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007189 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007190 if (ret) {
7191 bio_put(bio);
7192 atomic_dec(&dip->pending_bios);
7193 goto out_err;
7194 }
7195
Miao Xiee65e1532010-11-22 03:04:43 +00007196 start_sector += submit_len >> 9;
7197 file_offset += submit_len;
7198
7199 submit_len = 0;
7200 nr_pages = 0;
7201
7202 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7203 start_sector, GFP_NOFS);
7204 if (!bio)
7205 goto out_err;
7206 bio->bi_private = dip;
7207 bio->bi_end_io = btrfs_end_dio_bio;
7208
7209 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007210 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007211 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007212 &map_length, NULL, 0);
7213 if (ret) {
7214 bio_put(bio);
7215 goto out_err;
7216 }
7217 } else {
7218 submit_len += bvec->bv_len;
7219 nr_pages ++;
7220 bvec++;
7221 }
7222 }
7223
Josef Bacik02f57c72011-04-06 14:25:44 -04007224submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007225 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007226 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007227 if (!ret)
7228 return 0;
7229
7230 bio_put(bio);
7231out_err:
7232 dip->errors = 1;
7233 /*
7234 * before atomic variable goto zero, we must
7235 * make sure dip->errors is perceived to be set.
7236 */
7237 smp_mb__before_atomic_dec();
7238 if (atomic_dec_and_test(&dip->pending_bios))
7239 bio_io_error(dip->orig_bio);
7240
7241 /* bio_end_io() will handle error, so we needn't return it */
7242 return 0;
7243}
7244
Josef Bacik4b46fce2010-05-23 11:00:55 -04007245static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
7246 loff_t file_offset)
7247{
7248 struct btrfs_root *root = BTRFS_I(inode)->root;
7249 struct btrfs_dio_private *dip;
7250 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007251 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007252 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007253 int ret = 0;
7254
7255 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7256
7257 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7258 if (!dip) {
7259 ret = -ENOMEM;
7260 goto free_ordered;
7261 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007262
7263 dip->private = bio->bi_private;
7264 dip->inode = inode;
7265 dip->logical_offset = file_offset;
7266
Josef Bacik4b46fce2010-05-23 11:00:55 -04007267 dip->bytes = 0;
7268 do {
7269 dip->bytes += bvec->bv_len;
7270 bvec++;
7271 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
7272
Chris Mason46bfbb52010-05-26 11:04:10 -04007273 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007274 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007275 dip->errors = 0;
7276 dip->orig_bio = bio;
7277 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007278
7279 if (write)
7280 bio->bi_end_io = btrfs_endio_direct_write;
7281 else
7282 bio->bi_end_io = btrfs_endio_direct_read;
7283
Miao Xiee65e1532010-11-22 03:04:43 +00007284 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7285 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007286 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007287free_ordered:
7288 /*
7289 * If this is a write, we need to clean up the reserved space and kill
7290 * the ordered extent.
7291 */
7292 if (write) {
7293 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007294 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007295 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7296 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7297 btrfs_free_reserved_extent(root, ordered->start,
7298 ordered->disk_len);
7299 btrfs_put_ordered_extent(ordered);
7300 btrfs_put_ordered_extent(ordered);
7301 }
7302 bio_endio(bio, ret);
7303}
7304
Chris Mason5a5f79b2010-05-26 21:33:37 -04007305static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7306 const struct iovec *iov, loff_t offset,
7307 unsigned long nr_segs)
7308{
7309 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007310 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007311 size_t size;
7312 unsigned long addr;
7313 unsigned blocksize_mask = root->sectorsize - 1;
7314 ssize_t retval = -EINVAL;
7315 loff_t end = offset;
7316
7317 if (offset & blocksize_mask)
7318 goto out;
7319
7320 /* Check the memory alignment. Blocks cannot straddle pages */
7321 for (seg = 0; seg < nr_segs; seg++) {
7322 addr = (unsigned long)iov[seg].iov_base;
7323 size = iov[seg].iov_len;
7324 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007325 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007326 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007327
7328 /* If this is a write we don't need to check anymore */
7329 if (rw & WRITE)
7330 continue;
7331
7332 /*
7333 * Check to make sure we don't have duplicate iov_base's in this
7334 * iovec, if so return EINVAL, otherwise we'll get csum errors
7335 * when reading back.
7336 */
7337 for (i = seg + 1; i < nr_segs; i++) {
7338 if (iov[seg].iov_base == iov[i].iov_base)
7339 goto out;
7340 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007341 }
7342 retval = 0;
7343out:
7344 return retval;
7345}
Josef Bacikeb838e72012-07-31 16:28:48 -04007346
Chris Mason16432982008-04-10 10:23:21 -04007347static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7348 const struct iovec *iov, loff_t offset,
7349 unsigned long nr_segs)
7350{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007351 struct file *file = iocb->ki_filp;
7352 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007353 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007354 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007355 bool wakeup = true;
7356 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007357 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007358
Chris Mason5a5f79b2010-05-26 21:33:37 -04007359 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007360 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007361 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007362
Miao Xie38851cc2013-02-08 07:04:11 +00007363 atomic_inc(&inode->i_dio_count);
7364 smp_mb__after_atomic_inc();
7365
Miao Xie09348562013-02-07 10:12:07 +00007366 if (rw & WRITE) {
7367 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007368 /*
7369 * If the write DIO is beyond the EOF, we need update
7370 * the isize, but it is protected by i_mutex. So we can
7371 * not unlock the i_mutex at this case.
7372 */
7373 if (offset + count <= inode->i_size) {
7374 mutex_unlock(&inode->i_mutex);
7375 relock = true;
7376 }
Miao Xie09348562013-02-07 10:12:07 +00007377 ret = btrfs_delalloc_reserve_space(inode, count);
7378 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007379 goto out;
7380 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7381 &BTRFS_I(inode)->runtime_flags))) {
7382 inode_dio_done(inode);
7383 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7384 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007385 }
7386
7387 ret = __blockdev_direct_IO(rw, iocb, inode,
7388 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7389 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007390 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007391 if (rw & WRITE) {
7392 if (ret < 0 && ret != -EIOCBQUEUED)
7393 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007394 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007395 btrfs_delalloc_release_space(inode,
7396 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007397 else
7398 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007399 }
Miao Xie38851cc2013-02-08 07:04:11 +00007400out:
Miao Xie2e60a512013-02-08 07:01:08 +00007401 if (wakeup)
7402 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007403 if (relock)
7404 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007405
7406 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007407}
7408
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007409#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7410
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007411static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7412 __u64 start, __u64 len)
7413{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007414 int ret;
7415
7416 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7417 if (ret)
7418 return ret;
7419
Chris Masonec29ed52011-02-23 16:23:20 -05007420 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007421}
7422
Chris Mason9ebefb182007-06-15 13:50:00 -04007423int btrfs_readpage(struct file *file, struct page *page)
7424{
Chris Masond1310b22008-01-24 16:13:08 -05007425 struct extent_io_tree *tree;
7426 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007427 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007428}
Chris Mason1832a6d2007-12-21 16:27:21 -05007429
Chris Mason39279cc2007-06-12 06:35:45 -04007430static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7431{
Chris Masond1310b22008-01-24 16:13:08 -05007432 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04007433
7434
7435 if (current->flags & PF_MEMALLOC) {
7436 redirty_page_for_writepage(wbc, page);
7437 unlock_page(page);
7438 return 0;
7439 }
Chris Masond1310b22008-01-24 16:13:08 -05007440 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007441 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7442}
Chris Mason39279cc2007-06-12 06:35:45 -04007443
Chris Masonf4219502008-07-22 11:18:09 -04007444int btrfs_writepages(struct address_space *mapping,
7445 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007446{
Chris Masond1310b22008-01-24 16:13:08 -05007447 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007448
Chris Masond1310b22008-01-24 16:13:08 -05007449 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007450 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7451}
7452
Chris Mason3ab2fb52007-11-08 10:59:22 -05007453static int
7454btrfs_readpages(struct file *file, struct address_space *mapping,
7455 struct list_head *pages, unsigned nr_pages)
7456{
Chris Masond1310b22008-01-24 16:13:08 -05007457 struct extent_io_tree *tree;
7458 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007459 return extent_readpages(tree, mapping, pages, nr_pages,
7460 btrfs_get_extent);
7461}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007462static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007463{
Chris Masond1310b22008-01-24 16:13:08 -05007464 struct extent_io_tree *tree;
7465 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007466 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007467
Chris Masond1310b22008-01-24 16:13:08 -05007468 tree = &BTRFS_I(page->mapping->host)->io_tree;
7469 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007470 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007471 if (ret == 1) {
7472 ClearPagePrivate(page);
7473 set_page_private(page, 0);
7474 page_cache_release(page);
7475 }
7476 return ret;
7477}
Chris Mason39279cc2007-06-12 06:35:45 -04007478
Chris Masone6dcd2d2008-07-17 12:53:50 -04007479static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7480{
Chris Mason98509cf2008-09-11 15:51:43 -04007481 if (PageWriteback(page) || PageDirty(page))
7482 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007483 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007484}
7485
Chris Masona52d9a82007-08-27 16:49:44 -04007486static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7487{
Josef Bacik5fd02042012-05-02 14:00:54 -04007488 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007489 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007490 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007491 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007492 u64 page_start = page_offset(page);
7493 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007494
Chris Mason8b62b722009-09-02 16:53:46 -04007495 /*
7496 * we have the page locked, so new writeback can't start,
7497 * and the dirty bit won't be cleared while we are here.
7498 *
7499 * Wait for IO on this page so that we can safely clear
7500 * the PagePrivate2 bit and do ordered accounting
7501 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007502 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007503
Josef Bacik5fd02042012-05-02 14:00:54 -04007504 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007505 if (offset) {
7506 btrfs_releasepage(page, GFP_NOFS);
7507 return;
7508 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007509 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007510 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007511 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007512 /*
7513 * IO on this page will never be started, so we need
7514 * to account for any ordered extents now
7515 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007516 clear_extent_bit(tree, page_start, page_end,
7517 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007518 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7519 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007520 /*
7521 * whoever cleared the private bit is responsible
7522 * for the finish_ordered_io
7523 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007524 if (TestClearPagePrivate2(page) &&
7525 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7526 PAGE_CACHE_SIZE, 1)) {
7527 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007528 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007529 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007530 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007531 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007532 }
7533 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007534 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007535 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7536 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007537 __btrfs_releasepage(page, GFP_NOFS);
7538
Chris Mason4a096752008-07-21 10:29:44 -04007539 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04007540 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04007541 ClearPagePrivate(page);
7542 set_page_private(page, 0);
7543 page_cache_release(page);
7544 }
Chris Mason39279cc2007-06-12 06:35:45 -04007545}
7546
Chris Mason9ebefb182007-06-15 13:50:00 -04007547/*
7548 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7549 * called from a page fault handler when a page is first dirtied. Hence we must
7550 * be careful to check for EOF conditions here. We set the page up correctly
7551 * for a written page which means we get ENOSPC checking when writing into
7552 * holes and correct delalloc and unwritten extent mapping on filesystems that
7553 * support these features.
7554 *
7555 * We are not allowed to take the i_mutex here so we have to play games to
7556 * protect against truncate races as the page could now be beyond EOF. Because
7557 * vmtruncate() writes the inode size before removing pages, once we have the
7558 * page lock we can determine safely if the page is beyond EOF. If it is not
7559 * beyond EOF, then the page is guaranteed safe against truncation until we
7560 * unlock the page.
7561 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007562int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007563{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007564 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007565 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007566 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007567 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7568 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007569 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007570 char *kaddr;
7571 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007572 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007573 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007574 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007575 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007576 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007577
Jan Karab2b5ef52012-06-12 16:20:45 +02007578 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007579 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007580 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007581 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007582 reserved = 1;
7583 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007584 if (ret) {
7585 if (ret == -ENOMEM)
7586 ret = VM_FAULT_OOM;
7587 else /* -ENOSPC, -EIO, etc */
7588 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007589 if (reserved)
7590 goto out;
7591 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007592 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007593
Nick Piggin56a76f82009-03-31 15:23:23 -07007594 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007595again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007596 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007597 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007598 page_start = page_offset(page);
7599 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007600
Chris Mason9ebefb182007-06-15 13:50:00 -04007601 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007602 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007603 /* page got truncated out from underneath us */
7604 goto out_unlock;
7605 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007606 wait_on_page_writeback(page);
7607
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007608 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007609 set_page_extent_mapped(page);
7610
Chris Masoneb84ae02008-07-17 13:53:27 -04007611 /*
7612 * we can't set the delalloc bits if there are pending ordered
7613 * extents. Drop our locks and wait for them to finish
7614 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007615 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7616 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007617 unlock_extent_cached(io_tree, page_start, page_end,
7618 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007619 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007620 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007621 btrfs_put_ordered_extent(ordered);
7622 goto again;
7623 }
7624
Josef Bacikfbf19082009-10-01 17:10:23 -04007625 /*
7626 * XXX - page_mkwrite gets called every time the page is dirtied, even
7627 * if it was already dirty, so for space accounting reasons we need to
7628 * clear any delalloc bits for the range we are fixing to save. There
7629 * is probably a better way to do this, but for now keep consistent with
7630 * prepare_pages in the normal write path.
7631 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007632 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007633 EXTENT_DIRTY | EXTENT_DELALLOC |
7634 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007635 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007636
Josef Bacik2ac55d42010-02-03 19:33:23 +00007637 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7638 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007639 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007640 unlock_extent_cached(io_tree, page_start, page_end,
7641 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007642 ret = VM_FAULT_SIGBUS;
7643 goto out_unlock;
7644 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007645 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007646
7647 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007648 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007649 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007650 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007651 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007652
Chris Masone6dcd2d2008-07-17 12:53:50 -04007653 if (zero_start != PAGE_CACHE_SIZE) {
7654 kaddr = kmap(page);
7655 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7656 flush_dcache_page(page);
7657 kunmap(page);
7658 }
Chris Mason247e7432008-07-17 12:53:51 -04007659 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007660 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007661 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007662
Chris Mason257c62e2009-10-13 13:21:08 -04007663 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7664 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007665 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007666
Josef Bacik2ac55d42010-02-03 19:33:23 +00007667 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007668
7669out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007670 if (!ret) {
7671 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007672 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007673 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007674 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007675out:
Josef Bacikec39e182012-01-12 19:10:12 -05007676 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007677out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007678 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007679 return ret;
7680}
7681
Josef Bacika41ad392011-01-31 15:30:16 -05007682static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007683{
7684 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007685 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007686 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007687 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007688 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007689 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007690 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007691
Josef Bacik2aaa6652012-08-29 14:27:18 -04007692 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007693 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007694 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007695
Chris Mason4a096752008-07-21 10:29:44 -04007696 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007697 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007698
Josef Bacikfcb80c22011-05-03 10:40:22 -04007699 /*
7700 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7701 * 3 things going on here
7702 *
7703 * 1) We need to reserve space for our orphan item and the space to
7704 * delete our orphan item. Lord knows we don't want to have a dangling
7705 * orphan item because we didn't reserve space to remove it.
7706 *
7707 * 2) We need to reserve space to update our inode.
7708 *
7709 * 3) We need to have something to cache all the space that is going to
7710 * be free'd up by the truncate operation, but also have some slack
7711 * space reserved in case it uses space during the truncate (thank you
7712 * very much snapshotting).
7713 *
7714 * And we need these to all be seperate. The fact is we can use alot of
7715 * space doing the truncate, and we have no earthly idea how much space
7716 * we will use, so we need the truncate reservation to be seperate so it
7717 * doesn't end up using space reserved for updating the inode or
7718 * removing the orphan item. We also need to be able to stop the
7719 * transaction and start a new one, which means we need to be able to
7720 * update the inode several times, and we have no idea of knowing how
7721 * many times that will be, so we can't just reserve 1 item for the
7722 * entirety of the opration, so that has to be done seperately as well.
7723 * Then there is the orphan item, which does indeed need to be held on
7724 * to for the whole operation, and we need nobody to touch this reserved
7725 * space except the orphan code.
7726 *
7727 * So that leaves us with
7728 *
7729 * 1) root->orphan_block_rsv - for the orphan deletion.
7730 * 2) rsv - for the truncate reservation, which we will steal from the
7731 * transaction reservation.
7732 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7733 * updating the inode.
7734 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007735 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007736 if (!rsv)
7737 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007738 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007739 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007740
Josef Bacik907cbce2011-08-08 13:46:15 -04007741 /*
Josef Bacik07127182011-08-19 10:29:59 -04007742 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007743 * 1 for updating the inode.
7744 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007745 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007746 if (IS_ERR(trans)) {
7747 err = PTR_ERR(trans);
7748 goto out;
7749 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007750
Josef Bacik907cbce2011-08-08 13:46:15 -04007751 /* Migrate the slack space for the truncate to our reserve */
7752 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7753 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007754 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007755
Chris Mason5a3f23d2009-03-31 13:27:11 -04007756 /*
7757 * setattr is responsible for setting the ordered_data_close flag,
7758 * but that is only tested during the last file release. That
7759 * could happen well after the next commit, leaving a great big
7760 * window where new writes may get lost if someone chooses to write
7761 * to this file after truncating to zero
7762 *
7763 * The inode doesn't have any dirty data here, and so if we commit
7764 * this is a noop. If someone immediately starts writing to the inode
7765 * it is very likely we'll catch some of their writes in this
7766 * transaction, and the commit will find this file on the ordered
7767 * data list with good things to send down.
7768 *
7769 * This is a best effort solution, there is still a window where
7770 * using truncate to replace the contents of the file will
7771 * end up with a zero length file after a crash.
7772 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007773 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7774 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007775 btrfs_add_ordered_operation(trans, root, inode);
7776
Josef Bacik5dc562c2012-08-17 13:14:17 -04007777 /*
7778 * So if we truncate and then write and fsync we normally would just
7779 * write the extents that changed, which is a problem if we need to
7780 * first truncate that entire inode. So set this flag so we write out
7781 * all of the extents in the inode to the sync log so we're completely
7782 * safe.
7783 */
7784 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007785 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007786
Yan, Zheng80825102009-11-12 09:35:36 +00007787 while (1) {
7788 ret = btrfs_truncate_inode_items(trans, root, inode,
7789 inode->i_size,
7790 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007791 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007792 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007793 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007794 }
Chris Mason39279cc2007-06-12 06:35:45 -04007795
Josef Bacikfcb80c22011-05-03 10:40:22 -04007796 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007797 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007798 if (ret) {
7799 err = ret;
7800 break;
7801 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007802
Yan, Zheng80825102009-11-12 09:35:36 +00007803 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007804 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007805
7806 trans = btrfs_start_transaction(root, 2);
7807 if (IS_ERR(trans)) {
7808 ret = err = PTR_ERR(trans);
7809 trans = NULL;
7810 break;
7811 }
7812
7813 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7814 rsv, min_size);
7815 BUG_ON(ret); /* shouldn't happen */
7816 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007817 }
7818
7819 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007820 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007821 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007822 if (ret)
7823 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007824 }
7825
Chris Mason917c16b2011-11-08 14:49:59 -05007826 if (trans) {
7827 trans->block_rsv = &root->fs_info->trans_block_rsv;
7828 ret = btrfs_update_inode(trans, root, inode);
7829 if (ret && !err)
7830 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007831
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007832 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007833 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007834 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007835
7836out:
7837 btrfs_free_block_rsv(root, rsv);
7838
Josef Bacik3893e332011-01-31 16:03:11 -05007839 if (ret && !err)
7840 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007841
Josef Bacik3893e332011-01-31 16:03:11 -05007842 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007843}
7844
Sven Wegener3b963622008-06-09 21:57:42 -04007845/*
Chris Masond352ac62008-09-29 15:18:18 -04007846 * create a new subvolume directory/inode (helper for the ioctl).
7847 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007848int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007849 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007850{
Chris Mason39279cc2007-06-12 06:35:45 -04007851 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007852 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007853 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007854
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007855 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7856 new_dirid, new_dirid,
7857 S_IFDIR | (~current_umask() & S_IRWXUGO),
7858 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007859 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007860 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007861 inode->i_op = &btrfs_dir_inode_operations;
7862 inode->i_fop = &btrfs_dir_file_operations;
7863
Miklos Szeredibfe86842011-10-28 14:13:29 +02007864 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007865 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007866
Yan, Zheng76dda932009-09-21 16:00:26 -04007867 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007868
Yan, Zheng76dda932009-09-21 16:00:26 -04007869 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007870 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007871}
7872
Chris Mason39279cc2007-06-12 06:35:45 -04007873struct inode *btrfs_alloc_inode(struct super_block *sb)
7874{
7875 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007876 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007877
7878 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7879 if (!ei)
7880 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007881
7882 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007883 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007884 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007885 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007886 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007887 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007888 ei->disk_i_size = 0;
7889 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007890 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007891 ei->index_cnt = (u64)-1;
7892 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007893 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007894
Josef Bacik9e0baf62011-07-15 15:16:44 +00007895 spin_lock_init(&ei->lock);
7896 ei->outstanding_extents = 0;
7897 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007898
Josef Bacik72ac3c02012-05-23 14:13:11 -04007899 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007900 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007901
Miao Xie16cdcec2011-04-22 18:12:22 +08007902 ei->delayed_node = NULL;
7903
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007904 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007905 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007906 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7907 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007908 ei->io_tree.track_uptodate = 1;
7909 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007910 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007911 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007912 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007913 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007914 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007915 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007916 RB_CLEAR_NODE(&ei->rb_node);
7917
7918 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007919}
7920
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007921static void btrfs_i_callback(struct rcu_head *head)
7922{
7923 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007924 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7925}
7926
Chris Mason39279cc2007-06-12 06:35:45 -04007927void btrfs_destroy_inode(struct inode *inode)
7928{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007929 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007930 struct btrfs_root *root = BTRFS_I(inode)->root;
7931
Al Virob3d9b7a2012-06-09 13:51:19 -04007932 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007933 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007934 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7935 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007936 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7937 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007938
Chris Mason5a3f23d2009-03-31 13:27:11 -04007939 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007940 * This can happen where we create an inode, but somebody else also
7941 * created the same inode and we need to destroy the one we already
7942 * created.
7943 */
7944 if (!root)
7945 goto free;
7946
7947 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007948 * Make sure we're properly removed from the ordered operation
7949 * lists.
7950 */
7951 smp_mb();
7952 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7953 spin_lock(&root->fs_info->ordered_extent_lock);
7954 list_del_init(&BTRFS_I(inode)->ordered_operations);
7955 spin_unlock(&root->fs_info->ordered_extent_lock);
7956 }
7957
Josef Bacik8a35d952012-05-23 14:26:42 -04007958 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7959 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007960 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7961 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007962 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007963 }
Josef Bacik7b128762008-07-24 12:17:14 -04007964
Chris Masond3977122009-01-05 21:25:51 -05007965 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007966 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7967 if (!ordered)
7968 break;
7969 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007970 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7971 (unsigned long long)ordered->file_offset,
7972 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007973 btrfs_remove_ordered_extent(inode, ordered);
7974 btrfs_put_ordered_extent(ordered);
7975 btrfs_put_ordered_extent(ordered);
7976 }
7977 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007978 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007979 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007980free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007981 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007982 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007983}
7984
Al Viro45321ac2010-06-07 13:43:19 -04007985int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007986{
7987 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007988
Liu Bofa6ac872013-02-20 14:10:23 +00007989 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007990 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007991 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007992 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007993 else
Al Viro45321ac2010-06-07 13:43:19 -04007994 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007995}
7996
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007997static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007998{
7999 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8000
8001 inode_init_once(&ei->vfs_inode);
8002}
8003
8004void btrfs_destroy_cachep(void)
8005{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008006 /*
8007 * Make sure all delayed rcu free inodes are flushed before we
8008 * destroy cache.
8009 */
8010 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008011 if (btrfs_inode_cachep)
8012 kmem_cache_destroy(btrfs_inode_cachep);
8013 if (btrfs_trans_handle_cachep)
8014 kmem_cache_destroy(btrfs_trans_handle_cachep);
8015 if (btrfs_transaction_cachep)
8016 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008017 if (btrfs_path_cachep)
8018 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008019 if (btrfs_free_space_cachep)
8020 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008021 if (btrfs_delalloc_work_cachep)
8022 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008023}
8024
8025int btrfs_init_cachep(void)
8026{
David Sterba837e1972012-09-07 03:00:48 -06008027 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008028 sizeof(struct btrfs_inode), 0,
8029 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008030 if (!btrfs_inode_cachep)
8031 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008032
David Sterba837e1972012-09-07 03:00:48 -06008033 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008034 sizeof(struct btrfs_trans_handle), 0,
8035 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008036 if (!btrfs_trans_handle_cachep)
8037 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008038
David Sterba837e1972012-09-07 03:00:48 -06008039 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008040 sizeof(struct btrfs_transaction), 0,
8041 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008042 if (!btrfs_transaction_cachep)
8043 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008044
David Sterba837e1972012-09-07 03:00:48 -06008045 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008046 sizeof(struct btrfs_path), 0,
8047 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008048 if (!btrfs_path_cachep)
8049 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008050
David Sterba837e1972012-09-07 03:00:48 -06008051 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008052 sizeof(struct btrfs_free_space), 0,
8053 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8054 if (!btrfs_free_space_cachep)
8055 goto fail;
8056
Miao Xie8ccf6f192012-10-25 09:28:04 +00008057 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8058 sizeof(struct btrfs_delalloc_work), 0,
8059 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8060 NULL);
8061 if (!btrfs_delalloc_work_cachep)
8062 goto fail;
8063
Chris Mason39279cc2007-06-12 06:35:45 -04008064 return 0;
8065fail:
8066 btrfs_destroy_cachep();
8067 return -ENOMEM;
8068}
8069
8070static int btrfs_getattr(struct vfsmount *mnt,
8071 struct dentry *dentry, struct kstat *stat)
8072{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008073 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008074 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008075 u32 blocksize = inode->i_sb->s_blocksize;
8076
Chris Mason39279cc2007-06-12 06:35:45 -04008077 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008078 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008079 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008080
8081 spin_lock(&BTRFS_I(inode)->lock);
8082 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8083 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008084 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008085 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008086 return 0;
8087}
8088
Chris Masond3977122009-01-05 21:25:51 -05008089static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8090 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008091{
8092 struct btrfs_trans_handle *trans;
8093 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008094 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008095 struct inode *new_inode = new_dentry->d_inode;
8096 struct inode *old_inode = old_dentry->d_inode;
8097 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008098 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008099 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008100 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008101 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008102
Li Zefan33345d012011-04-20 10:31:50 +08008103 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008104 return -EPERM;
8105
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008106 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008107 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008108 return -EXDEV;
8109
Li Zefan33345d012011-04-20 10:31:50 +08008110 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8111 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008112 return -ENOTEMPTY;
8113
Chris Mason39279cc2007-06-12 06:35:45 -04008114 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008115 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008116 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008117
8118
8119 /* check for collisions, even if the name isn't there */
8120 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8121 new_dentry->d_name.name,
8122 new_dentry->d_name.len);
8123
8124 if (ret) {
8125 if (ret == -EEXIST) {
8126 /* we shouldn't get
8127 * eexist without a new_inode */
8128 if (!new_inode) {
8129 WARN_ON(1);
8130 return ret;
8131 }
8132 } else {
8133 /* maybe -EOVERFLOW */
8134 return ret;
8135 }
8136 }
8137 ret = 0;
8138
Chris Mason5a3f23d2009-03-31 13:27:11 -04008139 /*
8140 * we're using rename to replace one file with another.
8141 * and the replacement file is large. Start IO on it now so
8142 * we don't add too much work to the end of the transaction
8143 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008144 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008145 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8146 filemap_flush(old_inode->i_mapping);
8147
Yan, Zheng76dda932009-09-21 16:00:26 -04008148 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008149 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008150 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008151 /*
8152 * We want to reserve the absolute worst case amount of items. So if
8153 * both inodes are subvols and we need to unlink them then that would
8154 * require 4 item modifications, but if they are both normal inodes it
8155 * would require 5 item modifications, so we'll assume their normal
8156 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8157 * should cover the worst case number of items we'll modify.
8158 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008159 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008160 if (IS_ERR(trans)) {
8161 ret = PTR_ERR(trans);
8162 goto out_notrans;
8163 }
Chris Mason5f39d392007-10-15 16:14:19 -04008164
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008165 if (dest != root)
8166 btrfs_record_root_in_trans(trans, dest);
8167
Yan, Zhenga5719522009-09-24 09:17:31 -04008168 ret = btrfs_set_inode_index(new_dir, &index);
8169 if (ret)
8170 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008171
Li Zefan33345d012011-04-20 10:31:50 +08008172 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008173 /* force full log commit if subvolume involved. */
8174 root->fs_info->last_trans_log_full_commit = trans->transid;
8175 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008176 ret = btrfs_insert_inode_ref(trans, dest,
8177 new_dentry->d_name.name,
8178 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008179 old_ino,
8180 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008181 if (ret)
8182 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008183 /*
8184 * this is an ugly little race, but the rename is required
8185 * to make sure that if we crash, the inode is either at the
8186 * old name or the new one. pinning the log transaction lets
8187 * us make sure we don't allow a log commit to come in after
8188 * we unlink the name but before we add the new name back in.
8189 */
8190 btrfs_pin_log_trans(root);
8191 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008192 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008193 * make sure the inode gets flushed if it is replacing
8194 * something.
8195 */
Li Zefan33345d012011-04-20 10:31:50 +08008196 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008197 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008198
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008199 inode_inc_iversion(old_dir);
8200 inode_inc_iversion(new_dir);
8201 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008202 old_dir->i_ctime = old_dir->i_mtime = ctime;
8203 new_dir->i_ctime = new_dir->i_mtime = ctime;
8204 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008205
Chris Mason12fcfd22009-03-24 10:24:20 -04008206 if (old_dentry->d_parent != new_dentry->d_parent)
8207 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8208
Li Zefan33345d012011-04-20 10:31:50 +08008209 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008210 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8211 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8212 old_dentry->d_name.name,
8213 old_dentry->d_name.len);
8214 } else {
Al Viro92986792011-03-04 17:14:37 +00008215 ret = __btrfs_unlink_inode(trans, root, old_dir,
8216 old_dentry->d_inode,
8217 old_dentry->d_name.name,
8218 old_dentry->d_name.len);
8219 if (!ret)
8220 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008221 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008222 if (ret) {
8223 btrfs_abort_transaction(trans, root, ret);
8224 goto out_fail;
8225 }
Chris Mason39279cc2007-06-12 06:35:45 -04008226
8227 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008228 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008229 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008230 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008231 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8232 root_objectid = BTRFS_I(new_inode)->location.objectid;
8233 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8234 root_objectid,
8235 new_dentry->d_name.name,
8236 new_dentry->d_name.len);
8237 BUG_ON(new_inode->i_nlink == 0);
8238 } else {
8239 ret = btrfs_unlink_inode(trans, dest, new_dir,
8240 new_dentry->d_inode,
8241 new_dentry->d_name.name,
8242 new_dentry->d_name.len);
8243 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008244 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008245 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008246 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008247 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008248 if (ret) {
8249 btrfs_abort_transaction(trans, root, ret);
8250 goto out_fail;
8251 }
Chris Mason39279cc2007-06-12 06:35:45 -04008252 }
Josef Bacikaec74772008-07-24 12:12:38 -04008253
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008254 ret = btrfs_add_link(trans, new_dir, old_inode,
8255 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008256 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008257 if (ret) {
8258 btrfs_abort_transaction(trans, root, ret);
8259 goto out_fail;
8260 }
Chris Mason39279cc2007-06-12 06:35:45 -04008261
Li Zefan33345d012011-04-20 10:31:50 +08008262 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008263 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008264 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008265 btrfs_end_log_trans(root);
8266 }
Chris Mason39279cc2007-06-12 06:35:45 -04008267out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008268 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008269out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008270 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008271 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008272
Chris Mason39279cc2007-06-12 06:35:45 -04008273 return ret;
8274}
8275
Miao Xie8ccf6f192012-10-25 09:28:04 +00008276static void btrfs_run_delalloc_work(struct btrfs_work *work)
8277{
8278 struct btrfs_delalloc_work *delalloc_work;
8279
8280 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8281 work);
8282 if (delalloc_work->wait)
8283 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8284 else
8285 filemap_flush(delalloc_work->inode->i_mapping);
8286
8287 if (delalloc_work->delay_iput)
8288 btrfs_add_delayed_iput(delalloc_work->inode);
8289 else
8290 iput(delalloc_work->inode);
8291 complete(&delalloc_work->completion);
8292}
8293
8294struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8295 int wait, int delay_iput)
8296{
8297 struct btrfs_delalloc_work *work;
8298
8299 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8300 if (!work)
8301 return NULL;
8302
8303 init_completion(&work->completion);
8304 INIT_LIST_HEAD(&work->list);
8305 work->inode = inode;
8306 work->wait = wait;
8307 work->delay_iput = delay_iput;
8308 work->work.func = btrfs_run_delalloc_work;
8309
8310 return work;
8311}
8312
8313void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8314{
8315 wait_for_completion(&work->completion);
8316 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8317}
8318
Chris Masond352ac62008-09-29 15:18:18 -04008319/*
8320 * some fairly slow code that needs optimization. This walks the list
8321 * of all the inodes with pending delalloc and forces them to disk.
8322 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008323int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008324{
Chris Masonea8c2812008-08-04 23:17:27 -04008325 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008326 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008327 struct btrfs_delalloc_work *work, *next;
8328 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008329 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008330 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008331
Yan Zhengc146afa2008-11-12 14:34:12 -05008332 if (root->fs_info->sb->s_flags & MS_RDONLY)
8333 return -EROFS;
8334
Miao Xie8ccf6f192012-10-25 09:28:04 +00008335 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008336 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008337
Chris Mason75eff682008-12-15 15:54:40 -05008338 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008339 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8340 while (!list_empty(&splice)) {
8341 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008342 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008343
8344 list_del_init(&binode->delalloc_inodes);
8345
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008346 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008347 if (!inode) {
8348 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8349 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008350 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008351 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008352
8353 list_add_tail(&binode->delalloc_inodes,
8354 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008355 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008356
8357 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8358 if (unlikely(!work)) {
8359 ret = -ENOMEM;
8360 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008361 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008362 list_add_tail(&work->list, &works);
8363 btrfs_queue_worker(&root->fs_info->flush_workers,
8364 &work->work);
8365
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008366 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008367 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008368 }
Chris Mason75eff682008-12-15 15:54:40 -05008369 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008370
Miao Xie1eafa6c2013-01-22 10:49:00 +00008371 list_for_each_entry_safe(work, next, &works, list) {
8372 list_del_init(&work->list);
8373 btrfs_wait_and_free_delalloc_work(work);
8374 }
8375
Chris Mason8c8bee12008-09-29 11:19:10 -04008376 /* the filemap_flush will queue IO into the worker threads, but
8377 * we have to make sure the IO is actually started and that
8378 * ordered extents get created before we return
8379 */
8380 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008381 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008382 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008383 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008384 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8385 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008386 }
8387 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008388 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008389out:
8390 list_for_each_entry_safe(work, next, &works, list) {
8391 list_del_init(&work->list);
8392 btrfs_wait_and_free_delalloc_work(work);
8393 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008394
8395 if (!list_empty_careful(&splice)) {
8396 spin_lock(&root->fs_info->delalloc_lock);
8397 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8398 spin_unlock(&root->fs_info->delalloc_lock);
8399 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008400 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008401}
8402
Chris Mason39279cc2007-06-12 06:35:45 -04008403static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8404 const char *symname)
8405{
8406 struct btrfs_trans_handle *trans;
8407 struct btrfs_root *root = BTRFS_I(dir)->root;
8408 struct btrfs_path *path;
8409 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008410 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008411 int err;
8412 int drop_inode = 0;
8413 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008414 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008415 int name_len;
8416 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008417 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008418 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008419 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008420
8421 name_len = strlen(symname) + 1;
8422 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8423 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008424
Josef Bacik9ed74f22009-09-11 16:12:44 -04008425 /*
8426 * 2 items for inode item and ref
8427 * 2 items for dir items
8428 * 1 item for xattr if selinux is on
8429 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008430 trans = btrfs_start_transaction(root, 5);
8431 if (IS_ERR(trans))
8432 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008433
Li Zefan581bb052011-04-20 10:06:11 +08008434 err = btrfs_find_free_ino(root, &objectid);
8435 if (err)
8436 goto out_unlock;
8437
Josef Bacikaec74772008-07-24 12:12:38 -04008438 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008439 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008440 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008441 if (IS_ERR(inode)) {
8442 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008443 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008444 }
Chris Mason39279cc2007-06-12 06:35:45 -04008445
Eric Paris2a7dba32011-02-01 11:05:39 -05008446 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008447 if (err) {
8448 drop_inode = 1;
8449 goto out_unlock;
8450 }
8451
Casey Schauflerad19db72011-12-15 10:09:07 -05008452 /*
8453 * If the active LSM wants to access the inode during
8454 * d_instantiate it needs these. Smack checks to see
8455 * if the filesystem supports xattrs by looking at the
8456 * ops vector.
8457 */
8458 inode->i_fop = &btrfs_file_operations;
8459 inode->i_op = &btrfs_file_inode_operations;
8460
Josef Bacika1b075d2010-11-19 20:36:11 +00008461 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008462 if (err)
8463 drop_inode = 1;
8464 else {
8465 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008466 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008467 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008468 }
Chris Mason39279cc2007-06-12 06:35:45 -04008469 if (drop_inode)
8470 goto out_unlock;
8471
8472 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008473 if (!path) {
8474 err = -ENOMEM;
8475 drop_inode = 1;
8476 goto out_unlock;
8477 }
Li Zefan33345d012011-04-20 10:31:50 +08008478 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008479 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008480 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8481 datasize = btrfs_file_extent_calc_inline_size(name_len);
8482 err = btrfs_insert_empty_item(trans, root, path, &key,
8483 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008484 if (err) {
8485 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008486 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008487 goto out_unlock;
8488 }
Chris Mason5f39d392007-10-15 16:14:19 -04008489 leaf = path->nodes[0];
8490 ei = btrfs_item_ptr(leaf, path->slots[0],
8491 struct btrfs_file_extent_item);
8492 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8493 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008494 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008495 btrfs_set_file_extent_encryption(leaf, ei, 0);
8496 btrfs_set_file_extent_compression(leaf, ei, 0);
8497 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8498 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8499
Chris Mason39279cc2007-06-12 06:35:45 -04008500 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008501 write_extent_buffer(leaf, symname, ptr, name_len);
8502 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008503 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008504
Chris Mason39279cc2007-06-12 06:35:45 -04008505 inode->i_op = &btrfs_symlink_inode_operations;
8506 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008507 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008508 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008509 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008510 err = btrfs_update_inode(trans, root, inode);
8511 if (err)
8512 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008513
8514out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008515 if (!err)
8516 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008517 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008518 if (drop_inode) {
8519 inode_dec_link_count(inode);
8520 iput(inode);
8521 }
Liu Bob53d3f52012-11-14 14:34:34 +00008522 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008523 return err;
8524}
Chris Mason16432982008-04-10 10:23:21 -04008525
Josef Bacik0af3d002010-06-21 14:48:16 -04008526static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8527 u64 start, u64 num_bytes, u64 min_size,
8528 loff_t actual_len, u64 *alloc_hint,
8529 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008530{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008531 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8532 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008533 struct btrfs_root *root = BTRFS_I(inode)->root;
8534 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008535 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008536 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008537 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008538 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008539 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008540
Josef Bacik0af3d002010-06-21 14:48:16 -04008541 if (trans)
8542 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008543 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008544 if (own_trans) {
8545 trans = btrfs_start_transaction(root, 3);
8546 if (IS_ERR(trans)) {
8547 ret = PTR_ERR(trans);
8548 break;
8549 }
Yan Zhengd899e052008-10-30 14:25:28 -04008550 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008551
Chris Mason154ea282013-03-05 11:11:26 -05008552 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8553 cur_bytes = max(cur_bytes, min_size);
8554 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008555 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008556 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008557 if (own_trans)
8558 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008559 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008560 }
8561
Yan Zhengd899e052008-10-30 14:25:28 -04008562 ret = insert_reserved_file_extent(trans, inode,
8563 cur_offset, ins.objectid,
8564 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008565 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008566 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008567 if (ret) {
8568 btrfs_abort_transaction(trans, root, ret);
8569 if (own_trans)
8570 btrfs_end_transaction(trans, root);
8571 break;
8572 }
Chris Masona1ed8352009-09-11 12:27:37 -04008573 btrfs_drop_extent_cache(inode, cur_offset,
8574 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008575
Josef Bacik5dc562c2012-08-17 13:14:17 -04008576 em = alloc_extent_map();
8577 if (!em) {
8578 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8579 &BTRFS_I(inode)->runtime_flags);
8580 goto next;
8581 }
8582
8583 em->start = cur_offset;
8584 em->orig_start = cur_offset;
8585 em->len = ins.offset;
8586 em->block_start = ins.objectid;
8587 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008588 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008589 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008590 em->bdev = root->fs_info->fs_devices->latest_bdev;
8591 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8592 em->generation = trans->transid;
8593
8594 while (1) {
8595 write_lock(&em_tree->lock);
8596 ret = add_extent_mapping(em_tree, em);
8597 if (!ret)
8598 list_move(&em->list,
8599 &em_tree->modified_extents);
8600 write_unlock(&em_tree->lock);
8601 if (ret != -EEXIST)
8602 break;
8603 btrfs_drop_extent_cache(inode, cur_offset,
8604 cur_offset + ins.offset - 1,
8605 0);
8606 }
8607 free_extent_map(em);
8608next:
Yan Zhengd899e052008-10-30 14:25:28 -04008609 num_bytes -= ins.offset;
8610 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008611 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008612
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008613 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008614 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008615 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008616 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008617 (actual_len > inode->i_size) &&
8618 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008619 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008620 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008621 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008622 i_size = cur_offset;
8623 i_size_write(inode, i_size);
8624 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008625 }
8626
Yan Zhengd899e052008-10-30 14:25:28 -04008627 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008628
8629 if (ret) {
8630 btrfs_abort_transaction(trans, root, ret);
8631 if (own_trans)
8632 btrfs_end_transaction(trans, root);
8633 break;
8634 }
Yan Zhengd899e052008-10-30 14:25:28 -04008635
Josef Bacik0af3d002010-06-21 14:48:16 -04008636 if (own_trans)
8637 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008638 }
Yan Zhengd899e052008-10-30 14:25:28 -04008639 return ret;
8640}
8641
Josef Bacik0af3d002010-06-21 14:48:16 -04008642int btrfs_prealloc_file_range(struct inode *inode, int mode,
8643 u64 start, u64 num_bytes, u64 min_size,
8644 loff_t actual_len, u64 *alloc_hint)
8645{
8646 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8647 min_size, actual_len, alloc_hint,
8648 NULL);
8649}
8650
8651int btrfs_prealloc_file_range_trans(struct inode *inode,
8652 struct btrfs_trans_handle *trans, int mode,
8653 u64 start, u64 num_bytes, u64 min_size,
8654 loff_t actual_len, u64 *alloc_hint)
8655{
8656 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8657 min_size, actual_len, alloc_hint, trans);
8658}
8659
Chris Masone6dcd2d2008-07-17 12:53:50 -04008660static int btrfs_set_page_dirty(struct page *page)
8661{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008662 return __set_page_dirty_nobuffers(page);
8663}
8664
Al Viro10556cb22011-06-20 19:28:19 -04008665static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008666{
Li Zefanb83cc962010-12-20 16:04:08 +08008667 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008668 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008669
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008670 if (mask & MAY_WRITE &&
8671 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8672 if (btrfs_root_readonly(root))
8673 return -EROFS;
8674 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8675 return -EACCES;
8676 }
Al Viro2830ba72011-06-20 19:16:29 -04008677 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008678}
Chris Mason39279cc2007-06-12 06:35:45 -04008679
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008680static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008681 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008682 .lookup = btrfs_lookup,
8683 .create = btrfs_create,
8684 .unlink = btrfs_unlink,
8685 .link = btrfs_link,
8686 .mkdir = btrfs_mkdir,
8687 .rmdir = btrfs_rmdir,
8688 .rename = btrfs_rename,
8689 .symlink = btrfs_symlink,
8690 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008691 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008692 .setxattr = btrfs_setxattr,
8693 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008694 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008695 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008696 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008697 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008698};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008699static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008700 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008701 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008702 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008703};
Yan, Zheng76dda932009-09-21 16:00:26 -04008704
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008705static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008706 .llseek = generic_file_llseek,
8707 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008708 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008709 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008710#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008711 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008712#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008713 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008714 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008715};
8716
Chris Masond1310b22008-01-24 16:13:08 -05008717static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008718 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008719 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008720 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008721 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008722 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008723 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008724 .set_bit_hook = btrfs_set_bit_hook,
8725 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008726 .merge_extent_hook = btrfs_merge_extent_hook,
8727 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008728};
8729
Chris Mason35054392009-01-21 13:11:13 -05008730/*
8731 * btrfs doesn't support the bmap operation because swapfiles
8732 * use bmap to make a mapping of extents in the file. They assume
8733 * these extents won't change over the life of the file and they
8734 * use the bmap result to do IO directly to the drive.
8735 *
8736 * the btrfs bmap call would return logical addresses that aren't
8737 * suitable for IO and they also will change frequently as COW
8738 * operations happen. So, swapfile + btrfs == corruption.
8739 *
8740 * For now we're avoiding this by dropping bmap.
8741 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008742static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008743 .readpage = btrfs_readpage,
8744 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008745 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008746 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008747 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008748 .invalidatepage = btrfs_invalidatepage,
8749 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008750 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008751 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008752};
8753
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008754static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008755 .readpage = btrfs_readpage,
8756 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008757 .invalidatepage = btrfs_invalidatepage,
8758 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008759};
8760
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008761static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008762 .getattr = btrfs_getattr,
8763 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008764 .setxattr = btrfs_setxattr,
8765 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008766 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008767 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008768 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008769 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008770 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008771 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008772};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008773static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008774 .getattr = btrfs_getattr,
8775 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008776 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008777 .setxattr = btrfs_setxattr,
8778 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008779 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008780 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008781 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008782 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008783};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008784static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008785 .readlink = generic_readlink,
8786 .follow_link = page_follow_link_light,
8787 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008788 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008789 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008790 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008791 .setxattr = btrfs_setxattr,
8792 .getxattr = btrfs_getxattr,
8793 .listxattr = btrfs_listxattr,
8794 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008795 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008796 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008797};
Yan, Zheng76dda932009-09-21 16:00:26 -04008798
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008799const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008800 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008801 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008802};