blob: 0eab7b67e109fc4f43e1d06b4d7fb30f7f2afb47 [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);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400735 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400736 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500737 if (ret != -EEXIST) {
738 free_extent_map(em);
739 break;
740 }
741 btrfs_drop_extent_cache(inode, async_extent->start,
742 async_extent->start +
743 async_extent->ram_size - 1, 0);
744 }
745
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500746 if (ret)
747 goto out_free_reserve;
748
Li Zefan261507a02010-12-17 14:21:50 +0800749 ret = btrfs_add_ordered_extent_compress(inode,
750 async_extent->start,
751 ins.objectid,
752 async_extent->ram_size,
753 ins.offset,
754 BTRFS_ORDERED_COMPRESSED,
755 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500756 if (ret)
757 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500758
Chris Mason771ed682008-11-06 22:02:51 -0500759 /*
760 * clear dirty, set writeback and unlock the pages.
761 */
762 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400763 &BTRFS_I(inode)->io_tree,
764 async_extent->start,
765 async_extent->start +
766 async_extent->ram_size - 1,
767 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
768 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400769 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400770 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500771
772 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500773 async_extent->start,
774 async_extent->ram_size,
775 ins.objectid,
776 ins.offset, async_extent->pages,
777 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500778 alloc_hint = ins.objectid + ins.offset;
779 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500780 if (ret)
781 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500782 cond_resched();
783 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100784 ret = 0;
785out:
786 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500787out_free_reserve:
788 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100789out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500790 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
791 async_extent->start,
792 async_extent->start +
793 async_extent->ram_size - 1,
794 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
795 EXTENT_CLEAR_UNLOCK |
796 EXTENT_CLEAR_DELALLOC |
797 EXTENT_CLEAR_DIRTY |
798 EXTENT_SET_WRITEBACK |
799 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100800 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500801 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500802}
803
Josef Bacik4b46fce2010-05-23 11:00:55 -0400804static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
805 u64 num_bytes)
806{
807 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
808 struct extent_map *em;
809 u64 alloc_hint = 0;
810
811 read_lock(&em_tree->lock);
812 em = search_extent_mapping(em_tree, start, num_bytes);
813 if (em) {
814 /*
815 * if block start isn't an actual block number then find the
816 * first block in this inode and use that as a hint. If that
817 * block is also bogus then just don't worry about it.
818 */
819 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
820 free_extent_map(em);
821 em = search_extent_mapping(em_tree, 0, 0);
822 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
823 alloc_hint = em->block_start;
824 if (em)
825 free_extent_map(em);
826 } else {
827 alloc_hint = em->block_start;
828 free_extent_map(em);
829 }
830 }
831 read_unlock(&em_tree->lock);
832
833 return alloc_hint;
834}
835
Chris Mason771ed682008-11-06 22:02:51 -0500836/*
837 * when extent_io.c finds a delayed allocation range in the file,
838 * the call backs end up in this code. The basic idea is to
839 * allocate extents on disk for the range, and create ordered data structs
840 * in ram to track those extents.
841 *
842 * locked_page is the page that writepage had locked already. We use
843 * it to make sure we don't do extra locks or unlocks.
844 *
845 * *page_started is set to one if we unlock locked_page and do everything
846 * required to start IO on it. It may be clean and already done with
847 * IO when we return.
848 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000849static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
850 struct inode *inode,
851 struct btrfs_root *root,
852 struct page *locked_page,
853 u64 start, u64 end, int *page_started,
854 unsigned long *nr_written,
855 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500856{
Chris Mason771ed682008-11-06 22:02:51 -0500857 u64 alloc_hint = 0;
858 u64 num_bytes;
859 unsigned long ram_size;
860 u64 disk_num_bytes;
861 u64 cur_alloc_size;
862 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500863 struct btrfs_key ins;
864 struct extent_map *em;
865 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
866 int ret = 0;
867
Liu Bo83eea1f2012-07-10 05:28:39 -0600868 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500869
Qu Wenruofda28322013-02-26 08:10:22 +0000870 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500871 num_bytes = max(blocksize, num_bytes);
872 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500873
Chris Mason4cb53002011-05-24 15:35:30 -0400874 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400875 if (num_bytes < 64 * 1024 &&
876 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400877 btrfs_add_inode_defrag(trans, inode);
878
Chris Mason771ed682008-11-06 22:02:51 -0500879 if (start == 0) {
880 /* lets try to make an inline extent */
881 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000882 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500883 if (ret == 0) {
884 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400885 &BTRFS_I(inode)->io_tree,
886 start, end, NULL,
887 EXTENT_CLEAR_UNLOCK_PAGE |
888 EXTENT_CLEAR_UNLOCK |
889 EXTENT_CLEAR_DELALLOC |
890 EXTENT_CLEAR_DIRTY |
891 EXTENT_SET_WRITEBACK |
892 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000893
Chris Mason771ed682008-11-06 22:02:51 -0500894 *nr_written = *nr_written +
895 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
896 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500897 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100898 } else if (ret < 0) {
899 btrfs_abort_transaction(trans, root, ret);
900 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500901 }
902 }
Chris Masonc8b97812008-10-29 14:49:59 -0400903
904 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200905 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400906
Josef Bacik4b46fce2010-05-23 11:00:55 -0400907 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400908 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400909
Chris Masond3977122009-01-05 21:25:51 -0500910 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400911 unsigned long op;
912
Josef Bacik287a0ab2010-03-19 18:07:23 +0000913 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400914 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500915 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500916 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100917 if (ret < 0) {
918 btrfs_abort_transaction(trans, root, ret);
919 goto out_unlock;
920 }
Chris Masond3977122009-01-05 21:25:51 -0500921
David Sterba172ddd62011-04-21 00:48:27 +0200922 em = alloc_extent_map();
Liu Boace68ba2013-04-22 10:53:47 +0000923 if (!em)
924 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400925 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500926 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500927 ram_size = ins.offset;
928 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400929 em->mod_start = em->start;
930 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400931
Chris Masone6dcd2d2008-07-17 12:53:50 -0400932 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400933 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500934 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400935 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400936 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400937 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400938 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400939
Chris Masond3977122009-01-05 21:25:51 -0500940 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400941 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400942 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400943 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400944 if (ret != -EEXIST) {
945 free_extent_map(em);
946 break;
947 }
948 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400949 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400950 }
Liu Boace68ba2013-04-22 10:53:47 +0000951 if (ret)
952 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400953
Chris Mason98d20f62008-04-14 09:46:10 -0400954 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400955 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500956 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000957 if (ret)
958 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400959
Yan Zheng17d217f2008-12-12 10:03:38 -0500960 if (root->root_key.objectid ==
961 BTRFS_DATA_RELOC_TREE_OBJECTID) {
962 ret = btrfs_reloc_clone_csums(inode, start,
963 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100964 if (ret) {
965 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000966 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100967 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500968 }
969
Chris Masond3977122009-01-05 21:25:51 -0500970 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400971 break;
Chris Masond3977122009-01-05 21:25:51 -0500972
Chris Masonc8b97812008-10-29 14:49:59 -0400973 /* we're not doing compressed IO, don't unlock the first
974 * page (which the caller expects to stay locked), don't
975 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400976 *
977 * Do set the Private2 bit so we know this page was properly
978 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400979 */
Chris Masona791e352009-10-08 11:27:10 -0400980 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
981 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
982 EXTENT_SET_PRIVATE2;
983
Chris Masonc8b97812008-10-29 14:49:59 -0400984 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
985 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400986 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400987 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500988 num_bytes -= cur_alloc_size;
989 alloc_hint = ins.objectid + ins.offset;
990 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400991 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100992out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500993 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000994
Liu Boace68ba2013-04-22 10:53:47 +0000995out_reserve:
996 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
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);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001390 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001391 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001392 if (ret != -EEXIST) {
1393 free_extent_map(em);
1394 break;
1395 }
1396 btrfs_drop_extent_cache(inode, em->start,
1397 em->start + em->len - 1, 0);
1398 }
1399 type = BTRFS_ORDERED_PREALLOC;
1400 } else {
1401 type = BTRFS_ORDERED_NOCOW;
1402 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001403
1404 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001405 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001406 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001407
Yan, Zhengefa56462010-05-16 10:49:59 -04001408 if (root->root_key.objectid ==
1409 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1410 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1411 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001412 if (ret) {
1413 btrfs_abort_transaction(trans, root, ret);
1414 goto error;
1415 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001416 }
1417
Yan Zhengd899e052008-10-30 14:25:28 -04001418 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001419 cur_offset, cur_offset + num_bytes - 1,
1420 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1421 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1422 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001423 cur_offset = extent_end;
1424 if (cur_offset > end)
1425 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001426 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001427 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001428
Josef Bacik17ca04a2012-05-31 15:58:55 -04001429 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001430 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001431 cur_offset = end;
1432 }
1433
Yan Zheng80ff3852008-10-30 14:20:02 -04001434 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001435 ret = __cow_file_range(trans, inode, root, locked_page,
1436 cow_start, end,
1437 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001438 if (ret) {
1439 btrfs_abort_transaction(trans, root, ret);
1440 goto error;
1441 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001442 }
1443
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444error:
Miao Xiea698d0752012-09-20 01:51:59 -06001445 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001446 if (!ret)
1447 ret = err;
1448
Josef Bacik17ca04a2012-05-31 15:58:55 -04001449 if (ret && cur_offset < end)
1450 extent_clear_unlock_delalloc(inode,
1451 &BTRFS_I(inode)->io_tree,
1452 cur_offset, end, locked_page,
1453 EXTENT_CLEAR_UNLOCK_PAGE |
1454 EXTENT_CLEAR_UNLOCK |
1455 EXTENT_CLEAR_DELALLOC |
1456 EXTENT_CLEAR_DIRTY |
1457 EXTENT_SET_WRITEBACK |
1458 EXTENT_END_WRITEBACK);
1459
Yan Zheng7ea394f2008-08-05 13:05:02 -04001460 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001461 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001462}
1463
Chris Masond352ac62008-09-29 15:18:18 -04001464/*
1465 * extent_io.c call back to do delayed allocation processing
1466 */
Chris Masonc8b97812008-10-29 14:49:59 -04001467static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001468 u64 start, u64 end, int *page_started,
1469 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001470{
Chris Masonbe20aa92007-12-17 20:14:01 -05001471 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001472 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001473
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001474 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001475 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001476 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001477 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001478 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001479 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001480 } else if (!btrfs_test_opt(root, COMPRESS) &&
1481 !(BTRFS_I(inode)->force_compress) &&
1482 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001483 ret = cow_file_range(inode, locked_page, start, end,
1484 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001485 } else {
1486 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1487 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001488 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001489 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001490 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001491 return ret;
1492}
1493
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001494static void btrfs_split_extent_hook(struct inode *inode,
1495 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001496{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001497 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001498 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001499 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001500
Josef Bacik9e0baf62011-07-15 15:16:44 +00001501 spin_lock(&BTRFS_I(inode)->lock);
1502 BTRFS_I(inode)->outstanding_extents++;
1503 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001504}
1505
1506/*
1507 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1508 * extents so we can keep track of new extents that are just merged onto old
1509 * extents, such as when we are doing sequential writes, so we can properly
1510 * account for the metadata space we'll need.
1511 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001512static void btrfs_merge_extent_hook(struct inode *inode,
1513 struct extent_state *new,
1514 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001515{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001516 /* not delalloc, ignore it */
1517 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001518 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001519
Josef Bacik9e0baf62011-07-15 15:16:44 +00001520 spin_lock(&BTRFS_I(inode)->lock);
1521 BTRFS_I(inode)->outstanding_extents--;
1522 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001523}
1524
Chris Masond352ac62008-09-29 15:18:18 -04001525/*
1526 * extent_io.c set_bit_hook, used to track delayed allocation
1527 * bytes in this file, and to maintain the list of inodes that
1528 * have pending delalloc work to be done.
1529 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001530static void btrfs_set_bit_hook(struct inode *inode,
1531 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001532{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001533
Chris Mason75eff682008-12-15 15:54:40 -05001534 /*
1535 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001536 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001537 * bit, which is only set or cleared with irqs on
1538 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001539 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001540 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001541 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001542 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001543
Josef Bacik9e0baf62011-07-15 15:16:44 +00001544 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001545 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001546 } else {
1547 spin_lock(&BTRFS_I(inode)->lock);
1548 BTRFS_I(inode)->outstanding_extents++;
1549 spin_unlock(&BTRFS_I(inode)->lock);
1550 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001551
Miao Xie963d6782013-01-29 10:10:51 +00001552 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1553 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001554 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001555 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001556 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1557 &BTRFS_I(inode)->runtime_flags)) {
1558 spin_lock(&root->fs_info->delalloc_lock);
1559 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1560 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1561 &root->fs_info->delalloc_inodes);
1562 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1563 &BTRFS_I(inode)->runtime_flags);
1564 }
1565 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001566 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001567 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001568 }
Chris Mason291d6732008-01-29 15:55:23 -05001569}
1570
Chris Masond352ac62008-09-29 15:18:18 -04001571/*
1572 * extent_io.c clear_bit_hook, see set_bit_hook for why
1573 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001574static void btrfs_clear_bit_hook(struct inode *inode,
1575 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001576{
Chris Mason75eff682008-12-15 15:54:40 -05001577 /*
1578 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001579 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001580 * bit, which is only set or cleared with irqs on
1581 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001582 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001583 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001584 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001585 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001586
Josef Bacik9e0baf62011-07-15 15:16:44 +00001587 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001588 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001589 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1590 spin_lock(&BTRFS_I(inode)->lock);
1591 BTRFS_I(inode)->outstanding_extents--;
1592 spin_unlock(&BTRFS_I(inode)->lock);
1593 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001594
1595 if (*bits & EXTENT_DO_ACCOUNTING)
1596 btrfs_delalloc_release_metadata(inode, len);
1597
Josef Bacik0cb59c92010-07-02 12:14:14 -04001598 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1599 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001600 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001601
Miao Xie963d6782013-01-29 10:10:51 +00001602 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1603 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001604 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001605 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001606 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001607 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1608 &BTRFS_I(inode)->runtime_flags)) {
1609 spin_lock(&root->fs_info->delalloc_lock);
1610 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1611 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1612 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1613 &BTRFS_I(inode)->runtime_flags);
1614 }
1615 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001616 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001617 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001618 }
Chris Mason291d6732008-01-29 15:55:23 -05001619}
1620
Chris Masond352ac62008-09-29 15:18:18 -04001621/*
1622 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1623 * we don't create bios that span stripes or chunks
1624 */
David Woodhouse64a16702009-07-15 23:29:37 +01001625int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001626 size_t size, struct bio *bio,
1627 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001628{
1629 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001630 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001631 u64 length = 0;
1632 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001633 int ret;
1634
Chris Mason771ed682008-11-06 22:02:51 -05001635 if (bio_flags & EXTENT_BIO_COMPRESSED)
1636 return 0;
1637
Chris Masonf2d8d742008-04-21 10:03:05 -04001638 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001639 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001640 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001641 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001642 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001643 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001644 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001645 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001646 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001647}
1648
Chris Masond352ac62008-09-29 15:18:18 -04001649/*
1650 * in order to insert checksums into the metadata in large chunks,
1651 * we wait until bio submission time. All the pages in the bio are
1652 * checksummed and sums are attached onto the ordered extent record.
1653 *
1654 * At IO completion time the cums attached on the ordered extent record
1655 * are inserted into the btree
1656 */
Chris Masond3977122009-01-05 21:25:51 -05001657static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1658 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001659 unsigned long bio_flags,
1660 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001661{
Chris Mason065631f2008-02-20 12:07:25 -05001662 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001663 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001664
Chris Masond20f7042008-12-08 16:58:54 -05001665 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001666 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001667 return 0;
1668}
Chris Masone0156402008-04-16 11:15:20 -04001669
Chris Mason4a69a412008-11-06 22:03:00 -05001670/*
1671 * in order to insert checksums into the metadata in large chunks,
1672 * we wait until bio submission time. All the pages in the bio are
1673 * checksummed and sums are attached onto the ordered extent record.
1674 *
1675 * At IO completion time the cums attached on the ordered extent record
1676 * are inserted into the btree
1677 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001678static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001679 int mirror_num, unsigned long bio_flags,
1680 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001681{
1682 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001683 int ret;
1684
1685 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1686 if (ret)
1687 bio_endio(bio, ret);
1688 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001689}
1690
Chris Masond352ac62008-09-29 15:18:18 -04001691/*
Chris Masoncad321a2008-12-17 14:51:42 -05001692 * extent_io.c submission hook. This does the right thing for csum calculation
1693 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001694 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001695static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001696 int mirror_num, unsigned long bio_flags,
1697 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001698{
1699 struct btrfs_root *root = BTRFS_I(inode)->root;
1700 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001701 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001702 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001703 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001704
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001705 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001706
Liu Bo83eea1f2012-07-10 05:28:39 -06001707 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001708 metadata = 2;
1709
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001710 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001711 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1712 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001713 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001714
Chris Masond20f7042008-12-08 16:58:54 -05001715 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001716 ret = btrfs_submit_compressed_read(inode, bio,
1717 mirror_num,
1718 bio_flags);
1719 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001720 } else if (!skip_sum) {
1721 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1722 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001723 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001724 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001725 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001726 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001727 /* csum items have already been cloned */
1728 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1729 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001730 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001731 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001732 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001733 bio_flags, bio_offset,
1734 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001735 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001736 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001737 } else if (!skip_sum) {
1738 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1739 if (ret)
1740 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001741 }
1742
Chris Mason0b86a832008-03-24 15:01:56 -04001743mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001744 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1745
1746out:
1747 if (ret < 0)
1748 bio_endio(bio, ret);
1749 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001750}
Chris Mason6885f302008-02-20 16:11:05 -05001751
Chris Masond352ac62008-09-29 15:18:18 -04001752/*
1753 * given a list of ordered sums record them in the inode. This happens
1754 * at IO completion time based on sums calculated at bio submission time.
1755 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001756static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001757 struct inode *inode, u64 file_offset,
1758 struct list_head *list)
1759{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001760 struct btrfs_ordered_sum *sum;
1761
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001762 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001763 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001764 btrfs_csum_file_blocks(trans,
1765 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001766 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001767 }
1768 return 0;
1769}
1770
Josef Bacik2ac55d42010-02-03 19:33:23 +00001771int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1772 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001773{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001774 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001775 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001776 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001777}
1778
Chris Masond352ac62008-09-29 15:18:18 -04001779/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001780struct btrfs_writepage_fixup {
1781 struct page *page;
1782 struct btrfs_work work;
1783};
1784
Christoph Hellwigb2950862008-12-02 09:54:17 -05001785static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001786{
1787 struct btrfs_writepage_fixup *fixup;
1788 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001789 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001790 struct page *page;
1791 struct inode *inode;
1792 u64 page_start;
1793 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001794 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001795
1796 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1797 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001798again:
Chris Mason247e7432008-07-17 12:53:51 -04001799 lock_page(page);
1800 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1801 ClearPageChecked(page);
1802 goto out_page;
1803 }
1804
1805 inode = page->mapping->host;
1806 page_start = page_offset(page);
1807 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1808
Josef Bacik2ac55d42010-02-03 19:33:23 +00001809 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001810 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001811
1812 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001813 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001814 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001815
1816 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1817 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001818 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1819 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001820 unlock_page(page);
1821 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001822 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001823 goto again;
1824 }
Chris Mason247e7432008-07-17 12:53:51 -04001825
Jeff Mahoney87826df2012-02-15 16:23:57 +01001826 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1827 if (ret) {
1828 mapping_set_error(page->mapping, ret);
1829 end_extent_writepage(page, ret, page_start, page_end);
1830 ClearPageChecked(page);
1831 goto out;
1832 }
1833
Josef Bacik2ac55d42010-02-03 19:33:23 +00001834 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001835 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001836 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001837out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001838 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1839 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001840out_page:
1841 unlock_page(page);
1842 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001843 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001844}
1845
1846/*
1847 * There are a few paths in the higher layers of the kernel that directly
1848 * set the page dirty bit without asking the filesystem if it is a
1849 * good idea. This causes problems because we want to make sure COW
1850 * properly happens and the data=ordered rules are followed.
1851 *
Chris Masonc8b97812008-10-29 14:49:59 -04001852 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001853 * hasn't been properly setup for IO. We kick off an async process
1854 * to fix it up. The async helper will wait for ordered extents, set
1855 * the delalloc bit and make it safe to write the page.
1856 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001857static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001858{
1859 struct inode *inode = page->mapping->host;
1860 struct btrfs_writepage_fixup *fixup;
1861 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001862
Chris Mason8b62b722009-09-02 16:53:46 -04001863 /* this page is properly in the ordered list */
1864 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001865 return 0;
1866
1867 if (PageChecked(page))
1868 return -EAGAIN;
1869
1870 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1871 if (!fixup)
1872 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001873
Chris Mason247e7432008-07-17 12:53:51 -04001874 SetPageChecked(page);
1875 page_cache_get(page);
1876 fixup->work.func = btrfs_writepage_fixup_worker;
1877 fixup->page = page;
1878 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001879 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001880}
1881
Yan Zhengd899e052008-10-30 14:25:28 -04001882static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1883 struct inode *inode, u64 file_pos,
1884 u64 disk_bytenr, u64 disk_num_bytes,
1885 u64 num_bytes, u64 ram_bytes,
1886 u8 compression, u8 encryption,
1887 u16 other_encoding, int extent_type)
1888{
1889 struct btrfs_root *root = BTRFS_I(inode)->root;
1890 struct btrfs_file_extent_item *fi;
1891 struct btrfs_path *path;
1892 struct extent_buffer *leaf;
1893 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001894 int ret;
1895
1896 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001897 if (!path)
1898 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001899
Chris Masonb9473432009-03-13 11:00:37 -04001900 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001901
1902 /*
1903 * we may be replacing one extent in the tree with another.
1904 * The new extent is pinned in the extent map, and we don't want
1905 * to drop it from the cache until it is completely in the btree.
1906 *
1907 * So, tell btrfs_drop_extents to leave this extent in the cache.
1908 * the caller is expected to unpin it and allow it to be merged
1909 * with the others.
1910 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001911 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001912 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001913 if (ret)
1914 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001915
Li Zefan33345d012011-04-20 10:31:50 +08001916 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001917 ins.offset = file_pos;
1918 ins.type = BTRFS_EXTENT_DATA_KEY;
1919 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001920 if (ret)
1921 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001922 leaf = path->nodes[0];
1923 fi = btrfs_item_ptr(leaf, path->slots[0],
1924 struct btrfs_file_extent_item);
1925 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1926 btrfs_set_file_extent_type(leaf, fi, extent_type);
1927 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1928 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1929 btrfs_set_file_extent_offset(leaf, fi, 0);
1930 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1931 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1932 btrfs_set_file_extent_compression(leaf, fi, compression);
1933 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1934 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001935
Yan Zhengd899e052008-10-30 14:25:28 -04001936 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001937 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001938
1939 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001940
1941 ins.objectid = disk_bytenr;
1942 ins.offset = disk_num_bytes;
1943 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001944 ret = btrfs_alloc_reserved_file_extent(trans, root,
1945 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001946 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001947out:
Yan Zhengd899e052008-10-30 14:25:28 -04001948 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001949
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001950 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001951}
1952
Liu Bo38c227d2013-01-29 03:18:40 +00001953/* snapshot-aware defrag */
1954struct sa_defrag_extent_backref {
1955 struct rb_node node;
1956 struct old_sa_defrag_extent *old;
1957 u64 root_id;
1958 u64 inum;
1959 u64 file_pos;
1960 u64 extent_offset;
1961 u64 num_bytes;
1962 u64 generation;
1963};
1964
1965struct old_sa_defrag_extent {
1966 struct list_head list;
1967 struct new_sa_defrag_extent *new;
1968
1969 u64 extent_offset;
1970 u64 bytenr;
1971 u64 offset;
1972 u64 len;
1973 int count;
1974};
1975
1976struct new_sa_defrag_extent {
1977 struct rb_root root;
1978 struct list_head head;
1979 struct btrfs_path *path;
1980 struct inode *inode;
1981 u64 file_pos;
1982 u64 len;
1983 u64 bytenr;
1984 u64 disk_len;
1985 u8 compress_type;
1986};
1987
1988static int backref_comp(struct sa_defrag_extent_backref *b1,
1989 struct sa_defrag_extent_backref *b2)
1990{
1991 if (b1->root_id < b2->root_id)
1992 return -1;
1993 else if (b1->root_id > b2->root_id)
1994 return 1;
1995
1996 if (b1->inum < b2->inum)
1997 return -1;
1998 else if (b1->inum > b2->inum)
1999 return 1;
2000
2001 if (b1->file_pos < b2->file_pos)
2002 return -1;
2003 else if (b1->file_pos > b2->file_pos)
2004 return 1;
2005
2006 /*
2007 * [------------------------------] ===> (a range of space)
2008 * |<--->| |<---->| =============> (fs/file tree A)
2009 * |<---------------------------->| ===> (fs/file tree B)
2010 *
2011 * A range of space can refer to two file extents in one tree while
2012 * refer to only one file extent in another tree.
2013 *
2014 * So we may process a disk offset more than one time(two extents in A)
2015 * and locate at the same extent(one extent in B), then insert two same
2016 * backrefs(both refer to the extent in B).
2017 */
2018 return 0;
2019}
2020
2021static void backref_insert(struct rb_root *root,
2022 struct sa_defrag_extent_backref *backref)
2023{
2024 struct rb_node **p = &root->rb_node;
2025 struct rb_node *parent = NULL;
2026 struct sa_defrag_extent_backref *entry;
2027 int ret;
2028
2029 while (*p) {
2030 parent = *p;
2031 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2032
2033 ret = backref_comp(backref, entry);
2034 if (ret < 0)
2035 p = &(*p)->rb_left;
2036 else
2037 p = &(*p)->rb_right;
2038 }
2039
2040 rb_link_node(&backref->node, parent, p);
2041 rb_insert_color(&backref->node, root);
2042}
2043
2044/*
2045 * Note the backref might has changed, and in this case we just return 0.
2046 */
2047static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2048 void *ctx)
2049{
2050 struct btrfs_file_extent_item *extent;
2051 struct btrfs_fs_info *fs_info;
2052 struct old_sa_defrag_extent *old = ctx;
2053 struct new_sa_defrag_extent *new = old->new;
2054 struct btrfs_path *path = new->path;
2055 struct btrfs_key key;
2056 struct btrfs_root *root;
2057 struct sa_defrag_extent_backref *backref;
2058 struct extent_buffer *leaf;
2059 struct inode *inode = new->inode;
2060 int slot;
2061 int ret;
2062 u64 extent_offset;
2063 u64 num_bytes;
2064
2065 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2066 inum == btrfs_ino(inode))
2067 return 0;
2068
2069 key.objectid = root_id;
2070 key.type = BTRFS_ROOT_ITEM_KEY;
2071 key.offset = (u64)-1;
2072
2073 fs_info = BTRFS_I(inode)->root->fs_info;
2074 root = btrfs_read_fs_root_no_name(fs_info, &key);
2075 if (IS_ERR(root)) {
2076 if (PTR_ERR(root) == -ENOENT)
2077 return 0;
2078 WARN_ON(1);
2079 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2080 inum, offset, root_id);
2081 return PTR_ERR(root);
2082 }
2083
2084 key.objectid = inum;
2085 key.type = BTRFS_EXTENT_DATA_KEY;
2086 if (offset > (u64)-1 << 32)
2087 key.offset = 0;
2088 else
2089 key.offset = offset;
2090
2091 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2092 if (ret < 0) {
2093 WARN_ON(1);
2094 return ret;
2095 }
2096
2097 while (1) {
2098 cond_resched();
2099
2100 leaf = path->nodes[0];
2101 slot = path->slots[0];
2102
2103 if (slot >= btrfs_header_nritems(leaf)) {
2104 ret = btrfs_next_leaf(root, path);
2105 if (ret < 0) {
2106 goto out;
2107 } else if (ret > 0) {
2108 ret = 0;
2109 goto out;
2110 }
2111 continue;
2112 }
2113
2114 path->slots[0]++;
2115
2116 btrfs_item_key_to_cpu(leaf, &key, slot);
2117
2118 if (key.objectid > inum)
2119 goto out;
2120
2121 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2122 continue;
2123
2124 extent = btrfs_item_ptr(leaf, slot,
2125 struct btrfs_file_extent_item);
2126
2127 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2128 continue;
2129
2130 extent_offset = btrfs_file_extent_offset(leaf, extent);
2131 if (key.offset - extent_offset != offset)
2132 continue;
2133
2134 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2135 if (extent_offset >= old->extent_offset + old->offset +
2136 old->len || extent_offset + num_bytes <=
2137 old->extent_offset + old->offset)
2138 continue;
2139
2140 break;
2141 }
2142
2143 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2144 if (!backref) {
2145 ret = -ENOENT;
2146 goto out;
2147 }
2148
2149 backref->root_id = root_id;
2150 backref->inum = inum;
2151 backref->file_pos = offset + extent_offset;
2152 backref->num_bytes = num_bytes;
2153 backref->extent_offset = extent_offset;
2154 backref->generation = btrfs_file_extent_generation(leaf, extent);
2155 backref->old = old;
2156 backref_insert(&new->root, backref);
2157 old->count++;
2158out:
2159 btrfs_release_path(path);
2160 WARN_ON(ret);
2161 return ret;
2162}
2163
2164static noinline bool record_extent_backrefs(struct btrfs_path *path,
2165 struct new_sa_defrag_extent *new)
2166{
2167 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2168 struct old_sa_defrag_extent *old, *tmp;
2169 int ret;
2170
2171 new->path = path;
2172
2173 list_for_each_entry_safe(old, tmp, &new->head, list) {
2174 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2175 path, record_one_backref,
2176 old);
2177 BUG_ON(ret < 0 && ret != -ENOENT);
2178
2179 /* no backref to be processed for this extent */
2180 if (!old->count) {
2181 list_del(&old->list);
2182 kfree(old);
2183 }
2184 }
2185
2186 if (list_empty(&new->head))
2187 return false;
2188
2189 return true;
2190}
2191
2192static int relink_is_mergable(struct extent_buffer *leaf,
2193 struct btrfs_file_extent_item *fi,
2194 u64 disk_bytenr)
2195{
2196 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2197 return 0;
2198
2199 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2200 return 0;
2201
2202 if (btrfs_file_extent_compression(leaf, fi) ||
2203 btrfs_file_extent_encryption(leaf, fi) ||
2204 btrfs_file_extent_other_encoding(leaf, fi))
2205 return 0;
2206
2207 return 1;
2208}
2209
2210/*
2211 * Note the backref might has changed, and in this case we just return 0.
2212 */
2213static noinline int relink_extent_backref(struct btrfs_path *path,
2214 struct sa_defrag_extent_backref *prev,
2215 struct sa_defrag_extent_backref *backref)
2216{
2217 struct btrfs_file_extent_item *extent;
2218 struct btrfs_file_extent_item *item;
2219 struct btrfs_ordered_extent *ordered;
2220 struct btrfs_trans_handle *trans;
2221 struct btrfs_fs_info *fs_info;
2222 struct btrfs_root *root;
2223 struct btrfs_key key;
2224 struct extent_buffer *leaf;
2225 struct old_sa_defrag_extent *old = backref->old;
2226 struct new_sa_defrag_extent *new = old->new;
2227 struct inode *src_inode = new->inode;
2228 struct inode *inode;
2229 struct extent_state *cached = NULL;
2230 int ret = 0;
2231 u64 start;
2232 u64 len;
2233 u64 lock_start;
2234 u64 lock_end;
2235 bool merge = false;
2236 int index;
2237
2238 if (prev && prev->root_id == backref->root_id &&
2239 prev->inum == backref->inum &&
2240 prev->file_pos + prev->num_bytes == backref->file_pos)
2241 merge = true;
2242
2243 /* step 1: get root */
2244 key.objectid = backref->root_id;
2245 key.type = BTRFS_ROOT_ITEM_KEY;
2246 key.offset = (u64)-1;
2247
2248 fs_info = BTRFS_I(src_inode)->root->fs_info;
2249 index = srcu_read_lock(&fs_info->subvol_srcu);
2250
2251 root = btrfs_read_fs_root_no_name(fs_info, &key);
2252 if (IS_ERR(root)) {
2253 srcu_read_unlock(&fs_info->subvol_srcu, index);
2254 if (PTR_ERR(root) == -ENOENT)
2255 return 0;
2256 return PTR_ERR(root);
2257 }
2258 if (btrfs_root_refs(&root->root_item) == 0) {
2259 srcu_read_unlock(&fs_info->subvol_srcu, index);
2260 /* parse ENOENT to 0 */
2261 return 0;
2262 }
2263
2264 /* step 2: get inode */
2265 key.objectid = backref->inum;
2266 key.type = BTRFS_INODE_ITEM_KEY;
2267 key.offset = 0;
2268
2269 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2270 if (IS_ERR(inode)) {
2271 srcu_read_unlock(&fs_info->subvol_srcu, index);
2272 return 0;
2273 }
2274
2275 srcu_read_unlock(&fs_info->subvol_srcu, index);
2276
2277 /* step 3: relink backref */
2278 lock_start = backref->file_pos;
2279 lock_end = backref->file_pos + backref->num_bytes - 1;
2280 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2281 0, &cached);
2282
2283 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2284 if (ordered) {
2285 btrfs_put_ordered_extent(ordered);
2286 goto out_unlock;
2287 }
2288
2289 trans = btrfs_join_transaction(root);
2290 if (IS_ERR(trans)) {
2291 ret = PTR_ERR(trans);
2292 goto out_unlock;
2293 }
2294
2295 key.objectid = backref->inum;
2296 key.type = BTRFS_EXTENT_DATA_KEY;
2297 key.offset = backref->file_pos;
2298
2299 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2300 if (ret < 0) {
2301 goto out_free_path;
2302 } else if (ret > 0) {
2303 ret = 0;
2304 goto out_free_path;
2305 }
2306
2307 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2308 struct btrfs_file_extent_item);
2309
2310 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2311 backref->generation)
2312 goto out_free_path;
2313
2314 btrfs_release_path(path);
2315
2316 start = backref->file_pos;
2317 if (backref->extent_offset < old->extent_offset + old->offset)
2318 start += old->extent_offset + old->offset -
2319 backref->extent_offset;
2320
2321 len = min(backref->extent_offset + backref->num_bytes,
2322 old->extent_offset + old->offset + old->len);
2323 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2324
2325 ret = btrfs_drop_extents(trans, root, inode, start,
2326 start + len, 1);
2327 if (ret)
2328 goto out_free_path;
2329again:
2330 key.objectid = btrfs_ino(inode);
2331 key.type = BTRFS_EXTENT_DATA_KEY;
2332 key.offset = start;
2333
Liu Boa09a0a72013-03-11 09:20:58 +00002334 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002335 if (merge) {
2336 struct btrfs_file_extent_item *fi;
2337 u64 extent_len;
2338 struct btrfs_key found_key;
2339
2340 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2341 if (ret < 0)
2342 goto out_free_path;
2343
2344 path->slots[0]--;
2345 leaf = path->nodes[0];
2346 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2347
2348 fi = btrfs_item_ptr(leaf, path->slots[0],
2349 struct btrfs_file_extent_item);
2350 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2351
2352 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2353 extent_len + found_key.offset == start) {
2354 btrfs_set_file_extent_num_bytes(leaf, fi,
2355 extent_len + len);
2356 btrfs_mark_buffer_dirty(leaf);
2357 inode_add_bytes(inode, len);
2358
2359 ret = 1;
2360 goto out_free_path;
2361 } else {
2362 merge = false;
2363 btrfs_release_path(path);
2364 goto again;
2365 }
2366 }
2367
2368 ret = btrfs_insert_empty_item(trans, root, path, &key,
2369 sizeof(*extent));
2370 if (ret) {
2371 btrfs_abort_transaction(trans, root, ret);
2372 goto out_free_path;
2373 }
2374
2375 leaf = path->nodes[0];
2376 item = btrfs_item_ptr(leaf, path->slots[0],
2377 struct btrfs_file_extent_item);
2378 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2379 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2380 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2381 btrfs_set_file_extent_num_bytes(leaf, item, len);
2382 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2383 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2384 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2385 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2386 btrfs_set_file_extent_encryption(leaf, item, 0);
2387 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2388
2389 btrfs_mark_buffer_dirty(leaf);
2390 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002391 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002392
2393 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2394 new->disk_len, 0,
2395 backref->root_id, backref->inum,
2396 new->file_pos, 0); /* start - extent_offset */
2397 if (ret) {
2398 btrfs_abort_transaction(trans, root, ret);
2399 goto out_free_path;
2400 }
2401
2402 ret = 1;
2403out_free_path:
2404 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002405 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002406 btrfs_end_transaction(trans, root);
2407out_unlock:
2408 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2409 &cached, GFP_NOFS);
2410 iput(inode);
2411 return ret;
2412}
2413
2414static void relink_file_extents(struct new_sa_defrag_extent *new)
2415{
2416 struct btrfs_path *path;
2417 struct old_sa_defrag_extent *old, *tmp;
2418 struct sa_defrag_extent_backref *backref;
2419 struct sa_defrag_extent_backref *prev = NULL;
2420 struct inode *inode;
2421 struct btrfs_root *root;
2422 struct rb_node *node;
2423 int ret;
2424
2425 inode = new->inode;
2426 root = BTRFS_I(inode)->root;
2427
2428 path = btrfs_alloc_path();
2429 if (!path)
2430 return;
2431
2432 if (!record_extent_backrefs(path, new)) {
2433 btrfs_free_path(path);
2434 goto out;
2435 }
2436 btrfs_release_path(path);
2437
2438 while (1) {
2439 node = rb_first(&new->root);
2440 if (!node)
2441 break;
2442 rb_erase(node, &new->root);
2443
2444 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2445
2446 ret = relink_extent_backref(path, prev, backref);
2447 WARN_ON(ret < 0);
2448
2449 kfree(prev);
2450
2451 if (ret == 1)
2452 prev = backref;
2453 else
2454 prev = NULL;
2455 cond_resched();
2456 }
2457 kfree(prev);
2458
2459 btrfs_free_path(path);
2460
2461 list_for_each_entry_safe(old, tmp, &new->head, list) {
2462 list_del(&old->list);
2463 kfree(old);
2464 }
2465out:
2466 atomic_dec(&root->fs_info->defrag_running);
2467 wake_up(&root->fs_info->transaction_wait);
2468
2469 kfree(new);
2470}
2471
2472static struct new_sa_defrag_extent *
2473record_old_file_extents(struct inode *inode,
2474 struct btrfs_ordered_extent *ordered)
2475{
2476 struct btrfs_root *root = BTRFS_I(inode)->root;
2477 struct btrfs_path *path;
2478 struct btrfs_key key;
2479 struct old_sa_defrag_extent *old, *tmp;
2480 struct new_sa_defrag_extent *new;
2481 int ret;
2482
2483 new = kmalloc(sizeof(*new), GFP_NOFS);
2484 if (!new)
2485 return NULL;
2486
2487 new->inode = inode;
2488 new->file_pos = ordered->file_offset;
2489 new->len = ordered->len;
2490 new->bytenr = ordered->start;
2491 new->disk_len = ordered->disk_len;
2492 new->compress_type = ordered->compress_type;
2493 new->root = RB_ROOT;
2494 INIT_LIST_HEAD(&new->head);
2495
2496 path = btrfs_alloc_path();
2497 if (!path)
2498 goto out_kfree;
2499
2500 key.objectid = btrfs_ino(inode);
2501 key.type = BTRFS_EXTENT_DATA_KEY;
2502 key.offset = new->file_pos;
2503
2504 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2505 if (ret < 0)
2506 goto out_free_path;
2507 if (ret > 0 && path->slots[0] > 0)
2508 path->slots[0]--;
2509
2510 /* find out all the old extents for the file range */
2511 while (1) {
2512 struct btrfs_file_extent_item *extent;
2513 struct extent_buffer *l;
2514 int slot;
2515 u64 num_bytes;
2516 u64 offset;
2517 u64 end;
2518 u64 disk_bytenr;
2519 u64 extent_offset;
2520
2521 l = path->nodes[0];
2522 slot = path->slots[0];
2523
2524 if (slot >= btrfs_header_nritems(l)) {
2525 ret = btrfs_next_leaf(root, path);
2526 if (ret < 0)
2527 goto out_free_list;
2528 else if (ret > 0)
2529 break;
2530 continue;
2531 }
2532
2533 btrfs_item_key_to_cpu(l, &key, slot);
2534
2535 if (key.objectid != btrfs_ino(inode))
2536 break;
2537 if (key.type != BTRFS_EXTENT_DATA_KEY)
2538 break;
2539 if (key.offset >= new->file_pos + new->len)
2540 break;
2541
2542 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2543
2544 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2545 if (key.offset + num_bytes < new->file_pos)
2546 goto next;
2547
2548 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2549 if (!disk_bytenr)
2550 goto next;
2551
2552 extent_offset = btrfs_file_extent_offset(l, extent);
2553
2554 old = kmalloc(sizeof(*old), GFP_NOFS);
2555 if (!old)
2556 goto out_free_list;
2557
2558 offset = max(new->file_pos, key.offset);
2559 end = min(new->file_pos + new->len, key.offset + num_bytes);
2560
2561 old->bytenr = disk_bytenr;
2562 old->extent_offset = extent_offset;
2563 old->offset = offset - key.offset;
2564 old->len = end - offset;
2565 old->new = new;
2566 old->count = 0;
2567 list_add_tail(&old->list, &new->head);
2568next:
2569 path->slots[0]++;
2570 cond_resched();
2571 }
2572
2573 btrfs_free_path(path);
2574 atomic_inc(&root->fs_info->defrag_running);
2575
2576 return new;
2577
2578out_free_list:
2579 list_for_each_entry_safe(old, tmp, &new->head, list) {
2580 list_del(&old->list);
2581 kfree(old);
2582 }
2583out_free_path:
2584 btrfs_free_path(path);
2585out_kfree:
2586 kfree(new);
2587 return NULL;
2588}
2589
Chris Mason5d13a982009-03-13 11:41:46 -04002590/*
2591 * helper function for btrfs_finish_ordered_io, this
2592 * just reads in some of the csum leaves to prime them into ram
2593 * before we start the transaction. It limits the amount of btree
2594 * reads required while inside the transaction.
2595 */
Chris Masond352ac62008-09-29 15:18:18 -04002596/* as ordered data IO finishes, this gets called so we can finish
2597 * an ordered extent if the range of bytes in the file it covers are
2598 * fully written.
2599 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002600static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002601{
Josef Bacik5fd02042012-05-02 14:00:54 -04002602 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002603 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002604 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002605 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002606 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002607 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002608 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002609 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002610 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002611
Liu Bo83eea1f2012-07-10 05:28:39 -06002612 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002613
Josef Bacik5fd02042012-05-02 14:00:54 -04002614 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2615 ret = -EIO;
2616 goto out;
2617 }
2618
Yan, Zhengc2167752009-11-12 09:34:21 +00002619 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002620 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002621 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2622 if (nolock)
2623 trans = btrfs_join_transaction_nolock(root);
2624 else
2625 trans = btrfs_join_transaction(root);
2626 if (IS_ERR(trans)) {
2627 ret = PTR_ERR(trans);
2628 trans = NULL;
2629 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002630 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002631 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2632 ret = btrfs_update_inode_fallback(trans, root, inode);
2633 if (ret) /* -ENOMEM or corruption */
2634 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002635 goto out;
2636 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002637
Josef Bacik2ac55d42010-02-03 19:33:23 +00002638 lock_extent_bits(io_tree, ordered_extent->file_offset,
2639 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002640 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002641
Liu Bo38c227d2013-01-29 03:18:40 +00002642 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2643 ordered_extent->file_offset + ordered_extent->len - 1,
2644 EXTENT_DEFRAG, 1, cached_state);
2645 if (ret) {
2646 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2647 if (last_snapshot >= BTRFS_I(inode)->generation)
2648 /* the inode is shared */
2649 new = record_old_file_extents(inode, ordered_extent);
2650
2651 clear_extent_bit(io_tree, ordered_extent->file_offset,
2652 ordered_extent->file_offset + ordered_extent->len - 1,
2653 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2654 }
2655
Josef Bacik0cb59c92010-07-02 12:14:14 -04002656 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002657 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002658 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002659 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002660 if (IS_ERR(trans)) {
2661 ret = PTR_ERR(trans);
2662 trans = NULL;
2663 goto out_unlock;
2664 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002665 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002666
Chris Masonc8b97812008-10-29 14:49:59 -04002667 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002668 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002669 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002670 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002671 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002672 ordered_extent->file_offset,
2673 ordered_extent->file_offset +
2674 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002675 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002676 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002677 ret = insert_reserved_file_extent(trans, inode,
2678 ordered_extent->file_offset,
2679 ordered_extent->start,
2680 ordered_extent->disk_len,
2681 ordered_extent->len,
2682 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002683 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002684 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002685 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002686 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2687 ordered_extent->file_offset, ordered_extent->len,
2688 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002689 if (ret < 0) {
2690 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002691 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002692 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002693
Chris Masone6dcd2d2008-07-17 12:53:50 -04002694 add_pending_csums(trans, inode, ordered_extent->file_offset,
2695 &ordered_extent->list);
2696
Josef Bacik6c760c02012-11-09 10:53:21 -05002697 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2698 ret = btrfs_update_inode_fallback(trans, root, inode);
2699 if (ret) { /* -ENOMEM or corruption */
2700 btrfs_abort_transaction(trans, root, ret);
2701 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002702 }
2703 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002704out_unlock:
2705 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2706 ordered_extent->file_offset +
2707 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002708out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002709 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002710 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002711 if (trans)
2712 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002713
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002714 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002715 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2716 ordered_extent->file_offset +
2717 ordered_extent->len - 1, NULL, GFP_NOFS);
2718
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002719 /*
2720 * If the ordered extent had an IOERR or something else went
2721 * wrong we need to return the space for this ordered extent
2722 * back to the allocator.
2723 */
2724 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2725 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2726 btrfs_free_reserved_extent(root, ordered_extent->start,
2727 ordered_extent->disk_len);
2728 }
2729
2730
Josef Bacik5fd02042012-05-02 14:00:54 -04002731 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002732 * This needs to be done to make sure anybody waiting knows we are done
2733 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002734 */
2735 btrfs_remove_ordered_extent(inode, ordered_extent);
2736
Liu Bo38c227d2013-01-29 03:18:40 +00002737 /* for snapshot-aware defrag */
2738 if (new)
2739 relink_file_extents(new);
2740
Chris Masone6dcd2d2008-07-17 12:53:50 -04002741 /* once for us */
2742 btrfs_put_ordered_extent(ordered_extent);
2743 /* once for the tree */
2744 btrfs_put_ordered_extent(ordered_extent);
2745
Josef Bacik5fd02042012-05-02 14:00:54 -04002746 return ret;
2747}
2748
2749static void finish_ordered_fn(struct btrfs_work *work)
2750{
2751 struct btrfs_ordered_extent *ordered_extent;
2752 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2753 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002754}
2755
Christoph Hellwigb2950862008-12-02 09:54:17 -05002756static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002757 struct extent_state *state, int uptodate)
2758{
Josef Bacik5fd02042012-05-02 14:00:54 -04002759 struct inode *inode = page->mapping->host;
2760 struct btrfs_root *root = BTRFS_I(inode)->root;
2761 struct btrfs_ordered_extent *ordered_extent = NULL;
2762 struct btrfs_workers *workers;
2763
liubo1abe9b82011-03-24 11:18:59 +00002764 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2765
Chris Mason8b62b722009-09-02 16:53:46 -04002766 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002767 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2768 end - start + 1, uptodate))
2769 return 0;
2770
2771 ordered_extent->work.func = finish_ordered_fn;
2772 ordered_extent->work.flags = 0;
2773
Liu Bo83eea1f2012-07-10 05:28:39 -06002774 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002775 workers = &root->fs_info->endio_freespace_worker;
2776 else
2777 workers = &root->fs_info->endio_write_workers;
2778 btrfs_queue_worker(workers, &ordered_extent->work);
2779
2780 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002781}
2782
Chris Masond352ac62008-09-29 15:18:18 -04002783/*
Chris Masond352ac62008-09-29 15:18:18 -04002784 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002785 * if there's a match, we allow the bio to finish. If not, the code in
2786 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002787 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002788static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002789 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002790{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002791 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002792 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002793 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002794 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002795 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002796 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002797 struct btrfs_root *root = BTRFS_I(inode)->root;
2798 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002799 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2800 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002801
Chris Masond20f7042008-12-08 16:58:54 -05002802 if (PageChecked(page)) {
2803 ClearPageChecked(page);
2804 goto good;
2805 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002806
2807 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002808 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002809
Yan Zheng17d217f2008-12-12 10:03:38 -05002810 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002811 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002812 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2813 GFP_NOFS);
2814 return 0;
2815 }
2816
Yanc2e639f2008-02-04 08:57:25 -05002817 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002818 private = state->private;
2819 ret = 0;
2820 } else {
2821 ret = get_state_private(io_tree, start, &private);
2822 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002823 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002824 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002825 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002826
Liu Bob0496682013-03-14 14:57:45 +00002827 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002828 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002829 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002830 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002831
Cong Wang7ac687d2011-11-25 23:14:28 +08002832 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002833good:
Chris Mason07157aa2007-08-30 08:50:51 -04002834 return 0;
2835
2836zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002837 if (__ratelimit(&_rs))
2838 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2839 (unsigned long long)btrfs_ino(page->mapping->host),
2840 (unsigned long long)start, csum,
2841 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002842 memset(kaddr + offset, 1, end - start + 1);
2843 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002844 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002845 if (private == 0)
2846 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002847 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002848}
Chris Masonb888db2b2007-08-27 16:49:44 -04002849
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002850struct delayed_iput {
2851 struct list_head list;
2852 struct inode *inode;
2853};
2854
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002855/* JDM: If this is fs-wide, why can't we add a pointer to
2856 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002857void btrfs_add_delayed_iput(struct inode *inode)
2858{
2859 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2860 struct delayed_iput *delayed;
2861
2862 if (atomic_add_unless(&inode->i_count, -1, 1))
2863 return;
2864
2865 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2866 delayed->inode = inode;
2867
2868 spin_lock(&fs_info->delayed_iput_lock);
2869 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2870 spin_unlock(&fs_info->delayed_iput_lock);
2871}
2872
2873void btrfs_run_delayed_iputs(struct btrfs_root *root)
2874{
2875 LIST_HEAD(list);
2876 struct btrfs_fs_info *fs_info = root->fs_info;
2877 struct delayed_iput *delayed;
2878 int empty;
2879
2880 spin_lock(&fs_info->delayed_iput_lock);
2881 empty = list_empty(&fs_info->delayed_iputs);
2882 spin_unlock(&fs_info->delayed_iput_lock);
2883 if (empty)
2884 return;
2885
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002886 spin_lock(&fs_info->delayed_iput_lock);
2887 list_splice_init(&fs_info->delayed_iputs, &list);
2888 spin_unlock(&fs_info->delayed_iput_lock);
2889
2890 while (!list_empty(&list)) {
2891 delayed = list_entry(list.next, struct delayed_iput, list);
2892 list_del(&delayed->list);
2893 iput(delayed->inode);
2894 kfree(delayed);
2895 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002896}
2897
Yan, Zhengd68fc572010-05-16 10:49:58 -04002898/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002899 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002900 * files in the subvolume, it removes orphan item and frees block_rsv
2901 * structure.
2902 */
2903void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2904 struct btrfs_root *root)
2905{
Josef Bacik90290e12011-12-02 15:44:12 -05002906 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002907 int ret;
2908
Josef Bacik8a35d952012-05-23 14:26:42 -04002909 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002910 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2911 return;
2912
Josef Bacik90290e12011-12-02 15:44:12 -05002913 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002914 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002915 spin_unlock(&root->orphan_lock);
2916 return;
2917 }
2918
2919 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2920 spin_unlock(&root->orphan_lock);
2921 return;
2922 }
2923
2924 block_rsv = root->orphan_block_rsv;
2925 root->orphan_block_rsv = NULL;
2926 spin_unlock(&root->orphan_lock);
2927
Yan, Zhengd68fc572010-05-16 10:49:58 -04002928 if (root->orphan_item_inserted &&
2929 btrfs_root_refs(&root->root_item) > 0) {
2930 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2931 root->root_key.objectid);
2932 BUG_ON(ret);
2933 root->orphan_item_inserted = 0;
2934 }
2935
Josef Bacik90290e12011-12-02 15:44:12 -05002936 if (block_rsv) {
2937 WARN_ON(block_rsv->size > 0);
2938 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002939 }
2940}
2941
2942/*
Josef Bacik7b128762008-07-24 12:17:14 -04002943 * This creates an orphan entry for the given inode in case something goes
2944 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002945 *
2946 * NOTE: caller of this function should reserve 5 units of metadata for
2947 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002948 */
2949int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2950{
2951 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002952 struct btrfs_block_rsv *block_rsv = NULL;
2953 int reserve = 0;
2954 int insert = 0;
2955 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002956
Yan, Zhengd68fc572010-05-16 10:49:58 -04002957 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002958 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002959 if (!block_rsv)
2960 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002961 }
2962
Yan, Zhengd68fc572010-05-16 10:49:58 -04002963 spin_lock(&root->orphan_lock);
2964 if (!root->orphan_block_rsv) {
2965 root->orphan_block_rsv = block_rsv;
2966 } else if (block_rsv) {
2967 btrfs_free_block_rsv(root, block_rsv);
2968 block_rsv = NULL;
2969 }
Josef Bacik7b128762008-07-24 12:17:14 -04002970
Josef Bacik8a35d952012-05-23 14:26:42 -04002971 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2972 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002973#if 0
2974 /*
2975 * For proper ENOSPC handling, we should do orphan
2976 * cleanup when mounting. But this introduces backward
2977 * compatibility issue.
2978 */
2979 if (!xchg(&root->orphan_item_inserted, 1))
2980 insert = 2;
2981 else
2982 insert = 1;
2983#endif
2984 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002985 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002986 }
Josef Bacik7b128762008-07-24 12:17:14 -04002987
Josef Bacik72ac3c02012-05-23 14:13:11 -04002988 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2989 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002990 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002991 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002992
Yan, Zhengd68fc572010-05-16 10:49:58 -04002993 /* grab metadata reservation from transaction handle */
2994 if (reserve) {
2995 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002996 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002997 }
2998
2999 /* insert an orphan item to track this unlinked/truncated file */
3000 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003001 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003002 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003003 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3004 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003005 btrfs_abort_transaction(trans, root, ret);
3006 return ret;
3007 }
3008 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003009 }
3010
3011 /* insert an orphan item to track subvolume contains orphan files */
3012 if (insert >= 2) {
3013 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3014 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003015 if (ret && ret != -EEXIST) {
3016 btrfs_abort_transaction(trans, root, ret);
3017 return ret;
3018 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003019 }
3020 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003021}
3022
3023/*
3024 * We have done the truncate/delete so we can go ahead and remove the orphan
3025 * item for this particular inode.
3026 */
3027int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
3028{
3029 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003030 int delete_item = 0;
3031 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003032 int ret = 0;
3033
Yan, Zhengd68fc572010-05-16 10:49:58 -04003034 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003035 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3036 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003037 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003038
Josef Bacik72ac3c02012-05-23 14:13:11 -04003039 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3040 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003041 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003042 spin_unlock(&root->orphan_lock);
3043
3044 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003045 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003046 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003047 }
3048
Josef Bacik8a35d952012-05-23 14:26:42 -04003049 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003050 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003051 atomic_dec(&root->orphan_inodes);
3052 }
Josef Bacik7b128762008-07-24 12:17:14 -04003053
Yan, Zhengd68fc572010-05-16 10:49:58 -04003054 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003055}
3056
3057/*
3058 * this cleans up any orphans that may be left on the list from the last use
3059 * of this root.
3060 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003061int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003062{
3063 struct btrfs_path *path;
3064 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003065 struct btrfs_key key, found_key;
3066 struct btrfs_trans_handle *trans;
3067 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003068 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003069 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3070
Yan, Zhengd68fc572010-05-16 10:49:58 -04003071 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003072 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003073
3074 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003075 if (!path) {
3076 ret = -ENOMEM;
3077 goto out;
3078 }
Josef Bacik7b128762008-07-24 12:17:14 -04003079 path->reada = -1;
3080
3081 key.objectid = BTRFS_ORPHAN_OBJECTID;
3082 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3083 key.offset = (u64)-1;
3084
Josef Bacik7b128762008-07-24 12:17:14 -04003085 while (1) {
3086 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003087 if (ret < 0)
3088 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003089
3090 /*
3091 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003092 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003093 * find the key and see if we have stuff that matches
3094 */
3095 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003096 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003097 if (path->slots[0] == 0)
3098 break;
3099 path->slots[0]--;
3100 }
3101
3102 /* pull out the item */
3103 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003104 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3105
3106 /* make sure the item matches what we want */
3107 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3108 break;
3109 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3110 break;
3111
3112 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003113 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003114
3115 /*
3116 * this is where we are basically btrfs_lookup, without the
3117 * crossing root thing. we store the inode number in the
3118 * offset of the orphan item.
3119 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003120
3121 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003122 btrfs_err(root->fs_info,
3123 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003124 ret = -EINVAL;
3125 goto out;
3126 }
3127
3128 last_objectid = found_key.offset;
3129
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003130 found_key.objectid = found_key.offset;
3131 found_key.type = BTRFS_INODE_ITEM_KEY;
3132 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003133 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003134 ret = PTR_RET(inode);
3135 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003136 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003137
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003138 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3139 struct btrfs_root *dead_root;
3140 struct btrfs_fs_info *fs_info = root->fs_info;
3141 int is_dead_root = 0;
3142
3143 /*
3144 * this is an orphan in the tree root. Currently these
3145 * could come from 2 sources:
3146 * a) a snapshot deletion in progress
3147 * b) a free space cache inode
3148 * We need to distinguish those two, as the snapshot
3149 * orphan must not get deleted.
3150 * find_dead_roots already ran before us, so if this
3151 * is a snapshot deletion, we should find the root
3152 * in the dead_roots list
3153 */
3154 spin_lock(&fs_info->trans_lock);
3155 list_for_each_entry(dead_root, &fs_info->dead_roots,
3156 root_list) {
3157 if (dead_root->root_key.objectid ==
3158 found_key.objectid) {
3159 is_dead_root = 1;
3160 break;
3161 }
3162 }
3163 spin_unlock(&fs_info->trans_lock);
3164 if (is_dead_root) {
3165 /* prevent this orphan from being found again */
3166 key.offset = found_key.objectid - 1;
3167 continue;
3168 }
3169 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003170 /*
3171 * Inode is already gone but the orphan item is still there,
3172 * kill the orphan item.
3173 */
3174 if (ret == -ESTALE) {
3175 trans = btrfs_start_transaction(root, 1);
3176 if (IS_ERR(trans)) {
3177 ret = PTR_ERR(trans);
3178 goto out;
3179 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003180 btrfs_debug(root->fs_info, "auto deleting %Lu",
3181 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003182 ret = btrfs_del_orphan_item(trans, root,
3183 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003184 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003185 btrfs_end_transaction(trans, root);
3186 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003187 }
Josef Bacik7b128762008-07-24 12:17:14 -04003188
Josef Bacik7b128762008-07-24 12:17:14 -04003189 /*
3190 * add this inode to the orphan list so btrfs_orphan_del does
3191 * the proper thing when we hit it
3192 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003193 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3194 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003195 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003196
Josef Bacik7b128762008-07-24 12:17:14 -04003197 /* if we have links, this was a truncate, lets do that */
3198 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003199 if (!S_ISREG(inode->i_mode)) {
3200 WARN_ON(1);
3201 iput(inode);
3202 continue;
3203 }
Josef Bacik7b128762008-07-24 12:17:14 -04003204 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003205
3206 /* 1 for the orphan item deletion. */
3207 trans = btrfs_start_transaction(root, 1);
3208 if (IS_ERR(trans)) {
3209 ret = PTR_ERR(trans);
3210 goto out;
3211 }
3212 ret = btrfs_orphan_add(trans, inode);
3213 btrfs_end_transaction(trans, root);
3214 if (ret)
3215 goto out;
3216
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003217 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003218 if (ret)
3219 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003220 } else {
3221 nr_unlink++;
3222 }
3223
3224 /* this will do delete_inode and everything for us */
3225 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003226 if (ret)
3227 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003228 }
Miao Xie3254c872011-11-10 20:45:05 -05003229 /* release the path since we're done with it */
3230 btrfs_release_path(path);
3231
Yan, Zhengd68fc572010-05-16 10:49:58 -04003232 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3233
3234 if (root->orphan_block_rsv)
3235 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3236 (u64)-1);
3237
3238 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003239 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003240 if (!IS_ERR(trans))
3241 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003242 }
Josef Bacik7b128762008-07-24 12:17:14 -04003243
3244 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003245 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003246 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003247 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003248
3249out:
3250 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003251 btrfs_crit(root->fs_info,
3252 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003253 btrfs_free_path(path);
3254 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003255}
3256
Chris Masond352ac62008-09-29 15:18:18 -04003257/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003258 * very simple check to peek ahead in the leaf looking for xattrs. If we
3259 * don't find any xattrs, we know there can't be any acls.
3260 *
3261 * slot is the slot the inode is in, objectid is the objectid of the inode
3262 */
3263static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3264 int slot, u64 objectid)
3265{
3266 u32 nritems = btrfs_header_nritems(leaf);
3267 struct btrfs_key found_key;
3268 int scanned = 0;
3269
3270 slot++;
3271 while (slot < nritems) {
3272 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3273
3274 /* we found a different objectid, there must not be acls */
3275 if (found_key.objectid != objectid)
3276 return 0;
3277
3278 /* we found an xattr, assume we've got an acl */
3279 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3280 return 1;
3281
3282 /*
3283 * we found a key greater than an xattr key, there can't
3284 * be any acls later on
3285 */
3286 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3287 return 0;
3288
3289 slot++;
3290 scanned++;
3291
3292 /*
3293 * it goes inode, inode backrefs, xattrs, extents,
3294 * so if there are a ton of hard links to an inode there can
3295 * be a lot of backrefs. Don't waste time searching too hard,
3296 * this is just an optimization
3297 */
3298 if (scanned >= 8)
3299 break;
3300 }
3301 /* we hit the end of the leaf before we found an xattr or
3302 * something larger than an xattr. We have to assume the inode
3303 * has acls
3304 */
3305 return 1;
3306}
3307
3308/*
Chris Masond352ac62008-09-29 15:18:18 -04003309 * read an inode from the btree into the in-memory inode
3310 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003311static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003312{
3313 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003314 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003315 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003316 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003317 struct btrfs_root *root = BTRFS_I(inode)->root;
3318 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003319 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003320 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003321 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003322 bool filled = false;
3323
3324 ret = btrfs_fill_inode(inode, &rdev);
3325 if (!ret)
3326 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003327
3328 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003329 if (!path)
3330 goto make_bad;
3331
Josef Bacikd90c7322011-05-17 09:50:54 -04003332 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003333 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003334
Chris Mason39279cc2007-06-12 06:35:45 -04003335 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003336 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003337 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003338
Chris Mason5f39d392007-10-15 16:14:19 -04003339 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003340
3341 if (filled)
3342 goto cache_acl;
3343
Chris Mason5f39d392007-10-15 16:14:19 -04003344 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3345 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003346 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003347 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003348 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3349 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003350 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003351
3352 tspec = btrfs_inode_atime(inode_item);
3353 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3354 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3355
3356 tspec = btrfs_inode_mtime(inode_item);
3357 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3358 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3359
3360 tspec = btrfs_inode_ctime(inode_item);
3361 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3362 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3363
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003364 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003365 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003366 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3367
3368 /*
3369 * If we were modified in the current generation and evicted from memory
3370 * and then re-read we need to do a full sync since we don't have any
3371 * idea about which extents were modified before we were evicted from
3372 * cache.
3373 */
3374 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3375 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3376 &BTRFS_I(inode)->runtime_flags);
3377
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003378 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003379 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003380 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003381 rdev = btrfs_inode_rdev(leaf, inode_item);
3382
Josef Bacikaec74772008-07-24 12:12:38 -04003383 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003384 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003385cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003386 /*
3387 * try to precache a NULL acl entry for files that don't have
3388 * any xattrs or acls
3389 */
Li Zefan33345d012011-04-20 10:31:50 +08003390 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3391 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003392 if (!maybe_acls)
3393 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003394
Chris Mason39279cc2007-06-12 06:35:45 -04003395 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003396
Chris Mason39279cc2007-06-12 06:35:45 -04003397 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003398 case S_IFREG:
3399 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003400 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003401 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003402 inode->i_fop = &btrfs_file_operations;
3403 inode->i_op = &btrfs_file_inode_operations;
3404 break;
3405 case S_IFDIR:
3406 inode->i_fop = &btrfs_dir_file_operations;
3407 if (root == root->fs_info->tree_root)
3408 inode->i_op = &btrfs_dir_ro_inode_operations;
3409 else
3410 inode->i_op = &btrfs_dir_inode_operations;
3411 break;
3412 case S_IFLNK:
3413 inode->i_op = &btrfs_symlink_inode_operations;
3414 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003415 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003416 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003417 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003418 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003419 init_special_inode(inode, inode->i_mode, rdev);
3420 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003421 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003422
3423 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003424 return;
3425
3426make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003427 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003428 make_bad_inode(inode);
3429}
3430
Chris Masond352ac62008-09-29 15:18:18 -04003431/*
3432 * given a leaf and an inode, copy the inode fields into the leaf
3433 */
Chris Masone02119d2008-09-05 16:13:11 -04003434static void fill_inode_item(struct btrfs_trans_handle *trans,
3435 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003436 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003437 struct inode *inode)
3438{
Liu Bo51fab692012-12-27 09:01:21 +00003439 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003440
Liu Bo51fab692012-12-27 09:01:21 +00003441 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003442
Liu Bo51fab692012-12-27 09:01:21 +00003443 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3444 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3445 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3446 &token);
3447 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3448 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003449
Liu Bo51fab692012-12-27 09:01:21 +00003450 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3451 inode->i_atime.tv_sec, &token);
3452 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3453 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003454
Liu Bo51fab692012-12-27 09:01:21 +00003455 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3456 inode->i_mtime.tv_sec, &token);
3457 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3458 inode->i_mtime.tv_nsec, &token);
3459
3460 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3461 inode->i_ctime.tv_sec, &token);
3462 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3463 inode->i_ctime.tv_nsec, &token);
3464
3465 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3466 &token);
3467 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3468 &token);
3469 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3470 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3471 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3472 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3473 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003474}
3475
Chris Masond352ac62008-09-29 15:18:18 -04003476/*
3477 * copy everything in the in-memory inode into the btree.
3478 */
Chris Mason21151332011-11-10 20:39:08 -05003479static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003480 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003481{
3482 struct btrfs_inode_item *inode_item;
3483 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003484 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003485 int ret;
3486
3487 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003488 if (!path)
3489 return -ENOMEM;
3490
Chris Masonb9473432009-03-13 11:00:37 -04003491 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003492 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3493 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003494 if (ret) {
3495 if (ret > 0)
3496 ret = -ENOENT;
3497 goto failed;
3498 }
3499
Chris Masonb4ce94d2009-02-04 09:25:08 -05003500 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003501 leaf = path->nodes[0];
3502 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003503 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003504
Chris Masone02119d2008-09-05 16:13:11 -04003505 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003506 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003507 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003508 ret = 0;
3509failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003510 btrfs_free_path(path);
3511 return ret;
3512}
3513
Chris Masond352ac62008-09-29 15:18:18 -04003514/*
Chris Mason21151332011-11-10 20:39:08 -05003515 * copy everything in the in-memory inode into the btree.
3516 */
3517noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3518 struct btrfs_root *root, struct inode *inode)
3519{
3520 int ret;
3521
3522 /*
3523 * If the inode is a free space inode, we can deadlock during commit
3524 * if we put it into the delayed code.
3525 *
3526 * The data relocation inode should also be directly updated
3527 * without delay
3528 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003529 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003530 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003531 btrfs_update_root_times(trans, root);
3532
Chris Mason21151332011-11-10 20:39:08 -05003533 ret = btrfs_delayed_update_inode(trans, root, inode);
3534 if (!ret)
3535 btrfs_set_inode_last_trans(trans, inode);
3536 return ret;
3537 }
3538
3539 return btrfs_update_inode_item(trans, root, inode);
3540}
3541
Josef Bacikbe6aef62012-10-22 15:43:12 -04003542noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3543 struct btrfs_root *root,
3544 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003545{
3546 int ret;
3547
3548 ret = btrfs_update_inode(trans, root, inode);
3549 if (ret == -ENOSPC)
3550 return btrfs_update_inode_item(trans, root, inode);
3551 return ret;
3552}
3553
3554/*
Chris Masond352ac62008-09-29 15:18:18 -04003555 * unlink helper that gets used here in inode.c and in the tree logging
3556 * recovery code. It remove a link in a directory with a given name, and
3557 * also drops the back refs in the inode to the directory
3558 */
Al Viro92986792011-03-04 17:14:37 +00003559static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3560 struct btrfs_root *root,
3561 struct inode *dir, struct inode *inode,
3562 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003563{
3564 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003565 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003566 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003567 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003568 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003569 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003570 u64 ino = btrfs_ino(inode);
3571 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003572
3573 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003574 if (!path) {
3575 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003576 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003577 }
3578
Chris Masonb9473432009-03-13 11:00:37 -04003579 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003580 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003581 name, name_len, -1);
3582 if (IS_ERR(di)) {
3583 ret = PTR_ERR(di);
3584 goto err;
3585 }
3586 if (!di) {
3587 ret = -ENOENT;
3588 goto err;
3589 }
Chris Mason5f39d392007-10-15 16:14:19 -04003590 leaf = path->nodes[0];
3591 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003592 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003593 if (ret)
3594 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003595 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003596
Li Zefan33345d012011-04-20 10:31:50 +08003597 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3598 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003599 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003600 btrfs_info(root->fs_info,
3601 "failed to delete reference to %.*s, inode %llu parent %llu",
3602 name_len, name,
3603 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003604 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003605 goto err;
3606 }
3607
Miao Xie16cdcec2011-04-22 18:12:22 +08003608 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003609 if (ret) {
3610 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003611 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003612 }
Chris Mason39279cc2007-06-12 06:35:45 -04003613
Chris Masone02119d2008-09-05 16:13:11 -04003614 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003615 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003616 if (ret != 0 && ret != -ENOENT) {
3617 btrfs_abort_transaction(trans, root, ret);
3618 goto err;
3619 }
Chris Masone02119d2008-09-05 16:13:11 -04003620
3621 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3622 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003623 if (ret == -ENOENT)
3624 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003625 else if (ret)
3626 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003627err:
3628 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003629 if (ret)
3630 goto out;
3631
3632 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003633 inode_inc_iversion(inode);
3634 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003635 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003636 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003637out:
Chris Mason39279cc2007-06-12 06:35:45 -04003638 return ret;
3639}
3640
Al Viro92986792011-03-04 17:14:37 +00003641int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3642 struct btrfs_root *root,
3643 struct inode *dir, struct inode *inode,
3644 const char *name, int name_len)
3645{
3646 int ret;
3647 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3648 if (!ret) {
3649 btrfs_drop_nlink(inode);
3650 ret = btrfs_update_inode(trans, root, inode);
3651 }
3652 return ret;
3653}
3654
3655
Yan, Zhenga22285a2010-05-16 10:48:46 -04003656/* helper to check if there is any shared block in the path */
3657static int check_path_shared(struct btrfs_root *root,
3658 struct btrfs_path *path)
3659{
3660 struct extent_buffer *eb;
3661 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00003662 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003663
3664 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003665 int ret;
3666
Yan, Zhenga22285a2010-05-16 10:48:46 -04003667 if (!path->nodes[level])
3668 break;
3669 eb = path->nodes[level];
3670 if (!btrfs_block_can_be_shared(root, eb))
3671 continue;
Josef Bacik3173a182013-03-07 14:22:04 -05003672 ret = btrfs_lookup_extent_info(NULL, root, eb->start, level, 1,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003673 &refs, NULL);
3674 if (refs > 1)
3675 return 1;
3676 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003677 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003678}
3679
3680/*
3681 * helper to start transaction for unlink and rmdir.
3682 *
3683 * unlink and rmdir are special in btrfs, they do not always free space.
3684 * so in enospc case, we should make sure they will free space before
3685 * allowing them to use the global metadata reservation.
3686 */
3687static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3688 struct dentry *dentry)
3689{
3690 struct btrfs_trans_handle *trans;
3691 struct btrfs_root *root = BTRFS_I(dir)->root;
3692 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003693 struct btrfs_dir_item *di;
3694 struct inode *inode = dentry->d_inode;
3695 u64 index;
3696 int check_link = 1;
3697 int err = -ENOSPC;
3698 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003699 u64 ino = btrfs_ino(inode);
3700 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003701
Josef Bacike70bea52011-10-11 14:18:24 -04003702 /*
3703 * 1 for the possible orphan item
3704 * 1 for the dir item
3705 * 1 for the dir index
3706 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003707 * 1 for the inode
3708 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003709 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003710 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3711 return trans;
3712
Li Zefan33345d012011-04-20 10:31:50 +08003713 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003714 return ERR_PTR(-ENOSPC);
3715
3716 /* check if there is someone else holds reference */
3717 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3718 return ERR_PTR(-ENOSPC);
3719
3720 if (atomic_read(&inode->i_count) > 2)
3721 return ERR_PTR(-ENOSPC);
3722
3723 if (xchg(&root->fs_info->enospc_unlink, 1))
3724 return ERR_PTR(-ENOSPC);
3725
3726 path = btrfs_alloc_path();
3727 if (!path) {
3728 root->fs_info->enospc_unlink = 0;
3729 return ERR_PTR(-ENOMEM);
3730 }
3731
Josef Bacik3880a1b2011-10-14 14:46:51 -04003732 /* 1 for the orphan item */
3733 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003734 if (IS_ERR(trans)) {
3735 btrfs_free_path(path);
3736 root->fs_info->enospc_unlink = 0;
3737 return trans;
3738 }
3739
3740 path->skip_locking = 1;
3741 path->search_commit_root = 1;
3742
3743 ret = btrfs_lookup_inode(trans, root, path,
3744 &BTRFS_I(dir)->location, 0);
3745 if (ret < 0) {
3746 err = ret;
3747 goto out;
3748 }
3749 if (ret == 0) {
3750 if (check_path_shared(root, path))
3751 goto out;
3752 } else {
3753 check_link = 0;
3754 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003755 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003756
3757 ret = btrfs_lookup_inode(trans, root, path,
3758 &BTRFS_I(inode)->location, 0);
3759 if (ret < 0) {
3760 err = ret;
3761 goto out;
3762 }
3763 if (ret == 0) {
3764 if (check_path_shared(root, path))
3765 goto out;
3766 } else {
3767 check_link = 0;
3768 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003769 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003770
3771 if (ret == 0 && S_ISREG(inode->i_mode)) {
3772 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003773 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003774 if (ret < 0) {
3775 err = ret;
3776 goto out;
3777 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003778 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003779 if (check_path_shared(root, path))
3780 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003781 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003782 }
3783
3784 if (!check_link) {
3785 err = 0;
3786 goto out;
3787 }
3788
Li Zefan33345d012011-04-20 10:31:50 +08003789 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003790 dentry->d_name.name, dentry->d_name.len, 0);
3791 if (IS_ERR(di)) {
3792 err = PTR_ERR(di);
3793 goto out;
3794 }
3795 if (di) {
3796 if (check_path_shared(root, path))
3797 goto out;
3798 } else {
3799 err = 0;
3800 goto out;
3801 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003802 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003803
Mark Fashehf1863732012-08-08 11:32:27 -07003804 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3805 dentry->d_name.len, ino, dir_ino, 0,
3806 &index);
3807 if (ret) {
3808 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003809 goto out;
3810 }
Mark Fashehf1863732012-08-08 11:32:27 -07003811
Yan, Zhenga22285a2010-05-16 10:48:46 -04003812 if (check_path_shared(root, path))
3813 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003814
David Sterbab3b4aa72011-04-21 01:20:15 +02003815 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003816
Miao Xie16cdcec2011-04-22 18:12:22 +08003817 /*
3818 * This is a commit root search, if we can lookup inode item and other
3819 * relative items in the commit root, it means the transaction of
3820 * dir/file creation has been committed, and the dir index item that we
3821 * delay to insert has also been inserted into the commit root. So
3822 * we needn't worry about the delayed insertion of the dir index item
3823 * here.
3824 */
Li Zefan33345d012011-04-20 10:31:50 +08003825 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003826 dentry->d_name.name, dentry->d_name.len, 0);
3827 if (IS_ERR(di)) {
3828 err = PTR_ERR(di);
3829 goto out;
3830 }
3831 BUG_ON(ret == -ENOENT);
3832 if (check_path_shared(root, path))
3833 goto out;
3834
3835 err = 0;
3836out:
3837 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003838 /* Migrate the orphan reservation over */
3839 if (!err)
3840 err = btrfs_block_rsv_migrate(trans->block_rsv,
3841 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003842 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003843
Yan, Zhenga22285a2010-05-16 10:48:46 -04003844 if (err) {
3845 btrfs_end_transaction(trans, root);
3846 root->fs_info->enospc_unlink = 0;
3847 return ERR_PTR(err);
3848 }
3849
3850 trans->block_rsv = &root->fs_info->global_block_rsv;
3851 return trans;
3852}
3853
3854static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3855 struct btrfs_root *root)
3856{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003857 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003858 btrfs_block_rsv_release(root, trans->block_rsv,
3859 trans->bytes_reserved);
3860 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003861 BUG_ON(!root->fs_info->enospc_unlink);
3862 root->fs_info->enospc_unlink = 0;
3863 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003864 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003865}
3866
Chris Mason39279cc2007-06-12 06:35:45 -04003867static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3868{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003869 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003870 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003871 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003872 int ret;
3873
Yan, Zhenga22285a2010-05-16 10:48:46 -04003874 trans = __unlink_start_trans(dir, dentry);
3875 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003876 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003877
Chris Mason12fcfd22009-03-24 10:24:20 -04003878 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3879
Chris Masone02119d2008-09-05 16:13:11 -04003880 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3881 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003882 if (ret)
3883 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003884
Yan, Zhenga22285a2010-05-16 10:48:46 -04003885 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003886 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003887 if (ret)
3888 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003889 }
Josef Bacik7b128762008-07-24 12:17:14 -04003890
Tsutomu Itohb5324022011-07-19 07:27:20 +00003891out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003892 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003893 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003894 return ret;
3895}
3896
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003897int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3898 struct btrfs_root *root,
3899 struct inode *dir, u64 objectid,
3900 const char *name, int name_len)
3901{
3902 struct btrfs_path *path;
3903 struct extent_buffer *leaf;
3904 struct btrfs_dir_item *di;
3905 struct btrfs_key key;
3906 u64 index;
3907 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003908 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003909
3910 path = btrfs_alloc_path();
3911 if (!path)
3912 return -ENOMEM;
3913
Li Zefan33345d012011-04-20 10:31:50 +08003914 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003915 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003916 if (IS_ERR_OR_NULL(di)) {
3917 if (!di)
3918 ret = -ENOENT;
3919 else
3920 ret = PTR_ERR(di);
3921 goto out;
3922 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003923
3924 leaf = path->nodes[0];
3925 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3926 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3927 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003928 if (ret) {
3929 btrfs_abort_transaction(trans, root, ret);
3930 goto out;
3931 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003932 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003933
3934 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3935 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003936 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003937 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003938 if (ret != -ENOENT) {
3939 btrfs_abort_transaction(trans, root, ret);
3940 goto out;
3941 }
Li Zefan33345d012011-04-20 10:31:50 +08003942 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003943 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003944 if (IS_ERR_OR_NULL(di)) {
3945 if (!di)
3946 ret = -ENOENT;
3947 else
3948 ret = PTR_ERR(di);
3949 btrfs_abort_transaction(trans, root, ret);
3950 goto out;
3951 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003952
3953 leaf = path->nodes[0];
3954 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003955 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003956 index = key.offset;
3957 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003958 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003959
Miao Xie16cdcec2011-04-22 18:12:22 +08003960 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003961 if (ret) {
3962 btrfs_abort_transaction(trans, root, ret);
3963 goto out;
3964 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003965
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003966 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003967 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003968 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003969 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003970 if (ret)
3971 btrfs_abort_transaction(trans, root, ret);
3972out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003973 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003974 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003975}
3976
Chris Mason39279cc2007-06-12 06:35:45 -04003977static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3978{
3979 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003980 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003981 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003982 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003983
David Sterbab3ae2442012-09-13 16:04:34 -06003984 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003985 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003986 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3987 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003988
Yan, Zhenga22285a2010-05-16 10:48:46 -04003989 trans = __unlink_start_trans(dir, dentry);
3990 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003991 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003992
Li Zefan33345d012011-04-20 10:31:50 +08003993 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003994 err = btrfs_unlink_subvol(trans, root, dir,
3995 BTRFS_I(inode)->location.objectid,
3996 dentry->d_name.name,
3997 dentry->d_name.len);
3998 goto out;
3999 }
4000
Josef Bacik7b128762008-07-24 12:17:14 -04004001 err = btrfs_orphan_add(trans, inode);
4002 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004003 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004004
Chris Mason39279cc2007-06-12 06:35:45 -04004005 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004006 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4007 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004008 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004009 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004010out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04004011 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004012 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004013
Chris Mason39279cc2007-06-12 06:35:45 -04004014 return err;
4015}
4016
Chris Mason323ac952008-10-01 19:05:46 -04004017/*
Chris Mason39279cc2007-06-12 06:35:45 -04004018 * this can truncate away extent items, csum items and directory items.
4019 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004020 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004021 *
4022 * csum items that cross the new i_size are truncated to the new size
4023 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004024 *
4025 * min_type is the minimum key type to truncate down to. If set to 0, this
4026 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004027 */
Yan, Zheng80825102009-11-12 09:35:36 +00004028int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4029 struct btrfs_root *root,
4030 struct inode *inode,
4031 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004032{
Chris Mason39279cc2007-06-12 06:35:45 -04004033 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004034 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004035 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004036 struct btrfs_key key;
4037 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004038 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004039 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004040 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004041 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004042 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004043 int found_extent;
4044 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004045 int pending_del_nr = 0;
4046 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004047 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004048 int ret;
4049 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004050 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004051
4052 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004053
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004054 path = btrfs_alloc_path();
4055 if (!path)
4056 return -ENOMEM;
4057 path->reada = -1;
4058
Josef Bacik5dc562c2012-08-17 13:14:17 -04004059 /*
4060 * We want to drop from the next block forward in case this new size is
4061 * not block aligned since we will be keeping the last block of the
4062 * extent just the way it is.
4063 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004064 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004065 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4066 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004067
Miao Xie16cdcec2011-04-22 18:12:22 +08004068 /*
4069 * This function is also used to drop the items in the log tree before
4070 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4071 * it is used to drop the loged items. So we shouldn't kill the delayed
4072 * items.
4073 */
4074 if (min_type == 0 && root == BTRFS_I(inode)->root)
4075 btrfs_kill_delayed_inode_items(inode);
4076
Li Zefan33345d012011-04-20 10:31:50 +08004077 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004078 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004079 key.type = (u8)-1;
4080
Chris Mason85e21ba2008-01-29 15:11:36 -05004081search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004082 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004083 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004084 if (ret < 0) {
4085 err = ret;
4086 goto out;
4087 }
Chris Masond3977122009-01-05 21:25:51 -05004088
Chris Mason85e21ba2008-01-29 15:11:36 -05004089 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004090 /* there are no items in the tree for us to truncate, we're
4091 * done
4092 */
Yan, Zheng80825102009-11-12 09:35:36 +00004093 if (path->slots[0] == 0)
4094 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004095 path->slots[0]--;
4096 }
4097
Chris Masond3977122009-01-05 21:25:51 -05004098 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004099 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004100 leaf = path->nodes[0];
4101 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4102 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004103
Li Zefan33345d012011-04-20 10:31:50 +08004104 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004105 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004106
Chris Mason85e21ba2008-01-29 15:11:36 -05004107 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004108 break;
4109
Chris Mason5f39d392007-10-15 16:14:19 -04004110 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004111 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004112 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004113 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004114 extent_type = btrfs_file_extent_type(leaf, fi);
4115 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004116 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004117 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004118 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004119 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004120 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004121 }
Yan008630c2007-11-07 13:31:09 -05004122 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004123 }
Yan, Zheng80825102009-11-12 09:35:36 +00004124 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004125 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004126 } else {
4127 if (item_end < new_size)
4128 break;
4129 if (found_key.offset >= new_size)
4130 del_item = 1;
4131 else
4132 del_item = 0;
4133 }
Chris Mason39279cc2007-06-12 06:35:45 -04004134 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004135 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004136 if (found_type != BTRFS_EXTENT_DATA_KEY)
4137 goto delete;
4138
4139 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004140 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004141 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004142 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004143 u64 orig_num_bytes =
4144 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004145 extent_num_bytes = ALIGN(new_size -
4146 found_key.offset,
4147 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004148 btrfs_set_file_extent_num_bytes(leaf, fi,
4149 extent_num_bytes);
4150 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004151 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004152 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004153 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004154 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004155 } else {
Chris Masondb945352007-10-15 16:15:53 -04004156 extent_num_bytes =
4157 btrfs_file_extent_disk_num_bytes(leaf,
4158 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004159 extent_offset = found_key.offset -
4160 btrfs_file_extent_offset(leaf, fi);
4161
Chris Mason39279cc2007-06-12 06:35:45 -04004162 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004163 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004164 if (extent_start != 0) {
4165 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004166 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004167 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004168 }
4169 }
Chris Mason90692182008-02-08 13:49:28 -05004170 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004171 /*
4172 * we can't truncate inline items that have had
4173 * special encodings
4174 */
4175 if (!del_item &&
4176 btrfs_file_extent_compression(leaf, fi) == 0 &&
4177 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4178 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004179 u32 size = new_size - found_key.offset;
4180
4181 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004182 inode_sub_bytes(inode, item_end + 1 -
4183 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004184 }
4185 size =
4186 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004187 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004188 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004189 inode_sub_bytes(inode, item_end + 1 -
4190 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004191 }
Chris Mason39279cc2007-06-12 06:35:45 -04004192 }
Chris Mason179e29e2007-11-01 11:28:41 -04004193delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004194 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004195 if (!pending_del_nr) {
4196 /* no pending yet, add ourselves */
4197 pending_del_slot = path->slots[0];
4198 pending_del_nr = 1;
4199 } else if (pending_del_nr &&
4200 path->slots[0] + 1 == pending_del_slot) {
4201 /* hop on the pending chunk */
4202 pending_del_nr++;
4203 pending_del_slot = path->slots[0];
4204 } else {
Chris Masond3977122009-01-05 21:25:51 -05004205 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004206 }
Chris Mason39279cc2007-06-12 06:35:45 -04004207 } else {
4208 break;
4209 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004210 if (found_extent && (root->ref_cows ||
4211 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004212 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004213 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004214 extent_num_bytes, 0,
4215 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004216 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004217 BUG_ON(ret);
4218 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004219
Yan, Zheng80825102009-11-12 09:35:36 +00004220 if (found_type == BTRFS_INODE_ITEM_KEY)
4221 break;
4222
4223 if (path->slots[0] == 0 ||
4224 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004225 if (pending_del_nr) {
4226 ret = btrfs_del_items(trans, root, path,
4227 pending_del_slot,
4228 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004229 if (ret) {
4230 btrfs_abort_transaction(trans,
4231 root, ret);
4232 goto error;
4233 }
Yan, Zheng80825102009-11-12 09:35:36 +00004234 pending_del_nr = 0;
4235 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004236 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004237 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004238 } else {
4239 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004240 }
Chris Mason39279cc2007-06-12 06:35:45 -04004241 }
Yan, Zheng80825102009-11-12 09:35:36 +00004242out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004243 if (pending_del_nr) {
4244 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4245 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004246 if (ret)
4247 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004248 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004249error:
Chris Mason39279cc2007-06-12 06:35:45 -04004250 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004251 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004252}
4253
Chris Masona52d9a82007-08-27 16:49:44 -04004254/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004255 * btrfs_truncate_page - read, zero a chunk and write a page
4256 * @inode - inode that we're zeroing
4257 * @from - the offset to start zeroing
4258 * @len - the length to zero, 0 to zero the entire range respective to the
4259 * offset
4260 * @front - zero up to the offset instead of from the offset on
4261 *
4262 * This will find the page for the "from" offset and cow the page and zero the
4263 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004264 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004265int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4266 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004267{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004268 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004269 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004270 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4271 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004272 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004273 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004274 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004275 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4276 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4277 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004278 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004279 int ret = 0;
4280 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004281 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004282
Josef Bacik2aaa6652012-08-29 14:27:18 -04004283 if ((offset & (blocksize - 1)) == 0 &&
4284 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004285 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004286 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004287 if (ret)
4288 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004289
Chris Mason211c17f2008-05-15 09:13:45 -04004290again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004291 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004292 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004293 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004294 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004295 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004296 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004297
4298 page_start = page_offset(page);
4299 page_end = page_start + PAGE_CACHE_SIZE - 1;
4300
Chris Masona52d9a82007-08-27 16:49:44 -04004301 if (!PageUptodate(page)) {
4302 ret = btrfs_readpage(NULL, page);
4303 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004304 if (page->mapping != mapping) {
4305 unlock_page(page);
4306 page_cache_release(page);
4307 goto again;
4308 }
Chris Masona52d9a82007-08-27 16:49:44 -04004309 if (!PageUptodate(page)) {
4310 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004311 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004312 }
4313 }
Chris Mason211c17f2008-05-15 09:13:45 -04004314 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004315
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004316 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004317 set_page_extent_mapped(page);
4318
4319 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4320 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004321 unlock_extent_cached(io_tree, page_start, page_end,
4322 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004323 unlock_page(page);
4324 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004325 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004326 btrfs_put_ordered_extent(ordered);
4327 goto again;
4328 }
4329
Josef Bacik2ac55d42010-02-03 19:33:23 +00004330 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004331 EXTENT_DIRTY | EXTENT_DELALLOC |
4332 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004333 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004334
Josef Bacik2ac55d42010-02-03 19:33:23 +00004335 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4336 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004337 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004338 unlock_extent_cached(io_tree, page_start, page_end,
4339 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004340 goto out_unlock;
4341 }
4342
Chris Masone6dcd2d2008-07-17 12:53:50 -04004343 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004344 if (!len)
4345 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004346 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004347 if (front)
4348 memset(kaddr, 0, offset);
4349 else
4350 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004351 flush_dcache_page(page);
4352 kunmap(page);
4353 }
Chris Mason247e7432008-07-17 12:53:51 -04004354 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004355 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004356 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4357 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004358
Chris Mason89642222008-07-24 09:41:53 -04004359out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004360 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004361 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004362 unlock_page(page);
4363 page_cache_release(page);
4364out:
4365 return ret;
4366}
4367
Josef Bacik695a0d02011-03-04 15:46:53 -05004368/*
4369 * This function puts in dummy file extents for the area we're creating a hole
4370 * for. So if we are truncating this file to a larger size we need to insert
4371 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4372 * the range between oldsize and size
4373 */
Josef Bacika41ad392011-01-31 15:30:16 -05004374int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004375{
4376 struct btrfs_trans_handle *trans;
4377 struct btrfs_root *root = BTRFS_I(inode)->root;
4378 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004379 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004380 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004381 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004382 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4383 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004384 u64 last_byte;
4385 u64 cur_offset;
4386 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004387 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004388
4389 if (size <= hole_start)
4390 return 0;
4391
Yan Zheng9036c102008-10-30 14:19:41 -04004392 while (1) {
4393 struct btrfs_ordered_extent *ordered;
4394 btrfs_wait_ordered_range(inode, hole_start,
4395 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004396 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004397 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004398 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4399 if (!ordered)
4400 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004401 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4402 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004403 btrfs_put_ordered_extent(ordered);
4404 }
4405
Yan Zheng9036c102008-10-30 14:19:41 -04004406 cur_offset = hole_start;
4407 while (1) {
4408 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4409 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004410 if (IS_ERR(em)) {
4411 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004412 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004413 break;
4414 }
Yan Zheng9036c102008-10-30 14:19:41 -04004415 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004416 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004417 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004418 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004419 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004420
Miao Xie36423202011-12-14 20:12:02 -05004421 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004422 if (IS_ERR(trans)) {
4423 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004424 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004425 }
Yan, Zheng80825102009-11-12 09:35:36 +00004426
Josef Bacik5dc562c2012-08-17 13:14:17 -04004427 err = btrfs_drop_extents(trans, root, inode,
4428 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004429 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004430 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004431 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004432 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004433 break;
Miao Xie5b397372011-09-11 10:52:24 -04004434 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004435
Yan Zheng9036c102008-10-30 14:19:41 -04004436 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004437 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004438 0, hole_size, 0, hole_size,
4439 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004440 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004441 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004442 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004443 break;
Miao Xie5b397372011-09-11 10:52:24 -04004444 }
Yan, Zheng80825102009-11-12 09:35:36 +00004445
Josef Bacik5dc562c2012-08-17 13:14:17 -04004446 btrfs_drop_extent_cache(inode, cur_offset,
4447 cur_offset + hole_size - 1, 0);
4448 hole_em = alloc_extent_map();
4449 if (!hole_em) {
4450 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4451 &BTRFS_I(inode)->runtime_flags);
4452 goto next;
4453 }
4454 hole_em->start = cur_offset;
4455 hole_em->len = hole_size;
4456 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004457
Josef Bacik5dc562c2012-08-17 13:14:17 -04004458 hole_em->block_start = EXTENT_MAP_HOLE;
4459 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004460 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004461 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004462 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4463 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4464 hole_em->generation = trans->transid;
4465
4466 while (1) {
4467 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004468 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004469 write_unlock(&em_tree->lock);
4470 if (err != -EEXIST)
4471 break;
4472 btrfs_drop_extent_cache(inode, cur_offset,
4473 cur_offset +
4474 hole_size - 1, 0);
4475 }
4476 free_extent_map(hole_em);
4477next:
Miao Xie36423202011-12-14 20:12:02 -05004478 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004479 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004480 }
4481 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004482 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004483 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004484 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004485 break;
4486 }
4487
Yan, Zhenga22285a2010-05-16 10:48:46 -04004488 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004489 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4490 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004491 return err;
4492}
4493
Eric Sandeen3972f262013-01-12 02:57:22 +00004494static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004495{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004496 struct btrfs_root *root = BTRFS_I(inode)->root;
4497 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004498 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004499 loff_t newsize = attr->ia_size;
4500 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004501 int ret;
4502
Josef Bacika41ad392011-01-31 15:30:16 -05004503 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004504 return 0;
4505
Eric Sandeen3972f262013-01-12 02:57:22 +00004506 /*
4507 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4508 * special case where we need to update the times despite not having
4509 * these flags set. For all other operations the VFS set these flags
4510 * explicitly if it wants a timestamp update.
4511 */
4512 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4513 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4514
Josef Bacika41ad392011-01-31 15:30:16 -05004515 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004516 truncate_pagecache(inode, oldsize, newsize);
4517 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004518 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004519 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004520
Miao Xief4a2f4c2011-12-14 20:12:01 -05004521 trans = btrfs_start_transaction(root, 1);
4522 if (IS_ERR(trans))
4523 return PTR_ERR(trans);
4524
4525 i_size_write(inode, newsize);
4526 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4527 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004528 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004529 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004530
Josef Bacika41ad392011-01-31 15:30:16 -05004531 /*
4532 * We're truncating a file that used to have good data down to
4533 * zero. Make sure it gets into the ordered flush list so that
4534 * any new writes get down to disk quickly.
4535 */
4536 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004537 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4538 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004539
Josef Bacikf3fe8202013-01-07 17:03:21 -05004540 /*
4541 * 1 for the orphan item we're going to add
4542 * 1 for the orphan item deletion.
4543 */
4544 trans = btrfs_start_transaction(root, 2);
4545 if (IS_ERR(trans))
4546 return PTR_ERR(trans);
4547
4548 /*
4549 * We need to do this in case we fail at _any_ point during the
4550 * actual truncate. Once we do the truncate_setsize we could
4551 * invalidate pages which forces any outstanding ordered io to
4552 * be instantly completed which will give us extents that need
4553 * to be truncated. If we fail to get an orphan inode down we
4554 * could have left over extents that were never meant to live,
4555 * so we need to garuntee from this point on that everything
4556 * will be consistent.
4557 */
4558 ret = btrfs_orphan_add(trans, inode);
4559 btrfs_end_transaction(trans, root);
4560 if (ret)
4561 return ret;
4562
Josef Bacika41ad392011-01-31 15:30:16 -05004563 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4564 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004565
4566 /* Disable nonlocked read DIO to avoid the end less truncate */
4567 btrfs_inode_block_unlocked_dio(inode);
4568 inode_dio_wait(inode);
4569 btrfs_inode_resume_unlocked_dio(inode);
4570
Josef Bacika41ad392011-01-31 15:30:16 -05004571 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004572 if (ret && inode->i_nlink)
4573 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004574 }
4575
Josef Bacika41ad392011-01-31 15:30:16 -05004576 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004577}
4578
Chris Mason39279cc2007-06-12 06:35:45 -04004579static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4580{
4581 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004582 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004583 int err;
4584
Li Zefanb83cc962010-12-20 16:04:08 +08004585 if (btrfs_root_readonly(root))
4586 return -EROFS;
4587
Chris Mason39279cc2007-06-12 06:35:45 -04004588 err = inode_change_ok(inode, attr);
4589 if (err)
4590 return err;
4591
Chris Mason5a3f23d2009-03-31 13:27:11 -04004592 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004593 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004594 if (err)
4595 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004596 }
Yan Zheng9036c102008-10-30 14:19:41 -04004597
Christoph Hellwig10257742010-06-04 11:30:02 +02004598 if (attr->ia_valid) {
4599 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004600 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004601 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004602
Josef Bacik22c44fe2011-11-30 10:45:38 -05004603 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004604 err = btrfs_acl_chmod(inode);
4605 }
4606
Chris Mason39279cc2007-06-12 06:35:45 -04004607 return err;
4608}
Chris Mason61295eb2008-01-14 16:24:38 -05004609
Al Virobd555972010-06-07 11:35:40 -04004610void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004611{
4612 struct btrfs_trans_handle *trans;
4613 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004614 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004615 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004616 int ret;
4617
liubo1abe9b82011-03-24 11:18:59 +00004618 trace_btrfs_inode_evict(inode);
4619
Chris Mason39279cc2007-06-12 06:35:45 -04004620 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004621 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004622 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004623 goto no_delete;
4624
Chris Mason39279cc2007-06-12 06:35:45 -04004625 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004626 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004627 goto no_delete;
4628 }
Al Virobd555972010-06-07 11:35:40 -04004629 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004630 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004631
Yan, Zhengc71bf092009-11-12 09:34:40 +00004632 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004633 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004634 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004635 goto no_delete;
4636 }
4637
Yan, Zheng76dda932009-09-21 16:00:26 -04004638 if (inode->i_nlink > 0) {
4639 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4640 goto no_delete;
4641 }
4642
Miao Xie0e8c36a2012-12-19 06:59:51 +00004643 ret = btrfs_commit_inode_delayed_inode(inode);
4644 if (ret) {
4645 btrfs_orphan_del(NULL, inode);
4646 goto no_delete;
4647 }
4648
Miao Xie66d8f3d2012-09-06 04:02:28 -06004649 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004650 if (!rsv) {
4651 btrfs_orphan_del(NULL, inode);
4652 goto no_delete;
4653 }
Josef Bacik4a338542011-08-29 11:01:31 -04004654 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004655 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004656 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004657
Chris Masondbe674a2008-07-17 12:54:05 -04004658 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004659
Josef Bacik4289a662011-08-05 13:22:24 -04004660 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004661 * This is a bit simpler than btrfs_truncate since we've already
4662 * reserved our space for our orphan item in the unlink, so we just
4663 * need to reserve some slack space in case we add bytes and update
4664 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004665 */
Yan, Zheng80825102009-11-12 09:35:36 +00004666 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004667 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4668 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004669
Josef Bacik726c35f2011-09-26 15:46:06 -04004670 /*
4671 * Try and steal from the global reserve since we will
4672 * likely not use this space anyway, we want to try as
4673 * hard as possible to get this to work.
4674 */
4675 if (ret)
4676 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4677
Yan, Zhengd68fc572010-05-16 10:49:58 -04004678 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004679 btrfs_warn(root->fs_info,
4680 "Could not get space for a delete, will truncate on mount %d",
4681 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004682 btrfs_orphan_del(NULL, inode);
4683 btrfs_free_block_rsv(root, rsv);
4684 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004685 }
4686
Miao Xie0e8c36a2012-12-19 06:59:51 +00004687 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004688 if (IS_ERR(trans)) {
4689 btrfs_orphan_del(NULL, inode);
4690 btrfs_free_block_rsv(root, rsv);
4691 goto no_delete;
4692 }
4693
4694 trans->block_rsv = rsv;
4695
Yan, Zhengd68fc572010-05-16 10:49:58 -04004696 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004697 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004698 break;
4699
Miao Xie8407aa42012-09-07 01:43:32 -06004700 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004701 btrfs_end_transaction(trans, root);
4702 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004703 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004704 }
4705
Josef Bacik4289a662011-08-05 13:22:24 -04004706 btrfs_free_block_rsv(root, rsv);
4707
Yan, Zheng80825102009-11-12 09:35:36 +00004708 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004709 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004710 ret = btrfs_orphan_del(trans, inode);
4711 BUG_ON(ret);
4712 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004713
Josef Bacik4289a662011-08-05 13:22:24 -04004714 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004715 if (!(root == root->fs_info->tree_root ||
4716 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004717 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004718
Chris Mason54aa1f42007-06-22 14:16:25 -04004719 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004720 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004721no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004722 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004723 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004724}
4725
4726/*
4727 * this returns the key found in the dir entry in the location pointer.
4728 * If no dir entries were found, location->objectid is 0.
4729 */
4730static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4731 struct btrfs_key *location)
4732{
4733 const char *name = dentry->d_name.name;
4734 int namelen = dentry->d_name.len;
4735 struct btrfs_dir_item *di;
4736 struct btrfs_path *path;
4737 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004738 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004739
4740 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004741 if (!path)
4742 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004743
Li Zefan33345d012011-04-20 10:31:50 +08004744 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004745 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004746 if (IS_ERR(di))
4747 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004748
David Sterbac7040052011-04-19 18:00:01 +02004749 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004750 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004751
Chris Mason5f39d392007-10-15 16:14:19 -04004752 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004753out:
Chris Mason39279cc2007-06-12 06:35:45 -04004754 btrfs_free_path(path);
4755 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004756out_err:
4757 location->objectid = 0;
4758 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004759}
4760
4761/*
4762 * when we hit a tree root in a directory, the btrfs part of the inode
4763 * needs to be changed to reflect the root directory of the tree root. This
4764 * is kind of like crossing a mount point.
4765 */
4766static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004767 struct inode *dir,
4768 struct dentry *dentry,
4769 struct btrfs_key *location,
4770 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004771{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004772 struct btrfs_path *path;
4773 struct btrfs_root *new_root;
4774 struct btrfs_root_ref *ref;
4775 struct extent_buffer *leaf;
4776 int ret;
4777 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004778
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004779 path = btrfs_alloc_path();
4780 if (!path) {
4781 err = -ENOMEM;
4782 goto out;
4783 }
Chris Mason39279cc2007-06-12 06:35:45 -04004784
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004785 err = -ENOENT;
4786 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4787 BTRFS_I(dir)->root->root_key.objectid,
4788 location->objectid);
4789 if (ret) {
4790 if (ret < 0)
4791 err = ret;
4792 goto out;
4793 }
Chris Mason39279cc2007-06-12 06:35:45 -04004794
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004795 leaf = path->nodes[0];
4796 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004797 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004798 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4799 goto out;
4800
4801 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4802 (unsigned long)(ref + 1),
4803 dentry->d_name.len);
4804 if (ret)
4805 goto out;
4806
David Sterbab3b4aa72011-04-21 01:20:15 +02004807 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004808
4809 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4810 if (IS_ERR(new_root)) {
4811 err = PTR_ERR(new_root);
4812 goto out;
4813 }
4814
4815 if (btrfs_root_refs(&new_root->root_item) == 0) {
4816 err = -ENOENT;
4817 goto out;
4818 }
4819
4820 *sub_root = new_root;
4821 location->objectid = btrfs_root_dirid(&new_root->root_item);
4822 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004823 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004824 err = 0;
4825out:
4826 btrfs_free_path(path);
4827 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004828}
4829
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004830static void inode_tree_add(struct inode *inode)
4831{
4832 struct btrfs_root *root = BTRFS_I(inode)->root;
4833 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004834 struct rb_node **p;
4835 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004836 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004837again:
4838 p = &root->inode_tree.rb_node;
4839 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004840
Al Viro1d3382cb2010-10-23 15:19:20 -04004841 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004842 return;
4843
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004844 spin_lock(&root->inode_lock);
4845 while (*p) {
4846 parent = *p;
4847 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4848
Li Zefan33345d012011-04-20 10:31:50 +08004849 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004850 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004851 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004852 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004853 else {
4854 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004855 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004856 rb_erase(parent, &root->inode_tree);
4857 RB_CLEAR_NODE(parent);
4858 spin_unlock(&root->inode_lock);
4859 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004860 }
4861 }
4862 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4863 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4864 spin_unlock(&root->inode_lock);
4865}
4866
4867static void inode_tree_del(struct inode *inode)
4868{
4869 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004870 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004871
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004872 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004873 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004874 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004875 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004876 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004877 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004878 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004879
Josef Bacik0af3d002010-06-21 14:48:16 -04004880 /*
4881 * Free space cache has inodes in the tree root, but the tree root has a
4882 * root_refs of 0, so this could end up dropping the tree root as a
4883 * snapshot, so we need the extra !root->fs_info->tree_root check to
4884 * make sure we don't drop it.
4885 */
4886 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4887 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004888 synchronize_srcu(&root->fs_info->subvol_srcu);
4889 spin_lock(&root->inode_lock);
4890 empty = RB_EMPTY_ROOT(&root->inode_tree);
4891 spin_unlock(&root->inode_lock);
4892 if (empty)
4893 btrfs_add_dead_root(root);
4894 }
4895}
4896
Jeff Mahoney143bede2012-03-01 14:56:26 +01004897void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004898{
4899 struct rb_node *node;
4900 struct rb_node *prev;
4901 struct btrfs_inode *entry;
4902 struct inode *inode;
4903 u64 objectid = 0;
4904
4905 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4906
4907 spin_lock(&root->inode_lock);
4908again:
4909 node = root->inode_tree.rb_node;
4910 prev = NULL;
4911 while (node) {
4912 prev = node;
4913 entry = rb_entry(node, struct btrfs_inode, rb_node);
4914
Li Zefan33345d012011-04-20 10:31:50 +08004915 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004916 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004917 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004918 node = node->rb_right;
4919 else
4920 break;
4921 }
4922 if (!node) {
4923 while (prev) {
4924 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004925 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004926 node = prev;
4927 break;
4928 }
4929 prev = rb_next(prev);
4930 }
4931 }
4932 while (node) {
4933 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004934 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004935 inode = igrab(&entry->vfs_inode);
4936 if (inode) {
4937 spin_unlock(&root->inode_lock);
4938 if (atomic_read(&inode->i_count) > 1)
4939 d_prune_aliases(inode);
4940 /*
Al Viro45321ac2010-06-07 13:43:19 -04004941 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004942 * the inode cache when its usage count
4943 * hits zero.
4944 */
4945 iput(inode);
4946 cond_resched();
4947 spin_lock(&root->inode_lock);
4948 goto again;
4949 }
4950
4951 if (cond_resched_lock(&root->inode_lock))
4952 goto again;
4953
4954 node = rb_next(node);
4955 }
4956 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004957}
4958
Chris Masone02119d2008-09-05 16:13:11 -04004959static int btrfs_init_locked_inode(struct inode *inode, void *p)
4960{
4961 struct btrfs_iget_args *args = p;
4962 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004963 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004964 return 0;
4965}
4966
4967static int btrfs_find_actor(struct inode *inode, void *opaque)
4968{
4969 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004970 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004971 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004972}
4973
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004974static struct inode *btrfs_iget_locked(struct super_block *s,
4975 u64 objectid,
4976 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004977{
4978 struct inode *inode;
4979 struct btrfs_iget_args args;
4980 args.ino = objectid;
4981 args.root = root;
4982
4983 inode = iget5_locked(s, objectid, btrfs_find_actor,
4984 btrfs_init_locked_inode,
4985 (void *)&args);
4986 return inode;
4987}
4988
Balaji Rao1a54ef82008-07-21 02:01:04 +05304989/* Get an inode object given its location and corresponding root.
4990 * Returns in *is_new if the inode was read from disk
4991 */
4992struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004993 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304994{
4995 struct inode *inode;
4996
4997 inode = btrfs_iget_locked(s, location->objectid, root);
4998 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004999 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305000
5001 if (inode->i_state & I_NEW) {
5002 BTRFS_I(inode)->root = root;
5003 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
5004 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005005 if (!is_bad_inode(inode)) {
5006 inode_tree_add(inode);
5007 unlock_new_inode(inode);
5008 if (new)
5009 *new = 1;
5010 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005011 unlock_new_inode(inode);
5012 iput(inode);
5013 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005014 }
5015 }
5016
Balaji Rao1a54ef82008-07-21 02:01:04 +05305017 return inode;
5018}
5019
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005020static struct inode *new_simple_dir(struct super_block *s,
5021 struct btrfs_key *key,
5022 struct btrfs_root *root)
5023{
5024 struct inode *inode = new_inode(s);
5025
5026 if (!inode)
5027 return ERR_PTR(-ENOMEM);
5028
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005029 BTRFS_I(inode)->root = root;
5030 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005031 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005032
5033 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005034 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005035 inode->i_fop = &simple_dir_operations;
5036 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5037 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5038
5039 return inode;
5040}
5041
Chris Mason3de45862008-11-17 21:02:50 -05005042struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005043{
Chris Masond3977122009-01-05 21:25:51 -05005044 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005045 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005046 struct btrfs_root *sub_root = root;
5047 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005048 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005049 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005050
5051 if (dentry->d_name.len > BTRFS_NAME_LEN)
5052 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005053
Jeff Layton39e3c952012-11-28 11:30:53 -05005054 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005055 if (ret < 0)
5056 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005057
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005058 if (location.objectid == 0)
5059 return NULL;
5060
5061 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005062 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005063 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005064 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005065
5066 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5067
Yan, Zheng76dda932009-09-21 16:00:26 -04005068 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005069 ret = fixup_tree_root_location(root, dir, dentry,
5070 &location, &sub_root);
5071 if (ret < 0) {
5072 if (ret != -ENOENT)
5073 inode = ERR_PTR(ret);
5074 else
5075 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5076 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005077 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005078 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005079 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5080
Julia Lawall34d19ba2011-01-24 19:55:19 +00005081 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005082 down_read(&root->fs_info->cleanup_work_sem);
5083 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005084 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005085 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005086 if (ret)
5087 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005088 }
5089
Chris Mason3de45862008-11-17 21:02:50 -05005090 return inode;
5091}
5092
Nick Pigginfe15ce42011-01-07 17:49:23 +11005093static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005094{
5095 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005096 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005097
Li Zefan848cce02012-02-21 17:04:28 +08005098 if (!inode && !IS_ROOT(dentry))
5099 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005100
Li Zefan848cce02012-02-21 17:04:28 +08005101 if (inode) {
5102 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005103 if (btrfs_root_refs(&root->root_item) == 0)
5104 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005105
5106 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5107 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005108 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005109 return 0;
5110}
5111
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005112static void btrfs_dentry_release(struct dentry *dentry)
5113{
5114 if (dentry->d_fsdata)
5115 kfree(dentry->d_fsdata);
5116}
5117
Chris Mason3de45862008-11-17 21:02:50 -05005118static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005119 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005120{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005121 struct dentry *ret;
5122
5123 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005124 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005125}
5126
Miao Xie16cdcec2011-04-22 18:12:22 +08005127unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005128 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5129};
5130
David Woodhousecbdf5a22008-08-06 19:42:33 +01005131static int btrfs_real_readdir(struct file *filp, void *dirent,
5132 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005133{
Al Viro496ad9a2013-01-23 17:07:38 -05005134 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005135 struct btrfs_root *root = BTRFS_I(inode)->root;
5136 struct btrfs_item *item;
5137 struct btrfs_dir_item *di;
5138 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005139 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005140 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005141 struct list_head ins_list;
5142 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005143 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005144 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005145 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005146 unsigned char d_type;
5147 int over = 0;
5148 u32 di_cur;
5149 u32 di_total;
5150 u32 di_len;
5151 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005152 char tmp_name[32];
5153 char *name_ptr;
5154 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005155 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005156
5157 /* FIXME, use a real flag for deciding about the key type */
5158 if (root->fs_info->tree_root == root)
5159 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005160
Chris Mason39544012007-12-12 14:38:19 -05005161 /* special case for "." */
5162 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005163 over = filldir(dirent, ".", 1,
5164 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005165 if (over)
5166 return 0;
5167 filp->f_pos = 1;
5168 }
Chris Mason39544012007-12-12 14:38:19 -05005169 /* special case for .., just use the back ref */
5170 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005171 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005172 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005173 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005174 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005175 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005176 filp->f_pos = 2;
5177 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005178 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005179 if (!path)
5180 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005181
Josef Bacik026fd312011-05-13 10:32:11 -04005182 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005183
Miao Xie16cdcec2011-04-22 18:12:22 +08005184 if (key_type == BTRFS_DIR_INDEX_KEY) {
5185 INIT_LIST_HEAD(&ins_list);
5186 INIT_LIST_HEAD(&del_list);
5187 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5188 }
5189
Chris Mason39279cc2007-06-12 06:35:45 -04005190 btrfs_set_key_type(&key, key_type);
5191 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005192 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005193
Chris Mason39279cc2007-06-12 06:35:45 -04005194 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5195 if (ret < 0)
5196 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005197
5198 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005199 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005200 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005201 if (slot >= btrfs_header_nritems(leaf)) {
5202 ret = btrfs_next_leaf(root, path);
5203 if (ret < 0)
5204 goto err;
5205 else if (ret > 0)
5206 break;
5207 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005208 }
Chris Mason3de45862008-11-17 21:02:50 -05005209
Chris Mason5f39d392007-10-15 16:14:19 -04005210 item = btrfs_item_nr(leaf, slot);
5211 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5212
5213 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005214 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005215 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005216 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005217 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005218 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005219 if (key_type == BTRFS_DIR_INDEX_KEY &&
5220 btrfs_should_delete_dir_index(&del_list,
5221 found_key.offset))
5222 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005223
5224 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005225 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005226
Chris Mason39279cc2007-06-12 06:35:45 -04005227 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5228 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005229 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005230
5231 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005232 struct btrfs_key location;
5233
Josef Bacik22a94d42011-03-16 16:47:17 -04005234 if (verify_dir_item(root, leaf, di))
5235 break;
5236
Chris Mason5f39d392007-10-15 16:14:19 -04005237 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005238 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005239 name_ptr = tmp_name;
5240 } else {
5241 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005242 if (!name_ptr) {
5243 ret = -ENOMEM;
5244 goto err;
5245 }
Chris Mason5f39d392007-10-15 16:14:19 -04005246 }
5247 read_extent_buffer(leaf, name_ptr,
5248 (unsigned long)(di + 1), name_len);
5249
5250 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5251 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005252
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005253
Chris Mason3de45862008-11-17 21:02:50 -05005254 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005255 * skip it.
5256 *
5257 * In contrast to old kernels, we insert the snapshot's
5258 * dir item and dir index after it has been created, so
5259 * we won't find a reference to our own snapshot. We
5260 * still keep the following code for backward
5261 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005262 */
5263 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5264 location.objectid == root->root_key.objectid) {
5265 over = 0;
5266 goto skip;
5267 }
Chris Mason5f39d392007-10-15 16:14:19 -04005268 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005269 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005270 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005271
Chris Mason3de45862008-11-17 21:02:50 -05005272skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005273 if (name_ptr != tmp_name)
5274 kfree(name_ptr);
5275
Chris Mason39279cc2007-06-12 06:35:45 -04005276 if (over)
5277 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005278 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005279 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005280 di_cur += di_len;
5281 di = (struct btrfs_dir_item *)((char *)di + di_len);
5282 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005283next:
5284 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005285 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005286
Miao Xie16cdcec2011-04-22 18:12:22 +08005287 if (key_type == BTRFS_DIR_INDEX_KEY) {
5288 if (is_curr)
5289 filp->f_pos++;
5290 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5291 &ins_list);
5292 if (ret)
5293 goto nopos;
5294 }
5295
David Woodhouse49593bf2008-08-17 17:08:36 +01005296 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005297 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005298 /*
5299 * 32-bit glibc will use getdents64, but then strtol -
5300 * so the last number we can serve is this.
5301 */
5302 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005303 else
5304 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005305nopos:
5306 ret = 0;
5307err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005308 if (key_type == BTRFS_DIR_INDEX_KEY)
5309 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005310 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005311 return ret;
5312}
5313
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005314int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005315{
5316 struct btrfs_root *root = BTRFS_I(inode)->root;
5317 struct btrfs_trans_handle *trans;
5318 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005319 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005320
Josef Bacik72ac3c02012-05-23 14:13:11 -04005321 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005322 return 0;
5323
Liu Bo83eea1f2012-07-10 05:28:39 -06005324 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005325 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005326
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005327 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005328 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005329 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005330 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005331 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005332 if (IS_ERR(trans))
5333 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005334 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005335 }
5336 return ret;
5337}
5338
5339/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005340 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005341 * inode changes. But, it is most likely to find the inode in cache.
5342 * FIXME, needs more benchmarking...there are no reasons other than performance
5343 * to keep or drop this code.
5344 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05005345int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005346{
5347 struct btrfs_root *root = BTRFS_I(inode)->root;
5348 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005349 int ret;
5350
Josef Bacik72ac3c02012-05-23 14:13:11 -04005351 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005352 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005353
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005354 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005355 if (IS_ERR(trans))
5356 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005357
5358 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005359 if (ret && ret == -ENOSPC) {
5360 /* whoops, lets try again with the full transaction */
5361 btrfs_end_transaction(trans, root);
5362 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005363 if (IS_ERR(trans))
5364 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005365
Chris Mason94b60442010-05-26 11:02:00 -04005366 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005367 }
Chris Mason39279cc2007-06-12 06:35:45 -04005368 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005369 if (BTRFS_I(inode)->delayed_node)
5370 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005371
5372 return ret;
5373}
5374
5375/*
5376 * This is a copy of file_update_time. We need this so we can return error on
5377 * ENOSPC for updating the inode in the case of file write and mmap writes.
5378 */
Josef Bacike41f9412012-03-26 09:46:47 -04005379static int btrfs_update_time(struct inode *inode, struct timespec *now,
5380 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005381{
Alexander Block2bc5565282012-06-15 09:49:33 +02005382 struct btrfs_root *root = BTRFS_I(inode)->root;
5383
5384 if (btrfs_root_readonly(root))
5385 return -EROFS;
5386
Josef Bacike41f9412012-03-26 09:46:47 -04005387 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005388 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005389 if (flags & S_CTIME)
5390 inode->i_ctime = *now;
5391 if (flags & S_MTIME)
5392 inode->i_mtime = *now;
5393 if (flags & S_ATIME)
5394 inode->i_atime = *now;
5395 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005396}
5397
Chris Masond352ac62008-09-29 15:18:18 -04005398/*
5399 * find the highest existing sequence number in a directory
5400 * and then set the in-memory index_cnt variable to reflect
5401 * free sequence numbers
5402 */
Josef Bacikaec74772008-07-24 12:12:38 -04005403static int btrfs_set_inode_index_count(struct inode *inode)
5404{
5405 struct btrfs_root *root = BTRFS_I(inode)->root;
5406 struct btrfs_key key, found_key;
5407 struct btrfs_path *path;
5408 struct extent_buffer *leaf;
5409 int ret;
5410
Li Zefan33345d012011-04-20 10:31:50 +08005411 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005412 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5413 key.offset = (u64)-1;
5414
5415 path = btrfs_alloc_path();
5416 if (!path)
5417 return -ENOMEM;
5418
5419 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5420 if (ret < 0)
5421 goto out;
5422 /* FIXME: we should be able to handle this */
5423 if (ret == 0)
5424 goto out;
5425 ret = 0;
5426
5427 /*
5428 * MAGIC NUMBER EXPLANATION:
5429 * since we search a directory based on f_pos we have to start at 2
5430 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5431 * else has to start at 2
5432 */
5433 if (path->slots[0] == 0) {
5434 BTRFS_I(inode)->index_cnt = 2;
5435 goto out;
5436 }
5437
5438 path->slots[0]--;
5439
5440 leaf = path->nodes[0];
5441 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5442
Li Zefan33345d012011-04-20 10:31:50 +08005443 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005444 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5445 BTRFS_I(inode)->index_cnt = 2;
5446 goto out;
5447 }
5448
5449 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5450out:
5451 btrfs_free_path(path);
5452 return ret;
5453}
5454
Chris Masond352ac62008-09-29 15:18:18 -04005455/*
5456 * helper to find a free sequence number in a given directory. This current
5457 * code is very simple, later versions will do smarter things in the btree
5458 */
Chris Mason3de45862008-11-17 21:02:50 -05005459int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005460{
5461 int ret = 0;
5462
5463 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005464 ret = btrfs_inode_delayed_dir_index_count(dir);
5465 if (ret) {
5466 ret = btrfs_set_inode_index_count(dir);
5467 if (ret)
5468 return ret;
5469 }
Josef Bacikaec74772008-07-24 12:12:38 -04005470 }
5471
Chris Mason00e4e6b2008-08-05 11:18:09 -04005472 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005473 BTRFS_I(dir)->index_cnt++;
5474
5475 return ret;
5476}
5477
Chris Mason39279cc2007-06-12 06:35:45 -04005478static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5479 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005480 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005481 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005482 u64 ref_objectid, u64 objectid,
5483 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005484{
5485 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005486 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005487 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005488 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005489 struct btrfs_inode_ref *ref;
5490 struct btrfs_key key[2];
5491 u32 sizes[2];
5492 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005493 int ret;
5494 int owner;
5495
Chris Mason5f39d392007-10-15 16:14:19 -04005496 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005497 if (!path)
5498 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005499
Chris Mason39279cc2007-06-12 06:35:45 -04005500 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005501 if (!inode) {
5502 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005503 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005504 }
Chris Mason39279cc2007-06-12 06:35:45 -04005505
Li Zefan581bb052011-04-20 10:06:11 +08005506 /*
5507 * we have to initialize this early, so we can reclaim the inode
5508 * number if we fail afterwards in this function.
5509 */
5510 inode->i_ino = objectid;
5511
Josef Bacikaec74772008-07-24 12:12:38 -04005512 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005513 trace_btrfs_inode_request(dir);
5514
Chris Mason3de45862008-11-17 21:02:50 -05005515 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005516 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005517 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005518 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005519 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005520 }
Josef Bacikaec74772008-07-24 12:12:38 -04005521 }
5522 /*
5523 * index_cnt is ignored for everything but a dir,
5524 * btrfs_get_inode_index_count has an explanation for the magic
5525 * number
5526 */
5527 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005528 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005529 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005530 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005531
Josef Bacik5dc562c2012-08-17 13:14:17 -04005532 /*
5533 * We could have gotten an inode number from somebody who was fsynced
5534 * and then removed in this same transaction, so let's just set full
5535 * sync since it will be a full sync anyway and this will blow away the
5536 * old info in the log.
5537 */
5538 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5539
Al Viro569254b2011-07-24 17:08:40 -04005540 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005541 owner = 0;
5542 else
5543 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005544
5545 key[0].objectid = objectid;
5546 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5547 key[0].offset = 0;
5548
Mark Fashehf1863732012-08-08 11:32:27 -07005549 /*
5550 * Start new inodes with an inode_ref. This is slightly more
5551 * efficient for small numbers of hard links since they will
5552 * be packed into one item. Extended refs will kick in if we
5553 * add more hard links than can fit in the ref item.
5554 */
Chris Mason9c583092008-01-29 15:15:18 -05005555 key[1].objectid = objectid;
5556 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5557 key[1].offset = ref_objectid;
5558
5559 sizes[0] = sizeof(struct btrfs_inode_item);
5560 sizes[1] = name_len + sizeof(*ref);
5561
Chris Masonb9473432009-03-13 11:00:37 -04005562 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005563 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5564 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005565 goto fail;
5566
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005567 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005568 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005569 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005570 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5571 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005572 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5573 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005574 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005575
5576 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5577 struct btrfs_inode_ref);
5578 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005579 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005580 ptr = (unsigned long)(ref + 1);
5581 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5582
Chris Mason5f39d392007-10-15 16:14:19 -04005583 btrfs_mark_buffer_dirty(path->nodes[0]);
5584 btrfs_free_path(path);
5585
Chris Mason39279cc2007-06-12 06:35:45 -04005586 location = &BTRFS_I(inode)->location;
5587 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005588 location->offset = 0;
5589 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5590
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005591 btrfs_inherit_iflags(inode, dir);
5592
Al Viro569254b2011-07-24 17:08:40 -04005593 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005594 if (btrfs_test_opt(root, NODATASUM))
5595 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005596 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005597 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5598 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005599 }
5600
Chris Mason39279cc2007-06-12 06:35:45 -04005601 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005602 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005603
5604 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005605 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005606
Alexander Block8ea05e32012-07-25 17:35:53 +02005607 btrfs_update_root_times(trans, root);
5608
Chris Mason39279cc2007-06-12 06:35:45 -04005609 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005610fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005611 if (dir)
5612 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005613 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005614 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005615 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005616}
5617
5618static inline u8 btrfs_inode_type(struct inode *inode)
5619{
5620 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5621}
5622
Chris Masond352ac62008-09-29 15:18:18 -04005623/*
5624 * utility function to add 'inode' into 'parent_inode' with
5625 * a give name and a given sequence number.
5626 * if 'add_backref' is true, also insert a backref from the
5627 * inode to the parent directory.
5628 */
Chris Masone02119d2008-09-05 16:13:11 -04005629int btrfs_add_link(struct btrfs_trans_handle *trans,
5630 struct inode *parent_inode, struct inode *inode,
5631 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005632{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005633 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005634 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005635 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005636 u64 ino = btrfs_ino(inode);
5637 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005638
Li Zefan33345d012011-04-20 10:31:50 +08005639 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005640 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5641 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005642 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005643 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5644 key.offset = 0;
5645 }
Chris Mason39279cc2007-06-12 06:35:45 -04005646
Li Zefan33345d012011-04-20 10:31:50 +08005647 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005648 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5649 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005650 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005651 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005652 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5653 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005654 }
5655
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005656 /* Nothing to clean up yet */
5657 if (ret)
5658 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005659
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005660 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5661 parent_inode, &key,
5662 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005663 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005664 goto fail_dir_item;
5665 else if (ret) {
5666 btrfs_abort_transaction(trans, root, ret);
5667 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005668 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005669
5670 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5671 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005672 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005673 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5674 ret = btrfs_update_inode(trans, root, parent_inode);
5675 if (ret)
5676 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005677 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005678
5679fail_dir_item:
5680 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5681 u64 local_index;
5682 int err;
5683 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5684 key.objectid, root->root_key.objectid,
5685 parent_ino, &local_index, name, name_len);
5686
5687 } else if (add_backref) {
5688 u64 local_index;
5689 int err;
5690
5691 err = btrfs_del_inode_ref(trans, root, name, name_len,
5692 ino, parent_ino, &local_index);
5693 }
5694 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005695}
5696
5697static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005698 struct inode *dir, struct dentry *dentry,
5699 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005700{
Josef Bacika1b075d2010-11-19 20:36:11 +00005701 int err = btrfs_add_link(trans, dir, inode,
5702 dentry->d_name.name, dentry->d_name.len,
5703 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005704 if (err > 0)
5705 err = -EEXIST;
5706 return err;
5707}
5708
Josef Bacik618e21d2007-07-11 10:18:17 -04005709static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005710 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005711{
5712 struct btrfs_trans_handle *trans;
5713 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005714 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005715 int err;
5716 int drop_inode = 0;
5717 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005718 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005719
5720 if (!new_valid_dev(rdev))
5721 return -EINVAL;
5722
Josef Bacik9ed74f22009-09-11 16:12:44 -04005723 /*
5724 * 2 for inode item and ref
5725 * 2 for dir items
5726 * 1 for xattr if selinux is on
5727 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005728 trans = btrfs_start_transaction(root, 5);
5729 if (IS_ERR(trans))
5730 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005731
Li Zefan581bb052011-04-20 10:06:11 +08005732 err = btrfs_find_free_ino(root, &objectid);
5733 if (err)
5734 goto out_unlock;
5735
Josef Bacikaec74772008-07-24 12:12:38 -04005736 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005737 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005738 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005739 if (IS_ERR(inode)) {
5740 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005741 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005742 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005743
Eric Paris2a7dba32011-02-01 11:05:39 -05005744 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005745 if (err) {
5746 drop_inode = 1;
5747 goto out_unlock;
5748 }
5749
Casey Schauflerad19db72011-12-15 10:09:07 -05005750 /*
5751 * If the active LSM wants to access the inode during
5752 * d_instantiate it needs these. Smack checks to see
5753 * if the filesystem supports xattrs by looking at the
5754 * ops vector.
5755 */
5756
5757 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005758 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005759 if (err)
5760 drop_inode = 1;
5761 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005762 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005763 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005764 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005765 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005766out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005767 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005768 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005769 if (drop_inode) {
5770 inode_dec_link_count(inode);
5771 iput(inode);
5772 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005773 return err;
5774}
5775
Chris Mason39279cc2007-06-12 06:35:45 -04005776static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005777 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005778{
5779 struct btrfs_trans_handle *trans;
5780 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005781 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005782 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005783 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005784 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005785 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005786
Josef Bacik9ed74f22009-09-11 16:12:44 -04005787 /*
5788 * 2 for inode item and ref
5789 * 2 for dir items
5790 * 1 for xattr if selinux is on
5791 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005792 trans = btrfs_start_transaction(root, 5);
5793 if (IS_ERR(trans))
5794 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005795
Li Zefan581bb052011-04-20 10:06:11 +08005796 err = btrfs_find_free_ino(root, &objectid);
5797 if (err)
5798 goto out_unlock;
5799
Josef Bacikaec74772008-07-24 12:12:38 -04005800 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005801 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005802 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005803 if (IS_ERR(inode)) {
5804 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005805 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005806 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005807 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005808
Eric Paris2a7dba32011-02-01 11:05:39 -05005809 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005810 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005811 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005812
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005813 err = btrfs_update_inode(trans, root, inode);
5814 if (err)
5815 goto out_unlock;
5816
Casey Schauflerad19db72011-12-15 10:09:07 -05005817 /*
5818 * If the active LSM wants to access the inode during
5819 * d_instantiate it needs these. Smack checks to see
5820 * if the filesystem supports xattrs by looking at the
5821 * ops vector.
5822 */
5823 inode->i_fop = &btrfs_file_operations;
5824 inode->i_op = &btrfs_file_inode_operations;
5825
Josef Bacika1b075d2010-11-19 20:36:11 +00005826 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005827 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005828 goto out_unlock;
5829
5830 inode->i_mapping->a_ops = &btrfs_aops;
5831 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5832 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5833 d_instantiate(dentry, inode);
5834
Chris Mason39279cc2007-06-12 06:35:45 -04005835out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005836 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005837 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005838 inode_dec_link_count(inode);
5839 iput(inode);
5840 }
Liu Bob53d3f52012-11-14 14:34:34 +00005841 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005842 return err;
5843}
5844
5845static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5846 struct dentry *dentry)
5847{
5848 struct btrfs_trans_handle *trans;
5849 struct btrfs_root *root = BTRFS_I(dir)->root;
5850 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005851 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005852 int err;
5853 int drop_inode = 0;
5854
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005855 /* do not allow sys_link's with other subvols of the same device */
5856 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005857 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005858
Mark Fashehf1863732012-08-08 11:32:27 -07005859 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005860 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005861
Chris Mason3de45862008-11-17 21:02:50 -05005862 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005863 if (err)
5864 goto fail;
5865
Yan, Zhenga22285a2010-05-16 10:48:46 -04005866 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005867 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005868 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005869 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005870 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005871 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005872 if (IS_ERR(trans)) {
5873 err = PTR_ERR(trans);
5874 goto fail;
5875 }
Chris Mason5f39d392007-10-15 16:14:19 -04005876
Miao Xie31534952011-04-13 13:19:21 +08005877 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005878 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005879 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005880 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005881 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005882
Josef Bacika1b075d2010-11-19 20:36:11 +00005883 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005884
Yan, Zhenga5719522009-09-24 09:17:31 -04005885 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005886 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005887 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005888 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005889 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005890 if (err)
5891 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005892 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005893 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005894 }
Chris Mason39279cc2007-06-12 06:35:45 -04005895
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005896 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005897fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005898 if (drop_inode) {
5899 inode_dec_link_count(inode);
5900 iput(inode);
5901 }
Liu Bob53d3f52012-11-14 14:34:34 +00005902 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005903 return err;
5904}
5905
Al Viro18bb1db2011-07-26 01:41:39 -04005906static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005907{
Chris Masonb9d86662008-05-02 16:13:49 -04005908 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005909 struct btrfs_trans_handle *trans;
5910 struct btrfs_root *root = BTRFS_I(dir)->root;
5911 int err = 0;
5912 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005913 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005914 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005915
Josef Bacik9ed74f22009-09-11 16:12:44 -04005916 /*
5917 * 2 items for inode and ref
5918 * 2 items for dir items
5919 * 1 for xattr if selinux is on
5920 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005921 trans = btrfs_start_transaction(root, 5);
5922 if (IS_ERR(trans))
5923 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005924
Li Zefan581bb052011-04-20 10:06:11 +08005925 err = btrfs_find_free_ino(root, &objectid);
5926 if (err)
5927 goto out_fail;
5928
Josef Bacikaec74772008-07-24 12:12:38 -04005929 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005930 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005931 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005932 if (IS_ERR(inode)) {
5933 err = PTR_ERR(inode);
5934 goto out_fail;
5935 }
Chris Mason5f39d392007-10-15 16:14:19 -04005936
Chris Mason39279cc2007-06-12 06:35:45 -04005937 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005938
Eric Paris2a7dba32011-02-01 11:05:39 -05005939 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005940 if (err)
5941 goto out_fail;
5942
Chris Mason39279cc2007-06-12 06:35:45 -04005943 inode->i_op = &btrfs_dir_inode_operations;
5944 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005945
Chris Masondbe674a2008-07-17 12:54:05 -04005946 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005947 err = btrfs_update_inode(trans, root, inode);
5948 if (err)
5949 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005950
Josef Bacika1b075d2010-11-19 20:36:11 +00005951 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5952 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005953 if (err)
5954 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005955
Chris Mason39279cc2007-06-12 06:35:45 -04005956 d_instantiate(dentry, inode);
5957 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005958
5959out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005960 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005961 if (drop_on_err)
5962 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005963 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005964 return err;
5965}
5966
Chris Masond352ac62008-09-29 15:18:18 -04005967/* helper for btfs_get_extent. Given an existing extent in the tree,
5968 * and an extent that you want to insert, deal with overlap and insert
5969 * the new extent into the tree.
5970 */
Chris Mason3b951512008-04-17 11:29:12 -04005971static int merge_extent_mapping(struct extent_map_tree *em_tree,
5972 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005973 struct extent_map *em,
5974 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005975{
5976 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005977
Chris Masone6dcd2d2008-07-17 12:53:50 -04005978 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5979 start_diff = map_start - em->start;
5980 em->start = map_start;
5981 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005982 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5983 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005984 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005985 em->block_len -= start_diff;
5986 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005987 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005988}
5989
Chris Masonc8b97812008-10-29 14:49:59 -04005990static noinline int uncompress_inline(struct btrfs_path *path,
5991 struct inode *inode, struct page *page,
5992 size_t pg_offset, u64 extent_offset,
5993 struct btrfs_file_extent_item *item)
5994{
5995 int ret;
5996 struct extent_buffer *leaf = path->nodes[0];
5997 char *tmp;
5998 size_t max_size;
5999 unsigned long inline_size;
6000 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006001 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006002
6003 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006004 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006005 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6006 inline_size = btrfs_file_extent_inline_item_len(leaf,
6007 btrfs_item_nr(leaf, path->slots[0]));
6008 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006009 if (!tmp)
6010 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006011 ptr = btrfs_file_extent_inline_start(item);
6012
6013 read_extent_buffer(leaf, tmp, ptr, inline_size);
6014
Chris Mason5b050f02008-11-11 09:34:41 -05006015 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006016 ret = btrfs_decompress(compress_type, tmp, page,
6017 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006018 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006019 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006020 unsigned long copy_size = min_t(u64,
6021 PAGE_CACHE_SIZE - pg_offset,
6022 max_size - extent_offset);
6023 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006024 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006025 }
6026 kfree(tmp);
6027 return 0;
6028}
6029
Chris Masond352ac62008-09-29 15:18:18 -04006030/*
6031 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006032 * the ugly parts come from merging extents from the disk with the in-ram
6033 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006034 * where the in-ram extents might be locked pending data=ordered completion.
6035 *
6036 * This also copies inline extents directly into the page.
6037 */
Chris Masond3977122009-01-05 21:25:51 -05006038
Chris Masona52d9a82007-08-27 16:49:44 -04006039struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006040 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006041 int create)
6042{
6043 int ret;
6044 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006045 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006046 u64 extent_start = 0;
6047 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006048 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006049 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006050 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006051 struct btrfs_root *root = BTRFS_I(inode)->root;
6052 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006053 struct extent_buffer *leaf;
6054 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006055 struct extent_map *em = NULL;
6056 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006057 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006058 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006059 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006060
Chris Masona52d9a82007-08-27 16:49:44 -04006061again:
Chris Mason890871b2009-09-02 16:24:52 -04006062 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006063 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006064 if (em)
6065 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006066 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006067
Chris Masona52d9a82007-08-27 16:49:44 -04006068 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006069 if (em->start > start || em->start + em->len <= start)
6070 free_extent_map(em);
6071 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006072 free_extent_map(em);
6073 else
6074 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006075 }
David Sterba172ddd62011-04-21 00:48:27 +02006076 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006077 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006078 err = -ENOMEM;
6079 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006080 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006081 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006082 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006083 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006084 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006085 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006086
6087 if (!path) {
6088 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006089 if (!path) {
6090 err = -ENOMEM;
6091 goto out;
6092 }
6093 /*
6094 * Chances are we'll be called again, so go ahead and do
6095 * readahead
6096 */
6097 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006098 }
6099
Chris Mason179e29e2007-11-01 11:28:41 -04006100 ret = btrfs_lookup_file_extent(trans, root, path,
6101 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006102 if (ret < 0) {
6103 err = ret;
6104 goto out;
6105 }
6106
6107 if (ret != 0) {
6108 if (path->slots[0] == 0)
6109 goto not_found;
6110 path->slots[0]--;
6111 }
6112
Chris Mason5f39d392007-10-15 16:14:19 -04006113 leaf = path->nodes[0];
6114 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006115 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006116 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006117 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6118 found_type = btrfs_key_type(&found_key);
6119 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006120 found_type != BTRFS_EXTENT_DATA_KEY) {
6121 goto not_found;
6122 }
6123
Chris Mason5f39d392007-10-15 16:14:19 -04006124 found_type = btrfs_file_extent_type(leaf, item);
6125 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006126 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006127 if (found_type == BTRFS_FILE_EXTENT_REG ||
6128 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006129 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006130 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006131 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6132 size_t size;
6133 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006134 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006135 }
6136
6137 if (start >= extent_end) {
6138 path->slots[0]++;
6139 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6140 ret = btrfs_next_leaf(root, path);
6141 if (ret < 0) {
6142 err = ret;
6143 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006144 }
Yan Zheng9036c102008-10-30 14:19:41 -04006145 if (ret > 0)
6146 goto not_found;
6147 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006148 }
Yan Zheng9036c102008-10-30 14:19:41 -04006149 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6150 if (found_key.objectid != objectid ||
6151 found_key.type != BTRFS_EXTENT_DATA_KEY)
6152 goto not_found;
6153 if (start + len <= found_key.offset)
6154 goto not_found;
6155 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006156 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006157 em->len = found_key.offset - start;
6158 goto not_found_em;
6159 }
6160
Josef Bacikcc95bef2013-04-04 14:31:27 -04006161 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006162 if (found_type == BTRFS_FILE_EXTENT_REG ||
6163 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006164 em->start = extent_start;
6165 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006166 em->orig_start = extent_start -
6167 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006168 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6169 item);
Chris Masondb945352007-10-15 16:15:53 -04006170 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6171 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006172 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006173 goto insert;
6174 }
Li Zefan261507a02010-12-17 14:21:50 +08006175 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006176 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006177 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006178 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006179 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006180 } else {
6181 bytenr += btrfs_file_extent_offset(leaf, item);
6182 em->block_start = bytenr;
6183 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006184 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6185 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006186 }
Chris Masona52d9a82007-08-27 16:49:44 -04006187 goto insert;
6188 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006189 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006190 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006191 size_t size;
6192 size_t extent_offset;
6193 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006194
Chris Masona52d9a82007-08-27 16:49:44 -04006195 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006196 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006197 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006198 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006199 goto out;
6200 }
6201
Yan Zheng9036c102008-10-30 14:19:41 -04006202 size = btrfs_file_extent_inline_len(leaf, item);
6203 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006204 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006205 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006206 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006207 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006208 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006209 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006210 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006211 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006212 em->compress_type = compress_type;
6213 }
Yan689f9342007-10-29 11:41:07 -04006214 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006215 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006216 if (btrfs_file_extent_compression(leaf, item) !=
6217 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006218 ret = uncompress_inline(path, inode, page,
6219 pg_offset,
6220 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006221 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006222 } else {
6223 map = kmap(page);
6224 read_extent_buffer(leaf, map + pg_offset, ptr,
6225 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006226 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6227 memset(map + pg_offset + copy_size, 0,
6228 PAGE_CACHE_SIZE - pg_offset -
6229 copy_size);
6230 }
Chris Masonc8b97812008-10-29 14:49:59 -04006231 kunmap(page);
6232 }
Chris Mason179e29e2007-11-01 11:28:41 -04006233 flush_dcache_page(page);
6234 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006235 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006236 if (!trans) {
6237 kunmap(page);
6238 free_extent_map(em);
6239 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006240
David Sterbab3b4aa72011-04-21 01:20:15 +02006241 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006242 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006243
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006244 if (IS_ERR(trans))
6245 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006246 goto again;
6247 }
Chris Masonc8b97812008-10-29 14:49:59 -04006248 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006249 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006250 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006251 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006252 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006253 }
Chris Masond1310b22008-01-24 16:13:08 -05006254 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006255 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006256 goto insert;
6257 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006258 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006259 }
6260not_found:
6261 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006262 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006263 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006264not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006265 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006266 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006267insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006268 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006269 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006270 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6271 (unsigned long long)em->start,
6272 (unsigned long long)em->len,
6273 (unsigned long long)start,
6274 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006275 err = -EIO;
6276 goto out;
6277 }
Chris Masond1310b22008-01-24 16:13:08 -05006278
6279 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006280 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006281 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006282 /* it is possible that someone inserted the extent into the tree
6283 * while we had the lock dropped. It is also possible that
6284 * an overlapping map exists in the tree
6285 */
Chris Masona52d9a82007-08-27 16:49:44 -04006286 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006287 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006288
6289 ret = 0;
6290
Chris Mason3b951512008-04-17 11:29:12 -04006291 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006292 if (existing && (existing->start > start ||
6293 existing->start + existing->len <= start)) {
6294 free_extent_map(existing);
6295 existing = NULL;
6296 }
Chris Mason3b951512008-04-17 11:29:12 -04006297 if (!existing) {
6298 existing = lookup_extent_mapping(em_tree, em->start,
6299 em->len);
6300 if (existing) {
6301 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006302 em, start,
6303 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006304 free_extent_map(existing);
6305 if (err) {
6306 free_extent_map(em);
6307 em = NULL;
6308 }
6309 } else {
6310 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006311 free_extent_map(em);
6312 em = NULL;
6313 }
6314 } else {
6315 free_extent_map(em);
6316 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006317 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006318 }
Chris Masona52d9a82007-08-27 16:49:44 -04006319 }
Chris Mason890871b2009-09-02 16:24:52 -04006320 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006321out:
liubo1abe9b82011-03-24 11:18:59 +00006322
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006323 if (em)
6324 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006325
Chris Masonf4219502008-07-22 11:18:09 -04006326 if (path)
6327 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006328 if (trans) {
6329 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006330 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006331 err = ret;
6332 }
Chris Masona52d9a82007-08-27 16:49:44 -04006333 if (err) {
6334 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006335 return ERR_PTR(err);
6336 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006337 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006338 return em;
6339}
6340
Chris Masonec29ed52011-02-23 16:23:20 -05006341struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6342 size_t pg_offset, u64 start, u64 len,
6343 int create)
6344{
6345 struct extent_map *em;
6346 struct extent_map *hole_em = NULL;
6347 u64 range_start = start;
6348 u64 end;
6349 u64 found;
6350 u64 found_end;
6351 int err = 0;
6352
6353 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6354 if (IS_ERR(em))
6355 return em;
6356 if (em) {
6357 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006358 * if our em maps to
6359 * - a hole or
6360 * - a pre-alloc extent,
6361 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006362 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006363 if (em->block_start != EXTENT_MAP_HOLE &&
6364 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006365 return em;
6366 else
6367 hole_em = em;
6368 }
6369
6370 /* check to see if we've wrapped (len == -1 or similar) */
6371 end = start + len;
6372 if (end < start)
6373 end = (u64)-1;
6374 else
6375 end -= 1;
6376
6377 em = NULL;
6378
6379 /* ok, we didn't find anything, lets look for delalloc */
6380 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6381 end, len, EXTENT_DELALLOC, 1);
6382 found_end = range_start + found;
6383 if (found_end < range_start)
6384 found_end = (u64)-1;
6385
6386 /*
6387 * we didn't find anything useful, return
6388 * the original results from get_extent()
6389 */
6390 if (range_start > end || found_end <= start) {
6391 em = hole_em;
6392 hole_em = NULL;
6393 goto out;
6394 }
6395
6396 /* adjust the range_start to make sure it doesn't
6397 * go backwards from the start they passed in
6398 */
6399 range_start = max(start,range_start);
6400 found = found_end - range_start;
6401
6402 if (found > 0) {
6403 u64 hole_start = start;
6404 u64 hole_len = len;
6405
David Sterba172ddd62011-04-21 00:48:27 +02006406 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006407 if (!em) {
6408 err = -ENOMEM;
6409 goto out;
6410 }
6411 /*
6412 * when btrfs_get_extent can't find anything it
6413 * returns one huge hole
6414 *
6415 * make sure what it found really fits our range, and
6416 * adjust to make sure it is based on the start from
6417 * the caller
6418 */
6419 if (hole_em) {
6420 u64 calc_end = extent_map_end(hole_em);
6421
6422 if (calc_end <= start || (hole_em->start > end)) {
6423 free_extent_map(hole_em);
6424 hole_em = NULL;
6425 } else {
6426 hole_start = max(hole_em->start, start);
6427 hole_len = calc_end - hole_start;
6428 }
6429 }
6430 em->bdev = NULL;
6431 if (hole_em && range_start > hole_start) {
6432 /* our hole starts before our delalloc, so we
6433 * have to return just the parts of the hole
6434 * that go until the delalloc starts
6435 */
6436 em->len = min(hole_len,
6437 range_start - hole_start);
6438 em->start = hole_start;
6439 em->orig_start = hole_start;
6440 /*
6441 * don't adjust block start at all,
6442 * it is fixed at EXTENT_MAP_HOLE
6443 */
6444 em->block_start = hole_em->block_start;
6445 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006446 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6447 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006448 } else {
6449 em->start = range_start;
6450 em->len = found;
6451 em->orig_start = range_start;
6452 em->block_start = EXTENT_MAP_DELALLOC;
6453 em->block_len = found;
6454 }
6455 } else if (hole_em) {
6456 return hole_em;
6457 }
6458out:
6459
6460 free_extent_map(hole_em);
6461 if (err) {
6462 free_extent_map(em);
6463 return ERR_PTR(err);
6464 }
6465 return em;
6466}
6467
Josef Bacik4b46fce2010-05-23 11:00:55 -04006468static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6469 u64 start, u64 len)
6470{
6471 struct btrfs_root *root = BTRFS_I(inode)->root;
6472 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006473 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006474 struct btrfs_key ins;
6475 u64 alloc_hint;
6476 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006477
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006478 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006479 if (IS_ERR(trans))
6480 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006481
6482 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6483
6484 alloc_hint = get_extent_allocation_hint(inode, start, len);
6485 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006486 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006487 if (ret) {
6488 em = ERR_PTR(ret);
6489 goto out;
6490 }
6491
Josef Bacik70c8a912012-10-11 16:54:30 -04006492 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006493 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006494 if (IS_ERR(em))
6495 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006496
6497 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6498 ins.offset, ins.offset, 0);
6499 if (ret) {
6500 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6501 em = ERR_PTR(ret);
6502 }
6503out:
6504 btrfs_end_transaction(trans, root);
6505 return em;
6506}
6507
Chris Mason46bfbb52010-05-26 11:04:10 -04006508/*
6509 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6510 * block must be cow'd
6511 */
6512static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
Josef Bacikeb384b52013-04-24 16:32:55 -04006513 struct inode *inode, u64 offset, u64 *len,
6514 u64 *orig_start, u64 *orig_block_len,
6515 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006516{
6517 struct btrfs_path *path;
6518 int ret;
6519 struct extent_buffer *leaf;
6520 struct btrfs_root *root = BTRFS_I(inode)->root;
6521 struct btrfs_file_extent_item *fi;
6522 struct btrfs_key key;
6523 u64 disk_bytenr;
6524 u64 backref_offset;
6525 u64 extent_end;
6526 u64 num_bytes;
6527 int slot;
6528 int found_type;
6529
6530 path = btrfs_alloc_path();
6531 if (!path)
6532 return -ENOMEM;
6533
Li Zefan33345d012011-04-20 10:31:50 +08006534 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006535 offset, 0);
6536 if (ret < 0)
6537 goto out;
6538
6539 slot = path->slots[0];
6540 if (ret == 1) {
6541 if (slot == 0) {
6542 /* can't find the item, must cow */
6543 ret = 0;
6544 goto out;
6545 }
6546 slot--;
6547 }
6548 ret = 0;
6549 leaf = path->nodes[0];
6550 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006551 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006552 key.type != BTRFS_EXTENT_DATA_KEY) {
6553 /* not our file or wrong item type, must cow */
6554 goto out;
6555 }
6556
6557 if (key.offset > offset) {
6558 /* Wrong offset, must cow */
6559 goto out;
6560 }
6561
6562 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6563 found_type = btrfs_file_extent_type(leaf, fi);
6564 if (found_type != BTRFS_FILE_EXTENT_REG &&
6565 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6566 /* not a regular extent, must cow */
6567 goto out;
6568 }
6569 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6570 backref_offset = btrfs_file_extent_offset(leaf, fi);
6571
Josef Bacikeb384b52013-04-24 16:32:55 -04006572 *orig_start = key.offset - backref_offset;
6573 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6574 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6575
Chris Mason46bfbb52010-05-26 11:04:10 -04006576 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikeb384b52013-04-24 16:32:55 -04006577 if (extent_end < offset + *len) {
Chris Mason46bfbb52010-05-26 11:04:10 -04006578 /* extent doesn't include our full range, must cow */
6579 goto out;
6580 }
6581
6582 if (btrfs_extent_readonly(root, disk_bytenr))
6583 goto out;
6584
6585 /*
6586 * look for other files referencing this extent, if we
6587 * find any we must cow
6588 */
Li Zefan33345d012011-04-20 10:31:50 +08006589 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006590 key.offset - backref_offset, disk_bytenr))
6591 goto out;
6592
6593 /*
6594 * adjust disk_bytenr and num_bytes to cover just the bytes
6595 * in this extent we are about to write. If there
6596 * are any csums in that range we have to cow in order
6597 * to keep the csums correct
6598 */
6599 disk_bytenr += backref_offset;
6600 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006601 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006602 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6603 goto out;
6604 /*
6605 * all of the above have passed, it is safe to overwrite this extent
6606 * without cow
6607 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006608 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006609 ret = 1;
6610out:
6611 btrfs_free_path(path);
6612 return ret;
6613}
6614
Josef Bacikeb838e72012-07-31 16:28:48 -04006615static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6616 struct extent_state **cached_state, int writing)
6617{
6618 struct btrfs_ordered_extent *ordered;
6619 int ret = 0;
6620
6621 while (1) {
6622 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6623 0, cached_state);
6624 /*
6625 * We're concerned with the entire range that we're going to be
6626 * doing DIO to, so we need to make sure theres no ordered
6627 * extents in this range.
6628 */
6629 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6630 lockend - lockstart + 1);
6631
6632 /*
6633 * We need to make sure there are no buffered pages in this
6634 * range either, we could have raced between the invalidate in
6635 * generic_file_direct_write and locking the extent. The
6636 * invalidate needs to happen so that reads after a write do not
6637 * get stale data.
6638 */
6639 if (!ordered && (!writing ||
6640 !test_range_bit(&BTRFS_I(inode)->io_tree,
6641 lockstart, lockend, EXTENT_UPTODATE, 0,
6642 *cached_state)))
6643 break;
6644
6645 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6646 cached_state, GFP_NOFS);
6647
6648 if (ordered) {
6649 btrfs_start_ordered_extent(inode, ordered, 1);
6650 btrfs_put_ordered_extent(ordered);
6651 } else {
6652 /* Screw you mmap */
6653 ret = filemap_write_and_wait_range(inode->i_mapping,
6654 lockstart,
6655 lockend);
6656 if (ret)
6657 break;
6658
6659 /*
6660 * If we found a page that couldn't be invalidated just
6661 * fall back to buffered.
6662 */
6663 ret = invalidate_inode_pages2_range(inode->i_mapping,
6664 lockstart >> PAGE_CACHE_SHIFT,
6665 lockend >> PAGE_CACHE_SHIFT);
6666 if (ret)
6667 break;
6668 }
6669
6670 cond_resched();
6671 }
6672
6673 return ret;
6674}
6675
Josef Bacik69ffb542012-09-11 15:40:07 -04006676static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6677 u64 len, u64 orig_start,
6678 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006679 u64 orig_block_len, u64 ram_bytes,
6680 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006681{
6682 struct extent_map_tree *em_tree;
6683 struct extent_map *em;
6684 struct btrfs_root *root = BTRFS_I(inode)->root;
6685 int ret;
6686
6687 em_tree = &BTRFS_I(inode)->extent_tree;
6688 em = alloc_extent_map();
6689 if (!em)
6690 return ERR_PTR(-ENOMEM);
6691
6692 em->start = start;
6693 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006694 em->mod_start = start;
6695 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006696 em->len = len;
6697 em->block_len = block_len;
6698 em->block_start = block_start;
6699 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006700 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006701 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006702 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006703 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6704 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006705 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006706
6707 do {
6708 btrfs_drop_extent_cache(inode, em->start,
6709 em->start + em->len - 1, 0);
6710 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006711 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006712 write_unlock(&em_tree->lock);
6713 } while (ret == -EEXIST);
6714
6715 if (ret) {
6716 free_extent_map(em);
6717 return ERR_PTR(ret);
6718 }
6719
6720 return em;
6721}
6722
6723
Josef Bacik4b46fce2010-05-23 11:00:55 -04006724static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6725 struct buffer_head *bh_result, int create)
6726{
6727 struct extent_map *em;
6728 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006729 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006730 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006731 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006732 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006733 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006734 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006735 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006736
Miao Xie172a5042013-02-21 02:48:22 -07006737 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006738 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006739 else
Josef Bacikc3298612012-08-03 16:49:19 -04006740 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006741
Josef Bacikc3298612012-08-03 16:49:19 -04006742 lockstart = start;
6743 lockend = start + len - 1;
6744
Josef Bacikeb838e72012-07-31 16:28:48 -04006745 /*
6746 * If this errors out it's because we couldn't invalidate pagecache for
6747 * this range and we need to fallback to buffered.
6748 */
6749 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6750 return -ENOTBLK;
6751
Josef Bacik4b46fce2010-05-23 11:00:55 -04006752 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006753 if (IS_ERR(em)) {
6754 ret = PTR_ERR(em);
6755 goto unlock_err;
6756 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006757
6758 /*
6759 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6760 * io. INLINE is special, and we could probably kludge it in here, but
6761 * it's still buffered so for safety lets just fall back to the generic
6762 * buffered path.
6763 *
6764 * For COMPRESSED we _have_ to read the entire extent in so we can
6765 * decompress it, so there will be buffering required no matter what we
6766 * do, so go ahead and fallback to buffered.
6767 *
6768 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6769 * to buffered IO. Don't blame me, this is the price we pay for using
6770 * the generic code.
6771 */
6772 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6773 em->block_start == EXTENT_MAP_INLINE) {
6774 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006775 ret = -ENOTBLK;
6776 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006777 }
6778
6779 /* Just a good old fashioned hole, return */
6780 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6781 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6782 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006783 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006784 }
6785
6786 /*
6787 * We don't allocate a new extent in the following cases
6788 *
6789 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6790 * existing extent.
6791 * 2) The extent is marked as PREALLOC. We're good to go here and can
6792 * just use the extent.
6793 *
6794 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006795 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006796 len = min(len, em->len - (start - em->start));
6797 lockstart = start + len;
6798 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006799 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006800
6801 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6802 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6803 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006804 int type;
6805 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006806 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006807
6808 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6809 type = BTRFS_ORDERED_PREALLOC;
6810 else
6811 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006812 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006813 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006814
6815 /*
6816 * we're not going to log anything, but we do need
6817 * to make sure the current transaction stays open
6818 * while we look for nocow cross refs
6819 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006820 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006821 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006822 goto must_cow;
6823
Josef Bacikeb384b52013-04-24 16:32:55 -04006824 if (can_nocow_odirect(trans, inode, start, &len, &orig_start,
6825 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006826 if (type == BTRFS_ORDERED_PREALLOC) {
6827 free_extent_map(em);
6828 em = create_pinned_em(inode, start, len,
6829 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006830 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006831 orig_block_len,
6832 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006833 if (IS_ERR(em)) {
6834 btrfs_end_transaction(trans, root);
6835 goto unlock_err;
6836 }
6837 }
6838
Chris Mason46bfbb52010-05-26 11:04:10 -04006839 ret = btrfs_add_ordered_extent_dio(inode, start,
6840 block_start, len, len, type);
6841 btrfs_end_transaction(trans, root);
6842 if (ret) {
6843 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006844 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006845 }
6846 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006847 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006848 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006849 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006850must_cow:
6851 /*
6852 * this will cow the extent, reset the len in case we changed
6853 * it above
6854 */
6855 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006856 free_extent_map(em);
6857 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006858 if (IS_ERR(em)) {
6859 ret = PTR_ERR(em);
6860 goto unlock_err;
6861 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006862 len = min(len, em->len - (start - em->start));
6863unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006864 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6865 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006866 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006867 bh_result->b_bdev = em->bdev;
6868 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006869 if (create) {
6870 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6871 set_buffer_new(bh_result);
6872
6873 /*
6874 * Need to update the i_size under the extent lock so buffered
6875 * readers will get the updated i_size when we unlock.
6876 */
6877 if (start + len > i_size_read(inode))
6878 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006879
Miao Xie172a5042013-02-21 02:48:22 -07006880 spin_lock(&BTRFS_I(inode)->lock);
6881 BTRFS_I(inode)->outstanding_extents++;
6882 spin_unlock(&BTRFS_I(inode)->lock);
6883
Miao Xie09348562013-02-07 10:12:07 +00006884 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6885 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6886 &cached_state, GFP_NOFS);
6887 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006888 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006889
Josef Bacikeb838e72012-07-31 16:28:48 -04006890 /*
6891 * In the case of write we need to clear and unlock the entire range,
6892 * in the case of read we need to unlock only the end area that we
6893 * aren't using if there is any left over space.
6894 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006895 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006896 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6897 lockend, unlock_bits, 1, 0,
6898 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006899 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006900 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006901 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006902
Josef Bacik4b46fce2010-05-23 11:00:55 -04006903 free_extent_map(em);
6904
6905 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006906
6907unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006908 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6909 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6910 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006911}
6912
6913struct btrfs_dio_private {
6914 struct inode *inode;
6915 u64 logical_offset;
6916 u64 disk_bytenr;
6917 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006918 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006919
6920 /* number of bios pending for this dio */
6921 atomic_t pending_bios;
6922
6923 /* IO errors */
6924 int errors;
6925
6926 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006927};
6928
6929static void btrfs_endio_direct_read(struct bio *bio, int err)
6930{
Miao Xiee65e1532010-11-22 03:04:43 +00006931 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006932 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6933 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006934 struct inode *inode = dip->inode;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006935 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006936 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006937
6938 start = dip->logical_offset;
6939 do {
6940 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6941 struct page *page = bvec->bv_page;
6942 char *kaddr;
6943 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006944 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006945 unsigned long flags;
6946
Josef Bacikc3298612012-08-03 16:49:19 -04006947 if (get_state_private(&BTRFS_I(inode)->io_tree,
6948 start, &private))
6949 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006950 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006951 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006952 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006953 csum, bvec->bv_len);
6954 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006955 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006956 local_irq_restore(flags);
6957
6958 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006959 if (csum != private) {
6960failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006961 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6962 (unsigned long long)btrfs_ino(inode),
6963 (unsigned long long)start,
6964 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006965 err = -EIO;
6966 }
6967 }
6968
6969 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006970 bvec++;
6971 } while (bvec <= bvec_end);
6972
6973 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006974 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006975 bio->bi_private = dip->private;
6976
Josef Bacik4b46fce2010-05-23 11:00:55 -04006977 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006978
6979 /* If we had a csum failure make sure to clear the uptodate flag */
6980 if (err)
6981 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006982 dio_end_io(bio, err);
6983}
6984
6985static void btrfs_endio_direct_write(struct bio *bio, int err)
6986{
6987 struct btrfs_dio_private *dip = bio->bi_private;
6988 struct inode *inode = dip->inode;
6989 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006990 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006991 u64 ordered_offset = dip->logical_offset;
6992 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006993 int ret;
6994
6995 if (err)
6996 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006997again:
6998 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6999 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007000 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007001 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007002 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007003
Josef Bacik5fd02042012-05-02 14:00:54 -04007004 ordered->work.func = finish_ordered_fn;
7005 ordered->work.flags = 0;
7006 btrfs_queue_worker(&root->fs_info->endio_write_workers,
7007 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007008out_test:
7009 /*
7010 * our bio might span multiple ordered extents. If we haven't
7011 * completed the accounting for the whole dio, go back and try again
7012 */
7013 if (ordered_offset < dip->logical_offset + dip->bytes) {
7014 ordered_bytes = dip->logical_offset + dip->bytes -
7015 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007016 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007017 goto again;
7018 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007019out_done:
7020 bio->bi_private = dip->private;
7021
Josef Bacik4b46fce2010-05-23 11:00:55 -04007022 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007023
7024 /* If we had an error make sure to clear the uptodate flag */
7025 if (err)
7026 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007027 dio_end_io(bio, err);
7028}
7029
Chris Masoneaf25d92010-05-25 09:48:28 -04007030static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7031 struct bio *bio, int mirror_num,
7032 unsigned long bio_flags, u64 offset)
7033{
7034 int ret;
7035 struct btrfs_root *root = BTRFS_I(inode)->root;
7036 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007037 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007038 return 0;
7039}
7040
Miao Xiee65e1532010-11-22 03:04:43 +00007041static void btrfs_end_dio_bio(struct bio *bio, int err)
7042{
7043 struct btrfs_dio_private *dip = bio->bi_private;
7044
7045 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007046 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007047 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007048 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007049 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007050 dip->errors = 1;
7051
7052 /*
7053 * before atomic variable goto zero, we must make sure
7054 * dip->errors is perceived to be set.
7055 */
7056 smp_mb__before_atomic_dec();
7057 }
7058
7059 /* if there are more bios still pending for this dio, just exit */
7060 if (!atomic_dec_and_test(&dip->pending_bios))
7061 goto out;
7062
7063 if (dip->errors)
7064 bio_io_error(dip->orig_bio);
7065 else {
7066 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
7067 bio_endio(dip->orig_bio, 0);
7068 }
7069out:
7070 bio_put(bio);
7071}
7072
7073static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7074 u64 first_sector, gfp_t gfp_flags)
7075{
7076 int nr_vecs = bio_get_nr_vecs(bdev);
7077 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7078}
7079
7080static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7081 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007082 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007083{
7084 int write = rw & REQ_WRITE;
7085 struct btrfs_root *root = BTRFS_I(inode)->root;
7086 int ret;
7087
Josef Bacikb812ce22012-11-16 13:56:32 -05007088 if (async_submit)
7089 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7090
Miao Xiee65e1532010-11-22 03:04:43 +00007091 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007092
7093 if (!write) {
7094 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7095 if (ret)
7096 goto err;
7097 }
Miao Xiee65e1532010-11-22 03:04:43 +00007098
Josef Bacik1ae39932011-04-06 14:41:34 -04007099 if (skip_sum)
7100 goto map;
7101
7102 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007103 ret = btrfs_wq_submit_bio(root->fs_info,
7104 inode, rw, bio, 0, 0,
7105 file_offset,
7106 __btrfs_submit_bio_start_direct_io,
7107 __btrfs_submit_bio_done);
7108 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007109 } else if (write) {
7110 /*
7111 * If we aren't doing async submit, calculate the csum of the
7112 * bio now.
7113 */
7114 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7115 if (ret)
7116 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007117 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007118 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007119 if (ret)
7120 goto err;
7121 }
Miao Xiee65e1532010-11-22 03:04:43 +00007122
Josef Bacik1ae39932011-04-06 14:41:34 -04007123map:
7124 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007125err:
7126 bio_put(bio);
7127 return ret;
7128}
7129
7130static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7131 int skip_sum)
7132{
7133 struct inode *inode = dip->inode;
7134 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007135 struct bio *bio;
7136 struct bio *orig_bio = dip->orig_bio;
7137 struct bio_vec *bvec = orig_bio->bi_io_vec;
7138 u64 start_sector = orig_bio->bi_sector;
7139 u64 file_offset = dip->logical_offset;
7140 u64 submit_len = 0;
7141 u64 map_length;
7142 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007143 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007144 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007145
Miao Xiee65e1532010-11-22 03:04:43 +00007146 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007147 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007148 &map_length, NULL, 0);
7149 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007150 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007151 return -EIO;
7152 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007153 if (map_length >= orig_bio->bi_size) {
7154 bio = orig_bio;
7155 goto submit;
7156 }
7157
David Woodhouse53b381b2013-01-29 18:40:14 -05007158 /* async crcs make it difficult to collect full stripe writes. */
7159 if (btrfs_get_alloc_profile(root, 1) &
7160 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7161 async_submit = 0;
7162 else
7163 async_submit = 1;
7164
Josef Bacik02f57c72011-04-06 14:25:44 -04007165 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7166 if (!bio)
7167 return -ENOMEM;
7168 bio->bi_private = dip;
7169 bio->bi_end_io = btrfs_end_dio_bio;
7170 atomic_inc(&dip->pending_bios);
7171
Miao Xiee65e1532010-11-22 03:04:43 +00007172 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7173 if (unlikely(map_length < submit_len + bvec->bv_len ||
7174 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7175 bvec->bv_offset) < bvec->bv_len)) {
7176 /*
7177 * inc the count before we submit the bio so
7178 * we know the end IO handler won't happen before
7179 * we inc the count. Otherwise, the dip might get freed
7180 * before we're done setting it up
7181 */
7182 atomic_inc(&dip->pending_bios);
7183 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7184 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007185 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007186 if (ret) {
7187 bio_put(bio);
7188 atomic_dec(&dip->pending_bios);
7189 goto out_err;
7190 }
7191
Miao Xiee65e1532010-11-22 03:04:43 +00007192 start_sector += submit_len >> 9;
7193 file_offset += submit_len;
7194
7195 submit_len = 0;
7196 nr_pages = 0;
7197
7198 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7199 start_sector, GFP_NOFS);
7200 if (!bio)
7201 goto out_err;
7202 bio->bi_private = dip;
7203 bio->bi_end_io = btrfs_end_dio_bio;
7204
7205 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007206 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007207 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007208 &map_length, NULL, 0);
7209 if (ret) {
7210 bio_put(bio);
7211 goto out_err;
7212 }
7213 } else {
7214 submit_len += bvec->bv_len;
7215 nr_pages ++;
7216 bvec++;
7217 }
7218 }
7219
Josef Bacik02f57c72011-04-06 14:25:44 -04007220submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007221 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007222 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007223 if (!ret)
7224 return 0;
7225
7226 bio_put(bio);
7227out_err:
7228 dip->errors = 1;
7229 /*
7230 * before atomic variable goto zero, we must
7231 * make sure dip->errors is perceived to be set.
7232 */
7233 smp_mb__before_atomic_dec();
7234 if (atomic_dec_and_test(&dip->pending_bios))
7235 bio_io_error(dip->orig_bio);
7236
7237 /* bio_end_io() will handle error, so we needn't return it */
7238 return 0;
7239}
7240
Josef Bacik4b46fce2010-05-23 11:00:55 -04007241static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
7242 loff_t file_offset)
7243{
7244 struct btrfs_root *root = BTRFS_I(inode)->root;
7245 struct btrfs_dio_private *dip;
7246 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007247 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007248 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249 int ret = 0;
7250
7251 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7252
7253 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7254 if (!dip) {
7255 ret = -ENOMEM;
7256 goto free_ordered;
7257 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007258
7259 dip->private = bio->bi_private;
7260 dip->inode = inode;
7261 dip->logical_offset = file_offset;
7262
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263 dip->bytes = 0;
7264 do {
7265 dip->bytes += bvec->bv_len;
7266 bvec++;
7267 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
7268
Chris Mason46bfbb52010-05-26 11:04:10 -04007269 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007270 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007271 dip->errors = 0;
7272 dip->orig_bio = bio;
7273 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007274
7275 if (write)
7276 bio->bi_end_io = btrfs_endio_direct_write;
7277 else
7278 bio->bi_end_io = btrfs_endio_direct_read;
7279
Miao Xiee65e1532010-11-22 03:04:43 +00007280 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7281 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007282 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283free_ordered:
7284 /*
7285 * If this is a write, we need to clean up the reserved space and kill
7286 * the ordered extent.
7287 */
7288 if (write) {
7289 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007290 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007291 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7292 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7293 btrfs_free_reserved_extent(root, ordered->start,
7294 ordered->disk_len);
7295 btrfs_put_ordered_extent(ordered);
7296 btrfs_put_ordered_extent(ordered);
7297 }
7298 bio_endio(bio, ret);
7299}
7300
Chris Mason5a5f79b2010-05-26 21:33:37 -04007301static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7302 const struct iovec *iov, loff_t offset,
7303 unsigned long nr_segs)
7304{
7305 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007306 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007307 size_t size;
7308 unsigned long addr;
7309 unsigned blocksize_mask = root->sectorsize - 1;
7310 ssize_t retval = -EINVAL;
7311 loff_t end = offset;
7312
7313 if (offset & blocksize_mask)
7314 goto out;
7315
7316 /* Check the memory alignment. Blocks cannot straddle pages */
7317 for (seg = 0; seg < nr_segs; seg++) {
7318 addr = (unsigned long)iov[seg].iov_base;
7319 size = iov[seg].iov_len;
7320 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007321 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007322 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007323
7324 /* If this is a write we don't need to check anymore */
7325 if (rw & WRITE)
7326 continue;
7327
7328 /*
7329 * Check to make sure we don't have duplicate iov_base's in this
7330 * iovec, if so return EINVAL, otherwise we'll get csum errors
7331 * when reading back.
7332 */
7333 for (i = seg + 1; i < nr_segs; i++) {
7334 if (iov[seg].iov_base == iov[i].iov_base)
7335 goto out;
7336 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007337 }
7338 retval = 0;
7339out:
7340 return retval;
7341}
Josef Bacikeb838e72012-07-31 16:28:48 -04007342
Chris Mason16432982008-04-10 10:23:21 -04007343static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7344 const struct iovec *iov, loff_t offset,
7345 unsigned long nr_segs)
7346{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007347 struct file *file = iocb->ki_filp;
7348 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007349 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007350 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007351 bool wakeup = true;
7352 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007353 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007354
Chris Mason5a5f79b2010-05-26 21:33:37 -04007355 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007356 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007357 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007358
Miao Xie38851cc2013-02-08 07:04:11 +00007359 atomic_inc(&inode->i_dio_count);
7360 smp_mb__after_atomic_inc();
7361
Miao Xie09348562013-02-07 10:12:07 +00007362 if (rw & WRITE) {
7363 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007364 /*
7365 * If the write DIO is beyond the EOF, we need update
7366 * the isize, but it is protected by i_mutex. So we can
7367 * not unlock the i_mutex at this case.
7368 */
7369 if (offset + count <= inode->i_size) {
7370 mutex_unlock(&inode->i_mutex);
7371 relock = true;
7372 }
Miao Xie09348562013-02-07 10:12:07 +00007373 ret = btrfs_delalloc_reserve_space(inode, count);
7374 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007375 goto out;
7376 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7377 &BTRFS_I(inode)->runtime_flags))) {
7378 inode_dio_done(inode);
7379 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7380 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007381 }
7382
7383 ret = __blockdev_direct_IO(rw, iocb, inode,
7384 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7385 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007386 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007387 if (rw & WRITE) {
7388 if (ret < 0 && ret != -EIOCBQUEUED)
7389 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007390 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007391 btrfs_delalloc_release_space(inode,
7392 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007393 else
7394 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007395 }
Miao Xie38851cc2013-02-08 07:04:11 +00007396out:
Miao Xie2e60a512013-02-08 07:01:08 +00007397 if (wakeup)
7398 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007399 if (relock)
7400 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007401
7402 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007403}
7404
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007405#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7406
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007407static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7408 __u64 start, __u64 len)
7409{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007410 int ret;
7411
7412 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7413 if (ret)
7414 return ret;
7415
Chris Masonec29ed52011-02-23 16:23:20 -05007416 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007417}
7418
Chris Mason9ebefb182007-06-15 13:50:00 -04007419int btrfs_readpage(struct file *file, struct page *page)
7420{
Chris Masond1310b22008-01-24 16:13:08 -05007421 struct extent_io_tree *tree;
7422 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007423 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007424}
Chris Mason1832a6d2007-12-21 16:27:21 -05007425
Chris Mason39279cc2007-06-12 06:35:45 -04007426static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7427{
Chris Masond1310b22008-01-24 16:13:08 -05007428 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04007429
7430
7431 if (current->flags & PF_MEMALLOC) {
7432 redirty_page_for_writepage(wbc, page);
7433 unlock_page(page);
7434 return 0;
7435 }
Chris Masond1310b22008-01-24 16:13:08 -05007436 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007437 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7438}
Chris Mason39279cc2007-06-12 06:35:45 -04007439
Chris Masonf4219502008-07-22 11:18:09 -04007440int btrfs_writepages(struct address_space *mapping,
7441 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007442{
Chris Masond1310b22008-01-24 16:13:08 -05007443 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007444
Chris Masond1310b22008-01-24 16:13:08 -05007445 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007446 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7447}
7448
Chris Mason3ab2fb52007-11-08 10:59:22 -05007449static int
7450btrfs_readpages(struct file *file, struct address_space *mapping,
7451 struct list_head *pages, unsigned nr_pages)
7452{
Chris Masond1310b22008-01-24 16:13:08 -05007453 struct extent_io_tree *tree;
7454 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007455 return extent_readpages(tree, mapping, pages, nr_pages,
7456 btrfs_get_extent);
7457}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007458static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007459{
Chris Masond1310b22008-01-24 16:13:08 -05007460 struct extent_io_tree *tree;
7461 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007462 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007463
Chris Masond1310b22008-01-24 16:13:08 -05007464 tree = &BTRFS_I(page->mapping->host)->io_tree;
7465 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007466 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007467 if (ret == 1) {
7468 ClearPagePrivate(page);
7469 set_page_private(page, 0);
7470 page_cache_release(page);
7471 }
7472 return ret;
7473}
Chris Mason39279cc2007-06-12 06:35:45 -04007474
Chris Masone6dcd2d2008-07-17 12:53:50 -04007475static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7476{
Chris Mason98509cf2008-09-11 15:51:43 -04007477 if (PageWriteback(page) || PageDirty(page))
7478 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007479 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007480}
7481
Chris Masona52d9a82007-08-27 16:49:44 -04007482static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7483{
Josef Bacik5fd02042012-05-02 14:00:54 -04007484 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007485 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007486 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007487 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007488 u64 page_start = page_offset(page);
7489 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007490
Chris Mason8b62b722009-09-02 16:53:46 -04007491 /*
7492 * we have the page locked, so new writeback can't start,
7493 * and the dirty bit won't be cleared while we are here.
7494 *
7495 * Wait for IO on this page so that we can safely clear
7496 * the PagePrivate2 bit and do ordered accounting
7497 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007498 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007499
Josef Bacik5fd02042012-05-02 14:00:54 -04007500 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007501 if (offset) {
7502 btrfs_releasepage(page, GFP_NOFS);
7503 return;
7504 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007505 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007506 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007507 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007508 /*
7509 * IO on this page will never be started, so we need
7510 * to account for any ordered extents now
7511 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007512 clear_extent_bit(tree, page_start, page_end,
7513 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007514 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7515 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007516 /*
7517 * whoever cleared the private bit is responsible
7518 * for the finish_ordered_io
7519 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007520 if (TestClearPagePrivate2(page) &&
7521 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7522 PAGE_CACHE_SIZE, 1)) {
7523 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007524 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007525 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007526 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007527 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007528 }
7529 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007530 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007531 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7532 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007533 __btrfs_releasepage(page, GFP_NOFS);
7534
Chris Mason4a096752008-07-21 10:29:44 -04007535 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007536 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007537 ClearPagePrivate(page);
7538 set_page_private(page, 0);
7539 page_cache_release(page);
7540 }
Chris Mason39279cc2007-06-12 06:35:45 -04007541}
7542
Chris Mason9ebefb182007-06-15 13:50:00 -04007543/*
7544 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7545 * called from a page fault handler when a page is first dirtied. Hence we must
7546 * be careful to check for EOF conditions here. We set the page up correctly
7547 * for a written page which means we get ENOSPC checking when writing into
7548 * holes and correct delalloc and unwritten extent mapping on filesystems that
7549 * support these features.
7550 *
7551 * We are not allowed to take the i_mutex here so we have to play games to
7552 * protect against truncate races as the page could now be beyond EOF. Because
7553 * vmtruncate() writes the inode size before removing pages, once we have the
7554 * page lock we can determine safely if the page is beyond EOF. If it is not
7555 * beyond EOF, then the page is guaranteed safe against truncation until we
7556 * unlock the page.
7557 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007558int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007559{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007560 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007561 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007562 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007563 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7564 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007565 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007566 char *kaddr;
7567 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007568 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007569 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007570 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007571 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007572 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007573
Jan Karab2b5ef52012-06-12 16:20:45 +02007574 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007575 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007576 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007577 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007578 reserved = 1;
7579 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007580 if (ret) {
7581 if (ret == -ENOMEM)
7582 ret = VM_FAULT_OOM;
7583 else /* -ENOSPC, -EIO, etc */
7584 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007585 if (reserved)
7586 goto out;
7587 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007588 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007589
Nick Piggin56a76f82009-03-31 15:23:23 -07007590 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007591again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007592 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007593 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007594 page_start = page_offset(page);
7595 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007596
Chris Mason9ebefb182007-06-15 13:50:00 -04007597 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007598 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007599 /* page got truncated out from underneath us */
7600 goto out_unlock;
7601 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007602 wait_on_page_writeback(page);
7603
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007604 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007605 set_page_extent_mapped(page);
7606
Chris Masoneb84ae02008-07-17 13:53:27 -04007607 /*
7608 * we can't set the delalloc bits if there are pending ordered
7609 * extents. Drop our locks and wait for them to finish
7610 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007611 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7612 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007613 unlock_extent_cached(io_tree, page_start, page_end,
7614 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007615 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007616 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007617 btrfs_put_ordered_extent(ordered);
7618 goto again;
7619 }
7620
Josef Bacikfbf19082009-10-01 17:10:23 -04007621 /*
7622 * XXX - page_mkwrite gets called every time the page is dirtied, even
7623 * if it was already dirty, so for space accounting reasons we need to
7624 * clear any delalloc bits for the range we are fixing to save. There
7625 * is probably a better way to do this, but for now keep consistent with
7626 * prepare_pages in the normal write path.
7627 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007628 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007629 EXTENT_DIRTY | EXTENT_DELALLOC |
7630 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007631 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007632
Josef Bacik2ac55d42010-02-03 19:33:23 +00007633 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7634 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007635 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007636 unlock_extent_cached(io_tree, page_start, page_end,
7637 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007638 ret = VM_FAULT_SIGBUS;
7639 goto out_unlock;
7640 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007641 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007642
7643 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007644 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007645 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007646 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007647 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007648
Chris Masone6dcd2d2008-07-17 12:53:50 -04007649 if (zero_start != PAGE_CACHE_SIZE) {
7650 kaddr = kmap(page);
7651 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7652 flush_dcache_page(page);
7653 kunmap(page);
7654 }
Chris Mason247e7432008-07-17 12:53:51 -04007655 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007656 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007657 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007658
Chris Mason257c62e2009-10-13 13:21:08 -04007659 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7660 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007661 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007662
Josef Bacik2ac55d42010-02-03 19:33:23 +00007663 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007664
7665out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007666 if (!ret) {
7667 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007668 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007669 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007670 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007671out:
Josef Bacikec39e182012-01-12 19:10:12 -05007672 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007673out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007674 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007675 return ret;
7676}
7677
Josef Bacika41ad392011-01-31 15:30:16 -05007678static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007679{
7680 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007681 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007682 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007683 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007684 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007685 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007686 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007687
Josef Bacik2aaa6652012-08-29 14:27:18 -04007688 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007689 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007690 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007691
Chris Mason4a096752008-07-21 10:29:44 -04007692 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007693 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007694
Josef Bacikfcb80c22011-05-03 10:40:22 -04007695 /*
7696 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7697 * 3 things going on here
7698 *
7699 * 1) We need to reserve space for our orphan item and the space to
7700 * delete our orphan item. Lord knows we don't want to have a dangling
7701 * orphan item because we didn't reserve space to remove it.
7702 *
7703 * 2) We need to reserve space to update our inode.
7704 *
7705 * 3) We need to have something to cache all the space that is going to
7706 * be free'd up by the truncate operation, but also have some slack
7707 * space reserved in case it uses space during the truncate (thank you
7708 * very much snapshotting).
7709 *
7710 * And we need these to all be seperate. The fact is we can use alot of
7711 * space doing the truncate, and we have no earthly idea how much space
7712 * we will use, so we need the truncate reservation to be seperate so it
7713 * doesn't end up using space reserved for updating the inode or
7714 * removing the orphan item. We also need to be able to stop the
7715 * transaction and start a new one, which means we need to be able to
7716 * update the inode several times, and we have no idea of knowing how
7717 * many times that will be, so we can't just reserve 1 item for the
7718 * entirety of the opration, so that has to be done seperately as well.
7719 * Then there is the orphan item, which does indeed need to be held on
7720 * to for the whole operation, and we need nobody to touch this reserved
7721 * space except the orphan code.
7722 *
7723 * So that leaves us with
7724 *
7725 * 1) root->orphan_block_rsv - for the orphan deletion.
7726 * 2) rsv - for the truncate reservation, which we will steal from the
7727 * transaction reservation.
7728 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7729 * updating the inode.
7730 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007731 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007732 if (!rsv)
7733 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007734 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007735 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007736
Josef Bacik907cbce2011-08-08 13:46:15 -04007737 /*
Josef Bacik07127182011-08-19 10:29:59 -04007738 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007739 * 1 for updating the inode.
7740 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007741 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007742 if (IS_ERR(trans)) {
7743 err = PTR_ERR(trans);
7744 goto out;
7745 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007746
Josef Bacik907cbce2011-08-08 13:46:15 -04007747 /* Migrate the slack space for the truncate to our reserve */
7748 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7749 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007750 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007751
Chris Mason5a3f23d2009-03-31 13:27:11 -04007752 /*
7753 * setattr is responsible for setting the ordered_data_close flag,
7754 * but that is only tested during the last file release. That
7755 * could happen well after the next commit, leaving a great big
7756 * window where new writes may get lost if someone chooses to write
7757 * to this file after truncating to zero
7758 *
7759 * The inode doesn't have any dirty data here, and so if we commit
7760 * this is a noop. If someone immediately starts writing to the inode
7761 * it is very likely we'll catch some of their writes in this
7762 * transaction, and the commit will find this file on the ordered
7763 * data list with good things to send down.
7764 *
7765 * This is a best effort solution, there is still a window where
7766 * using truncate to replace the contents of the file will
7767 * end up with a zero length file after a crash.
7768 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007769 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7770 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007771 btrfs_add_ordered_operation(trans, root, inode);
7772
Josef Bacik5dc562c2012-08-17 13:14:17 -04007773 /*
7774 * So if we truncate and then write and fsync we normally would just
7775 * write the extents that changed, which is a problem if we need to
7776 * first truncate that entire inode. So set this flag so we write out
7777 * all of the extents in the inode to the sync log so we're completely
7778 * safe.
7779 */
7780 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007781 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007782
Yan, Zheng80825102009-11-12 09:35:36 +00007783 while (1) {
7784 ret = btrfs_truncate_inode_items(trans, root, inode,
7785 inode->i_size,
7786 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007787 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007788 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007789 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007790 }
Chris Mason39279cc2007-06-12 06:35:45 -04007791
Josef Bacikfcb80c22011-05-03 10:40:22 -04007792 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007793 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007794 if (ret) {
7795 err = ret;
7796 break;
7797 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007798
Yan, Zheng80825102009-11-12 09:35:36 +00007799 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007800 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007801
7802 trans = btrfs_start_transaction(root, 2);
7803 if (IS_ERR(trans)) {
7804 ret = err = PTR_ERR(trans);
7805 trans = NULL;
7806 break;
7807 }
7808
7809 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7810 rsv, min_size);
7811 BUG_ON(ret); /* shouldn't happen */
7812 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007813 }
7814
7815 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007816 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007817 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007818 if (ret)
7819 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007820 }
7821
Chris Mason917c16b2011-11-08 14:49:59 -05007822 if (trans) {
7823 trans->block_rsv = &root->fs_info->trans_block_rsv;
7824 ret = btrfs_update_inode(trans, root, inode);
7825 if (ret && !err)
7826 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007827
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007828 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007829 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007830 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007831
7832out:
7833 btrfs_free_block_rsv(root, rsv);
7834
Josef Bacik3893e332011-01-31 16:03:11 -05007835 if (ret && !err)
7836 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007837
Josef Bacik3893e332011-01-31 16:03:11 -05007838 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007839}
7840
Sven Wegener3b963622008-06-09 21:57:42 -04007841/*
Chris Masond352ac62008-09-29 15:18:18 -04007842 * create a new subvolume directory/inode (helper for the ioctl).
7843 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007844int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007845 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007846{
Chris Mason39279cc2007-06-12 06:35:45 -04007847 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007848 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007849 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007850
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007851 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7852 new_dirid, new_dirid,
7853 S_IFDIR | (~current_umask() & S_IRWXUGO),
7854 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007855 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007856 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007857 inode->i_op = &btrfs_dir_inode_operations;
7858 inode->i_fop = &btrfs_dir_file_operations;
7859
Miklos Szeredibfe86842011-10-28 14:13:29 +02007860 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007861 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007862
Yan, Zheng76dda932009-09-21 16:00:26 -04007863 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007864
Yan, Zheng76dda932009-09-21 16:00:26 -04007865 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007866 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007867}
7868
Chris Mason39279cc2007-06-12 06:35:45 -04007869struct inode *btrfs_alloc_inode(struct super_block *sb)
7870{
7871 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007872 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007873
7874 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7875 if (!ei)
7876 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007877
7878 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007879 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007880 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007881 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007882 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007883 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007884 ei->disk_i_size = 0;
7885 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007886 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007887 ei->index_cnt = (u64)-1;
7888 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007889 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007890
Josef Bacik9e0baf62011-07-15 15:16:44 +00007891 spin_lock_init(&ei->lock);
7892 ei->outstanding_extents = 0;
7893 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007894
Josef Bacik72ac3c02012-05-23 14:13:11 -04007895 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007896 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007897
Miao Xie16cdcec2011-04-22 18:12:22 +08007898 ei->delayed_node = NULL;
7899
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007900 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007901 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007902 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7903 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007904 ei->io_tree.track_uptodate = 1;
7905 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007906 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007907 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007908 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007909 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007910 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007911 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007912 RB_CLEAR_NODE(&ei->rb_node);
7913
7914 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007915}
7916
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007917static void btrfs_i_callback(struct rcu_head *head)
7918{
7919 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007920 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7921}
7922
Chris Mason39279cc2007-06-12 06:35:45 -04007923void btrfs_destroy_inode(struct inode *inode)
7924{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007925 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007926 struct btrfs_root *root = BTRFS_I(inode)->root;
7927
Al Virob3d9b7a2012-06-09 13:51:19 -04007928 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007929 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007930 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7931 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007932 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7933 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007934
Chris Mason5a3f23d2009-03-31 13:27:11 -04007935 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007936 * This can happen where we create an inode, but somebody else also
7937 * created the same inode and we need to destroy the one we already
7938 * created.
7939 */
7940 if (!root)
7941 goto free;
7942
7943 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007944 * Make sure we're properly removed from the ordered operation
7945 * lists.
7946 */
7947 smp_mb();
7948 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7949 spin_lock(&root->fs_info->ordered_extent_lock);
7950 list_del_init(&BTRFS_I(inode)->ordered_operations);
7951 spin_unlock(&root->fs_info->ordered_extent_lock);
7952 }
7953
Josef Bacik8a35d952012-05-23 14:26:42 -04007954 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7955 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007956 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7957 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007958 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007959 }
Josef Bacik7b128762008-07-24 12:17:14 -04007960
Chris Masond3977122009-01-05 21:25:51 -05007961 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007962 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7963 if (!ordered)
7964 break;
7965 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007966 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7967 (unsigned long long)ordered->file_offset,
7968 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007969 btrfs_remove_ordered_extent(inode, ordered);
7970 btrfs_put_ordered_extent(ordered);
7971 btrfs_put_ordered_extent(ordered);
7972 }
7973 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007974 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007975 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007976free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007977 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007978 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007979}
7980
Al Viro45321ac2010-06-07 13:43:19 -04007981int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007982{
7983 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007984
Liu Bofa6ac872013-02-20 14:10:23 +00007985 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007986 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007987 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007988 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007989 else
Al Viro45321ac2010-06-07 13:43:19 -04007990 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007991}
7992
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007993static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007994{
7995 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7996
7997 inode_init_once(&ei->vfs_inode);
7998}
7999
8000void btrfs_destroy_cachep(void)
8001{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008002 /*
8003 * Make sure all delayed rcu free inodes are flushed before we
8004 * destroy cache.
8005 */
8006 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008007 if (btrfs_inode_cachep)
8008 kmem_cache_destroy(btrfs_inode_cachep);
8009 if (btrfs_trans_handle_cachep)
8010 kmem_cache_destroy(btrfs_trans_handle_cachep);
8011 if (btrfs_transaction_cachep)
8012 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008013 if (btrfs_path_cachep)
8014 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008015 if (btrfs_free_space_cachep)
8016 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008017 if (btrfs_delalloc_work_cachep)
8018 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008019}
8020
8021int btrfs_init_cachep(void)
8022{
David Sterba837e1972012-09-07 03:00:48 -06008023 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008024 sizeof(struct btrfs_inode), 0,
8025 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008026 if (!btrfs_inode_cachep)
8027 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008028
David Sterba837e1972012-09-07 03:00:48 -06008029 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008030 sizeof(struct btrfs_trans_handle), 0,
8031 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008032 if (!btrfs_trans_handle_cachep)
8033 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008034
David Sterba837e1972012-09-07 03:00:48 -06008035 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008036 sizeof(struct btrfs_transaction), 0,
8037 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008038 if (!btrfs_transaction_cachep)
8039 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008040
David Sterba837e1972012-09-07 03:00:48 -06008041 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008042 sizeof(struct btrfs_path), 0,
8043 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008044 if (!btrfs_path_cachep)
8045 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008046
David Sterba837e1972012-09-07 03:00:48 -06008047 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008048 sizeof(struct btrfs_free_space), 0,
8049 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8050 if (!btrfs_free_space_cachep)
8051 goto fail;
8052
Miao Xie8ccf6f192012-10-25 09:28:04 +00008053 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8054 sizeof(struct btrfs_delalloc_work), 0,
8055 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8056 NULL);
8057 if (!btrfs_delalloc_work_cachep)
8058 goto fail;
8059
Chris Mason39279cc2007-06-12 06:35:45 -04008060 return 0;
8061fail:
8062 btrfs_destroy_cachep();
8063 return -ENOMEM;
8064}
8065
8066static int btrfs_getattr(struct vfsmount *mnt,
8067 struct dentry *dentry, struct kstat *stat)
8068{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008069 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008070 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008071 u32 blocksize = inode->i_sb->s_blocksize;
8072
Chris Mason39279cc2007-06-12 06:35:45 -04008073 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008074 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008075 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008076
8077 spin_lock(&BTRFS_I(inode)->lock);
8078 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8079 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008080 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008081 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008082 return 0;
8083}
8084
Chris Masond3977122009-01-05 21:25:51 -05008085static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8086 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008087{
8088 struct btrfs_trans_handle *trans;
8089 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008090 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008091 struct inode *new_inode = new_dentry->d_inode;
8092 struct inode *old_inode = old_dentry->d_inode;
8093 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008094 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008095 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008096 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008097 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008098
Li Zefan33345d012011-04-20 10:31:50 +08008099 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008100 return -EPERM;
8101
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008102 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008103 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008104 return -EXDEV;
8105
Li Zefan33345d012011-04-20 10:31:50 +08008106 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8107 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008108 return -ENOTEMPTY;
8109
Chris Mason39279cc2007-06-12 06:35:45 -04008110 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008111 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008112 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008113
8114
8115 /* check for collisions, even if the name isn't there */
8116 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8117 new_dentry->d_name.name,
8118 new_dentry->d_name.len);
8119
8120 if (ret) {
8121 if (ret == -EEXIST) {
8122 /* we shouldn't get
8123 * eexist without a new_inode */
8124 if (!new_inode) {
8125 WARN_ON(1);
8126 return ret;
8127 }
8128 } else {
8129 /* maybe -EOVERFLOW */
8130 return ret;
8131 }
8132 }
8133 ret = 0;
8134
Chris Mason5a3f23d2009-03-31 13:27:11 -04008135 /*
8136 * we're using rename to replace one file with another.
8137 * and the replacement file is large. Start IO on it now so
8138 * we don't add too much work to the end of the transaction
8139 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008140 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008141 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8142 filemap_flush(old_inode->i_mapping);
8143
Yan, Zheng76dda932009-09-21 16:00:26 -04008144 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008145 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008146 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008147 /*
8148 * We want to reserve the absolute worst case amount of items. So if
8149 * both inodes are subvols and we need to unlink them then that would
8150 * require 4 item modifications, but if they are both normal inodes it
8151 * would require 5 item modifications, so we'll assume their normal
8152 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8153 * should cover the worst case number of items we'll modify.
8154 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008155 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008156 if (IS_ERR(trans)) {
8157 ret = PTR_ERR(trans);
8158 goto out_notrans;
8159 }
Chris Mason5f39d392007-10-15 16:14:19 -04008160
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008161 if (dest != root)
8162 btrfs_record_root_in_trans(trans, dest);
8163
Yan, Zhenga5719522009-09-24 09:17:31 -04008164 ret = btrfs_set_inode_index(new_dir, &index);
8165 if (ret)
8166 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008167
Li Zefan33345d012011-04-20 10:31:50 +08008168 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008169 /* force full log commit if subvolume involved. */
8170 root->fs_info->last_trans_log_full_commit = trans->transid;
8171 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008172 ret = btrfs_insert_inode_ref(trans, dest,
8173 new_dentry->d_name.name,
8174 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008175 old_ino,
8176 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008177 if (ret)
8178 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008179 /*
8180 * this is an ugly little race, but the rename is required
8181 * to make sure that if we crash, the inode is either at the
8182 * old name or the new one. pinning the log transaction lets
8183 * us make sure we don't allow a log commit to come in after
8184 * we unlink the name but before we add the new name back in.
8185 */
8186 btrfs_pin_log_trans(root);
8187 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008188 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008189 * make sure the inode gets flushed if it is replacing
8190 * something.
8191 */
Li Zefan33345d012011-04-20 10:31:50 +08008192 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008193 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008194
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008195 inode_inc_iversion(old_dir);
8196 inode_inc_iversion(new_dir);
8197 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008198 old_dir->i_ctime = old_dir->i_mtime = ctime;
8199 new_dir->i_ctime = new_dir->i_mtime = ctime;
8200 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008201
Chris Mason12fcfd22009-03-24 10:24:20 -04008202 if (old_dentry->d_parent != new_dentry->d_parent)
8203 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8204
Li Zefan33345d012011-04-20 10:31:50 +08008205 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008206 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8207 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8208 old_dentry->d_name.name,
8209 old_dentry->d_name.len);
8210 } else {
Al Viro92986792011-03-04 17:14:37 +00008211 ret = __btrfs_unlink_inode(trans, root, old_dir,
8212 old_dentry->d_inode,
8213 old_dentry->d_name.name,
8214 old_dentry->d_name.len);
8215 if (!ret)
8216 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008217 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008218 if (ret) {
8219 btrfs_abort_transaction(trans, root, ret);
8220 goto out_fail;
8221 }
Chris Mason39279cc2007-06-12 06:35:45 -04008222
8223 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008224 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008225 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008226 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008227 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8228 root_objectid = BTRFS_I(new_inode)->location.objectid;
8229 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8230 root_objectid,
8231 new_dentry->d_name.name,
8232 new_dentry->d_name.len);
8233 BUG_ON(new_inode->i_nlink == 0);
8234 } else {
8235 ret = btrfs_unlink_inode(trans, dest, new_dir,
8236 new_dentry->d_inode,
8237 new_dentry->d_name.name,
8238 new_dentry->d_name.len);
8239 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008240 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008241 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008242 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008243 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008244 if (ret) {
8245 btrfs_abort_transaction(trans, root, ret);
8246 goto out_fail;
8247 }
Chris Mason39279cc2007-06-12 06:35:45 -04008248 }
Josef Bacikaec74772008-07-24 12:12:38 -04008249
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008250 ret = btrfs_add_link(trans, new_dir, old_inode,
8251 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008252 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008253 if (ret) {
8254 btrfs_abort_transaction(trans, root, ret);
8255 goto out_fail;
8256 }
Chris Mason39279cc2007-06-12 06:35:45 -04008257
Li Zefan33345d012011-04-20 10:31:50 +08008258 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008259 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008260 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008261 btrfs_end_log_trans(root);
8262 }
Chris Mason39279cc2007-06-12 06:35:45 -04008263out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008264 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008265out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008266 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008267 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008268
Chris Mason39279cc2007-06-12 06:35:45 -04008269 return ret;
8270}
8271
Miao Xie8ccf6f192012-10-25 09:28:04 +00008272static void btrfs_run_delalloc_work(struct btrfs_work *work)
8273{
8274 struct btrfs_delalloc_work *delalloc_work;
8275
8276 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8277 work);
8278 if (delalloc_work->wait)
8279 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8280 else
8281 filemap_flush(delalloc_work->inode->i_mapping);
8282
8283 if (delalloc_work->delay_iput)
8284 btrfs_add_delayed_iput(delalloc_work->inode);
8285 else
8286 iput(delalloc_work->inode);
8287 complete(&delalloc_work->completion);
8288}
8289
8290struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8291 int wait, int delay_iput)
8292{
8293 struct btrfs_delalloc_work *work;
8294
8295 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8296 if (!work)
8297 return NULL;
8298
8299 init_completion(&work->completion);
8300 INIT_LIST_HEAD(&work->list);
8301 work->inode = inode;
8302 work->wait = wait;
8303 work->delay_iput = delay_iput;
8304 work->work.func = btrfs_run_delalloc_work;
8305
8306 return work;
8307}
8308
8309void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8310{
8311 wait_for_completion(&work->completion);
8312 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8313}
8314
Chris Masond352ac62008-09-29 15:18:18 -04008315/*
8316 * some fairly slow code that needs optimization. This walks the list
8317 * of all the inodes with pending delalloc and forces them to disk.
8318 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008319int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008320{
Chris Masonea8c2812008-08-04 23:17:27 -04008321 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008322 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008323 struct btrfs_delalloc_work *work, *next;
8324 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008325 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008326 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008327
Yan Zhengc146afa2008-11-12 14:34:12 -05008328 if (root->fs_info->sb->s_flags & MS_RDONLY)
8329 return -EROFS;
8330
Miao Xie8ccf6f192012-10-25 09:28:04 +00008331 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008332 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008333
Chris Mason75eff682008-12-15 15:54:40 -05008334 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008335 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8336 while (!list_empty(&splice)) {
8337 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008338 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008339
8340 list_del_init(&binode->delalloc_inodes);
8341
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008342 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008343 if (!inode) {
8344 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8345 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008346 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008347 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008348
8349 list_add_tail(&binode->delalloc_inodes,
8350 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008351 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008352
8353 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8354 if (unlikely(!work)) {
8355 ret = -ENOMEM;
8356 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008357 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008358 list_add_tail(&work->list, &works);
8359 btrfs_queue_worker(&root->fs_info->flush_workers,
8360 &work->work);
8361
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008362 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008363 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008364 }
Chris Mason75eff682008-12-15 15:54:40 -05008365 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008366
Miao Xie1eafa6c2013-01-22 10:49:00 +00008367 list_for_each_entry_safe(work, next, &works, list) {
8368 list_del_init(&work->list);
8369 btrfs_wait_and_free_delalloc_work(work);
8370 }
8371
Chris Mason8c8bee12008-09-29 11:19:10 -04008372 /* the filemap_flush will queue IO into the worker threads, but
8373 * we have to make sure the IO is actually started and that
8374 * ordered extents get created before we return
8375 */
8376 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008377 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008378 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008379 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008380 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8381 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008382 }
8383 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008384 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008385out:
8386 list_for_each_entry_safe(work, next, &works, list) {
8387 list_del_init(&work->list);
8388 btrfs_wait_and_free_delalloc_work(work);
8389 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008390
8391 if (!list_empty_careful(&splice)) {
8392 spin_lock(&root->fs_info->delalloc_lock);
8393 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8394 spin_unlock(&root->fs_info->delalloc_lock);
8395 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008396 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008397}
8398
Chris Mason39279cc2007-06-12 06:35:45 -04008399static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8400 const char *symname)
8401{
8402 struct btrfs_trans_handle *trans;
8403 struct btrfs_root *root = BTRFS_I(dir)->root;
8404 struct btrfs_path *path;
8405 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008406 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008407 int err;
8408 int drop_inode = 0;
8409 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008410 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008411 int name_len;
8412 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008413 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008414 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008415 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008416
8417 name_len = strlen(symname) + 1;
8418 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8419 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008420
Josef Bacik9ed74f22009-09-11 16:12:44 -04008421 /*
8422 * 2 items for inode item and ref
8423 * 2 items for dir items
8424 * 1 item for xattr if selinux is on
8425 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008426 trans = btrfs_start_transaction(root, 5);
8427 if (IS_ERR(trans))
8428 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008429
Li Zefan581bb052011-04-20 10:06:11 +08008430 err = btrfs_find_free_ino(root, &objectid);
8431 if (err)
8432 goto out_unlock;
8433
Josef Bacikaec74772008-07-24 12:12:38 -04008434 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008435 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008436 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008437 if (IS_ERR(inode)) {
8438 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008439 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008440 }
Chris Mason39279cc2007-06-12 06:35:45 -04008441
Eric Paris2a7dba32011-02-01 11:05:39 -05008442 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008443 if (err) {
8444 drop_inode = 1;
8445 goto out_unlock;
8446 }
8447
Casey Schauflerad19db72011-12-15 10:09:07 -05008448 /*
8449 * If the active LSM wants to access the inode during
8450 * d_instantiate it needs these. Smack checks to see
8451 * if the filesystem supports xattrs by looking at the
8452 * ops vector.
8453 */
8454 inode->i_fop = &btrfs_file_operations;
8455 inode->i_op = &btrfs_file_inode_operations;
8456
Josef Bacika1b075d2010-11-19 20:36:11 +00008457 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008458 if (err)
8459 drop_inode = 1;
8460 else {
8461 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008462 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008463 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008464 }
Chris Mason39279cc2007-06-12 06:35:45 -04008465 if (drop_inode)
8466 goto out_unlock;
8467
8468 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008469 if (!path) {
8470 err = -ENOMEM;
8471 drop_inode = 1;
8472 goto out_unlock;
8473 }
Li Zefan33345d012011-04-20 10:31:50 +08008474 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008475 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008476 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8477 datasize = btrfs_file_extent_calc_inline_size(name_len);
8478 err = btrfs_insert_empty_item(trans, root, path, &key,
8479 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008480 if (err) {
8481 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008482 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008483 goto out_unlock;
8484 }
Chris Mason5f39d392007-10-15 16:14:19 -04008485 leaf = path->nodes[0];
8486 ei = btrfs_item_ptr(leaf, path->slots[0],
8487 struct btrfs_file_extent_item);
8488 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8489 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008490 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008491 btrfs_set_file_extent_encryption(leaf, ei, 0);
8492 btrfs_set_file_extent_compression(leaf, ei, 0);
8493 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8494 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8495
Chris Mason39279cc2007-06-12 06:35:45 -04008496 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008497 write_extent_buffer(leaf, symname, ptr, name_len);
8498 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008499 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008500
Chris Mason39279cc2007-06-12 06:35:45 -04008501 inode->i_op = &btrfs_symlink_inode_operations;
8502 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008503 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008504 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008505 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008506 err = btrfs_update_inode(trans, root, inode);
8507 if (err)
8508 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008509
8510out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008511 if (!err)
8512 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008513 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008514 if (drop_inode) {
8515 inode_dec_link_count(inode);
8516 iput(inode);
8517 }
Liu Bob53d3f52012-11-14 14:34:34 +00008518 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008519 return err;
8520}
Chris Mason16432982008-04-10 10:23:21 -04008521
Josef Bacik0af3d002010-06-21 14:48:16 -04008522static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8523 u64 start, u64 num_bytes, u64 min_size,
8524 loff_t actual_len, u64 *alloc_hint,
8525 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008526{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008527 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8528 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008529 struct btrfs_root *root = BTRFS_I(inode)->root;
8530 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008531 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008532 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008533 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008534 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008535 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008536
Josef Bacik0af3d002010-06-21 14:48:16 -04008537 if (trans)
8538 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008539 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008540 if (own_trans) {
8541 trans = btrfs_start_transaction(root, 3);
8542 if (IS_ERR(trans)) {
8543 ret = PTR_ERR(trans);
8544 break;
8545 }
Yan Zhengd899e052008-10-30 14:25:28 -04008546 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008547
Chris Mason154ea282013-03-05 11:11:26 -05008548 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8549 cur_bytes = max(cur_bytes, min_size);
8550 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008551 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008552 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008553 if (own_trans)
8554 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008555 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008556 }
8557
Yan Zhengd899e052008-10-30 14:25:28 -04008558 ret = insert_reserved_file_extent(trans, inode,
8559 cur_offset, ins.objectid,
8560 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008561 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008562 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008563 if (ret) {
8564 btrfs_abort_transaction(trans, root, ret);
8565 if (own_trans)
8566 btrfs_end_transaction(trans, root);
8567 break;
8568 }
Chris Masona1ed8352009-09-11 12:27:37 -04008569 btrfs_drop_extent_cache(inode, cur_offset,
8570 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008571
Josef Bacik5dc562c2012-08-17 13:14:17 -04008572 em = alloc_extent_map();
8573 if (!em) {
8574 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8575 &BTRFS_I(inode)->runtime_flags);
8576 goto next;
8577 }
8578
8579 em->start = cur_offset;
8580 em->orig_start = cur_offset;
8581 em->len = ins.offset;
8582 em->block_start = ins.objectid;
8583 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008584 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008585 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008586 em->bdev = root->fs_info->fs_devices->latest_bdev;
8587 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8588 em->generation = trans->transid;
8589
8590 while (1) {
8591 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008592 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008593 write_unlock(&em_tree->lock);
8594 if (ret != -EEXIST)
8595 break;
8596 btrfs_drop_extent_cache(inode, cur_offset,
8597 cur_offset + ins.offset - 1,
8598 0);
8599 }
8600 free_extent_map(em);
8601next:
Yan Zhengd899e052008-10-30 14:25:28 -04008602 num_bytes -= ins.offset;
8603 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008604 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008605
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008606 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008607 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008608 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008609 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008610 (actual_len > inode->i_size) &&
8611 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008612 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008613 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008614 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008615 i_size = cur_offset;
8616 i_size_write(inode, i_size);
8617 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008618 }
8619
Yan Zhengd899e052008-10-30 14:25:28 -04008620 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008621
8622 if (ret) {
8623 btrfs_abort_transaction(trans, root, ret);
8624 if (own_trans)
8625 btrfs_end_transaction(trans, root);
8626 break;
8627 }
Yan Zhengd899e052008-10-30 14:25:28 -04008628
Josef Bacik0af3d002010-06-21 14:48:16 -04008629 if (own_trans)
8630 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008631 }
Yan Zhengd899e052008-10-30 14:25:28 -04008632 return ret;
8633}
8634
Josef Bacik0af3d002010-06-21 14:48:16 -04008635int btrfs_prealloc_file_range(struct inode *inode, int mode,
8636 u64 start, u64 num_bytes, u64 min_size,
8637 loff_t actual_len, u64 *alloc_hint)
8638{
8639 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8640 min_size, actual_len, alloc_hint,
8641 NULL);
8642}
8643
8644int btrfs_prealloc_file_range_trans(struct inode *inode,
8645 struct btrfs_trans_handle *trans, int mode,
8646 u64 start, u64 num_bytes, u64 min_size,
8647 loff_t actual_len, u64 *alloc_hint)
8648{
8649 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8650 min_size, actual_len, alloc_hint, trans);
8651}
8652
Chris Masone6dcd2d2008-07-17 12:53:50 -04008653static int btrfs_set_page_dirty(struct page *page)
8654{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008655 return __set_page_dirty_nobuffers(page);
8656}
8657
Al Viro10556cb2011-06-20 19:28:19 -04008658static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008659{
Li Zefanb83cc962010-12-20 16:04:08 +08008660 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008661 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008662
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008663 if (mask & MAY_WRITE &&
8664 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8665 if (btrfs_root_readonly(root))
8666 return -EROFS;
8667 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8668 return -EACCES;
8669 }
Al Viro2830ba72011-06-20 19:16:29 -04008670 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008671}
Chris Mason39279cc2007-06-12 06:35:45 -04008672
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008673static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008674 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008675 .lookup = btrfs_lookup,
8676 .create = btrfs_create,
8677 .unlink = btrfs_unlink,
8678 .link = btrfs_link,
8679 .mkdir = btrfs_mkdir,
8680 .rmdir = btrfs_rmdir,
8681 .rename = btrfs_rename,
8682 .symlink = btrfs_symlink,
8683 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008684 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008685 .setxattr = btrfs_setxattr,
8686 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008687 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008688 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008689 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008690 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008691};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008692static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008693 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008694 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008695 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008696};
Yan, Zheng76dda932009-09-21 16:00:26 -04008697
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008698static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008699 .llseek = generic_file_llseek,
8700 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008701 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008702 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008703#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008704 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008705#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008706 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008707 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008708};
8709
Chris Masond1310b22008-01-24 16:13:08 -05008710static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008711 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008712 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008713 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008714 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008715 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008716 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008717 .set_bit_hook = btrfs_set_bit_hook,
8718 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008719 .merge_extent_hook = btrfs_merge_extent_hook,
8720 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008721};
8722
Chris Mason35054392009-01-21 13:11:13 -05008723/*
8724 * btrfs doesn't support the bmap operation because swapfiles
8725 * use bmap to make a mapping of extents in the file. They assume
8726 * these extents won't change over the life of the file and they
8727 * use the bmap result to do IO directly to the drive.
8728 *
8729 * the btrfs bmap call would return logical addresses that aren't
8730 * suitable for IO and they also will change frequently as COW
8731 * operations happen. So, swapfile + btrfs == corruption.
8732 *
8733 * For now we're avoiding this by dropping bmap.
8734 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008735static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008736 .readpage = btrfs_readpage,
8737 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008738 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008739 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008740 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008741 .invalidatepage = btrfs_invalidatepage,
8742 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008743 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008744 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008745};
8746
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008747static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008748 .readpage = btrfs_readpage,
8749 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008750 .invalidatepage = btrfs_invalidatepage,
8751 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008752};
8753
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008754static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008755 .getattr = btrfs_getattr,
8756 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008757 .setxattr = btrfs_setxattr,
8758 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008759 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008760 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008761 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008762 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008763 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008764 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008765};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008766static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008767 .getattr = btrfs_getattr,
8768 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008769 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008770 .setxattr = btrfs_setxattr,
8771 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008772 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008773 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008774 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008775 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008776};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008777static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008778 .readlink = generic_readlink,
8779 .follow_link = page_follow_link_light,
8780 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008781 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008782 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008783 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008784 .setxattr = btrfs_setxattr,
8785 .getxattr = btrfs_getxattr,
8786 .listxattr = btrfs_listxattr,
8787 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008788 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008789 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008790};
Yan, Zheng76dda932009-09-21 16:00:26 -04008791
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008792const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008793 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008794 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008795};