blob: 898da0a01e040b2bb263f34275ec8afa50265d16 [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050045#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Chris Mason39279cc2007-06-12 06:35:45 -040060
61struct btrfs_iget_args {
62 u64 ino;
63 struct btrfs_root *root;
64};
65
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070066static const struct inode_operations btrfs_dir_inode_operations;
67static const struct inode_operations btrfs_symlink_inode_operations;
68static const struct inode_operations btrfs_dir_ro_inode_operations;
69static const struct inode_operations btrfs_special_inode_operations;
70static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070071static const struct address_space_operations btrfs_aops;
72static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070073static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050074static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040075
76static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000077static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078struct kmem_cache *btrfs_trans_handle_cachep;
79struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050081struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082
83#define S_SHIFT 12
84static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
85 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
86 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
87 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
88 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
89 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
90 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
91 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
92};
93
Eric Sandeen3972f262013-01-12 02:57:22 +000094static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050095static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040096static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050097static noinline int cow_file_range(struct inode *inode,
98 struct page *locked_page,
99 u64 start, u64 end, int *page_started,
100 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400101static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
102 u64 len, u64 orig_start,
103 u64 block_start, u64 block_len,
104 u64 orig_block_len, 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 Masonb888db22007-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 Masonb888db22007-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 Masonf03d9302009-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 Masonf03d9302009-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;
Chris Mason771ed682008-11-06 22:02:51 -0500726 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800727 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500728 set_bit(EXTENT_FLAG_PINNED, &em->flags);
729 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400730 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500731
Chris Masond3977122009-01-05 21:25:51 -0500732 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400733 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500734 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400735 if (!ret)
736 list_move(&em->list,
737 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400738 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500739 if (ret != -EEXIST) {
740 free_extent_map(em);
741 break;
742 }
743 btrfs_drop_extent_cache(inode, async_extent->start,
744 async_extent->start +
745 async_extent->ram_size - 1, 0);
746 }
747
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500748 if (ret)
749 goto out_free_reserve;
750
Li Zefan261507a02010-12-17 14:21:50 +0800751 ret = btrfs_add_ordered_extent_compress(inode,
752 async_extent->start,
753 ins.objectid,
754 async_extent->ram_size,
755 ins.offset,
756 BTRFS_ORDERED_COMPRESSED,
757 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500758 if (ret)
759 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500760
Chris Mason771ed682008-11-06 22:02:51 -0500761 /*
762 * clear dirty, set writeback and unlock the pages.
763 */
764 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400765 &BTRFS_I(inode)->io_tree,
766 async_extent->start,
767 async_extent->start +
768 async_extent->ram_size - 1,
769 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
770 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400771 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400772 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500773
774 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500775 async_extent->start,
776 async_extent->ram_size,
777 ins.objectid,
778 ins.offset, async_extent->pages,
779 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500780 alloc_hint = ins.objectid + ins.offset;
781 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500782 if (ret)
783 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500784 cond_resched();
785 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100786 ret = 0;
787out:
788 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500789out_free_reserve:
790 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100791out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500792 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
793 async_extent->start,
794 async_extent->start +
795 async_extent->ram_size - 1,
796 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
797 EXTENT_CLEAR_UNLOCK |
798 EXTENT_CLEAR_DELALLOC |
799 EXTENT_CLEAR_DIRTY |
800 EXTENT_SET_WRITEBACK |
801 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100802 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500803 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500804}
805
Josef Bacik4b46fce2010-05-23 11:00:55 -0400806static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
807 u64 num_bytes)
808{
809 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
810 struct extent_map *em;
811 u64 alloc_hint = 0;
812
813 read_lock(&em_tree->lock);
814 em = search_extent_mapping(em_tree, start, num_bytes);
815 if (em) {
816 /*
817 * if block start isn't an actual block number then find the
818 * first block in this inode and use that as a hint. If that
819 * block is also bogus then just don't worry about it.
820 */
821 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
822 free_extent_map(em);
823 em = search_extent_mapping(em_tree, 0, 0);
824 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
825 alloc_hint = em->block_start;
826 if (em)
827 free_extent_map(em);
828 } else {
829 alloc_hint = em->block_start;
830 free_extent_map(em);
831 }
832 }
833 read_unlock(&em_tree->lock);
834
835 return alloc_hint;
836}
837
Chris Mason771ed682008-11-06 22:02:51 -0500838/*
839 * when extent_io.c finds a delayed allocation range in the file,
840 * the call backs end up in this code. The basic idea is to
841 * allocate extents on disk for the range, and create ordered data structs
842 * in ram to track those extents.
843 *
844 * locked_page is the page that writepage had locked already. We use
845 * it to make sure we don't do extra locks or unlocks.
846 *
847 * *page_started is set to one if we unlock locked_page and do everything
848 * required to start IO on it. It may be clean and already done with
849 * IO when we return.
850 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000851static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
852 struct inode *inode,
853 struct btrfs_root *root,
854 struct page *locked_page,
855 u64 start, u64 end, int *page_started,
856 unsigned long *nr_written,
857 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500858{
Chris Mason771ed682008-11-06 22:02:51 -0500859 u64 alloc_hint = 0;
860 u64 num_bytes;
861 unsigned long ram_size;
862 u64 disk_num_bytes;
863 u64 cur_alloc_size;
864 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500865 struct btrfs_key ins;
866 struct extent_map *em;
867 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
868 int ret = 0;
869
Liu Bo83eea1f2012-07-10 05:28:39 -0600870 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500871
Qu Wenruofda28322013-02-26 08:10:22 +0000872 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500873 num_bytes = max(blocksize, num_bytes);
874 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500875
Chris Mason4cb53002011-05-24 15:35:30 -0400876 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400877 if (num_bytes < 64 * 1024 &&
878 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400879 btrfs_add_inode_defrag(trans, inode);
880
Chris Mason771ed682008-11-06 22:02:51 -0500881 if (start == 0) {
882 /* lets try to make an inline extent */
883 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000884 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500885 if (ret == 0) {
886 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400887 &BTRFS_I(inode)->io_tree,
888 start, end, NULL,
889 EXTENT_CLEAR_UNLOCK_PAGE |
890 EXTENT_CLEAR_UNLOCK |
891 EXTENT_CLEAR_DELALLOC |
892 EXTENT_CLEAR_DIRTY |
893 EXTENT_SET_WRITEBACK |
894 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000895
Chris Mason771ed682008-11-06 22:02:51 -0500896 *nr_written = *nr_written +
897 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
898 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500899 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100900 } else if (ret < 0) {
901 btrfs_abort_transaction(trans, root, ret);
902 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500903 }
904 }
Chris Masonc8b97812008-10-29 14:49:59 -0400905
906 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200907 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400908
Josef Bacik4b46fce2010-05-23 11:00:55 -0400909 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400910 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400911
Chris Masond3977122009-01-05 21:25:51 -0500912 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400913 unsigned long op;
914
Josef Bacik287a0ab2010-03-19 18:07:23 +0000915 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400916 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500917 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500918 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100919 if (ret < 0) {
920 btrfs_abort_transaction(trans, root, ret);
921 goto out_unlock;
922 }
Chris Masond3977122009-01-05 21:25:51 -0500923
David Sterba172ddd62011-04-21 00:48:27 +0200924 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100925 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400926 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500927 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500928 ram_size = ins.offset;
929 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400930 em->mod_start = em->start;
931 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400932
Chris Masone6dcd2d2008-07-17 12:53:50 -0400933 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400934 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500935 em->orig_block_len = ins.offset;
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);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400942 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400943 if (!ret)
944 list_move(&em->list,
945 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400946 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400947 if (ret != -EEXIST) {
948 free_extent_map(em);
949 break;
950 }
951 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400952 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400953 }
954
Chris Mason98d20f62008-04-14 09:46:10 -0400955 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400956 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500957 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100958 BUG_ON(ret); /* -ENOMEM */
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);
966 goto out_unlock;
967 }
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 Masonb888db22007-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
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100995out_unlock:
996 extent_clear_unlock_delalloc(inode,
997 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400998 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100999 EXTENT_CLEAR_UNLOCK_PAGE |
1000 EXTENT_CLEAR_UNLOCK |
1001 EXTENT_CLEAR_DELALLOC |
1002 EXTENT_CLEAR_DIRTY |
1003 EXTENT_SET_WRITEBACK |
1004 EXTENT_END_WRITEBACK);
1005
1006 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001007}
Chris Masonc8b97812008-10-29 14:49:59 -04001008
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001009static noinline int cow_file_range(struct inode *inode,
1010 struct page *locked_page,
1011 u64 start, u64 end, int *page_started,
1012 unsigned long *nr_written,
1013 int unlock)
1014{
1015 struct btrfs_trans_handle *trans;
1016 struct btrfs_root *root = BTRFS_I(inode)->root;
1017 int ret;
1018
1019 trans = btrfs_join_transaction(root);
1020 if (IS_ERR(trans)) {
1021 extent_clear_unlock_delalloc(inode,
1022 &BTRFS_I(inode)->io_tree,
1023 start, end, locked_page,
1024 EXTENT_CLEAR_UNLOCK_PAGE |
1025 EXTENT_CLEAR_UNLOCK |
1026 EXTENT_CLEAR_DELALLOC |
1027 EXTENT_CLEAR_DIRTY |
1028 EXTENT_SET_WRITEBACK |
1029 EXTENT_END_WRITEBACK);
1030 return PTR_ERR(trans);
1031 }
1032 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1033
1034 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1035 page_started, nr_written, unlock);
1036
1037 btrfs_end_transaction(trans, root);
1038
1039 return ret;
1040}
1041
Chris Mason771ed682008-11-06 22:02:51 -05001042/*
1043 * work queue call back to started compression on a file and pages
1044 */
1045static noinline void async_cow_start(struct btrfs_work *work)
1046{
1047 struct async_cow *async_cow;
1048 int num_added = 0;
1049 async_cow = container_of(work, struct async_cow, work);
1050
1051 compress_file_range(async_cow->inode, async_cow->locked_page,
1052 async_cow->start, async_cow->end, async_cow,
1053 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001054 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001055 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001056 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001057 }
Chris Mason771ed682008-11-06 22:02:51 -05001058}
1059
1060/*
1061 * work queue call back to submit previously compressed pages
1062 */
1063static noinline void async_cow_submit(struct btrfs_work *work)
1064{
1065 struct async_cow *async_cow;
1066 struct btrfs_root *root;
1067 unsigned long nr_pages;
1068
1069 async_cow = container_of(work, struct async_cow, work);
1070
1071 root = async_cow->root;
1072 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1073 PAGE_CACHE_SHIFT;
1074
Josef Bacik66657b32012-08-01 15:36:24 -04001075 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001076 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001077 waitqueue_active(&root->fs_info->async_submit_wait))
1078 wake_up(&root->fs_info->async_submit_wait);
1079
Chris Masond3977122009-01-05 21:25:51 -05001080 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001081 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001082}
Chris Masonc8b97812008-10-29 14:49:59 -04001083
Chris Mason771ed682008-11-06 22:02:51 -05001084static noinline void async_cow_free(struct btrfs_work *work)
1085{
1086 struct async_cow *async_cow;
1087 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001088 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001089 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001090 kfree(async_cow);
1091}
1092
1093static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1094 u64 start, u64 end, int *page_started,
1095 unsigned long *nr_written)
1096{
1097 struct async_cow *async_cow;
1098 struct btrfs_root *root = BTRFS_I(inode)->root;
1099 unsigned long nr_pages;
1100 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001101 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001102
Chris Masona3429ab2009-10-08 12:30:20 -04001103 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1104 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001105 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001106 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001107 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001108 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001109 async_cow->root = root;
1110 async_cow->locked_page = locked_page;
1111 async_cow->start = start;
1112
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001113 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001114 cur_end = end;
1115 else
1116 cur_end = min(end, start + 512 * 1024 - 1);
1117
1118 async_cow->end = cur_end;
1119 INIT_LIST_HEAD(&async_cow->extents);
1120
1121 async_cow->work.func = async_cow_start;
1122 async_cow->work.ordered_func = async_cow_submit;
1123 async_cow->work.ordered_free = async_cow_free;
1124 async_cow->work.flags = 0;
1125
Chris Mason771ed682008-11-06 22:02:51 -05001126 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1127 PAGE_CACHE_SHIFT;
1128 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1129
1130 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1131 &async_cow->work);
1132
1133 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1134 wait_event(root->fs_info->async_submit_wait,
1135 (atomic_read(&root->fs_info->async_delalloc_pages) <
1136 limit));
1137 }
1138
Chris Masond3977122009-01-05 21:25:51 -05001139 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001140 atomic_read(&root->fs_info->async_delalloc_pages)) {
1141 wait_event(root->fs_info->async_submit_wait,
1142 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1143 0));
1144 }
1145
1146 *nr_written += nr_pages;
1147 start = cur_end + 1;
1148 }
1149 *page_started = 1;
1150 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001151}
1152
Chris Masond3977122009-01-05 21:25:51 -05001153static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001154 u64 bytenr, u64 num_bytes)
1155{
1156 int ret;
1157 struct btrfs_ordered_sum *sums;
1158 LIST_HEAD(list);
1159
Yan Zheng07d400a2009-01-06 11:42:00 -05001160 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001161 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001162 if (ret == 0 && list_empty(&list))
1163 return 0;
1164
1165 while (!list_empty(&list)) {
1166 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1167 list_del(&sums->list);
1168 kfree(sums);
1169 }
1170 return 1;
1171}
1172
Chris Masond352ac62008-09-29 15:18:18 -04001173/*
1174 * when nowcow writeback call back. This checks for snapshots or COW copies
1175 * of the extents that exist in the file, and COWs the file as required.
1176 *
1177 * If no cow copies or snapshots exist, we write directly to the existing
1178 * blocks on disk
1179 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001180static noinline int run_delalloc_nocow(struct inode *inode,
1181 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001182 u64 start, u64 end, int *page_started, int force,
1183 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001184{
Chris Masonbe20aa92007-12-17 20:14:01 -05001185 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001186 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001187 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001188 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001189 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001190 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001191 u64 cow_start;
1192 u64 cur_offset;
1193 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001194 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001195 u64 disk_bytenr;
1196 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001197 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001198 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001199 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001200 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 int nocow;
1202 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001203 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001204 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001205
1206 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001207 if (!path) {
1208 extent_clear_unlock_delalloc(inode,
1209 &BTRFS_I(inode)->io_tree,
1210 start, end, locked_page,
1211 EXTENT_CLEAR_UNLOCK_PAGE |
1212 EXTENT_CLEAR_UNLOCK |
1213 EXTENT_CLEAR_DELALLOC |
1214 EXTENT_CLEAR_DIRTY |
1215 EXTENT_SET_WRITEBACK |
1216 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001217 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001218 }
Li Zefan82d59022011-04-20 10:33:24 +08001219
Liu Bo83eea1f2012-07-10 05:28:39 -06001220 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001221
1222 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001223 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001224 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001225 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001226
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001227 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001228 extent_clear_unlock_delalloc(inode,
1229 &BTRFS_I(inode)->io_tree,
1230 start, end, locked_page,
1231 EXTENT_CLEAR_UNLOCK_PAGE |
1232 EXTENT_CLEAR_UNLOCK |
1233 EXTENT_CLEAR_DELALLOC |
1234 EXTENT_CLEAR_DIRTY |
1235 EXTENT_SET_WRITEBACK |
1236 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001237 btrfs_free_path(path);
1238 return PTR_ERR(trans);
1239 }
1240
Josef Bacik74b21072011-04-13 12:02:53 -04001241 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001242
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 cow_start = (u64)-1;
1244 cur_offset = start;
1245 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001246 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001248 if (ret < 0) {
1249 btrfs_abort_transaction(trans, root, ret);
1250 goto error;
1251 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001252 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1253 leaf = path->nodes[0];
1254 btrfs_item_key_to_cpu(leaf, &found_key,
1255 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001256 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 found_key.type == BTRFS_EXTENT_DATA_KEY)
1258 path->slots[0]--;
1259 }
1260 check_prev = 0;
1261next_slot:
1262 leaf = path->nodes[0];
1263 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1264 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001265 if (ret < 0) {
1266 btrfs_abort_transaction(trans, root, ret);
1267 goto error;
1268 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 if (ret > 0)
1270 break;
1271 leaf = path->nodes[0];
1272 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001273
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 nocow = 0;
1275 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001276 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001277 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001278
Li Zefan33345d012011-04-20 10:31:50 +08001279 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1281 found_key.offset > end)
1282 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001283
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 if (found_key.offset > cur_offset) {
1285 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001286 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 goto out_check;
1288 }
Chris Masonc31f8832008-01-08 15:46:31 -05001289
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 fi = btrfs_item_ptr(leaf, path->slots[0],
1291 struct btrfs_file_extent_item);
1292 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001293
Yan Zhengd899e052008-10-30 14:25:28 -04001294 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1295 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001297 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 extent_end = found_key.offset +
1299 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001300 disk_num_bytes =
1301 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 if (extent_end <= start) {
1303 path->slots[0]++;
1304 goto next_slot;
1305 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001306 if (disk_bytenr == 0)
1307 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 if (btrfs_file_extent_compression(leaf, fi) ||
1309 btrfs_file_extent_encryption(leaf, fi) ||
1310 btrfs_file_extent_other_encoding(leaf, fi))
1311 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001312 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1313 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001314 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001316 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001317 found_key.offset -
1318 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001319 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001320 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001321 disk_bytenr += cur_offset - found_key.offset;
1322 num_bytes = min(end + 1, extent_end) - cur_offset;
1323 /*
1324 * force cow if csum exists in the range.
1325 * this ensure that csum for a given extent are
1326 * either valid or do not exist.
1327 */
1328 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1329 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001330 nocow = 1;
1331 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1332 extent_end = found_key.offset +
1333 btrfs_file_extent_inline_len(leaf, fi);
1334 extent_end = ALIGN(extent_end, root->sectorsize);
1335 } else {
1336 BUG_ON(1);
1337 }
1338out_check:
1339 if (extent_end <= start) {
1340 path->slots[0]++;
1341 goto next_slot;
1342 }
1343 if (!nocow) {
1344 if (cow_start == (u64)-1)
1345 cow_start = cur_offset;
1346 cur_offset = extent_end;
1347 if (cur_offset > end)
1348 break;
1349 path->slots[0]++;
1350 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001351 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001352
David Sterbab3b4aa72011-04-21 01:20:15 +02001353 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001355 ret = __cow_file_range(trans, inode, root, locked_page,
1356 cow_start, found_key.offset - 1,
1357 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001358 if (ret) {
1359 btrfs_abort_transaction(trans, root, ret);
1360 goto error;
1361 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001363 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001364
Yan Zhengd899e052008-10-30 14:25:28 -04001365 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1366 struct extent_map *em;
1367 struct extent_map_tree *em_tree;
1368 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001369 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001370 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001371 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001372 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001373 em->len = num_bytes;
1374 em->block_len = num_bytes;
1375 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001376 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001377 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001378 em->mod_start = em->start;
1379 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001380 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001381 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001382 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001383 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001384 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001385 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001386 if (!ret)
1387 list_move(&em->list,
1388 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001389 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001390 if (ret != -EEXIST) {
1391 free_extent_map(em);
1392 break;
1393 }
1394 btrfs_drop_extent_cache(inode, em->start,
1395 em->start + em->len - 1, 0);
1396 }
1397 type = BTRFS_ORDERED_PREALLOC;
1398 } else {
1399 type = BTRFS_ORDERED_NOCOW;
1400 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001401
1402 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001403 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001404 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001405
Yan, Zhengefa56462010-05-16 10:49:59 -04001406 if (root->root_key.objectid ==
1407 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1408 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1409 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001410 if (ret) {
1411 btrfs_abort_transaction(trans, root, ret);
1412 goto error;
1413 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001414 }
1415
Yan Zhengd899e052008-10-30 14:25:28 -04001416 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001417 cur_offset, cur_offset + num_bytes - 1,
1418 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1419 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1420 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001421 cur_offset = extent_end;
1422 if (cur_offset > end)
1423 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001424 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001425 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001426
Josef Bacik17ca04a2012-05-31 15:58:55 -04001427 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001428 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001429 cur_offset = end;
1430 }
1431
Yan Zheng80ff3852008-10-30 14:20:02 -04001432 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001433 ret = __cow_file_range(trans, inode, root, locked_page,
1434 cow_start, end,
1435 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001436 if (ret) {
1437 btrfs_abort_transaction(trans, root, ret);
1438 goto error;
1439 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001440 }
1441
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001442error:
Miao Xiea698d0752012-09-20 01:51:59 -06001443 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444 if (!ret)
1445 ret = err;
1446
Josef Bacik17ca04a2012-05-31 15:58:55 -04001447 if (ret && cur_offset < end)
1448 extent_clear_unlock_delalloc(inode,
1449 &BTRFS_I(inode)->io_tree,
1450 cur_offset, end, locked_page,
1451 EXTENT_CLEAR_UNLOCK_PAGE |
1452 EXTENT_CLEAR_UNLOCK |
1453 EXTENT_CLEAR_DELALLOC |
1454 EXTENT_CLEAR_DIRTY |
1455 EXTENT_SET_WRITEBACK |
1456 EXTENT_END_WRITEBACK);
1457
Yan Zheng7ea394f2008-08-05 13:05:02 -04001458 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001459 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001460}
1461
Chris Masond352ac62008-09-29 15:18:18 -04001462/*
1463 * extent_io.c call back to do delayed allocation processing
1464 */
Chris Masonc8b97812008-10-29 14:49:59 -04001465static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001466 u64 start, u64 end, int *page_started,
1467 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001468{
Chris Masonbe20aa92007-12-17 20:14:01 -05001469 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001470 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001471
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001472 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001473 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001474 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001475 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001476 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001477 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001478 } else if (!btrfs_test_opt(root, COMPRESS) &&
1479 !(BTRFS_I(inode)->force_compress) &&
1480 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001481 ret = cow_file_range(inode, locked_page, start, end,
1482 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001483 } else {
1484 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1485 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001486 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001487 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001488 }
Chris Masonb888db22007-08-27 16:49:44 -04001489 return ret;
1490}
1491
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001492static void btrfs_split_extent_hook(struct inode *inode,
1493 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001494{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001495 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001496 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001497 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001498
Josef Bacik9e0baf62011-07-15 15:16:44 +00001499 spin_lock(&BTRFS_I(inode)->lock);
1500 BTRFS_I(inode)->outstanding_extents++;
1501 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001502}
1503
1504/*
1505 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1506 * extents so we can keep track of new extents that are just merged onto old
1507 * extents, such as when we are doing sequential writes, so we can properly
1508 * account for the metadata space we'll need.
1509 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001510static void btrfs_merge_extent_hook(struct inode *inode,
1511 struct extent_state *new,
1512 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001513{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001514 /* not delalloc, ignore it */
1515 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001516 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001517
Josef Bacik9e0baf62011-07-15 15:16:44 +00001518 spin_lock(&BTRFS_I(inode)->lock);
1519 BTRFS_I(inode)->outstanding_extents--;
1520 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001521}
1522
Chris Masond352ac62008-09-29 15:18:18 -04001523/*
1524 * extent_io.c set_bit_hook, used to track delayed allocation
1525 * bytes in this file, and to maintain the list of inodes that
1526 * have pending delalloc work to be done.
1527 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001528static void btrfs_set_bit_hook(struct inode *inode,
1529 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001530{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001531
Chris Mason75eff682008-12-15 15:54:40 -05001532 /*
1533 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001534 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001535 * bit, which is only set or cleared with irqs on
1536 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001537 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001538 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001539 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001540 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001541
Josef Bacik9e0baf62011-07-15 15:16:44 +00001542 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001543 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001544 } else {
1545 spin_lock(&BTRFS_I(inode)->lock);
1546 BTRFS_I(inode)->outstanding_extents++;
1547 spin_unlock(&BTRFS_I(inode)->lock);
1548 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001549
Miao Xie963d6782013-01-29 10:10:51 +00001550 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1551 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001552 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001553 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001554 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1555 &BTRFS_I(inode)->runtime_flags)) {
1556 spin_lock(&root->fs_info->delalloc_lock);
1557 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1558 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1559 &root->fs_info->delalloc_inodes);
1560 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1561 &BTRFS_I(inode)->runtime_flags);
1562 }
1563 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001564 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001565 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001566 }
Chris Mason291d6732008-01-29 15:55:23 -05001567}
1568
Chris Masond352ac62008-09-29 15:18:18 -04001569/*
1570 * extent_io.c clear_bit_hook, see set_bit_hook for why
1571 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001572static void btrfs_clear_bit_hook(struct inode *inode,
1573 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001574{
Chris Mason75eff682008-12-15 15:54:40 -05001575 /*
1576 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001577 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001578 * bit, which is only set or cleared with irqs on
1579 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001580 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001581 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001582 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001583 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001584
Josef Bacik9e0baf62011-07-15 15:16:44 +00001585 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001586 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001587 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1588 spin_lock(&BTRFS_I(inode)->lock);
1589 BTRFS_I(inode)->outstanding_extents--;
1590 spin_unlock(&BTRFS_I(inode)->lock);
1591 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001592
1593 if (*bits & EXTENT_DO_ACCOUNTING)
1594 btrfs_delalloc_release_metadata(inode, len);
1595
Josef Bacik0cb59c92010-07-02 12:14:14 -04001596 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1597 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001598 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001599
Miao Xie963d6782013-01-29 10:10:51 +00001600 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1601 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001602 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001603 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001604 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001605 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1606 &BTRFS_I(inode)->runtime_flags)) {
1607 spin_lock(&root->fs_info->delalloc_lock);
1608 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1609 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1610 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1611 &BTRFS_I(inode)->runtime_flags);
1612 }
1613 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001614 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001615 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001616 }
Chris Mason291d6732008-01-29 15:55:23 -05001617}
1618
Chris Masond352ac62008-09-29 15:18:18 -04001619/*
1620 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1621 * we don't create bios that span stripes or chunks
1622 */
David Woodhouse64a16702009-07-15 23:29:37 +01001623int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001624 size_t size, struct bio *bio,
1625 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001626{
1627 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001628 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001629 u64 length = 0;
1630 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001631 int ret;
1632
Chris Mason771ed682008-11-06 22:02:51 -05001633 if (bio_flags & EXTENT_BIO_COMPRESSED)
1634 return 0;
1635
Chris Masonf2d8d742008-04-21 10:03:05 -04001636 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001637 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001638 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001639 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001640 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001641 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001642 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001643 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001644 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001645}
1646
Chris Masond352ac62008-09-29 15:18:18 -04001647/*
1648 * in order to insert checksums into the metadata in large chunks,
1649 * we wait until bio submission time. All the pages in the bio are
1650 * checksummed and sums are attached onto the ordered extent record.
1651 *
1652 * At IO completion time the cums attached on the ordered extent record
1653 * are inserted into the btree
1654 */
Chris Masond3977122009-01-05 21:25:51 -05001655static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1656 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001657 unsigned long bio_flags,
1658 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001659{
Chris Mason065631f2008-02-20 12:07:25 -05001660 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001661 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001662
Chris Masond20f7042008-12-08 16:58:54 -05001663 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001664 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001665 return 0;
1666}
Chris Masone0156402008-04-16 11:15:20 -04001667
Chris Mason4a69a412008-11-06 22:03:00 -05001668/*
1669 * in order to insert checksums into the metadata in large chunks,
1670 * we wait until bio submission time. All the pages in the bio are
1671 * checksummed and sums are attached onto the ordered extent record.
1672 *
1673 * At IO completion time the cums attached on the ordered extent record
1674 * are inserted into the btree
1675 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001676static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001677 int mirror_num, unsigned long bio_flags,
1678 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001679{
1680 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001681 int ret;
1682
1683 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1684 if (ret)
1685 bio_endio(bio, ret);
1686 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001687}
1688
Chris Masond352ac62008-09-29 15:18:18 -04001689/*
Chris Masoncad321a2008-12-17 14:51:42 -05001690 * extent_io.c submission hook. This does the right thing for csum calculation
1691 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001692 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001693static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001694 int mirror_num, unsigned long bio_flags,
1695 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001696{
1697 struct btrfs_root *root = BTRFS_I(inode)->root;
1698 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001699 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001700 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001701 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001702
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001703 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001704
Liu Bo83eea1f2012-07-10 05:28:39 -06001705 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001706 metadata = 2;
1707
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001708 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001709 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1710 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001711 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001712
Chris Masond20f7042008-12-08 16:58:54 -05001713 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001714 ret = btrfs_submit_compressed_read(inode, bio,
1715 mirror_num,
1716 bio_flags);
1717 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001718 } else if (!skip_sum) {
1719 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1720 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001721 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001722 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001723 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001724 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001725 /* csum items have already been cloned */
1726 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1727 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001728 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001729 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001730 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001731 bio_flags, bio_offset,
1732 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001733 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001734 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001735 } else if (!skip_sum) {
1736 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1737 if (ret)
1738 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001739 }
1740
Chris Mason0b86a832008-03-24 15:01:56 -04001741mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001742 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1743
1744out:
1745 if (ret < 0)
1746 bio_endio(bio, ret);
1747 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001748}
Chris Mason6885f302008-02-20 16:11:05 -05001749
Chris Masond352ac62008-09-29 15:18:18 -04001750/*
1751 * given a list of ordered sums record them in the inode. This happens
1752 * at IO completion time based on sums calculated at bio submission time.
1753 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001754static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001755 struct inode *inode, u64 file_offset,
1756 struct list_head *list)
1757{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001758 struct btrfs_ordered_sum *sum;
1759
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001760 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001761 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001762 btrfs_csum_file_blocks(trans,
1763 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001764 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001765 }
1766 return 0;
1767}
1768
Josef Bacik2ac55d42010-02-03 19:33:23 +00001769int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1770 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001771{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001772 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001773 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001774 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001775}
1776
Chris Masond352ac62008-09-29 15:18:18 -04001777/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001778struct btrfs_writepage_fixup {
1779 struct page *page;
1780 struct btrfs_work work;
1781};
1782
Christoph Hellwigb2950862008-12-02 09:54:17 -05001783static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001784{
1785 struct btrfs_writepage_fixup *fixup;
1786 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001787 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001788 struct page *page;
1789 struct inode *inode;
1790 u64 page_start;
1791 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001792 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001793
1794 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1795 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001796again:
Chris Mason247e7432008-07-17 12:53:51 -04001797 lock_page(page);
1798 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1799 ClearPageChecked(page);
1800 goto out_page;
1801 }
1802
1803 inode = page->mapping->host;
1804 page_start = page_offset(page);
1805 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1806
Josef Bacik2ac55d42010-02-03 19:33:23 +00001807 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001808 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001809
1810 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001811 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001812 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001813
1814 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1815 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001816 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1817 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001818 unlock_page(page);
1819 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001820 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001821 goto again;
1822 }
Chris Mason247e7432008-07-17 12:53:51 -04001823
Jeff Mahoney87826df2012-02-15 16:23:57 +01001824 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1825 if (ret) {
1826 mapping_set_error(page->mapping, ret);
1827 end_extent_writepage(page, ret, page_start, page_end);
1828 ClearPageChecked(page);
1829 goto out;
1830 }
1831
Josef Bacik2ac55d42010-02-03 19:33:23 +00001832 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001833 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001834 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001835out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001836 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1837 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001838out_page:
1839 unlock_page(page);
1840 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001841 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001842}
1843
1844/*
1845 * There are a few paths in the higher layers of the kernel that directly
1846 * set the page dirty bit without asking the filesystem if it is a
1847 * good idea. This causes problems because we want to make sure COW
1848 * properly happens and the data=ordered rules are followed.
1849 *
Chris Masonc8b97812008-10-29 14:49:59 -04001850 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001851 * hasn't been properly setup for IO. We kick off an async process
1852 * to fix it up. The async helper will wait for ordered extents, set
1853 * the delalloc bit and make it safe to write the page.
1854 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001855static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001856{
1857 struct inode *inode = page->mapping->host;
1858 struct btrfs_writepage_fixup *fixup;
1859 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001860
Chris Mason8b62b722009-09-02 16:53:46 -04001861 /* this page is properly in the ordered list */
1862 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001863 return 0;
1864
1865 if (PageChecked(page))
1866 return -EAGAIN;
1867
1868 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1869 if (!fixup)
1870 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001871
Chris Mason247e7432008-07-17 12:53:51 -04001872 SetPageChecked(page);
1873 page_cache_get(page);
1874 fixup->work.func = btrfs_writepage_fixup_worker;
1875 fixup->page = page;
1876 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001877 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001878}
1879
Yan Zhengd899e052008-10-30 14:25:28 -04001880static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1881 struct inode *inode, u64 file_pos,
1882 u64 disk_bytenr, u64 disk_num_bytes,
1883 u64 num_bytes, u64 ram_bytes,
1884 u8 compression, u8 encryption,
1885 u16 other_encoding, int extent_type)
1886{
1887 struct btrfs_root *root = BTRFS_I(inode)->root;
1888 struct btrfs_file_extent_item *fi;
1889 struct btrfs_path *path;
1890 struct extent_buffer *leaf;
1891 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001892 int ret;
1893
1894 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001895 if (!path)
1896 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001897
Chris Masonb9473432009-03-13 11:00:37 -04001898 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001899
1900 /*
1901 * we may be replacing one extent in the tree with another.
1902 * The new extent is pinned in the extent map, and we don't want
1903 * to drop it from the cache until it is completely in the btree.
1904 *
1905 * So, tell btrfs_drop_extents to leave this extent in the cache.
1906 * the caller is expected to unpin it and allow it to be merged
1907 * with the others.
1908 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001909 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001910 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001911 if (ret)
1912 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001913
Li Zefan33345d012011-04-20 10:31:50 +08001914 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001915 ins.offset = file_pos;
1916 ins.type = BTRFS_EXTENT_DATA_KEY;
1917 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001918 if (ret)
1919 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001920 leaf = path->nodes[0];
1921 fi = btrfs_item_ptr(leaf, path->slots[0],
1922 struct btrfs_file_extent_item);
1923 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1924 btrfs_set_file_extent_type(leaf, fi, extent_type);
1925 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1926 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1927 btrfs_set_file_extent_offset(leaf, fi, 0);
1928 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1929 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1930 btrfs_set_file_extent_compression(leaf, fi, compression);
1931 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1932 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001933
Yan Zhengd899e052008-10-30 14:25:28 -04001934 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001935 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001936
1937 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001938
1939 ins.objectid = disk_bytenr;
1940 ins.offset = disk_num_bytes;
1941 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001942 ret = btrfs_alloc_reserved_file_extent(trans, root,
1943 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001944 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001945out:
Yan Zhengd899e052008-10-30 14:25:28 -04001946 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001947
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001948 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001949}
1950
Liu Bo38c227d2013-01-29 03:18:40 +00001951/* snapshot-aware defrag */
1952struct sa_defrag_extent_backref {
1953 struct rb_node node;
1954 struct old_sa_defrag_extent *old;
1955 u64 root_id;
1956 u64 inum;
1957 u64 file_pos;
1958 u64 extent_offset;
1959 u64 num_bytes;
1960 u64 generation;
1961};
1962
1963struct old_sa_defrag_extent {
1964 struct list_head list;
1965 struct new_sa_defrag_extent *new;
1966
1967 u64 extent_offset;
1968 u64 bytenr;
1969 u64 offset;
1970 u64 len;
1971 int count;
1972};
1973
1974struct new_sa_defrag_extent {
1975 struct rb_root root;
1976 struct list_head head;
1977 struct btrfs_path *path;
1978 struct inode *inode;
1979 u64 file_pos;
1980 u64 len;
1981 u64 bytenr;
1982 u64 disk_len;
1983 u8 compress_type;
1984};
1985
1986static int backref_comp(struct sa_defrag_extent_backref *b1,
1987 struct sa_defrag_extent_backref *b2)
1988{
1989 if (b1->root_id < b2->root_id)
1990 return -1;
1991 else if (b1->root_id > b2->root_id)
1992 return 1;
1993
1994 if (b1->inum < b2->inum)
1995 return -1;
1996 else if (b1->inum > b2->inum)
1997 return 1;
1998
1999 if (b1->file_pos < b2->file_pos)
2000 return -1;
2001 else if (b1->file_pos > b2->file_pos)
2002 return 1;
2003
2004 /*
2005 * [------------------------------] ===> (a range of space)
2006 * |<--->| |<---->| =============> (fs/file tree A)
2007 * |<---------------------------->| ===> (fs/file tree B)
2008 *
2009 * A range of space can refer to two file extents in one tree while
2010 * refer to only one file extent in another tree.
2011 *
2012 * So we may process a disk offset more than one time(two extents in A)
2013 * and locate at the same extent(one extent in B), then insert two same
2014 * backrefs(both refer to the extent in B).
2015 */
2016 return 0;
2017}
2018
2019static void backref_insert(struct rb_root *root,
2020 struct sa_defrag_extent_backref *backref)
2021{
2022 struct rb_node **p = &root->rb_node;
2023 struct rb_node *parent = NULL;
2024 struct sa_defrag_extent_backref *entry;
2025 int ret;
2026
2027 while (*p) {
2028 parent = *p;
2029 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2030
2031 ret = backref_comp(backref, entry);
2032 if (ret < 0)
2033 p = &(*p)->rb_left;
2034 else
2035 p = &(*p)->rb_right;
2036 }
2037
2038 rb_link_node(&backref->node, parent, p);
2039 rb_insert_color(&backref->node, root);
2040}
2041
2042/*
2043 * Note the backref might has changed, and in this case we just return 0.
2044 */
2045static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2046 void *ctx)
2047{
2048 struct btrfs_file_extent_item *extent;
2049 struct btrfs_fs_info *fs_info;
2050 struct old_sa_defrag_extent *old = ctx;
2051 struct new_sa_defrag_extent *new = old->new;
2052 struct btrfs_path *path = new->path;
2053 struct btrfs_key key;
2054 struct btrfs_root *root;
2055 struct sa_defrag_extent_backref *backref;
2056 struct extent_buffer *leaf;
2057 struct inode *inode = new->inode;
2058 int slot;
2059 int ret;
2060 u64 extent_offset;
2061 u64 num_bytes;
2062
2063 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2064 inum == btrfs_ino(inode))
2065 return 0;
2066
2067 key.objectid = root_id;
2068 key.type = BTRFS_ROOT_ITEM_KEY;
2069 key.offset = (u64)-1;
2070
2071 fs_info = BTRFS_I(inode)->root->fs_info;
2072 root = btrfs_read_fs_root_no_name(fs_info, &key);
2073 if (IS_ERR(root)) {
2074 if (PTR_ERR(root) == -ENOENT)
2075 return 0;
2076 WARN_ON(1);
2077 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2078 inum, offset, root_id);
2079 return PTR_ERR(root);
2080 }
2081
2082 key.objectid = inum;
2083 key.type = BTRFS_EXTENT_DATA_KEY;
2084 if (offset > (u64)-1 << 32)
2085 key.offset = 0;
2086 else
2087 key.offset = offset;
2088
2089 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2090 if (ret < 0) {
2091 WARN_ON(1);
2092 return ret;
2093 }
2094
2095 while (1) {
2096 cond_resched();
2097
2098 leaf = path->nodes[0];
2099 slot = path->slots[0];
2100
2101 if (slot >= btrfs_header_nritems(leaf)) {
2102 ret = btrfs_next_leaf(root, path);
2103 if (ret < 0) {
2104 goto out;
2105 } else if (ret > 0) {
2106 ret = 0;
2107 goto out;
2108 }
2109 continue;
2110 }
2111
2112 path->slots[0]++;
2113
2114 btrfs_item_key_to_cpu(leaf, &key, slot);
2115
2116 if (key.objectid > inum)
2117 goto out;
2118
2119 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2120 continue;
2121
2122 extent = btrfs_item_ptr(leaf, slot,
2123 struct btrfs_file_extent_item);
2124
2125 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2126 continue;
2127
2128 extent_offset = btrfs_file_extent_offset(leaf, extent);
2129 if (key.offset - extent_offset != offset)
2130 continue;
2131
2132 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2133 if (extent_offset >= old->extent_offset + old->offset +
2134 old->len || extent_offset + num_bytes <=
2135 old->extent_offset + old->offset)
2136 continue;
2137
2138 break;
2139 }
2140
2141 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2142 if (!backref) {
2143 ret = -ENOENT;
2144 goto out;
2145 }
2146
2147 backref->root_id = root_id;
2148 backref->inum = inum;
2149 backref->file_pos = offset + extent_offset;
2150 backref->num_bytes = num_bytes;
2151 backref->extent_offset = extent_offset;
2152 backref->generation = btrfs_file_extent_generation(leaf, extent);
2153 backref->old = old;
2154 backref_insert(&new->root, backref);
2155 old->count++;
2156out:
2157 btrfs_release_path(path);
2158 WARN_ON(ret);
2159 return ret;
2160}
2161
2162static noinline bool record_extent_backrefs(struct btrfs_path *path,
2163 struct new_sa_defrag_extent *new)
2164{
2165 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2166 struct old_sa_defrag_extent *old, *tmp;
2167 int ret;
2168
2169 new->path = path;
2170
2171 list_for_each_entry_safe(old, tmp, &new->head, list) {
2172 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2173 path, record_one_backref,
2174 old);
2175 BUG_ON(ret < 0 && ret != -ENOENT);
2176
2177 /* no backref to be processed for this extent */
2178 if (!old->count) {
2179 list_del(&old->list);
2180 kfree(old);
2181 }
2182 }
2183
2184 if (list_empty(&new->head))
2185 return false;
2186
2187 return true;
2188}
2189
2190static int relink_is_mergable(struct extent_buffer *leaf,
2191 struct btrfs_file_extent_item *fi,
2192 u64 disk_bytenr)
2193{
2194 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2195 return 0;
2196
2197 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2198 return 0;
2199
2200 if (btrfs_file_extent_compression(leaf, fi) ||
2201 btrfs_file_extent_encryption(leaf, fi) ||
2202 btrfs_file_extent_other_encoding(leaf, fi))
2203 return 0;
2204
2205 return 1;
2206}
2207
2208/*
2209 * Note the backref might has changed, and in this case we just return 0.
2210 */
2211static noinline int relink_extent_backref(struct btrfs_path *path,
2212 struct sa_defrag_extent_backref *prev,
2213 struct sa_defrag_extent_backref *backref)
2214{
2215 struct btrfs_file_extent_item *extent;
2216 struct btrfs_file_extent_item *item;
2217 struct btrfs_ordered_extent *ordered;
2218 struct btrfs_trans_handle *trans;
2219 struct btrfs_fs_info *fs_info;
2220 struct btrfs_root *root;
2221 struct btrfs_key key;
2222 struct extent_buffer *leaf;
2223 struct old_sa_defrag_extent *old = backref->old;
2224 struct new_sa_defrag_extent *new = old->new;
2225 struct inode *src_inode = new->inode;
2226 struct inode *inode;
2227 struct extent_state *cached = NULL;
2228 int ret = 0;
2229 u64 start;
2230 u64 len;
2231 u64 lock_start;
2232 u64 lock_end;
2233 bool merge = false;
2234 int index;
2235
2236 if (prev && prev->root_id == backref->root_id &&
2237 prev->inum == backref->inum &&
2238 prev->file_pos + prev->num_bytes == backref->file_pos)
2239 merge = true;
2240
2241 /* step 1: get root */
2242 key.objectid = backref->root_id;
2243 key.type = BTRFS_ROOT_ITEM_KEY;
2244 key.offset = (u64)-1;
2245
2246 fs_info = BTRFS_I(src_inode)->root->fs_info;
2247 index = srcu_read_lock(&fs_info->subvol_srcu);
2248
2249 root = btrfs_read_fs_root_no_name(fs_info, &key);
2250 if (IS_ERR(root)) {
2251 srcu_read_unlock(&fs_info->subvol_srcu, index);
2252 if (PTR_ERR(root) == -ENOENT)
2253 return 0;
2254 return PTR_ERR(root);
2255 }
2256 if (btrfs_root_refs(&root->root_item) == 0) {
2257 srcu_read_unlock(&fs_info->subvol_srcu, index);
2258 /* parse ENOENT to 0 */
2259 return 0;
2260 }
2261
2262 /* step 2: get inode */
2263 key.objectid = backref->inum;
2264 key.type = BTRFS_INODE_ITEM_KEY;
2265 key.offset = 0;
2266
2267 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2268 if (IS_ERR(inode)) {
2269 srcu_read_unlock(&fs_info->subvol_srcu, index);
2270 return 0;
2271 }
2272
2273 srcu_read_unlock(&fs_info->subvol_srcu, index);
2274
2275 /* step 3: relink backref */
2276 lock_start = backref->file_pos;
2277 lock_end = backref->file_pos + backref->num_bytes - 1;
2278 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2279 0, &cached);
2280
2281 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2282 if (ordered) {
2283 btrfs_put_ordered_extent(ordered);
2284 goto out_unlock;
2285 }
2286
2287 trans = btrfs_join_transaction(root);
2288 if (IS_ERR(trans)) {
2289 ret = PTR_ERR(trans);
2290 goto out_unlock;
2291 }
2292
2293 key.objectid = backref->inum;
2294 key.type = BTRFS_EXTENT_DATA_KEY;
2295 key.offset = backref->file_pos;
2296
2297 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2298 if (ret < 0) {
2299 goto out_free_path;
2300 } else if (ret > 0) {
2301 ret = 0;
2302 goto out_free_path;
2303 }
2304
2305 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2306 struct btrfs_file_extent_item);
2307
2308 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2309 backref->generation)
2310 goto out_free_path;
2311
2312 btrfs_release_path(path);
2313
2314 start = backref->file_pos;
2315 if (backref->extent_offset < old->extent_offset + old->offset)
2316 start += old->extent_offset + old->offset -
2317 backref->extent_offset;
2318
2319 len = min(backref->extent_offset + backref->num_bytes,
2320 old->extent_offset + old->offset + old->len);
2321 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2322
2323 ret = btrfs_drop_extents(trans, root, inode, start,
2324 start + len, 1);
2325 if (ret)
2326 goto out_free_path;
2327again:
2328 key.objectid = btrfs_ino(inode);
2329 key.type = BTRFS_EXTENT_DATA_KEY;
2330 key.offset = start;
2331
Liu Boa09a0a72013-03-11 09:20:58 +00002332 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002333 if (merge) {
2334 struct btrfs_file_extent_item *fi;
2335 u64 extent_len;
2336 struct btrfs_key found_key;
2337
2338 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2339 if (ret < 0)
2340 goto out_free_path;
2341
2342 path->slots[0]--;
2343 leaf = path->nodes[0];
2344 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2345
2346 fi = btrfs_item_ptr(leaf, path->slots[0],
2347 struct btrfs_file_extent_item);
2348 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2349
2350 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2351 extent_len + found_key.offset == start) {
2352 btrfs_set_file_extent_num_bytes(leaf, fi,
2353 extent_len + len);
2354 btrfs_mark_buffer_dirty(leaf);
2355 inode_add_bytes(inode, len);
2356
2357 ret = 1;
2358 goto out_free_path;
2359 } else {
2360 merge = false;
2361 btrfs_release_path(path);
2362 goto again;
2363 }
2364 }
2365
2366 ret = btrfs_insert_empty_item(trans, root, path, &key,
2367 sizeof(*extent));
2368 if (ret) {
2369 btrfs_abort_transaction(trans, root, ret);
2370 goto out_free_path;
2371 }
2372
2373 leaf = path->nodes[0];
2374 item = btrfs_item_ptr(leaf, path->slots[0],
2375 struct btrfs_file_extent_item);
2376 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2377 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2378 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2379 btrfs_set_file_extent_num_bytes(leaf, item, len);
2380 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2381 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2382 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2383 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2384 btrfs_set_file_extent_encryption(leaf, item, 0);
2385 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2386
2387 btrfs_mark_buffer_dirty(leaf);
2388 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002389 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002390
2391 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2392 new->disk_len, 0,
2393 backref->root_id, backref->inum,
2394 new->file_pos, 0); /* start - extent_offset */
2395 if (ret) {
2396 btrfs_abort_transaction(trans, root, ret);
2397 goto out_free_path;
2398 }
2399
2400 ret = 1;
2401out_free_path:
2402 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002403 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002404 btrfs_end_transaction(trans, root);
2405out_unlock:
2406 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2407 &cached, GFP_NOFS);
2408 iput(inode);
2409 return ret;
2410}
2411
2412static void relink_file_extents(struct new_sa_defrag_extent *new)
2413{
2414 struct btrfs_path *path;
2415 struct old_sa_defrag_extent *old, *tmp;
2416 struct sa_defrag_extent_backref *backref;
2417 struct sa_defrag_extent_backref *prev = NULL;
2418 struct inode *inode;
2419 struct btrfs_root *root;
2420 struct rb_node *node;
2421 int ret;
2422
2423 inode = new->inode;
2424 root = BTRFS_I(inode)->root;
2425
2426 path = btrfs_alloc_path();
2427 if (!path)
2428 return;
2429
2430 if (!record_extent_backrefs(path, new)) {
2431 btrfs_free_path(path);
2432 goto out;
2433 }
2434 btrfs_release_path(path);
2435
2436 while (1) {
2437 node = rb_first(&new->root);
2438 if (!node)
2439 break;
2440 rb_erase(node, &new->root);
2441
2442 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2443
2444 ret = relink_extent_backref(path, prev, backref);
2445 WARN_ON(ret < 0);
2446
2447 kfree(prev);
2448
2449 if (ret == 1)
2450 prev = backref;
2451 else
2452 prev = NULL;
2453 cond_resched();
2454 }
2455 kfree(prev);
2456
2457 btrfs_free_path(path);
2458
2459 list_for_each_entry_safe(old, tmp, &new->head, list) {
2460 list_del(&old->list);
2461 kfree(old);
2462 }
2463out:
2464 atomic_dec(&root->fs_info->defrag_running);
2465 wake_up(&root->fs_info->transaction_wait);
2466
2467 kfree(new);
2468}
2469
2470static struct new_sa_defrag_extent *
2471record_old_file_extents(struct inode *inode,
2472 struct btrfs_ordered_extent *ordered)
2473{
2474 struct btrfs_root *root = BTRFS_I(inode)->root;
2475 struct btrfs_path *path;
2476 struct btrfs_key key;
2477 struct old_sa_defrag_extent *old, *tmp;
2478 struct new_sa_defrag_extent *new;
2479 int ret;
2480
2481 new = kmalloc(sizeof(*new), GFP_NOFS);
2482 if (!new)
2483 return NULL;
2484
2485 new->inode = inode;
2486 new->file_pos = ordered->file_offset;
2487 new->len = ordered->len;
2488 new->bytenr = ordered->start;
2489 new->disk_len = ordered->disk_len;
2490 new->compress_type = ordered->compress_type;
2491 new->root = RB_ROOT;
2492 INIT_LIST_HEAD(&new->head);
2493
2494 path = btrfs_alloc_path();
2495 if (!path)
2496 goto out_kfree;
2497
2498 key.objectid = btrfs_ino(inode);
2499 key.type = BTRFS_EXTENT_DATA_KEY;
2500 key.offset = new->file_pos;
2501
2502 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2503 if (ret < 0)
2504 goto out_free_path;
2505 if (ret > 0 && path->slots[0] > 0)
2506 path->slots[0]--;
2507
2508 /* find out all the old extents for the file range */
2509 while (1) {
2510 struct btrfs_file_extent_item *extent;
2511 struct extent_buffer *l;
2512 int slot;
2513 u64 num_bytes;
2514 u64 offset;
2515 u64 end;
2516 u64 disk_bytenr;
2517 u64 extent_offset;
2518
2519 l = path->nodes[0];
2520 slot = path->slots[0];
2521
2522 if (slot >= btrfs_header_nritems(l)) {
2523 ret = btrfs_next_leaf(root, path);
2524 if (ret < 0)
2525 goto out_free_list;
2526 else if (ret > 0)
2527 break;
2528 continue;
2529 }
2530
2531 btrfs_item_key_to_cpu(l, &key, slot);
2532
2533 if (key.objectid != btrfs_ino(inode))
2534 break;
2535 if (key.type != BTRFS_EXTENT_DATA_KEY)
2536 break;
2537 if (key.offset >= new->file_pos + new->len)
2538 break;
2539
2540 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2541
2542 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2543 if (key.offset + num_bytes < new->file_pos)
2544 goto next;
2545
2546 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2547 if (!disk_bytenr)
2548 goto next;
2549
2550 extent_offset = btrfs_file_extent_offset(l, extent);
2551
2552 old = kmalloc(sizeof(*old), GFP_NOFS);
2553 if (!old)
2554 goto out_free_list;
2555
2556 offset = max(new->file_pos, key.offset);
2557 end = min(new->file_pos + new->len, key.offset + num_bytes);
2558
2559 old->bytenr = disk_bytenr;
2560 old->extent_offset = extent_offset;
2561 old->offset = offset - key.offset;
2562 old->len = end - offset;
2563 old->new = new;
2564 old->count = 0;
2565 list_add_tail(&old->list, &new->head);
2566next:
2567 path->slots[0]++;
2568 cond_resched();
2569 }
2570
2571 btrfs_free_path(path);
2572 atomic_inc(&root->fs_info->defrag_running);
2573
2574 return new;
2575
2576out_free_list:
2577 list_for_each_entry_safe(old, tmp, &new->head, list) {
2578 list_del(&old->list);
2579 kfree(old);
2580 }
2581out_free_path:
2582 btrfs_free_path(path);
2583out_kfree:
2584 kfree(new);
2585 return NULL;
2586}
2587
Chris Mason5d13a982009-03-13 11:41:46 -04002588/*
2589 * helper function for btrfs_finish_ordered_io, this
2590 * just reads in some of the csum leaves to prime them into ram
2591 * before we start the transaction. It limits the amount of btree
2592 * reads required while inside the transaction.
2593 */
Chris Masond352ac62008-09-29 15:18:18 -04002594/* as ordered data IO finishes, this gets called so we can finish
2595 * an ordered extent if the range of bytes in the file it covers are
2596 * fully written.
2597 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002598static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002599{
Josef Bacik5fd02042012-05-02 14:00:54 -04002600 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002601 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002602 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002603 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002604 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002605 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002606 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002607 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002608 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002609
Liu Bo83eea1f2012-07-10 05:28:39 -06002610 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002611
Josef Bacik5fd02042012-05-02 14:00:54 -04002612 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2613 ret = -EIO;
2614 goto out;
2615 }
2616
Yan, Zhengc2167752009-11-12 09:34:21 +00002617 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002618 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002619 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2620 if (nolock)
2621 trans = btrfs_join_transaction_nolock(root);
2622 else
2623 trans = btrfs_join_transaction(root);
2624 if (IS_ERR(trans)) {
2625 ret = PTR_ERR(trans);
2626 trans = NULL;
2627 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002628 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002629 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2630 ret = btrfs_update_inode_fallback(trans, root, inode);
2631 if (ret) /* -ENOMEM or corruption */
2632 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002633 goto out;
2634 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002635
Josef Bacik2ac55d42010-02-03 19:33:23 +00002636 lock_extent_bits(io_tree, ordered_extent->file_offset,
2637 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002638 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002639
Liu Bo38c227d2013-01-29 03:18:40 +00002640 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2641 ordered_extent->file_offset + ordered_extent->len - 1,
2642 EXTENT_DEFRAG, 1, cached_state);
2643 if (ret) {
2644 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2645 if (last_snapshot >= BTRFS_I(inode)->generation)
2646 /* the inode is shared */
2647 new = record_old_file_extents(inode, ordered_extent);
2648
2649 clear_extent_bit(io_tree, ordered_extent->file_offset,
2650 ordered_extent->file_offset + ordered_extent->len - 1,
2651 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2652 }
2653
Josef Bacik0cb59c92010-07-02 12:14:14 -04002654 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002655 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002656 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002657 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002658 if (IS_ERR(trans)) {
2659 ret = PTR_ERR(trans);
2660 trans = NULL;
2661 goto out_unlock;
2662 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002663 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002664
Chris Masonc8b97812008-10-29 14:49:59 -04002665 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002666 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002667 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002668 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002669 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002670 ordered_extent->file_offset,
2671 ordered_extent->file_offset +
2672 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002673 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002674 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002675 ret = insert_reserved_file_extent(trans, inode,
2676 ordered_extent->file_offset,
2677 ordered_extent->start,
2678 ordered_extent->disk_len,
2679 ordered_extent->len,
2680 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002681 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002682 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002683 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002684 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2685 ordered_extent->file_offset, ordered_extent->len,
2686 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002687 if (ret < 0) {
2688 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002689 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002690 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002691
Chris Masone6dcd2d2008-07-17 12:53:50 -04002692 add_pending_csums(trans, inode, ordered_extent->file_offset,
2693 &ordered_extent->list);
2694
Josef Bacik6c760c02012-11-09 10:53:21 -05002695 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2696 ret = btrfs_update_inode_fallback(trans, root, inode);
2697 if (ret) { /* -ENOMEM or corruption */
2698 btrfs_abort_transaction(trans, root, ret);
2699 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002700 }
2701 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002702out_unlock:
2703 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2704 ordered_extent->file_offset +
2705 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002706out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002707 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002708 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002709 if (trans)
2710 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002711
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002712 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002713 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2714 ordered_extent->file_offset +
2715 ordered_extent->len - 1, NULL, GFP_NOFS);
2716
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002717 /*
2718 * If the ordered extent had an IOERR or something else went
2719 * wrong we need to return the space for this ordered extent
2720 * back to the allocator.
2721 */
2722 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2723 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2724 btrfs_free_reserved_extent(root, ordered_extent->start,
2725 ordered_extent->disk_len);
2726 }
2727
2728
Josef Bacik5fd02042012-05-02 14:00:54 -04002729 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002730 * This needs to be done to make sure anybody waiting knows we are done
2731 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002732 */
2733 btrfs_remove_ordered_extent(inode, ordered_extent);
2734
Liu Bo38c227d2013-01-29 03:18:40 +00002735 /* for snapshot-aware defrag */
2736 if (new)
2737 relink_file_extents(new);
2738
Chris Masone6dcd2d2008-07-17 12:53:50 -04002739 /* once for us */
2740 btrfs_put_ordered_extent(ordered_extent);
2741 /* once for the tree */
2742 btrfs_put_ordered_extent(ordered_extent);
2743
Josef Bacik5fd02042012-05-02 14:00:54 -04002744 return ret;
2745}
2746
2747static void finish_ordered_fn(struct btrfs_work *work)
2748{
2749 struct btrfs_ordered_extent *ordered_extent;
2750 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2751 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002752}
2753
Christoph Hellwigb2950862008-12-02 09:54:17 -05002754static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002755 struct extent_state *state, int uptodate)
2756{
Josef Bacik5fd02042012-05-02 14:00:54 -04002757 struct inode *inode = page->mapping->host;
2758 struct btrfs_root *root = BTRFS_I(inode)->root;
2759 struct btrfs_ordered_extent *ordered_extent = NULL;
2760 struct btrfs_workers *workers;
2761
liubo1abe9b82011-03-24 11:18:59 +00002762 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2763
Chris Mason8b62b722009-09-02 16:53:46 -04002764 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002765 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2766 end - start + 1, uptodate))
2767 return 0;
2768
2769 ordered_extent->work.func = finish_ordered_fn;
2770 ordered_extent->work.flags = 0;
2771
Liu Bo83eea1f2012-07-10 05:28:39 -06002772 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002773 workers = &root->fs_info->endio_freespace_worker;
2774 else
2775 workers = &root->fs_info->endio_write_workers;
2776 btrfs_queue_worker(workers, &ordered_extent->work);
2777
2778 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002779}
2780
Chris Masond352ac62008-09-29 15:18:18 -04002781/*
Chris Masond352ac62008-09-29 15:18:18 -04002782 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002783 * if there's a match, we allow the bio to finish. If not, the code in
2784 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002785 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002786static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002787 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002788{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002789 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002790 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002791 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002792 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002793 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002794 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002795 struct btrfs_root *root = BTRFS_I(inode)->root;
2796 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002797
Chris Masond20f7042008-12-08 16:58:54 -05002798 if (PageChecked(page)) {
2799 ClearPageChecked(page);
2800 goto good;
2801 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002802
2803 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002804 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002805
Yan Zheng17d217f2008-12-12 10:03:38 -05002806 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002807 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002808 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2809 GFP_NOFS);
2810 return 0;
2811 }
2812
Yanc2e639f2008-02-04 08:57:25 -05002813 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002814 private = state->private;
2815 ret = 0;
2816 } else {
2817 ret = get_state_private(io_tree, start, &private);
2818 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002819 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002820 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002821 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002822
Chris Masonff79f812007-10-15 16:22:25 -04002823 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2824 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002825 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002826 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002827
Cong Wang7ac687d2011-11-25 23:14:28 +08002828 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002829good:
Chris Mason07157aa2007-08-30 08:50:51 -04002830 return 0;
2831
2832zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002833 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002834 "private %llu\n",
2835 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002836 (unsigned long long)start, csum,
2837 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002838 memset(kaddr + offset, 1, end - start + 1);
2839 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002840 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002841 if (private == 0)
2842 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002843 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002844}
Chris Masonb888db22007-08-27 16:49:44 -04002845
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002846struct delayed_iput {
2847 struct list_head list;
2848 struct inode *inode;
2849};
2850
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002851/* JDM: If this is fs-wide, why can't we add a pointer to
2852 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002853void btrfs_add_delayed_iput(struct inode *inode)
2854{
2855 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2856 struct delayed_iput *delayed;
2857
2858 if (atomic_add_unless(&inode->i_count, -1, 1))
2859 return;
2860
2861 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2862 delayed->inode = inode;
2863
2864 spin_lock(&fs_info->delayed_iput_lock);
2865 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2866 spin_unlock(&fs_info->delayed_iput_lock);
2867}
2868
2869void btrfs_run_delayed_iputs(struct btrfs_root *root)
2870{
2871 LIST_HEAD(list);
2872 struct btrfs_fs_info *fs_info = root->fs_info;
2873 struct delayed_iput *delayed;
2874 int empty;
2875
2876 spin_lock(&fs_info->delayed_iput_lock);
2877 empty = list_empty(&fs_info->delayed_iputs);
2878 spin_unlock(&fs_info->delayed_iput_lock);
2879 if (empty)
2880 return;
2881
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002882 spin_lock(&fs_info->delayed_iput_lock);
2883 list_splice_init(&fs_info->delayed_iputs, &list);
2884 spin_unlock(&fs_info->delayed_iput_lock);
2885
2886 while (!list_empty(&list)) {
2887 delayed = list_entry(list.next, struct delayed_iput, list);
2888 list_del(&delayed->list);
2889 iput(delayed->inode);
2890 kfree(delayed);
2891 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002892}
2893
Yan, Zhengd68fc572010-05-16 10:49:58 -04002894/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002895 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002896 * files in the subvolume, it removes orphan item and frees block_rsv
2897 * structure.
2898 */
2899void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2900 struct btrfs_root *root)
2901{
Josef Bacik90290e12011-12-02 15:44:12 -05002902 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002903 int ret;
2904
Josef Bacik8a35d952012-05-23 14:26:42 -04002905 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002906 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2907 return;
2908
Josef Bacik90290e12011-12-02 15:44:12 -05002909 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002910 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002911 spin_unlock(&root->orphan_lock);
2912 return;
2913 }
2914
2915 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2916 spin_unlock(&root->orphan_lock);
2917 return;
2918 }
2919
2920 block_rsv = root->orphan_block_rsv;
2921 root->orphan_block_rsv = NULL;
2922 spin_unlock(&root->orphan_lock);
2923
Yan, Zhengd68fc572010-05-16 10:49:58 -04002924 if (root->orphan_item_inserted &&
2925 btrfs_root_refs(&root->root_item) > 0) {
2926 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2927 root->root_key.objectid);
2928 BUG_ON(ret);
2929 root->orphan_item_inserted = 0;
2930 }
2931
Josef Bacik90290e12011-12-02 15:44:12 -05002932 if (block_rsv) {
2933 WARN_ON(block_rsv->size > 0);
2934 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002935 }
2936}
2937
2938/*
Josef Bacik7b128762008-07-24 12:17:14 -04002939 * This creates an orphan entry for the given inode in case something goes
2940 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002941 *
2942 * NOTE: caller of this function should reserve 5 units of metadata for
2943 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002944 */
2945int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2946{
2947 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002948 struct btrfs_block_rsv *block_rsv = NULL;
2949 int reserve = 0;
2950 int insert = 0;
2951 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002952
Yan, Zhengd68fc572010-05-16 10:49:58 -04002953 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002954 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002955 if (!block_rsv)
2956 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002957 }
2958
Yan, Zhengd68fc572010-05-16 10:49:58 -04002959 spin_lock(&root->orphan_lock);
2960 if (!root->orphan_block_rsv) {
2961 root->orphan_block_rsv = block_rsv;
2962 } else if (block_rsv) {
2963 btrfs_free_block_rsv(root, block_rsv);
2964 block_rsv = NULL;
2965 }
Josef Bacik7b128762008-07-24 12:17:14 -04002966
Josef Bacik8a35d952012-05-23 14:26:42 -04002967 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2968 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002969#if 0
2970 /*
2971 * For proper ENOSPC handling, we should do orphan
2972 * cleanup when mounting. But this introduces backward
2973 * compatibility issue.
2974 */
2975 if (!xchg(&root->orphan_item_inserted, 1))
2976 insert = 2;
2977 else
2978 insert = 1;
2979#endif
2980 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002981 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002982 }
Josef Bacik7b128762008-07-24 12:17:14 -04002983
Josef Bacik72ac3c02012-05-23 14:13:11 -04002984 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2985 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002986 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002987 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002988
Yan, Zhengd68fc572010-05-16 10:49:58 -04002989 /* grab metadata reservation from transaction handle */
2990 if (reserve) {
2991 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002992 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002993 }
2994
2995 /* insert an orphan item to track this unlinked/truncated file */
2996 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002997 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002998 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002999 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3000 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003001 btrfs_abort_transaction(trans, root, ret);
3002 return ret;
3003 }
3004 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003005 }
3006
3007 /* insert an orphan item to track subvolume contains orphan files */
3008 if (insert >= 2) {
3009 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3010 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003011 if (ret && ret != -EEXIST) {
3012 btrfs_abort_transaction(trans, root, ret);
3013 return ret;
3014 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003015 }
3016 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003017}
3018
3019/*
3020 * We have done the truncate/delete so we can go ahead and remove the orphan
3021 * item for this particular inode.
3022 */
3023int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
3024{
3025 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003026 int delete_item = 0;
3027 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003028 int ret = 0;
3029
Yan, Zhengd68fc572010-05-16 10:49:58 -04003030 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003031 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3032 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003033 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003034
Josef Bacik72ac3c02012-05-23 14:13:11 -04003035 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3036 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003037 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003038 spin_unlock(&root->orphan_lock);
3039
3040 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003041 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003042 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003043 }
3044
Josef Bacik8a35d952012-05-23 14:26:42 -04003045 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003046 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003047 atomic_dec(&root->orphan_inodes);
3048 }
Josef Bacik7b128762008-07-24 12:17:14 -04003049
Yan, Zhengd68fc572010-05-16 10:49:58 -04003050 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003051}
3052
3053/*
3054 * this cleans up any orphans that may be left on the list from the last use
3055 * of this root.
3056 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003057int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003058{
3059 struct btrfs_path *path;
3060 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003061 struct btrfs_key key, found_key;
3062 struct btrfs_trans_handle *trans;
3063 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003064 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003065 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3066
Yan, Zhengd68fc572010-05-16 10:49:58 -04003067 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003068 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003069
3070 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003071 if (!path) {
3072 ret = -ENOMEM;
3073 goto out;
3074 }
Josef Bacik7b128762008-07-24 12:17:14 -04003075 path->reada = -1;
3076
3077 key.objectid = BTRFS_ORPHAN_OBJECTID;
3078 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3079 key.offset = (u64)-1;
3080
Josef Bacik7b128762008-07-24 12:17:14 -04003081 while (1) {
3082 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003083 if (ret < 0)
3084 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003085
3086 /*
3087 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003088 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003089 * find the key and see if we have stuff that matches
3090 */
3091 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003092 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003093 if (path->slots[0] == 0)
3094 break;
3095 path->slots[0]--;
3096 }
3097
3098 /* pull out the item */
3099 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003100 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3101
3102 /* make sure the item matches what we want */
3103 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3104 break;
3105 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3106 break;
3107
3108 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003109 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003110
3111 /*
3112 * this is where we are basically btrfs_lookup, without the
3113 * crossing root thing. we store the inode number in the
3114 * offset of the orphan item.
3115 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003116
3117 if (found_key.offset == last_objectid) {
3118 printk(KERN_ERR "btrfs: Error removing orphan entry, "
3119 "stopping orphan cleanup\n");
3120 ret = -EINVAL;
3121 goto out;
3122 }
3123
3124 last_objectid = found_key.offset;
3125
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003126 found_key.objectid = found_key.offset;
3127 found_key.type = BTRFS_INODE_ITEM_KEY;
3128 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003129 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003130 ret = PTR_RET(inode);
3131 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003132 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003133
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003134 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3135 struct btrfs_root *dead_root;
3136 struct btrfs_fs_info *fs_info = root->fs_info;
3137 int is_dead_root = 0;
3138
3139 /*
3140 * this is an orphan in the tree root. Currently these
3141 * could come from 2 sources:
3142 * a) a snapshot deletion in progress
3143 * b) a free space cache inode
3144 * We need to distinguish those two, as the snapshot
3145 * orphan must not get deleted.
3146 * find_dead_roots already ran before us, so if this
3147 * is a snapshot deletion, we should find the root
3148 * in the dead_roots list
3149 */
3150 spin_lock(&fs_info->trans_lock);
3151 list_for_each_entry(dead_root, &fs_info->dead_roots,
3152 root_list) {
3153 if (dead_root->root_key.objectid ==
3154 found_key.objectid) {
3155 is_dead_root = 1;
3156 break;
3157 }
3158 }
3159 spin_unlock(&fs_info->trans_lock);
3160 if (is_dead_root) {
3161 /* prevent this orphan from being found again */
3162 key.offset = found_key.objectid - 1;
3163 continue;
3164 }
3165 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003166 /*
3167 * Inode is already gone but the orphan item is still there,
3168 * kill the orphan item.
3169 */
3170 if (ret == -ESTALE) {
3171 trans = btrfs_start_transaction(root, 1);
3172 if (IS_ERR(trans)) {
3173 ret = PTR_ERR(trans);
3174 goto out;
3175 }
Josef Bacik8a35d952012-05-23 14:26:42 -04003176 printk(KERN_ERR "auto deleting %Lu\n",
3177 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003178 ret = btrfs_del_orphan_item(trans, root,
3179 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003180 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003181 btrfs_end_transaction(trans, root);
3182 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003183 }
Josef Bacik7b128762008-07-24 12:17:14 -04003184
Josef Bacik7b128762008-07-24 12:17:14 -04003185 /*
3186 * add this inode to the orphan list so btrfs_orphan_del does
3187 * the proper thing when we hit it
3188 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003189 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3190 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003191 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003192
Josef Bacik7b128762008-07-24 12:17:14 -04003193 /* if we have links, this was a truncate, lets do that */
3194 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003195 if (!S_ISREG(inode->i_mode)) {
3196 WARN_ON(1);
3197 iput(inode);
3198 continue;
3199 }
Josef Bacik7b128762008-07-24 12:17:14 -04003200 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003201
3202 /* 1 for the orphan item deletion. */
3203 trans = btrfs_start_transaction(root, 1);
3204 if (IS_ERR(trans)) {
3205 ret = PTR_ERR(trans);
3206 goto out;
3207 }
3208 ret = btrfs_orphan_add(trans, inode);
3209 btrfs_end_transaction(trans, root);
3210 if (ret)
3211 goto out;
3212
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003213 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003214 if (ret)
3215 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003216 } else {
3217 nr_unlink++;
3218 }
3219
3220 /* this will do delete_inode and everything for us */
3221 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003222 if (ret)
3223 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003224 }
Miao Xie3254c872011-11-10 20:45:05 -05003225 /* release the path since we're done with it */
3226 btrfs_release_path(path);
3227
Yan, Zhengd68fc572010-05-16 10:49:58 -04003228 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3229
3230 if (root->orphan_block_rsv)
3231 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3232 (u64)-1);
3233
3234 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003235 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003236 if (!IS_ERR(trans))
3237 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003238 }
Josef Bacik7b128762008-07-24 12:17:14 -04003239
3240 if (nr_unlink)
3241 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
3242 if (nr_truncate)
3243 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003244
3245out:
3246 if (ret)
3247 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
3248 btrfs_free_path(path);
3249 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003250}
3251
Chris Masond352ac62008-09-29 15:18:18 -04003252/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003253 * very simple check to peek ahead in the leaf looking for xattrs. If we
3254 * don't find any xattrs, we know there can't be any acls.
3255 *
3256 * slot is the slot the inode is in, objectid is the objectid of the inode
3257 */
3258static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3259 int slot, u64 objectid)
3260{
3261 u32 nritems = btrfs_header_nritems(leaf);
3262 struct btrfs_key found_key;
3263 int scanned = 0;
3264
3265 slot++;
3266 while (slot < nritems) {
3267 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3268
3269 /* we found a different objectid, there must not be acls */
3270 if (found_key.objectid != objectid)
3271 return 0;
3272
3273 /* we found an xattr, assume we've got an acl */
3274 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3275 return 1;
3276
3277 /*
3278 * we found a key greater than an xattr key, there can't
3279 * be any acls later on
3280 */
3281 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3282 return 0;
3283
3284 slot++;
3285 scanned++;
3286
3287 /*
3288 * it goes inode, inode backrefs, xattrs, extents,
3289 * so if there are a ton of hard links to an inode there can
3290 * be a lot of backrefs. Don't waste time searching too hard,
3291 * this is just an optimization
3292 */
3293 if (scanned >= 8)
3294 break;
3295 }
3296 /* we hit the end of the leaf before we found an xattr or
3297 * something larger than an xattr. We have to assume the inode
3298 * has acls
3299 */
3300 return 1;
3301}
3302
3303/*
Chris Masond352ac62008-09-29 15:18:18 -04003304 * read an inode from the btree into the in-memory inode
3305 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003306static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003307{
3308 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003309 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003310 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003311 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003312 struct btrfs_root *root = BTRFS_I(inode)->root;
3313 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003314 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003315 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003316 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003317 bool filled = false;
3318
3319 ret = btrfs_fill_inode(inode, &rdev);
3320 if (!ret)
3321 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003322
3323 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003324 if (!path)
3325 goto make_bad;
3326
Josef Bacikd90c7322011-05-17 09:50:54 -04003327 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003328 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003329
Chris Mason39279cc2007-06-12 06:35:45 -04003330 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003331 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003332 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003333
Chris Mason5f39d392007-10-15 16:14:19 -04003334 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003335
3336 if (filled)
3337 goto cache_acl;
3338
Chris Mason5f39d392007-10-15 16:14:19 -04003339 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3340 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003341 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003342 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003343 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3344 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003345 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003346
3347 tspec = btrfs_inode_atime(inode_item);
3348 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3349 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3350
3351 tspec = btrfs_inode_mtime(inode_item);
3352 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3353 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3354
3355 tspec = btrfs_inode_ctime(inode_item);
3356 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3357 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3358
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003359 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003360 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003361 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3362
3363 /*
3364 * If we were modified in the current generation and evicted from memory
3365 * and then re-read we need to do a full sync since we don't have any
3366 * idea about which extents were modified before we were evicted from
3367 * cache.
3368 */
3369 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3370 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3371 &BTRFS_I(inode)->runtime_flags);
3372
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003373 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003374 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003375 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003376 rdev = btrfs_inode_rdev(leaf, inode_item);
3377
Josef Bacikaec74772008-07-24 12:12:38 -04003378 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003379 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003380cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003381 /*
3382 * try to precache a NULL acl entry for files that don't have
3383 * any xattrs or acls
3384 */
Li Zefan33345d012011-04-20 10:31:50 +08003385 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3386 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003387 if (!maybe_acls)
3388 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003389
Chris Mason39279cc2007-06-12 06:35:45 -04003390 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003391
Chris Mason39279cc2007-06-12 06:35:45 -04003392 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003393 case S_IFREG:
3394 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003395 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003396 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003397 inode->i_fop = &btrfs_file_operations;
3398 inode->i_op = &btrfs_file_inode_operations;
3399 break;
3400 case S_IFDIR:
3401 inode->i_fop = &btrfs_dir_file_operations;
3402 if (root == root->fs_info->tree_root)
3403 inode->i_op = &btrfs_dir_ro_inode_operations;
3404 else
3405 inode->i_op = &btrfs_dir_inode_operations;
3406 break;
3407 case S_IFLNK:
3408 inode->i_op = &btrfs_symlink_inode_operations;
3409 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003410 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003411 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003412 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003413 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003414 init_special_inode(inode, inode->i_mode, rdev);
3415 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003416 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003417
3418 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003419 return;
3420
3421make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003422 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003423 make_bad_inode(inode);
3424}
3425
Chris Masond352ac62008-09-29 15:18:18 -04003426/*
3427 * given a leaf and an inode, copy the inode fields into the leaf
3428 */
Chris Masone02119d2008-09-05 16:13:11 -04003429static void fill_inode_item(struct btrfs_trans_handle *trans,
3430 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003431 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003432 struct inode *inode)
3433{
Liu Bo51fab692012-12-27 09:01:21 +00003434 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003435
Liu Bo51fab692012-12-27 09:01:21 +00003436 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003437
Liu Bo51fab692012-12-27 09:01:21 +00003438 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3439 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3440 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3441 &token);
3442 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3443 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003444
Liu Bo51fab692012-12-27 09:01:21 +00003445 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3446 inode->i_atime.tv_sec, &token);
3447 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3448 inode->i_atime.tv_nsec, &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_mtime(item),
3451 inode->i_mtime.tv_sec, &token);
3452 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3453 inode->i_mtime.tv_nsec, &token);
3454
3455 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3456 inode->i_ctime.tv_sec, &token);
3457 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3458 inode->i_ctime.tv_nsec, &token);
3459
3460 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3461 &token);
3462 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3463 &token);
3464 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3465 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3466 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3467 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3468 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003469}
3470
Chris Masond352ac62008-09-29 15:18:18 -04003471/*
3472 * copy everything in the in-memory inode into the btree.
3473 */
Chris Mason21151332011-11-10 20:39:08 -05003474static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003475 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003476{
3477 struct btrfs_inode_item *inode_item;
3478 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003479 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003480 int ret;
3481
3482 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003483 if (!path)
3484 return -ENOMEM;
3485
Chris Masonb9473432009-03-13 11:00:37 -04003486 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003487 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3488 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003489 if (ret) {
3490 if (ret > 0)
3491 ret = -ENOENT;
3492 goto failed;
3493 }
3494
Chris Masonb4ce94d2009-02-04 09:25:08 -05003495 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003496 leaf = path->nodes[0];
3497 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003498 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003499
Chris Masone02119d2008-09-05 16:13:11 -04003500 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003501 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003502 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003503 ret = 0;
3504failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003505 btrfs_free_path(path);
3506 return ret;
3507}
3508
Chris Masond352ac62008-09-29 15:18:18 -04003509/*
Chris Mason21151332011-11-10 20:39:08 -05003510 * copy everything in the in-memory inode into the btree.
3511 */
3512noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3513 struct btrfs_root *root, struct inode *inode)
3514{
3515 int ret;
3516
3517 /*
3518 * If the inode is a free space inode, we can deadlock during commit
3519 * if we put it into the delayed code.
3520 *
3521 * The data relocation inode should also be directly updated
3522 * without delay
3523 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003524 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003525 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003526 btrfs_update_root_times(trans, root);
3527
Chris Mason21151332011-11-10 20:39:08 -05003528 ret = btrfs_delayed_update_inode(trans, root, inode);
3529 if (!ret)
3530 btrfs_set_inode_last_trans(trans, inode);
3531 return ret;
3532 }
3533
3534 return btrfs_update_inode_item(trans, root, inode);
3535}
3536
Josef Bacikbe6aef62012-10-22 15:43:12 -04003537noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3538 struct btrfs_root *root,
3539 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003540{
3541 int ret;
3542
3543 ret = btrfs_update_inode(trans, root, inode);
3544 if (ret == -ENOSPC)
3545 return btrfs_update_inode_item(trans, root, inode);
3546 return ret;
3547}
3548
3549/*
Chris Masond352ac62008-09-29 15:18:18 -04003550 * unlink helper that gets used here in inode.c and in the tree logging
3551 * recovery code. It remove a link in a directory with a given name, and
3552 * also drops the back refs in the inode to the directory
3553 */
Al Viro92986792011-03-04 17:14:37 +00003554static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3555 struct btrfs_root *root,
3556 struct inode *dir, struct inode *inode,
3557 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003558{
3559 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003560 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003561 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003562 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003563 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003564 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003565 u64 ino = btrfs_ino(inode);
3566 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003567
3568 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003569 if (!path) {
3570 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003571 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003572 }
3573
Chris Masonb9473432009-03-13 11:00:37 -04003574 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003575 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003576 name, name_len, -1);
3577 if (IS_ERR(di)) {
3578 ret = PTR_ERR(di);
3579 goto err;
3580 }
3581 if (!di) {
3582 ret = -ENOENT;
3583 goto err;
3584 }
Chris Mason5f39d392007-10-15 16:14:19 -04003585 leaf = path->nodes[0];
3586 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003587 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003588 if (ret)
3589 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003590 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003591
Li Zefan33345d012011-04-20 10:31:50 +08003592 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3593 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003594 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05003595 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08003596 "inode %llu parent %llu\n", name_len, name,
3597 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003598 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003599 goto err;
3600 }
3601
Miao Xie16cdcec2011-04-22 18:12:22 +08003602 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003603 if (ret) {
3604 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003605 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003606 }
Chris Mason39279cc2007-06-12 06:35:45 -04003607
Chris Masone02119d2008-09-05 16:13:11 -04003608 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003609 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003610 if (ret != 0 && ret != -ENOENT) {
3611 btrfs_abort_transaction(trans, root, ret);
3612 goto err;
3613 }
Chris Masone02119d2008-09-05 16:13:11 -04003614
3615 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3616 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003617 if (ret == -ENOENT)
3618 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003619err:
3620 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003621 if (ret)
3622 goto out;
3623
3624 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003625 inode_inc_iversion(inode);
3626 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003627 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003628 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003629out:
Chris Mason39279cc2007-06-12 06:35:45 -04003630 return ret;
3631}
3632
Al Viro92986792011-03-04 17:14:37 +00003633int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3634 struct btrfs_root *root,
3635 struct inode *dir, struct inode *inode,
3636 const char *name, int name_len)
3637{
3638 int ret;
3639 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3640 if (!ret) {
3641 btrfs_drop_nlink(inode);
3642 ret = btrfs_update_inode(trans, root, inode);
3643 }
3644 return ret;
3645}
3646
3647
Yan, Zhenga22285a2010-05-16 10:48:46 -04003648/* helper to check if there is any shared block in the path */
3649static int check_path_shared(struct btrfs_root *root,
3650 struct btrfs_path *path)
3651{
3652 struct extent_buffer *eb;
3653 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00003654 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003655
3656 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003657 int ret;
3658
Yan, Zhenga22285a2010-05-16 10:48:46 -04003659 if (!path->nodes[level])
3660 break;
3661 eb = path->nodes[level];
3662 if (!btrfs_block_can_be_shared(root, eb))
3663 continue;
3664 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
3665 &refs, NULL);
3666 if (refs > 1)
3667 return 1;
3668 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003669 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003670}
3671
3672/*
3673 * helper to start transaction for unlink and rmdir.
3674 *
3675 * unlink and rmdir are special in btrfs, they do not always free space.
3676 * so in enospc case, we should make sure they will free space before
3677 * allowing them to use the global metadata reservation.
3678 */
3679static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3680 struct dentry *dentry)
3681{
3682 struct btrfs_trans_handle *trans;
3683 struct btrfs_root *root = BTRFS_I(dir)->root;
3684 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003685 struct btrfs_dir_item *di;
3686 struct inode *inode = dentry->d_inode;
3687 u64 index;
3688 int check_link = 1;
3689 int err = -ENOSPC;
3690 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003691 u64 ino = btrfs_ino(inode);
3692 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003693
Josef Bacike70bea52011-10-11 14:18:24 -04003694 /*
3695 * 1 for the possible orphan item
3696 * 1 for the dir item
3697 * 1 for the dir index
3698 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003699 * 1 for the inode
3700 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003701 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3703 return trans;
3704
Li Zefan33345d012011-04-20 10:31:50 +08003705 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003706 return ERR_PTR(-ENOSPC);
3707
3708 /* check if there is someone else holds reference */
3709 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3710 return ERR_PTR(-ENOSPC);
3711
3712 if (atomic_read(&inode->i_count) > 2)
3713 return ERR_PTR(-ENOSPC);
3714
3715 if (xchg(&root->fs_info->enospc_unlink, 1))
3716 return ERR_PTR(-ENOSPC);
3717
3718 path = btrfs_alloc_path();
3719 if (!path) {
3720 root->fs_info->enospc_unlink = 0;
3721 return ERR_PTR(-ENOMEM);
3722 }
3723
Josef Bacik3880a1b2011-10-14 14:46:51 -04003724 /* 1 for the orphan item */
3725 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003726 if (IS_ERR(trans)) {
3727 btrfs_free_path(path);
3728 root->fs_info->enospc_unlink = 0;
3729 return trans;
3730 }
3731
3732 path->skip_locking = 1;
3733 path->search_commit_root = 1;
3734
3735 ret = btrfs_lookup_inode(trans, root, path,
3736 &BTRFS_I(dir)->location, 0);
3737 if (ret < 0) {
3738 err = ret;
3739 goto out;
3740 }
3741 if (ret == 0) {
3742 if (check_path_shared(root, path))
3743 goto out;
3744 } else {
3745 check_link = 0;
3746 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003747 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003748
3749 ret = btrfs_lookup_inode(trans, root, path,
3750 &BTRFS_I(inode)->location, 0);
3751 if (ret < 0) {
3752 err = ret;
3753 goto out;
3754 }
3755 if (ret == 0) {
3756 if (check_path_shared(root, path))
3757 goto out;
3758 } else {
3759 check_link = 0;
3760 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003761 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003762
3763 if (ret == 0 && S_ISREG(inode->i_mode)) {
3764 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003765 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003766 if (ret < 0) {
3767 err = ret;
3768 goto out;
3769 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003770 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003771 if (check_path_shared(root, path))
3772 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003773 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003774 }
3775
3776 if (!check_link) {
3777 err = 0;
3778 goto out;
3779 }
3780
Li Zefan33345d012011-04-20 10:31:50 +08003781 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003782 dentry->d_name.name, dentry->d_name.len, 0);
3783 if (IS_ERR(di)) {
3784 err = PTR_ERR(di);
3785 goto out;
3786 }
3787 if (di) {
3788 if (check_path_shared(root, path))
3789 goto out;
3790 } else {
3791 err = 0;
3792 goto out;
3793 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003794 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003795
Mark Fashehf1863732012-08-08 11:32:27 -07003796 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3797 dentry->d_name.len, ino, dir_ino, 0,
3798 &index);
3799 if (ret) {
3800 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003801 goto out;
3802 }
Mark Fashehf1863732012-08-08 11:32:27 -07003803
Yan, Zhenga22285a2010-05-16 10:48:46 -04003804 if (check_path_shared(root, path))
3805 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003806
David Sterbab3b4aa72011-04-21 01:20:15 +02003807 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003808
Miao Xie16cdcec2011-04-22 18:12:22 +08003809 /*
3810 * This is a commit root search, if we can lookup inode item and other
3811 * relative items in the commit root, it means the transaction of
3812 * dir/file creation has been committed, and the dir index item that we
3813 * delay to insert has also been inserted into the commit root. So
3814 * we needn't worry about the delayed insertion of the dir index item
3815 * here.
3816 */
Li Zefan33345d012011-04-20 10:31:50 +08003817 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003818 dentry->d_name.name, dentry->d_name.len, 0);
3819 if (IS_ERR(di)) {
3820 err = PTR_ERR(di);
3821 goto out;
3822 }
3823 BUG_ON(ret == -ENOENT);
3824 if (check_path_shared(root, path))
3825 goto out;
3826
3827 err = 0;
3828out:
3829 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003830 /* Migrate the orphan reservation over */
3831 if (!err)
3832 err = btrfs_block_rsv_migrate(trans->block_rsv,
3833 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003834 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003835
Yan, Zhenga22285a2010-05-16 10:48:46 -04003836 if (err) {
3837 btrfs_end_transaction(trans, root);
3838 root->fs_info->enospc_unlink = 0;
3839 return ERR_PTR(err);
3840 }
3841
3842 trans->block_rsv = &root->fs_info->global_block_rsv;
3843 return trans;
3844}
3845
3846static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3847 struct btrfs_root *root)
3848{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003849 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003850 btrfs_block_rsv_release(root, trans->block_rsv,
3851 trans->bytes_reserved);
3852 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003853 BUG_ON(!root->fs_info->enospc_unlink);
3854 root->fs_info->enospc_unlink = 0;
3855 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003856 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003857}
3858
Chris Mason39279cc2007-06-12 06:35:45 -04003859static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3860{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003861 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003862 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003863 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003864 int ret;
3865
Yan, Zhenga22285a2010-05-16 10:48:46 -04003866 trans = __unlink_start_trans(dir, dentry);
3867 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003868 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003869
Chris Mason12fcfd22009-03-24 10:24:20 -04003870 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3871
Chris Masone02119d2008-09-05 16:13:11 -04003872 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3873 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003874 if (ret)
3875 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003876
Yan, Zhenga22285a2010-05-16 10:48:46 -04003877 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003878 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003879 if (ret)
3880 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003881 }
Josef Bacik7b128762008-07-24 12:17:14 -04003882
Tsutomu Itohb5324022011-07-19 07:27:20 +00003883out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003884 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003885 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003886 return ret;
3887}
3888
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003889int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3890 struct btrfs_root *root,
3891 struct inode *dir, u64 objectid,
3892 const char *name, int name_len)
3893{
3894 struct btrfs_path *path;
3895 struct extent_buffer *leaf;
3896 struct btrfs_dir_item *di;
3897 struct btrfs_key key;
3898 u64 index;
3899 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003900 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003901
3902 path = btrfs_alloc_path();
3903 if (!path)
3904 return -ENOMEM;
3905
Li Zefan33345d012011-04-20 10:31:50 +08003906 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003907 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003908 if (IS_ERR_OR_NULL(di)) {
3909 if (!di)
3910 ret = -ENOENT;
3911 else
3912 ret = PTR_ERR(di);
3913 goto out;
3914 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003915
3916 leaf = path->nodes[0];
3917 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3918 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3919 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003920 if (ret) {
3921 btrfs_abort_transaction(trans, root, ret);
3922 goto out;
3923 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003924 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003925
3926 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3927 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003928 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003929 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003930 if (ret != -ENOENT) {
3931 btrfs_abort_transaction(trans, root, ret);
3932 goto out;
3933 }
Li Zefan33345d012011-04-20 10:31:50 +08003934 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003935 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003936 if (IS_ERR_OR_NULL(di)) {
3937 if (!di)
3938 ret = -ENOENT;
3939 else
3940 ret = PTR_ERR(di);
3941 btrfs_abort_transaction(trans, root, ret);
3942 goto out;
3943 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003944
3945 leaf = path->nodes[0];
3946 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003947 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003948 index = key.offset;
3949 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003950 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003951
Miao Xie16cdcec2011-04-22 18:12:22 +08003952 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003953 if (ret) {
3954 btrfs_abort_transaction(trans, root, ret);
3955 goto out;
3956 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003957
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003958 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003959 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003960 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003961 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003962 if (ret)
3963 btrfs_abort_transaction(trans, root, ret);
3964out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003965 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003966 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003967}
3968
Chris Mason39279cc2007-06-12 06:35:45 -04003969static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3970{
3971 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003972 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003973 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003974 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003975
David Sterbab3ae2442012-09-13 16:04:34 -06003976 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003977 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003978 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3979 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003980
Yan, Zhenga22285a2010-05-16 10:48:46 -04003981 trans = __unlink_start_trans(dir, dentry);
3982 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003983 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003984
Li Zefan33345d012011-04-20 10:31:50 +08003985 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003986 err = btrfs_unlink_subvol(trans, root, dir,
3987 BTRFS_I(inode)->location.objectid,
3988 dentry->d_name.name,
3989 dentry->d_name.len);
3990 goto out;
3991 }
3992
Josef Bacik7b128762008-07-24 12:17:14 -04003993 err = btrfs_orphan_add(trans, inode);
3994 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003995 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003996
Chris Mason39279cc2007-06-12 06:35:45 -04003997 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003998 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3999 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004000 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004001 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004002out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04004003 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004004 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004005
Chris Mason39279cc2007-06-12 06:35:45 -04004006 return err;
4007}
4008
Chris Mason323ac952008-10-01 19:05:46 -04004009/*
Chris Mason39279cc2007-06-12 06:35:45 -04004010 * this can truncate away extent items, csum items and directory items.
4011 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004012 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004013 *
4014 * csum items that cross the new i_size are truncated to the new size
4015 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004016 *
4017 * min_type is the minimum key type to truncate down to. If set to 0, this
4018 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004019 */
Yan, Zheng80825102009-11-12 09:35:36 +00004020int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4021 struct btrfs_root *root,
4022 struct inode *inode,
4023 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004024{
Chris Mason39279cc2007-06-12 06:35:45 -04004025 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004026 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004027 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004028 struct btrfs_key key;
4029 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004030 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004031 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004032 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004033 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004034 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004035 int found_extent;
4036 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004037 int pending_del_nr = 0;
4038 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004039 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004040 int ret;
4041 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004042 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004043
4044 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004045
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004046 path = btrfs_alloc_path();
4047 if (!path)
4048 return -ENOMEM;
4049 path->reada = -1;
4050
Josef Bacik5dc562c2012-08-17 13:14:17 -04004051 /*
4052 * We want to drop from the next block forward in case this new size is
4053 * not block aligned since we will be keeping the last block of the
4054 * extent just the way it is.
4055 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004056 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004057 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4058 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004059
Miao Xie16cdcec2011-04-22 18:12:22 +08004060 /*
4061 * This function is also used to drop the items in the log tree before
4062 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4063 * it is used to drop the loged items. So we shouldn't kill the delayed
4064 * items.
4065 */
4066 if (min_type == 0 && root == BTRFS_I(inode)->root)
4067 btrfs_kill_delayed_inode_items(inode);
4068
Li Zefan33345d012011-04-20 10:31:50 +08004069 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004070 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004071 key.type = (u8)-1;
4072
Chris Mason85e21ba2008-01-29 15:11:36 -05004073search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004074 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004075 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004076 if (ret < 0) {
4077 err = ret;
4078 goto out;
4079 }
Chris Masond3977122009-01-05 21:25:51 -05004080
Chris Mason85e21ba2008-01-29 15:11:36 -05004081 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004082 /* there are no items in the tree for us to truncate, we're
4083 * done
4084 */
Yan, Zheng80825102009-11-12 09:35:36 +00004085 if (path->slots[0] == 0)
4086 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004087 path->slots[0]--;
4088 }
4089
Chris Masond3977122009-01-05 21:25:51 -05004090 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004091 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004092 leaf = path->nodes[0];
4093 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4094 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004095
Li Zefan33345d012011-04-20 10:31:50 +08004096 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004097 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004098
Chris Mason85e21ba2008-01-29 15:11:36 -05004099 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004100 break;
4101
Chris Mason5f39d392007-10-15 16:14:19 -04004102 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004103 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004104 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004105 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004106 extent_type = btrfs_file_extent_type(leaf, fi);
4107 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004108 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004109 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004110 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004111 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004112 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004113 }
Yan008630c2007-11-07 13:31:09 -05004114 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004115 }
Yan, Zheng80825102009-11-12 09:35:36 +00004116 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004117 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004118 } else {
4119 if (item_end < new_size)
4120 break;
4121 if (found_key.offset >= new_size)
4122 del_item = 1;
4123 else
4124 del_item = 0;
4125 }
Chris Mason39279cc2007-06-12 06:35:45 -04004126 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004127 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004128 if (found_type != BTRFS_EXTENT_DATA_KEY)
4129 goto delete;
4130
4131 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004132 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004133 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004134 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004135 u64 orig_num_bytes =
4136 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004137 extent_num_bytes = ALIGN(new_size -
4138 found_key.offset,
4139 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004140 btrfs_set_file_extent_num_bytes(leaf, fi,
4141 extent_num_bytes);
4142 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004143 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004144 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004145 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004146 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004147 } else {
Chris Masondb945352007-10-15 16:15:53 -04004148 extent_num_bytes =
4149 btrfs_file_extent_disk_num_bytes(leaf,
4150 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004151 extent_offset = found_key.offset -
4152 btrfs_file_extent_offset(leaf, fi);
4153
Chris Mason39279cc2007-06-12 06:35:45 -04004154 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004155 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004156 if (extent_start != 0) {
4157 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004158 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004159 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004160 }
4161 }
Chris Mason90692182008-02-08 13:49:28 -05004162 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004163 /*
4164 * we can't truncate inline items that have had
4165 * special encodings
4166 */
4167 if (!del_item &&
4168 btrfs_file_extent_compression(leaf, fi) == 0 &&
4169 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4170 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004171 u32 size = new_size - found_key.offset;
4172
4173 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004174 inode_sub_bytes(inode, item_end + 1 -
4175 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004176 }
4177 size =
4178 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004179 btrfs_truncate_item(trans, root, path,
4180 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004181 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004182 inode_sub_bytes(inode, item_end + 1 -
4183 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004184 }
Chris Mason39279cc2007-06-12 06:35:45 -04004185 }
Chris Mason179e29e2007-11-01 11:28:41 -04004186delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004187 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004188 if (!pending_del_nr) {
4189 /* no pending yet, add ourselves */
4190 pending_del_slot = path->slots[0];
4191 pending_del_nr = 1;
4192 } else if (pending_del_nr &&
4193 path->slots[0] + 1 == pending_del_slot) {
4194 /* hop on the pending chunk */
4195 pending_del_nr++;
4196 pending_del_slot = path->slots[0];
4197 } else {
Chris Masond3977122009-01-05 21:25:51 -05004198 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004199 }
Chris Mason39279cc2007-06-12 06:35:45 -04004200 } else {
4201 break;
4202 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004203 if (found_extent && (root->ref_cows ||
4204 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004205 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004206 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004207 extent_num_bytes, 0,
4208 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004209 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004210 BUG_ON(ret);
4211 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004212
Yan, Zheng80825102009-11-12 09:35:36 +00004213 if (found_type == BTRFS_INODE_ITEM_KEY)
4214 break;
4215
4216 if (path->slots[0] == 0 ||
4217 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004218 if (pending_del_nr) {
4219 ret = btrfs_del_items(trans, root, path,
4220 pending_del_slot,
4221 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004222 if (ret) {
4223 btrfs_abort_transaction(trans,
4224 root, ret);
4225 goto error;
4226 }
Yan, Zheng80825102009-11-12 09:35:36 +00004227 pending_del_nr = 0;
4228 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004229 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004230 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004231 } else {
4232 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004233 }
Chris Mason39279cc2007-06-12 06:35:45 -04004234 }
Yan, Zheng80825102009-11-12 09:35:36 +00004235out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004236 if (pending_del_nr) {
4237 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4238 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004239 if (ret)
4240 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004241 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004242error:
Chris Mason39279cc2007-06-12 06:35:45 -04004243 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004244 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004245}
4246
Chris Masona52d9a82007-08-27 16:49:44 -04004247/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004248 * btrfs_truncate_page - read, zero a chunk and write a page
4249 * @inode - inode that we're zeroing
4250 * @from - the offset to start zeroing
4251 * @len - the length to zero, 0 to zero the entire range respective to the
4252 * offset
4253 * @front - zero up to the offset instead of from the offset on
4254 *
4255 * This will find the page for the "from" offset and cow the page and zero the
4256 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004257 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004258int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4259 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004260{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004261 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004262 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004263 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4264 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004265 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004266 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004267 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004268 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4269 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4270 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004271 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004272 int ret = 0;
4273 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004274 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004275
Josef Bacik2aaa6652012-08-29 14:27:18 -04004276 if ((offset & (blocksize - 1)) == 0 &&
4277 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004278 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004279 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004280 if (ret)
4281 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004282
Chris Mason211c17f2008-05-15 09:13:45 -04004283again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004284 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004285 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004286 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004287 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004288 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004289 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004290
4291 page_start = page_offset(page);
4292 page_end = page_start + PAGE_CACHE_SIZE - 1;
4293
Chris Masona52d9a82007-08-27 16:49:44 -04004294 if (!PageUptodate(page)) {
4295 ret = btrfs_readpage(NULL, page);
4296 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004297 if (page->mapping != mapping) {
4298 unlock_page(page);
4299 page_cache_release(page);
4300 goto again;
4301 }
Chris Masona52d9a82007-08-27 16:49:44 -04004302 if (!PageUptodate(page)) {
4303 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004304 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004305 }
4306 }
Chris Mason211c17f2008-05-15 09:13:45 -04004307 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004308
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004309 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004310 set_page_extent_mapped(page);
4311
4312 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4313 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004314 unlock_extent_cached(io_tree, page_start, page_end,
4315 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004316 unlock_page(page);
4317 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004318 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004319 btrfs_put_ordered_extent(ordered);
4320 goto again;
4321 }
4322
Josef Bacik2ac55d42010-02-03 19:33:23 +00004323 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004324 EXTENT_DIRTY | EXTENT_DELALLOC |
4325 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004326 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004327
Josef Bacik2ac55d42010-02-03 19:33:23 +00004328 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4329 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004330 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004331 unlock_extent_cached(io_tree, page_start, page_end,
4332 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004333 goto out_unlock;
4334 }
4335
Chris Masone6dcd2d2008-07-17 12:53:50 -04004336 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004337 if (!len)
4338 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004339 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004340 if (front)
4341 memset(kaddr, 0, offset);
4342 else
4343 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004344 flush_dcache_page(page);
4345 kunmap(page);
4346 }
Chris Mason247e7432008-07-17 12:53:51 -04004347 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004348 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004349 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4350 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004351
Chris Mason89642222008-07-24 09:41:53 -04004352out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004353 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004354 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004355 unlock_page(page);
4356 page_cache_release(page);
4357out:
4358 return ret;
4359}
4360
Josef Bacik695a0d02011-03-04 15:46:53 -05004361/*
4362 * This function puts in dummy file extents for the area we're creating a hole
4363 * for. So if we are truncating this file to a larger size we need to insert
4364 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4365 * the range between oldsize and size
4366 */
Josef Bacika41ad392011-01-31 15:30:16 -05004367int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004368{
4369 struct btrfs_trans_handle *trans;
4370 struct btrfs_root *root = BTRFS_I(inode)->root;
4371 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004372 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004373 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004374 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004375 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4376 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004377 u64 last_byte;
4378 u64 cur_offset;
4379 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004380 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004381
4382 if (size <= hole_start)
4383 return 0;
4384
Yan Zheng9036c102008-10-30 14:19:41 -04004385 while (1) {
4386 struct btrfs_ordered_extent *ordered;
4387 btrfs_wait_ordered_range(inode, hole_start,
4388 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004389 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004390 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004391 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4392 if (!ordered)
4393 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004394 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4395 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004396 btrfs_put_ordered_extent(ordered);
4397 }
4398
Yan Zheng9036c102008-10-30 14:19:41 -04004399 cur_offset = hole_start;
4400 while (1) {
4401 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4402 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004403 if (IS_ERR(em)) {
4404 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004405 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004406 break;
4407 }
Yan Zheng9036c102008-10-30 14:19:41 -04004408 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004409 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004410 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004411 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004412 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004413
Miao Xie36423202011-12-14 20:12:02 -05004414 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004415 if (IS_ERR(trans)) {
4416 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004417 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004418 }
Yan, Zheng80825102009-11-12 09:35:36 +00004419
Josef Bacik5dc562c2012-08-17 13:14:17 -04004420 err = btrfs_drop_extents(trans, root, inode,
4421 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004422 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004423 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004424 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004425 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004426 break;
Miao Xie5b397372011-09-11 10:52:24 -04004427 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004428
Yan Zheng9036c102008-10-30 14:19:41 -04004429 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004430 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004431 0, hole_size, 0, hole_size,
4432 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004433 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004434 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004435 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004436 break;
Miao Xie5b397372011-09-11 10:52:24 -04004437 }
Yan, Zheng80825102009-11-12 09:35:36 +00004438
Josef Bacik5dc562c2012-08-17 13:14:17 -04004439 btrfs_drop_extent_cache(inode, cur_offset,
4440 cur_offset + hole_size - 1, 0);
4441 hole_em = alloc_extent_map();
4442 if (!hole_em) {
4443 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4444 &BTRFS_I(inode)->runtime_flags);
4445 goto next;
4446 }
4447 hole_em->start = cur_offset;
4448 hole_em->len = hole_size;
4449 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004450
Josef Bacik5dc562c2012-08-17 13:14:17 -04004451 hole_em->block_start = EXTENT_MAP_HOLE;
4452 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004453 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004454 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4455 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4456 hole_em->generation = trans->transid;
4457
4458 while (1) {
4459 write_lock(&em_tree->lock);
4460 err = add_extent_mapping(em_tree, hole_em);
4461 if (!err)
4462 list_move(&hole_em->list,
4463 &em_tree->modified_extents);
4464 write_unlock(&em_tree->lock);
4465 if (err != -EEXIST)
4466 break;
4467 btrfs_drop_extent_cache(inode, cur_offset,
4468 cur_offset +
4469 hole_size - 1, 0);
4470 }
4471 free_extent_map(hole_em);
4472next:
Miao Xie36423202011-12-14 20:12:02 -05004473 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004474 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004475 }
4476 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004477 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004478 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004479 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004480 break;
4481 }
4482
Yan, Zhenga22285a2010-05-16 10:48:46 -04004483 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004484 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4485 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004486 return err;
4487}
4488
Eric Sandeen3972f262013-01-12 02:57:22 +00004489static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004490{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004491 struct btrfs_root *root = BTRFS_I(inode)->root;
4492 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004493 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004494 loff_t newsize = attr->ia_size;
4495 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004496 int ret;
4497
Josef Bacika41ad392011-01-31 15:30:16 -05004498 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004499 return 0;
4500
Eric Sandeen3972f262013-01-12 02:57:22 +00004501 /*
4502 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4503 * special case where we need to update the times despite not having
4504 * these flags set. For all other operations the VFS set these flags
4505 * explicitly if it wants a timestamp update.
4506 */
4507 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4508 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4509
Josef Bacika41ad392011-01-31 15:30:16 -05004510 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004511 truncate_pagecache(inode, oldsize, newsize);
4512 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004513 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004514 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004515
Miao Xief4a2f4c2011-12-14 20:12:01 -05004516 trans = btrfs_start_transaction(root, 1);
4517 if (IS_ERR(trans))
4518 return PTR_ERR(trans);
4519
4520 i_size_write(inode, newsize);
4521 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4522 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004523 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004524 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004525
Josef Bacika41ad392011-01-31 15:30:16 -05004526 /*
4527 * We're truncating a file that used to have good data down to
4528 * zero. Make sure it gets into the ordered flush list so that
4529 * any new writes get down to disk quickly.
4530 */
4531 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004532 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4533 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004534
Josef Bacikf3fe8202013-01-07 17:03:21 -05004535 /*
4536 * 1 for the orphan item we're going to add
4537 * 1 for the orphan item deletion.
4538 */
4539 trans = btrfs_start_transaction(root, 2);
4540 if (IS_ERR(trans))
4541 return PTR_ERR(trans);
4542
4543 /*
4544 * We need to do this in case we fail at _any_ point during the
4545 * actual truncate. Once we do the truncate_setsize we could
4546 * invalidate pages which forces any outstanding ordered io to
4547 * be instantly completed which will give us extents that need
4548 * to be truncated. If we fail to get an orphan inode down we
4549 * could have left over extents that were never meant to live,
4550 * so we need to garuntee from this point on that everything
4551 * will be consistent.
4552 */
4553 ret = btrfs_orphan_add(trans, inode);
4554 btrfs_end_transaction(trans, root);
4555 if (ret)
4556 return ret;
4557
Josef Bacika41ad392011-01-31 15:30:16 -05004558 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4559 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004560
4561 /* Disable nonlocked read DIO to avoid the end less truncate */
4562 btrfs_inode_block_unlocked_dio(inode);
4563 inode_dio_wait(inode);
4564 btrfs_inode_resume_unlocked_dio(inode);
4565
Josef Bacika41ad392011-01-31 15:30:16 -05004566 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004567 if (ret && inode->i_nlink)
4568 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004569 }
4570
Josef Bacika41ad392011-01-31 15:30:16 -05004571 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004572}
4573
Chris Mason39279cc2007-06-12 06:35:45 -04004574static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4575{
4576 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004577 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004578 int err;
4579
Li Zefanb83cc962010-12-20 16:04:08 +08004580 if (btrfs_root_readonly(root))
4581 return -EROFS;
4582
Chris Mason39279cc2007-06-12 06:35:45 -04004583 err = inode_change_ok(inode, attr);
4584 if (err)
4585 return err;
4586
Chris Mason5a3f23d2009-03-31 13:27:11 -04004587 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004588 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004589 if (err)
4590 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004591 }
Yan Zheng9036c102008-10-30 14:19:41 -04004592
Christoph Hellwig10257742010-06-04 11:30:02 +02004593 if (attr->ia_valid) {
4594 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004595 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004596 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004597
Josef Bacik22c44fe2011-11-30 10:45:38 -05004598 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004599 err = btrfs_acl_chmod(inode);
4600 }
4601
Chris Mason39279cc2007-06-12 06:35:45 -04004602 return err;
4603}
Chris Mason61295eb2008-01-14 16:24:38 -05004604
Al Virobd555972010-06-07 11:35:40 -04004605void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004606{
4607 struct btrfs_trans_handle *trans;
4608 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004609 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004610 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004611 int ret;
4612
liubo1abe9b82011-03-24 11:18:59 +00004613 trace_btrfs_inode_evict(inode);
4614
Chris Mason39279cc2007-06-12 06:35:45 -04004615 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004616 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004617 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004618 goto no_delete;
4619
Chris Mason39279cc2007-06-12 06:35:45 -04004620 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004621 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004622 goto no_delete;
4623 }
Al Virobd555972010-06-07 11:35:40 -04004624 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004625 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004626
Yan, Zhengc71bf092009-11-12 09:34:40 +00004627 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004628 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004629 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004630 goto no_delete;
4631 }
4632
Yan, Zheng76dda932009-09-21 16:00:26 -04004633 if (inode->i_nlink > 0) {
4634 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4635 goto no_delete;
4636 }
4637
Miao Xie0e8c36a2012-12-19 06:59:51 +00004638 ret = btrfs_commit_inode_delayed_inode(inode);
4639 if (ret) {
4640 btrfs_orphan_del(NULL, inode);
4641 goto no_delete;
4642 }
4643
Miao Xie66d8f3d2012-09-06 04:02:28 -06004644 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004645 if (!rsv) {
4646 btrfs_orphan_del(NULL, inode);
4647 goto no_delete;
4648 }
Josef Bacik4a338542011-08-29 11:01:31 -04004649 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004650 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004651 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004652
Chris Masondbe674a2008-07-17 12:54:05 -04004653 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004654
Josef Bacik4289a662011-08-05 13:22:24 -04004655 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004656 * This is a bit simpler than btrfs_truncate since we've already
4657 * reserved our space for our orphan item in the unlink, so we just
4658 * need to reserve some slack space in case we add bytes and update
4659 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004660 */
Yan, Zheng80825102009-11-12 09:35:36 +00004661 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004662 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4663 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004664
Josef Bacik726c35f2011-09-26 15:46:06 -04004665 /*
4666 * Try and steal from the global reserve since we will
4667 * likely not use this space anyway, we want to try as
4668 * hard as possible to get this to work.
4669 */
4670 if (ret)
4671 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4672
Yan, Zhengd68fc572010-05-16 10:49:58 -04004673 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04004674 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04004675 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004676 btrfs_orphan_del(NULL, inode);
4677 btrfs_free_block_rsv(root, rsv);
4678 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004679 }
4680
Miao Xie0e8c36a2012-12-19 06:59:51 +00004681 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004682 if (IS_ERR(trans)) {
4683 btrfs_orphan_del(NULL, inode);
4684 btrfs_free_block_rsv(root, rsv);
4685 goto no_delete;
4686 }
4687
4688 trans->block_rsv = rsv;
4689
Yan, Zhengd68fc572010-05-16 10:49:58 -04004690 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004691 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004692 break;
4693
Miao Xie8407aa42012-09-07 01:43:32 -06004694 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004695 btrfs_end_transaction(trans, root);
4696 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004697 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004698 }
4699
Josef Bacik4289a662011-08-05 13:22:24 -04004700 btrfs_free_block_rsv(root, rsv);
4701
Yan, Zheng80825102009-11-12 09:35:36 +00004702 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004703 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004704 ret = btrfs_orphan_del(trans, inode);
4705 BUG_ON(ret);
4706 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004707
Josef Bacik4289a662011-08-05 13:22:24 -04004708 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004709 if (!(root == root->fs_info->tree_root ||
4710 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004711 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004712
Chris Mason54aa1f42007-06-22 14:16:25 -04004713 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004714 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004715no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004716 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004717 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004718}
4719
4720/*
4721 * this returns the key found in the dir entry in the location pointer.
4722 * If no dir entries were found, location->objectid is 0.
4723 */
4724static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4725 struct btrfs_key *location)
4726{
4727 const char *name = dentry->d_name.name;
4728 int namelen = dentry->d_name.len;
4729 struct btrfs_dir_item *di;
4730 struct btrfs_path *path;
4731 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004732 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004733
4734 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004735 if (!path)
4736 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004737
Li Zefan33345d012011-04-20 10:31:50 +08004738 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004739 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004740 if (IS_ERR(di))
4741 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004742
David Sterbac7040052011-04-19 18:00:01 +02004743 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004744 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004745
Chris Mason5f39d392007-10-15 16:14:19 -04004746 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004747out:
Chris Mason39279cc2007-06-12 06:35:45 -04004748 btrfs_free_path(path);
4749 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004750out_err:
4751 location->objectid = 0;
4752 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004753}
4754
4755/*
4756 * when we hit a tree root in a directory, the btrfs part of the inode
4757 * needs to be changed to reflect the root directory of the tree root. This
4758 * is kind of like crossing a mount point.
4759 */
4760static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004761 struct inode *dir,
4762 struct dentry *dentry,
4763 struct btrfs_key *location,
4764 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004765{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004766 struct btrfs_path *path;
4767 struct btrfs_root *new_root;
4768 struct btrfs_root_ref *ref;
4769 struct extent_buffer *leaf;
4770 int ret;
4771 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004772
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004773 path = btrfs_alloc_path();
4774 if (!path) {
4775 err = -ENOMEM;
4776 goto out;
4777 }
Chris Mason39279cc2007-06-12 06:35:45 -04004778
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004779 err = -ENOENT;
4780 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4781 BTRFS_I(dir)->root->root_key.objectid,
4782 location->objectid);
4783 if (ret) {
4784 if (ret < 0)
4785 err = ret;
4786 goto out;
4787 }
Chris Mason39279cc2007-06-12 06:35:45 -04004788
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004789 leaf = path->nodes[0];
4790 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004791 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004792 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4793 goto out;
4794
4795 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4796 (unsigned long)(ref + 1),
4797 dentry->d_name.len);
4798 if (ret)
4799 goto out;
4800
David Sterbab3b4aa72011-04-21 01:20:15 +02004801 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004802
4803 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4804 if (IS_ERR(new_root)) {
4805 err = PTR_ERR(new_root);
4806 goto out;
4807 }
4808
4809 if (btrfs_root_refs(&new_root->root_item) == 0) {
4810 err = -ENOENT;
4811 goto out;
4812 }
4813
4814 *sub_root = new_root;
4815 location->objectid = btrfs_root_dirid(&new_root->root_item);
4816 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004817 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004818 err = 0;
4819out:
4820 btrfs_free_path(path);
4821 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004822}
4823
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004824static void inode_tree_add(struct inode *inode)
4825{
4826 struct btrfs_root *root = BTRFS_I(inode)->root;
4827 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004828 struct rb_node **p;
4829 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004830 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004831again:
4832 p = &root->inode_tree.rb_node;
4833 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004834
Al Viro1d3382cb2010-10-23 15:19:20 -04004835 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004836 return;
4837
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004838 spin_lock(&root->inode_lock);
4839 while (*p) {
4840 parent = *p;
4841 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4842
Li Zefan33345d012011-04-20 10:31:50 +08004843 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004844 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004845 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004846 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004847 else {
4848 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004849 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004850 rb_erase(parent, &root->inode_tree);
4851 RB_CLEAR_NODE(parent);
4852 spin_unlock(&root->inode_lock);
4853 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004854 }
4855 }
4856 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4857 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4858 spin_unlock(&root->inode_lock);
4859}
4860
4861static void inode_tree_del(struct inode *inode)
4862{
4863 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004864 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004865
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004866 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004867 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004868 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004869 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004870 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004871 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004872 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004873
Josef Bacik0af3d002010-06-21 14:48:16 -04004874 /*
4875 * Free space cache has inodes in the tree root, but the tree root has a
4876 * root_refs of 0, so this could end up dropping the tree root as a
4877 * snapshot, so we need the extra !root->fs_info->tree_root check to
4878 * make sure we don't drop it.
4879 */
4880 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4881 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004882 synchronize_srcu(&root->fs_info->subvol_srcu);
4883 spin_lock(&root->inode_lock);
4884 empty = RB_EMPTY_ROOT(&root->inode_tree);
4885 spin_unlock(&root->inode_lock);
4886 if (empty)
4887 btrfs_add_dead_root(root);
4888 }
4889}
4890
Jeff Mahoney143bede2012-03-01 14:56:26 +01004891void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004892{
4893 struct rb_node *node;
4894 struct rb_node *prev;
4895 struct btrfs_inode *entry;
4896 struct inode *inode;
4897 u64 objectid = 0;
4898
4899 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4900
4901 spin_lock(&root->inode_lock);
4902again:
4903 node = root->inode_tree.rb_node;
4904 prev = NULL;
4905 while (node) {
4906 prev = node;
4907 entry = rb_entry(node, struct btrfs_inode, rb_node);
4908
Li Zefan33345d012011-04-20 10:31:50 +08004909 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004910 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004911 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004912 node = node->rb_right;
4913 else
4914 break;
4915 }
4916 if (!node) {
4917 while (prev) {
4918 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004919 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004920 node = prev;
4921 break;
4922 }
4923 prev = rb_next(prev);
4924 }
4925 }
4926 while (node) {
4927 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004928 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004929 inode = igrab(&entry->vfs_inode);
4930 if (inode) {
4931 spin_unlock(&root->inode_lock);
4932 if (atomic_read(&inode->i_count) > 1)
4933 d_prune_aliases(inode);
4934 /*
Al Viro45321ac2010-06-07 13:43:19 -04004935 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004936 * the inode cache when its usage count
4937 * hits zero.
4938 */
4939 iput(inode);
4940 cond_resched();
4941 spin_lock(&root->inode_lock);
4942 goto again;
4943 }
4944
4945 if (cond_resched_lock(&root->inode_lock))
4946 goto again;
4947
4948 node = rb_next(node);
4949 }
4950 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004951}
4952
Chris Masone02119d2008-09-05 16:13:11 -04004953static int btrfs_init_locked_inode(struct inode *inode, void *p)
4954{
4955 struct btrfs_iget_args *args = p;
4956 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004957 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004958 return 0;
4959}
4960
4961static int btrfs_find_actor(struct inode *inode, void *opaque)
4962{
4963 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004964 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004965 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004966}
4967
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004968static struct inode *btrfs_iget_locked(struct super_block *s,
4969 u64 objectid,
4970 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004971{
4972 struct inode *inode;
4973 struct btrfs_iget_args args;
4974 args.ino = objectid;
4975 args.root = root;
4976
4977 inode = iget5_locked(s, objectid, btrfs_find_actor,
4978 btrfs_init_locked_inode,
4979 (void *)&args);
4980 return inode;
4981}
4982
Balaji Rao1a54ef82008-07-21 02:01:04 +05304983/* Get an inode object given its location and corresponding root.
4984 * Returns in *is_new if the inode was read from disk
4985 */
4986struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004987 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304988{
4989 struct inode *inode;
4990
4991 inode = btrfs_iget_locked(s, location->objectid, root);
4992 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004993 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304994
4995 if (inode->i_state & I_NEW) {
4996 BTRFS_I(inode)->root = root;
4997 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4998 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004999 if (!is_bad_inode(inode)) {
5000 inode_tree_add(inode);
5001 unlock_new_inode(inode);
5002 if (new)
5003 *new = 1;
5004 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005005 unlock_new_inode(inode);
5006 iput(inode);
5007 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005008 }
5009 }
5010
Balaji Rao1a54ef82008-07-21 02:01:04 +05305011 return inode;
5012}
5013
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005014static struct inode *new_simple_dir(struct super_block *s,
5015 struct btrfs_key *key,
5016 struct btrfs_root *root)
5017{
5018 struct inode *inode = new_inode(s);
5019
5020 if (!inode)
5021 return ERR_PTR(-ENOMEM);
5022
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005023 BTRFS_I(inode)->root = root;
5024 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005025 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005026
5027 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005028 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005029 inode->i_fop = &simple_dir_operations;
5030 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5031 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5032
5033 return inode;
5034}
5035
Chris Mason3de45862008-11-17 21:02:50 -05005036struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005037{
Chris Masond3977122009-01-05 21:25:51 -05005038 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005039 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005040 struct btrfs_root *sub_root = root;
5041 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005042 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005043 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005044
5045 if (dentry->d_name.len > BTRFS_NAME_LEN)
5046 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005047
Jeff Layton39e3c952012-11-28 11:30:53 -05005048 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005049 if (ret < 0)
5050 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005051
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005052 if (location.objectid == 0)
5053 return NULL;
5054
5055 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005056 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005057 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005058 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005059
5060 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5061
Yan, Zheng76dda932009-09-21 16:00:26 -04005062 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005063 ret = fixup_tree_root_location(root, dir, dentry,
5064 &location, &sub_root);
5065 if (ret < 0) {
5066 if (ret != -ENOENT)
5067 inode = ERR_PTR(ret);
5068 else
5069 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5070 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005071 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005072 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005073 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5074
Julia Lawall34d19ba2011-01-24 19:55:19 +00005075 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005076 down_read(&root->fs_info->cleanup_work_sem);
5077 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005078 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005079 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005080 if (ret)
5081 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005082 }
5083
Chris Mason3de45862008-11-17 21:02:50 -05005084 return inode;
5085}
5086
Nick Pigginfe15ce42011-01-07 17:49:23 +11005087static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005088{
5089 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005090 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005091
Li Zefan848cce02012-02-21 17:04:28 +08005092 if (!inode && !IS_ROOT(dentry))
5093 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005094
Li Zefan848cce02012-02-21 17:04:28 +08005095 if (inode) {
5096 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005097 if (btrfs_root_refs(&root->root_item) == 0)
5098 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005099
5100 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5101 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005102 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005103 return 0;
5104}
5105
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005106static void btrfs_dentry_release(struct dentry *dentry)
5107{
5108 if (dentry->d_fsdata)
5109 kfree(dentry->d_fsdata);
5110}
5111
Chris Mason3de45862008-11-17 21:02:50 -05005112static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005113 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005114{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005115 struct dentry *ret;
5116
5117 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005118 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005119}
5120
Miao Xie16cdcec2011-04-22 18:12:22 +08005121unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005122 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5123};
5124
David Woodhousecbdf5a22008-08-06 19:42:33 +01005125static int btrfs_real_readdir(struct file *filp, void *dirent,
5126 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005127{
Al Viro496ad9a2013-01-23 17:07:38 -05005128 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005129 struct btrfs_root *root = BTRFS_I(inode)->root;
5130 struct btrfs_item *item;
5131 struct btrfs_dir_item *di;
5132 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005133 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005134 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005135 struct list_head ins_list;
5136 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005137 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005138 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005139 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005140 unsigned char d_type;
5141 int over = 0;
5142 u32 di_cur;
5143 u32 di_total;
5144 u32 di_len;
5145 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005146 char tmp_name[32];
5147 char *name_ptr;
5148 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005149 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005150
5151 /* FIXME, use a real flag for deciding about the key type */
5152 if (root->fs_info->tree_root == root)
5153 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005154
Chris Mason39544012007-12-12 14:38:19 -05005155 /* special case for "." */
5156 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005157 over = filldir(dirent, ".", 1,
5158 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005159 if (over)
5160 return 0;
5161 filp->f_pos = 1;
5162 }
Chris Mason39544012007-12-12 14:38:19 -05005163 /* special case for .., just use the back ref */
5164 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005165 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005166 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005167 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005168 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005169 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005170 filp->f_pos = 2;
5171 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005172 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005173 if (!path)
5174 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005175
Josef Bacik026fd312011-05-13 10:32:11 -04005176 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005177
Miao Xie16cdcec2011-04-22 18:12:22 +08005178 if (key_type == BTRFS_DIR_INDEX_KEY) {
5179 INIT_LIST_HEAD(&ins_list);
5180 INIT_LIST_HEAD(&del_list);
5181 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5182 }
5183
Chris Mason39279cc2007-06-12 06:35:45 -04005184 btrfs_set_key_type(&key, key_type);
5185 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005186 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005187
Chris Mason39279cc2007-06-12 06:35:45 -04005188 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5189 if (ret < 0)
5190 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005191
5192 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005193 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005194 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005195 if (slot >= btrfs_header_nritems(leaf)) {
5196 ret = btrfs_next_leaf(root, path);
5197 if (ret < 0)
5198 goto err;
5199 else if (ret > 0)
5200 break;
5201 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005202 }
Chris Mason3de45862008-11-17 21:02:50 -05005203
Chris Mason5f39d392007-10-15 16:14:19 -04005204 item = btrfs_item_nr(leaf, slot);
5205 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5206
5207 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005208 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005209 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005210 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005211 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005212 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005213 if (key_type == BTRFS_DIR_INDEX_KEY &&
5214 btrfs_should_delete_dir_index(&del_list,
5215 found_key.offset))
5216 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005217
5218 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005219 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005220
Chris Mason39279cc2007-06-12 06:35:45 -04005221 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5222 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005223 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005224
5225 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005226 struct btrfs_key location;
5227
Josef Bacik22a94d42011-03-16 16:47:17 -04005228 if (verify_dir_item(root, leaf, di))
5229 break;
5230
Chris Mason5f39d392007-10-15 16:14:19 -04005231 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005232 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005233 name_ptr = tmp_name;
5234 } else {
5235 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005236 if (!name_ptr) {
5237 ret = -ENOMEM;
5238 goto err;
5239 }
Chris Mason5f39d392007-10-15 16:14:19 -04005240 }
5241 read_extent_buffer(leaf, name_ptr,
5242 (unsigned long)(di + 1), name_len);
5243
5244 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5245 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005246
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005247
Chris Mason3de45862008-11-17 21:02:50 -05005248 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005249 * skip it.
5250 *
5251 * In contrast to old kernels, we insert the snapshot's
5252 * dir item and dir index after it has been created, so
5253 * we won't find a reference to our own snapshot. We
5254 * still keep the following code for backward
5255 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005256 */
5257 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5258 location.objectid == root->root_key.objectid) {
5259 over = 0;
5260 goto skip;
5261 }
Chris Mason5f39d392007-10-15 16:14:19 -04005262 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005263 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005264 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005265
Chris Mason3de45862008-11-17 21:02:50 -05005266skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005267 if (name_ptr != tmp_name)
5268 kfree(name_ptr);
5269
Chris Mason39279cc2007-06-12 06:35:45 -04005270 if (over)
5271 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005272 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005273 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005274 di_cur += di_len;
5275 di = (struct btrfs_dir_item *)((char *)di + di_len);
5276 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005277next:
5278 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005279 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005280
Miao Xie16cdcec2011-04-22 18:12:22 +08005281 if (key_type == BTRFS_DIR_INDEX_KEY) {
5282 if (is_curr)
5283 filp->f_pos++;
5284 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5285 &ins_list);
5286 if (ret)
5287 goto nopos;
5288 }
5289
David Woodhouse49593bf2008-08-17 17:08:36 +01005290 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005291 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005292 /*
5293 * 32-bit glibc will use getdents64, but then strtol -
5294 * so the last number we can serve is this.
5295 */
5296 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005297 else
5298 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005299nopos:
5300 ret = 0;
5301err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005302 if (key_type == BTRFS_DIR_INDEX_KEY)
5303 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005304 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005305 return ret;
5306}
5307
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005308int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005309{
5310 struct btrfs_root *root = BTRFS_I(inode)->root;
5311 struct btrfs_trans_handle *trans;
5312 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005313 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005314
Josef Bacik72ac3c02012-05-23 14:13:11 -04005315 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005316 return 0;
5317
Liu Bo83eea1f2012-07-10 05:28:39 -06005318 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005319 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005320
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005321 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005322 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005323 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005324 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005325 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005326 if (IS_ERR(trans))
5327 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005328 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005329 }
5330 return ret;
5331}
5332
5333/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005334 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005335 * inode changes. But, it is most likely to find the inode in cache.
5336 * FIXME, needs more benchmarking...there are no reasons other than performance
5337 * to keep or drop this code.
5338 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05005339int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005340{
5341 struct btrfs_root *root = BTRFS_I(inode)->root;
5342 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005343 int ret;
5344
Josef Bacik72ac3c02012-05-23 14:13:11 -04005345 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005346 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005347
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005348 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005349 if (IS_ERR(trans))
5350 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005351
5352 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005353 if (ret && ret == -ENOSPC) {
5354 /* whoops, lets try again with the full transaction */
5355 btrfs_end_transaction(trans, root);
5356 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005357 if (IS_ERR(trans))
5358 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005359
Chris Mason94b60442010-05-26 11:02:00 -04005360 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005361 }
Chris Mason39279cc2007-06-12 06:35:45 -04005362 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005363 if (BTRFS_I(inode)->delayed_node)
5364 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005365
5366 return ret;
5367}
5368
5369/*
5370 * This is a copy of file_update_time. We need this so we can return error on
5371 * ENOSPC for updating the inode in the case of file write and mmap writes.
5372 */
Josef Bacike41f9412012-03-26 09:46:47 -04005373static int btrfs_update_time(struct inode *inode, struct timespec *now,
5374 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005375{
Alexander Block2bc5565282012-06-15 09:49:33 +02005376 struct btrfs_root *root = BTRFS_I(inode)->root;
5377
5378 if (btrfs_root_readonly(root))
5379 return -EROFS;
5380
Josef Bacike41f9412012-03-26 09:46:47 -04005381 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005382 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005383 if (flags & S_CTIME)
5384 inode->i_ctime = *now;
5385 if (flags & S_MTIME)
5386 inode->i_mtime = *now;
5387 if (flags & S_ATIME)
5388 inode->i_atime = *now;
5389 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005390}
5391
Chris Masond352ac62008-09-29 15:18:18 -04005392/*
5393 * find the highest existing sequence number in a directory
5394 * and then set the in-memory index_cnt variable to reflect
5395 * free sequence numbers
5396 */
Josef Bacikaec74772008-07-24 12:12:38 -04005397static int btrfs_set_inode_index_count(struct inode *inode)
5398{
5399 struct btrfs_root *root = BTRFS_I(inode)->root;
5400 struct btrfs_key key, found_key;
5401 struct btrfs_path *path;
5402 struct extent_buffer *leaf;
5403 int ret;
5404
Li Zefan33345d012011-04-20 10:31:50 +08005405 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005406 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5407 key.offset = (u64)-1;
5408
5409 path = btrfs_alloc_path();
5410 if (!path)
5411 return -ENOMEM;
5412
5413 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5414 if (ret < 0)
5415 goto out;
5416 /* FIXME: we should be able to handle this */
5417 if (ret == 0)
5418 goto out;
5419 ret = 0;
5420
5421 /*
5422 * MAGIC NUMBER EXPLANATION:
5423 * since we search a directory based on f_pos we have to start at 2
5424 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5425 * else has to start at 2
5426 */
5427 if (path->slots[0] == 0) {
5428 BTRFS_I(inode)->index_cnt = 2;
5429 goto out;
5430 }
5431
5432 path->slots[0]--;
5433
5434 leaf = path->nodes[0];
5435 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5436
Li Zefan33345d012011-04-20 10:31:50 +08005437 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005438 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5439 BTRFS_I(inode)->index_cnt = 2;
5440 goto out;
5441 }
5442
5443 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5444out:
5445 btrfs_free_path(path);
5446 return ret;
5447}
5448
Chris Masond352ac62008-09-29 15:18:18 -04005449/*
5450 * helper to find a free sequence number in a given directory. This current
5451 * code is very simple, later versions will do smarter things in the btree
5452 */
Chris Mason3de45862008-11-17 21:02:50 -05005453int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005454{
5455 int ret = 0;
5456
5457 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005458 ret = btrfs_inode_delayed_dir_index_count(dir);
5459 if (ret) {
5460 ret = btrfs_set_inode_index_count(dir);
5461 if (ret)
5462 return ret;
5463 }
Josef Bacikaec74772008-07-24 12:12:38 -04005464 }
5465
Chris Mason00e4e6b2008-08-05 11:18:09 -04005466 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005467 BTRFS_I(dir)->index_cnt++;
5468
5469 return ret;
5470}
5471
Chris Mason39279cc2007-06-12 06:35:45 -04005472static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5473 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005474 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005475 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005476 u64 ref_objectid, u64 objectid,
5477 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005478{
5479 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005480 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005481 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005482 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005483 struct btrfs_inode_ref *ref;
5484 struct btrfs_key key[2];
5485 u32 sizes[2];
5486 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005487 int ret;
5488 int owner;
5489
Chris Mason5f39d392007-10-15 16:14:19 -04005490 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005491 if (!path)
5492 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005493
Chris Mason39279cc2007-06-12 06:35:45 -04005494 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005495 if (!inode) {
5496 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005497 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005498 }
Chris Mason39279cc2007-06-12 06:35:45 -04005499
Li Zefan581bb052011-04-20 10:06:11 +08005500 /*
5501 * we have to initialize this early, so we can reclaim the inode
5502 * number if we fail afterwards in this function.
5503 */
5504 inode->i_ino = objectid;
5505
Josef Bacikaec74772008-07-24 12:12:38 -04005506 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005507 trace_btrfs_inode_request(dir);
5508
Chris Mason3de45862008-11-17 21:02:50 -05005509 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005510 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005511 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005512 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005513 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005514 }
Josef Bacikaec74772008-07-24 12:12:38 -04005515 }
5516 /*
5517 * index_cnt is ignored for everything but a dir,
5518 * btrfs_get_inode_index_count has an explanation for the magic
5519 * number
5520 */
5521 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005522 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005523 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005524 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005525
Josef Bacik5dc562c2012-08-17 13:14:17 -04005526 /*
5527 * We could have gotten an inode number from somebody who was fsynced
5528 * and then removed in this same transaction, so let's just set full
5529 * sync since it will be a full sync anyway and this will blow away the
5530 * old info in the log.
5531 */
5532 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5533
Al Viro569254b2011-07-24 17:08:40 -04005534 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005535 owner = 0;
5536 else
5537 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005538
5539 key[0].objectid = objectid;
5540 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5541 key[0].offset = 0;
5542
Mark Fashehf1863732012-08-08 11:32:27 -07005543 /*
5544 * Start new inodes with an inode_ref. This is slightly more
5545 * efficient for small numbers of hard links since they will
5546 * be packed into one item. Extended refs will kick in if we
5547 * add more hard links than can fit in the ref item.
5548 */
Chris Mason9c583092008-01-29 15:15:18 -05005549 key[1].objectid = objectid;
5550 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5551 key[1].offset = ref_objectid;
5552
5553 sizes[0] = sizeof(struct btrfs_inode_item);
5554 sizes[1] = name_len + sizeof(*ref);
5555
Chris Masonb9473432009-03-13 11:00:37 -04005556 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005557 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5558 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005559 goto fail;
5560
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005561 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005562 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005563 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005564 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5565 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005566 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5567 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005568 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005569
5570 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5571 struct btrfs_inode_ref);
5572 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005573 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005574 ptr = (unsigned long)(ref + 1);
5575 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5576
Chris Mason5f39d392007-10-15 16:14:19 -04005577 btrfs_mark_buffer_dirty(path->nodes[0]);
5578 btrfs_free_path(path);
5579
Chris Mason39279cc2007-06-12 06:35:45 -04005580 location = &BTRFS_I(inode)->location;
5581 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005582 location->offset = 0;
5583 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5584
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005585 btrfs_inherit_iflags(inode, dir);
5586
Al Viro569254b2011-07-24 17:08:40 -04005587 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005588 if (btrfs_test_opt(root, NODATASUM))
5589 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005590 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005591 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5592 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005593 }
5594
Chris Mason39279cc2007-06-12 06:35:45 -04005595 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005596 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005597
5598 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005599 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005600
Alexander Block8ea05e32012-07-25 17:35:53 +02005601 btrfs_update_root_times(trans, root);
5602
Chris Mason39279cc2007-06-12 06:35:45 -04005603 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005604fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005605 if (dir)
5606 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005607 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005608 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005609 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005610}
5611
5612static inline u8 btrfs_inode_type(struct inode *inode)
5613{
5614 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5615}
5616
Chris Masond352ac62008-09-29 15:18:18 -04005617/*
5618 * utility function to add 'inode' into 'parent_inode' with
5619 * a give name and a given sequence number.
5620 * if 'add_backref' is true, also insert a backref from the
5621 * inode to the parent directory.
5622 */
Chris Masone02119d2008-09-05 16:13:11 -04005623int btrfs_add_link(struct btrfs_trans_handle *trans,
5624 struct inode *parent_inode, struct inode *inode,
5625 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005626{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005627 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005628 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005629 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005630 u64 ino = btrfs_ino(inode);
5631 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005632
Li Zefan33345d012011-04-20 10:31:50 +08005633 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005634 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5635 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005636 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005637 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5638 key.offset = 0;
5639 }
Chris Mason39279cc2007-06-12 06:35:45 -04005640
Li Zefan33345d012011-04-20 10:31:50 +08005641 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005642 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5643 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005644 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005645 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005646 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5647 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005648 }
5649
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005650 /* Nothing to clean up yet */
5651 if (ret)
5652 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005653
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005654 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5655 parent_inode, &key,
5656 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005657 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005658 goto fail_dir_item;
5659 else if (ret) {
5660 btrfs_abort_transaction(trans, root, ret);
5661 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005662 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005663
5664 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5665 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005666 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005667 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5668 ret = btrfs_update_inode(trans, root, parent_inode);
5669 if (ret)
5670 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005671 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005672
5673fail_dir_item:
5674 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5675 u64 local_index;
5676 int err;
5677 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5678 key.objectid, root->root_key.objectid,
5679 parent_ino, &local_index, name, name_len);
5680
5681 } else if (add_backref) {
5682 u64 local_index;
5683 int err;
5684
5685 err = btrfs_del_inode_ref(trans, root, name, name_len,
5686 ino, parent_ino, &local_index);
5687 }
5688 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005689}
5690
5691static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005692 struct inode *dir, struct dentry *dentry,
5693 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005694{
Josef Bacika1b075d2010-11-19 20:36:11 +00005695 int err = btrfs_add_link(trans, dir, inode,
5696 dentry->d_name.name, dentry->d_name.len,
5697 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005698 if (err > 0)
5699 err = -EEXIST;
5700 return err;
5701}
5702
Josef Bacik618e21d2007-07-11 10:18:17 -04005703static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005704 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005705{
5706 struct btrfs_trans_handle *trans;
5707 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005708 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005709 int err;
5710 int drop_inode = 0;
5711 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005712 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005713
5714 if (!new_valid_dev(rdev))
5715 return -EINVAL;
5716
Josef Bacik9ed74f22009-09-11 16:12:44 -04005717 /*
5718 * 2 for inode item and ref
5719 * 2 for dir items
5720 * 1 for xattr if selinux is on
5721 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005722 trans = btrfs_start_transaction(root, 5);
5723 if (IS_ERR(trans))
5724 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005725
Li Zefan581bb052011-04-20 10:06:11 +08005726 err = btrfs_find_free_ino(root, &objectid);
5727 if (err)
5728 goto out_unlock;
5729
Josef Bacikaec74772008-07-24 12:12:38 -04005730 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005731 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005732 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005733 if (IS_ERR(inode)) {
5734 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005735 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005736 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005737
Eric Paris2a7dba32011-02-01 11:05:39 -05005738 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005739 if (err) {
5740 drop_inode = 1;
5741 goto out_unlock;
5742 }
5743
Casey Schauflerad19db72011-12-15 10:09:07 -05005744 /*
5745 * If the active LSM wants to access the inode during
5746 * d_instantiate it needs these. Smack checks to see
5747 * if the filesystem supports xattrs by looking at the
5748 * ops vector.
5749 */
5750
5751 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005752 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005753 if (err)
5754 drop_inode = 1;
5755 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005756 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005757 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005758 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005759 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005760out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005761 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005762 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005763 if (drop_inode) {
5764 inode_dec_link_count(inode);
5765 iput(inode);
5766 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005767 return err;
5768}
5769
Chris Mason39279cc2007-06-12 06:35:45 -04005770static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005771 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005772{
5773 struct btrfs_trans_handle *trans;
5774 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005775 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005776 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005777 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005778 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005779 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005780
Josef Bacik9ed74f22009-09-11 16:12:44 -04005781 /*
5782 * 2 for inode item and ref
5783 * 2 for dir items
5784 * 1 for xattr if selinux is on
5785 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005786 trans = btrfs_start_transaction(root, 5);
5787 if (IS_ERR(trans))
5788 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005789
Li Zefan581bb052011-04-20 10:06:11 +08005790 err = btrfs_find_free_ino(root, &objectid);
5791 if (err)
5792 goto out_unlock;
5793
Josef Bacikaec74772008-07-24 12:12:38 -04005794 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005795 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005796 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005797 if (IS_ERR(inode)) {
5798 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005799 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005800 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005801 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005802
Eric Paris2a7dba32011-02-01 11:05:39 -05005803 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005804 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005805 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005806
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005807 err = btrfs_update_inode(trans, root, inode);
5808 if (err)
5809 goto out_unlock;
5810
Casey Schauflerad19db72011-12-15 10:09:07 -05005811 /*
5812 * If the active LSM wants to access the inode during
5813 * d_instantiate it needs these. Smack checks to see
5814 * if the filesystem supports xattrs by looking at the
5815 * ops vector.
5816 */
5817 inode->i_fop = &btrfs_file_operations;
5818 inode->i_op = &btrfs_file_inode_operations;
5819
Josef Bacika1b075d2010-11-19 20:36:11 +00005820 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005821 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005822 goto out_unlock;
5823
5824 inode->i_mapping->a_ops = &btrfs_aops;
5825 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5826 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5827 d_instantiate(dentry, inode);
5828
Chris Mason39279cc2007-06-12 06:35:45 -04005829out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005830 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005831 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005832 inode_dec_link_count(inode);
5833 iput(inode);
5834 }
Liu Bob53d3f52012-11-14 14:34:34 +00005835 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005836 return err;
5837}
5838
5839static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5840 struct dentry *dentry)
5841{
5842 struct btrfs_trans_handle *trans;
5843 struct btrfs_root *root = BTRFS_I(dir)->root;
5844 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005845 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005846 int err;
5847 int drop_inode = 0;
5848
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005849 /* do not allow sys_link's with other subvols of the same device */
5850 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005851 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005852
Mark Fashehf1863732012-08-08 11:32:27 -07005853 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005854 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005855
Chris Mason3de45862008-11-17 21:02:50 -05005856 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005857 if (err)
5858 goto fail;
5859
Yan, Zhenga22285a2010-05-16 10:48:46 -04005860 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005861 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005862 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005863 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005864 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005865 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005866 if (IS_ERR(trans)) {
5867 err = PTR_ERR(trans);
5868 goto fail;
5869 }
Chris Mason5f39d392007-10-15 16:14:19 -04005870
Miao Xie31534952011-04-13 13:19:21 +08005871 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005872 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005873 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005874 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005875 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005876
Josef Bacika1b075d2010-11-19 20:36:11 +00005877 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005878
Yan, Zhenga5719522009-09-24 09:17:31 -04005879 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005880 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005881 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005882 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005883 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005884 if (err)
5885 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005886 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005887 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005888 }
Chris Mason39279cc2007-06-12 06:35:45 -04005889
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005890 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005891fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005892 if (drop_inode) {
5893 inode_dec_link_count(inode);
5894 iput(inode);
5895 }
Liu Bob53d3f52012-11-14 14:34:34 +00005896 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005897 return err;
5898}
5899
Al Viro18bb1db2011-07-26 01:41:39 -04005900static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005901{
Chris Masonb9d86662008-05-02 16:13:49 -04005902 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005903 struct btrfs_trans_handle *trans;
5904 struct btrfs_root *root = BTRFS_I(dir)->root;
5905 int err = 0;
5906 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005907 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005908 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005909
Josef Bacik9ed74f22009-09-11 16:12:44 -04005910 /*
5911 * 2 items for inode and ref
5912 * 2 items for dir items
5913 * 1 for xattr if selinux is on
5914 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005915 trans = btrfs_start_transaction(root, 5);
5916 if (IS_ERR(trans))
5917 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005918
Li Zefan581bb052011-04-20 10:06:11 +08005919 err = btrfs_find_free_ino(root, &objectid);
5920 if (err)
5921 goto out_fail;
5922
Josef Bacikaec74772008-07-24 12:12:38 -04005923 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005924 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005925 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005926 if (IS_ERR(inode)) {
5927 err = PTR_ERR(inode);
5928 goto out_fail;
5929 }
Chris Mason5f39d392007-10-15 16:14:19 -04005930
Chris Mason39279cc2007-06-12 06:35:45 -04005931 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005932
Eric Paris2a7dba32011-02-01 11:05:39 -05005933 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005934 if (err)
5935 goto out_fail;
5936
Chris Mason39279cc2007-06-12 06:35:45 -04005937 inode->i_op = &btrfs_dir_inode_operations;
5938 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005939
Chris Masondbe674a2008-07-17 12:54:05 -04005940 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005941 err = btrfs_update_inode(trans, root, inode);
5942 if (err)
5943 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005944
Josef Bacika1b075d2010-11-19 20:36:11 +00005945 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5946 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005947 if (err)
5948 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005949
Chris Mason39279cc2007-06-12 06:35:45 -04005950 d_instantiate(dentry, inode);
5951 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005952
5953out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005954 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005955 if (drop_on_err)
5956 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005957 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005958 return err;
5959}
5960
Chris Masond352ac62008-09-29 15:18:18 -04005961/* helper for btfs_get_extent. Given an existing extent in the tree,
5962 * and an extent that you want to insert, deal with overlap and insert
5963 * the new extent into the tree.
5964 */
Chris Mason3b951512008-04-17 11:29:12 -04005965static int merge_extent_mapping(struct extent_map_tree *em_tree,
5966 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005967 struct extent_map *em,
5968 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005969{
5970 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005971
Chris Masone6dcd2d2008-07-17 12:53:50 -04005972 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5973 start_diff = map_start - em->start;
5974 em->start = map_start;
5975 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005976 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5977 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005978 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005979 em->block_len -= start_diff;
5980 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005981 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005982}
5983
Chris Masonc8b97812008-10-29 14:49:59 -04005984static noinline int uncompress_inline(struct btrfs_path *path,
5985 struct inode *inode, struct page *page,
5986 size_t pg_offset, u64 extent_offset,
5987 struct btrfs_file_extent_item *item)
5988{
5989 int ret;
5990 struct extent_buffer *leaf = path->nodes[0];
5991 char *tmp;
5992 size_t max_size;
5993 unsigned long inline_size;
5994 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005995 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005996
5997 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005998 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005999 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6000 inline_size = btrfs_file_extent_inline_item_len(leaf,
6001 btrfs_item_nr(leaf, path->slots[0]));
6002 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006003 if (!tmp)
6004 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006005 ptr = btrfs_file_extent_inline_start(item);
6006
6007 read_extent_buffer(leaf, tmp, ptr, inline_size);
6008
Chris Mason5b050f02008-11-11 09:34:41 -05006009 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006010 ret = btrfs_decompress(compress_type, tmp, page,
6011 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006012 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006013 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006014 unsigned long copy_size = min_t(u64,
6015 PAGE_CACHE_SIZE - pg_offset,
6016 max_size - extent_offset);
6017 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006018 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006019 }
6020 kfree(tmp);
6021 return 0;
6022}
6023
Chris Masond352ac62008-09-29 15:18:18 -04006024/*
6025 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006026 * the ugly parts come from merging extents from the disk with the in-ram
6027 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006028 * where the in-ram extents might be locked pending data=ordered completion.
6029 *
6030 * This also copies inline extents directly into the page.
6031 */
Chris Masond3977122009-01-05 21:25:51 -05006032
Chris Masona52d9a82007-08-27 16:49:44 -04006033struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006034 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006035 int create)
6036{
6037 int ret;
6038 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006039 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006040 u64 extent_start = 0;
6041 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006042 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006043 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006044 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006045 struct btrfs_root *root = BTRFS_I(inode)->root;
6046 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006047 struct extent_buffer *leaf;
6048 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006049 struct extent_map *em = NULL;
6050 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006051 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006052 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006053 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006054
Chris Masona52d9a82007-08-27 16:49:44 -04006055again:
Chris Mason890871b2009-09-02 16:24:52 -04006056 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006057 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006058 if (em)
6059 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006060 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006061
Chris Masona52d9a82007-08-27 16:49:44 -04006062 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006063 if (em->start > start || em->start + em->len <= start)
6064 free_extent_map(em);
6065 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006066 free_extent_map(em);
6067 else
6068 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006069 }
David Sterba172ddd62011-04-21 00:48:27 +02006070 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006071 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006072 err = -ENOMEM;
6073 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006074 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006075 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006076 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006077 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006078 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006079 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006080
6081 if (!path) {
6082 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006083 if (!path) {
6084 err = -ENOMEM;
6085 goto out;
6086 }
6087 /*
6088 * Chances are we'll be called again, so go ahead and do
6089 * readahead
6090 */
6091 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006092 }
6093
Chris Mason179e29e2007-11-01 11:28:41 -04006094 ret = btrfs_lookup_file_extent(trans, root, path,
6095 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006096 if (ret < 0) {
6097 err = ret;
6098 goto out;
6099 }
6100
6101 if (ret != 0) {
6102 if (path->slots[0] == 0)
6103 goto not_found;
6104 path->slots[0]--;
6105 }
6106
Chris Mason5f39d392007-10-15 16:14:19 -04006107 leaf = path->nodes[0];
6108 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006109 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006110 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006111 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6112 found_type = btrfs_key_type(&found_key);
6113 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006114 found_type != BTRFS_EXTENT_DATA_KEY) {
6115 goto not_found;
6116 }
6117
Chris Mason5f39d392007-10-15 16:14:19 -04006118 found_type = btrfs_file_extent_type(leaf, item);
6119 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006120 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006121 if (found_type == BTRFS_FILE_EXTENT_REG ||
6122 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006123 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006124 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006125 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6126 size_t size;
6127 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006128 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006129 }
6130
6131 if (start >= extent_end) {
6132 path->slots[0]++;
6133 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6134 ret = btrfs_next_leaf(root, path);
6135 if (ret < 0) {
6136 err = ret;
6137 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006138 }
Yan Zheng9036c102008-10-30 14:19:41 -04006139 if (ret > 0)
6140 goto not_found;
6141 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006142 }
Yan Zheng9036c102008-10-30 14:19:41 -04006143 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6144 if (found_key.objectid != objectid ||
6145 found_key.type != BTRFS_EXTENT_DATA_KEY)
6146 goto not_found;
6147 if (start + len <= found_key.offset)
6148 goto not_found;
6149 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006150 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006151 em->len = found_key.offset - start;
6152 goto not_found_em;
6153 }
6154
Yan Zhengd899e052008-10-30 14:25:28 -04006155 if (found_type == BTRFS_FILE_EXTENT_REG ||
6156 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006157 em->start = extent_start;
6158 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006159 em->orig_start = extent_start -
6160 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006161 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6162 item);
Chris Masondb945352007-10-15 16:15:53 -04006163 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6164 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006165 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006166 goto insert;
6167 }
Li Zefan261507a02010-12-17 14:21:50 +08006168 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006169 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006170 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006171 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006172 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006173 } else {
6174 bytenr += btrfs_file_extent_offset(leaf, item);
6175 em->block_start = bytenr;
6176 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006177 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6178 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006179 }
Chris Masona52d9a82007-08-27 16:49:44 -04006180 goto insert;
6181 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006182 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006183 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006184 size_t size;
6185 size_t extent_offset;
6186 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006187
Chris Masona52d9a82007-08-27 16:49:44 -04006188 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006189 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006190 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006191 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006192 goto out;
6193 }
6194
Yan Zheng9036c102008-10-30 14:19:41 -04006195 size = btrfs_file_extent_inline_len(leaf, item);
6196 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006197 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006198 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006199 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006200 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006201 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006202 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006203 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006204 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006205 em->compress_type = compress_type;
6206 }
Yan689f9342007-10-29 11:41:07 -04006207 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006208 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006209 if (btrfs_file_extent_compression(leaf, item) !=
6210 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006211 ret = uncompress_inline(path, inode, page,
6212 pg_offset,
6213 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006214 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006215 } else {
6216 map = kmap(page);
6217 read_extent_buffer(leaf, map + pg_offset, ptr,
6218 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006219 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6220 memset(map + pg_offset + copy_size, 0,
6221 PAGE_CACHE_SIZE - pg_offset -
6222 copy_size);
6223 }
Chris Masonc8b97812008-10-29 14:49:59 -04006224 kunmap(page);
6225 }
Chris Mason179e29e2007-11-01 11:28:41 -04006226 flush_dcache_page(page);
6227 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006228 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006229 if (!trans) {
6230 kunmap(page);
6231 free_extent_map(em);
6232 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006233
David Sterbab3b4aa72011-04-21 01:20:15 +02006234 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006235 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006236
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006237 if (IS_ERR(trans))
6238 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006239 goto again;
6240 }
Chris Masonc8b97812008-10-29 14:49:59 -04006241 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006242 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006243 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006244 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006245 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006246 }
Chris Masond1310b22008-01-24 16:13:08 -05006247 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006248 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006249 goto insert;
6250 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006251 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006252 }
6253not_found:
6254 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006255 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006256 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006257not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006258 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006259 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006260insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006261 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006262 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05006263 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
6264 "[%llu %llu]\n", (unsigned long long)em->start,
6265 (unsigned long long)em->len,
6266 (unsigned long long)start,
6267 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006268 err = -EIO;
6269 goto out;
6270 }
Chris Masond1310b22008-01-24 16:13:08 -05006271
6272 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006273 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006274 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04006275 /* it is possible that someone inserted the extent into the tree
6276 * while we had the lock dropped. It is also possible that
6277 * an overlapping map exists in the tree
6278 */
Chris Masona52d9a82007-08-27 16:49:44 -04006279 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006280 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006281
6282 ret = 0;
6283
Chris Mason3b951512008-04-17 11:29:12 -04006284 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006285 if (existing && (existing->start > start ||
6286 existing->start + existing->len <= start)) {
6287 free_extent_map(existing);
6288 existing = NULL;
6289 }
Chris Mason3b951512008-04-17 11:29:12 -04006290 if (!existing) {
6291 existing = lookup_extent_mapping(em_tree, em->start,
6292 em->len);
6293 if (existing) {
6294 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006295 em, start,
6296 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006297 free_extent_map(existing);
6298 if (err) {
6299 free_extent_map(em);
6300 em = NULL;
6301 }
6302 } else {
6303 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006304 free_extent_map(em);
6305 em = NULL;
6306 }
6307 } else {
6308 free_extent_map(em);
6309 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006310 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006311 }
Chris Masona52d9a82007-08-27 16:49:44 -04006312 }
Chris Mason890871b2009-09-02 16:24:52 -04006313 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006314out:
liubo1abe9b82011-03-24 11:18:59 +00006315
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006316 if (em)
6317 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006318
Chris Masonf4219502008-07-22 11:18:09 -04006319 if (path)
6320 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006321 if (trans) {
6322 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006323 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006324 err = ret;
6325 }
Chris Masona52d9a82007-08-27 16:49:44 -04006326 if (err) {
6327 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006328 return ERR_PTR(err);
6329 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006330 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006331 return em;
6332}
6333
Chris Masonec29ed52011-02-23 16:23:20 -05006334struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6335 size_t pg_offset, u64 start, u64 len,
6336 int create)
6337{
6338 struct extent_map *em;
6339 struct extent_map *hole_em = NULL;
6340 u64 range_start = start;
6341 u64 end;
6342 u64 found;
6343 u64 found_end;
6344 int err = 0;
6345
6346 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6347 if (IS_ERR(em))
6348 return em;
6349 if (em) {
6350 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006351 * if our em maps to
6352 * - a hole or
6353 * - a pre-alloc extent,
6354 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006355 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006356 if (em->block_start != EXTENT_MAP_HOLE &&
6357 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006358 return em;
6359 else
6360 hole_em = em;
6361 }
6362
6363 /* check to see if we've wrapped (len == -1 or similar) */
6364 end = start + len;
6365 if (end < start)
6366 end = (u64)-1;
6367 else
6368 end -= 1;
6369
6370 em = NULL;
6371
6372 /* ok, we didn't find anything, lets look for delalloc */
6373 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6374 end, len, EXTENT_DELALLOC, 1);
6375 found_end = range_start + found;
6376 if (found_end < range_start)
6377 found_end = (u64)-1;
6378
6379 /*
6380 * we didn't find anything useful, return
6381 * the original results from get_extent()
6382 */
6383 if (range_start > end || found_end <= start) {
6384 em = hole_em;
6385 hole_em = NULL;
6386 goto out;
6387 }
6388
6389 /* adjust the range_start to make sure it doesn't
6390 * go backwards from the start they passed in
6391 */
6392 range_start = max(start,range_start);
6393 found = found_end - range_start;
6394
6395 if (found > 0) {
6396 u64 hole_start = start;
6397 u64 hole_len = len;
6398
David Sterba172ddd62011-04-21 00:48:27 +02006399 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006400 if (!em) {
6401 err = -ENOMEM;
6402 goto out;
6403 }
6404 /*
6405 * when btrfs_get_extent can't find anything it
6406 * returns one huge hole
6407 *
6408 * make sure what it found really fits our range, and
6409 * adjust to make sure it is based on the start from
6410 * the caller
6411 */
6412 if (hole_em) {
6413 u64 calc_end = extent_map_end(hole_em);
6414
6415 if (calc_end <= start || (hole_em->start > end)) {
6416 free_extent_map(hole_em);
6417 hole_em = NULL;
6418 } else {
6419 hole_start = max(hole_em->start, start);
6420 hole_len = calc_end - hole_start;
6421 }
6422 }
6423 em->bdev = NULL;
6424 if (hole_em && range_start > hole_start) {
6425 /* our hole starts before our delalloc, so we
6426 * have to return just the parts of the hole
6427 * that go until the delalloc starts
6428 */
6429 em->len = min(hole_len,
6430 range_start - hole_start);
6431 em->start = hole_start;
6432 em->orig_start = hole_start;
6433 /*
6434 * don't adjust block start at all,
6435 * it is fixed at EXTENT_MAP_HOLE
6436 */
6437 em->block_start = hole_em->block_start;
6438 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006439 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6440 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006441 } else {
6442 em->start = range_start;
6443 em->len = found;
6444 em->orig_start = range_start;
6445 em->block_start = EXTENT_MAP_DELALLOC;
6446 em->block_len = found;
6447 }
6448 } else if (hole_em) {
6449 return hole_em;
6450 }
6451out:
6452
6453 free_extent_map(hole_em);
6454 if (err) {
6455 free_extent_map(em);
6456 return ERR_PTR(err);
6457 }
6458 return em;
6459}
6460
Josef Bacik4b46fce2010-05-23 11:00:55 -04006461static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6462 u64 start, u64 len)
6463{
6464 struct btrfs_root *root = BTRFS_I(inode)->root;
6465 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006466 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006467 struct btrfs_key ins;
6468 u64 alloc_hint;
6469 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006470
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006471 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006472 if (IS_ERR(trans))
6473 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006474
6475 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6476
6477 alloc_hint = get_extent_allocation_hint(inode, start, len);
6478 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006479 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006480 if (ret) {
6481 em = ERR_PTR(ret);
6482 goto out;
6483 }
6484
Josef Bacik70c8a912012-10-11 16:54:30 -04006485 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
6486 ins.offset, ins.offset, 0);
6487 if (IS_ERR(em))
6488 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006489
6490 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6491 ins.offset, ins.offset, 0);
6492 if (ret) {
6493 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6494 em = ERR_PTR(ret);
6495 }
6496out:
6497 btrfs_end_transaction(trans, root);
6498 return em;
6499}
6500
Chris Mason46bfbb52010-05-26 11:04:10 -04006501/*
6502 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6503 * block must be cow'd
6504 */
6505static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
6506 struct inode *inode, u64 offset, u64 len)
6507{
6508 struct btrfs_path *path;
6509 int ret;
6510 struct extent_buffer *leaf;
6511 struct btrfs_root *root = BTRFS_I(inode)->root;
6512 struct btrfs_file_extent_item *fi;
6513 struct btrfs_key key;
6514 u64 disk_bytenr;
6515 u64 backref_offset;
6516 u64 extent_end;
6517 u64 num_bytes;
6518 int slot;
6519 int found_type;
6520
6521 path = btrfs_alloc_path();
6522 if (!path)
6523 return -ENOMEM;
6524
Li Zefan33345d012011-04-20 10:31:50 +08006525 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006526 offset, 0);
6527 if (ret < 0)
6528 goto out;
6529
6530 slot = path->slots[0];
6531 if (ret == 1) {
6532 if (slot == 0) {
6533 /* can't find the item, must cow */
6534 ret = 0;
6535 goto out;
6536 }
6537 slot--;
6538 }
6539 ret = 0;
6540 leaf = path->nodes[0];
6541 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006542 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006543 key.type != BTRFS_EXTENT_DATA_KEY) {
6544 /* not our file or wrong item type, must cow */
6545 goto out;
6546 }
6547
6548 if (key.offset > offset) {
6549 /* Wrong offset, must cow */
6550 goto out;
6551 }
6552
6553 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6554 found_type = btrfs_file_extent_type(leaf, fi);
6555 if (found_type != BTRFS_FILE_EXTENT_REG &&
6556 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6557 /* not a regular extent, must cow */
6558 goto out;
6559 }
6560 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6561 backref_offset = btrfs_file_extent_offset(leaf, fi);
6562
6563 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6564 if (extent_end < offset + len) {
6565 /* extent doesn't include our full range, must cow */
6566 goto out;
6567 }
6568
6569 if (btrfs_extent_readonly(root, disk_bytenr))
6570 goto out;
6571
6572 /*
6573 * look for other files referencing this extent, if we
6574 * find any we must cow
6575 */
Li Zefan33345d012011-04-20 10:31:50 +08006576 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006577 key.offset - backref_offset, disk_bytenr))
6578 goto out;
6579
6580 /*
6581 * adjust disk_bytenr and num_bytes to cover just the bytes
6582 * in this extent we are about to write. If there
6583 * are any csums in that range we have to cow in order
6584 * to keep the csums correct
6585 */
6586 disk_bytenr += backref_offset;
6587 disk_bytenr += offset - key.offset;
6588 num_bytes = min(offset + len, extent_end) - offset;
6589 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6590 goto out;
6591 /*
6592 * all of the above have passed, it is safe to overwrite this extent
6593 * without cow
6594 */
6595 ret = 1;
6596out:
6597 btrfs_free_path(path);
6598 return ret;
6599}
6600
Josef Bacikeb838e72012-07-31 16:28:48 -04006601static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6602 struct extent_state **cached_state, int writing)
6603{
6604 struct btrfs_ordered_extent *ordered;
6605 int ret = 0;
6606
6607 while (1) {
6608 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6609 0, cached_state);
6610 /*
6611 * We're concerned with the entire range that we're going to be
6612 * doing DIO to, so we need to make sure theres no ordered
6613 * extents in this range.
6614 */
6615 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6616 lockend - lockstart + 1);
6617
6618 /*
6619 * We need to make sure there are no buffered pages in this
6620 * range either, we could have raced between the invalidate in
6621 * generic_file_direct_write and locking the extent. The
6622 * invalidate needs to happen so that reads after a write do not
6623 * get stale data.
6624 */
6625 if (!ordered && (!writing ||
6626 !test_range_bit(&BTRFS_I(inode)->io_tree,
6627 lockstart, lockend, EXTENT_UPTODATE, 0,
6628 *cached_state)))
6629 break;
6630
6631 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6632 cached_state, GFP_NOFS);
6633
6634 if (ordered) {
6635 btrfs_start_ordered_extent(inode, ordered, 1);
6636 btrfs_put_ordered_extent(ordered);
6637 } else {
6638 /* Screw you mmap */
6639 ret = filemap_write_and_wait_range(inode->i_mapping,
6640 lockstart,
6641 lockend);
6642 if (ret)
6643 break;
6644
6645 /*
6646 * If we found a page that couldn't be invalidated just
6647 * fall back to buffered.
6648 */
6649 ret = invalidate_inode_pages2_range(inode->i_mapping,
6650 lockstart >> PAGE_CACHE_SHIFT,
6651 lockend >> PAGE_CACHE_SHIFT);
6652 if (ret)
6653 break;
6654 }
6655
6656 cond_resched();
6657 }
6658
6659 return ret;
6660}
6661
Josef Bacik69ffb542012-09-11 15:40:07 -04006662static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6663 u64 len, u64 orig_start,
6664 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05006665 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006666{
6667 struct extent_map_tree *em_tree;
6668 struct extent_map *em;
6669 struct btrfs_root *root = BTRFS_I(inode)->root;
6670 int ret;
6671
6672 em_tree = &BTRFS_I(inode)->extent_tree;
6673 em = alloc_extent_map();
6674 if (!em)
6675 return ERR_PTR(-ENOMEM);
6676
6677 em->start = start;
6678 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006679 em->mod_start = start;
6680 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006681 em->len = len;
6682 em->block_len = block_len;
6683 em->block_start = block_start;
6684 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006685 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006686 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006687 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6688 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006689 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006690
6691 do {
6692 btrfs_drop_extent_cache(inode, em->start,
6693 em->start + em->len - 1, 0);
6694 write_lock(&em_tree->lock);
6695 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04006696 if (!ret)
6697 list_move(&em->list,
6698 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006699 write_unlock(&em_tree->lock);
6700 } while (ret == -EEXIST);
6701
6702 if (ret) {
6703 free_extent_map(em);
6704 return ERR_PTR(ret);
6705 }
6706
6707 return em;
6708}
6709
6710
Josef Bacik4b46fce2010-05-23 11:00:55 -04006711static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6712 struct buffer_head *bh_result, int create)
6713{
6714 struct extent_map *em;
6715 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006716 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006717 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006718 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006719 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006720 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006721 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006722 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006723
Miao Xie172a5042013-02-21 02:48:22 -07006724 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006725 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006726 else
Josef Bacikc3298612012-08-03 16:49:19 -04006727 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006728
Josef Bacikc3298612012-08-03 16:49:19 -04006729 lockstart = start;
6730 lockend = start + len - 1;
6731
Josef Bacikeb838e72012-07-31 16:28:48 -04006732 /*
6733 * If this errors out it's because we couldn't invalidate pagecache for
6734 * this range and we need to fallback to buffered.
6735 */
6736 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6737 return -ENOTBLK;
6738
Josef Bacik4b46fce2010-05-23 11:00:55 -04006739 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006740 if (IS_ERR(em)) {
6741 ret = PTR_ERR(em);
6742 goto unlock_err;
6743 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006744
6745 /*
6746 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6747 * io. INLINE is special, and we could probably kludge it in here, but
6748 * it's still buffered so for safety lets just fall back to the generic
6749 * buffered path.
6750 *
6751 * For COMPRESSED we _have_ to read the entire extent in so we can
6752 * decompress it, so there will be buffering required no matter what we
6753 * do, so go ahead and fallback to buffered.
6754 *
6755 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6756 * to buffered IO. Don't blame me, this is the price we pay for using
6757 * the generic code.
6758 */
6759 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6760 em->block_start == EXTENT_MAP_INLINE) {
6761 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006762 ret = -ENOTBLK;
6763 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006764 }
6765
6766 /* Just a good old fashioned hole, return */
6767 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6768 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6769 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006770 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006771 }
6772
6773 /*
6774 * We don't allocate a new extent in the following cases
6775 *
6776 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6777 * existing extent.
6778 * 2) The extent is marked as PREALLOC. We're good to go here and can
6779 * just use the extent.
6780 *
6781 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006782 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006783 len = min(len, em->len - (start - em->start));
6784 lockstart = start + len;
6785 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006786 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006787
6788 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6789 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6790 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006791 int type;
6792 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006793 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006794
6795 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6796 type = BTRFS_ORDERED_PREALLOC;
6797 else
6798 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006799 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006800 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006801
6802 /*
6803 * we're not going to log anything, but we do need
6804 * to make sure the current transaction stays open
6805 * while we look for nocow cross refs
6806 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006807 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006808 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006809 goto must_cow;
6810
6811 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006812 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006813 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006814
6815 if (type == BTRFS_ORDERED_PREALLOC) {
6816 free_extent_map(em);
6817 em = create_pinned_em(inode, start, len,
6818 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006819 block_start, len,
6820 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006821 if (IS_ERR(em)) {
6822 btrfs_end_transaction(trans, root);
6823 goto unlock_err;
6824 }
6825 }
6826
Chris Mason46bfbb52010-05-26 11:04:10 -04006827 ret = btrfs_add_ordered_extent_dio(inode, start,
6828 block_start, len, len, type);
6829 btrfs_end_transaction(trans, root);
6830 if (ret) {
6831 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006832 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006833 }
6834 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006835 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006836 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006837 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006838must_cow:
6839 /*
6840 * this will cow the extent, reset the len in case we changed
6841 * it above
6842 */
6843 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006844 free_extent_map(em);
6845 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006846 if (IS_ERR(em)) {
6847 ret = PTR_ERR(em);
6848 goto unlock_err;
6849 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006850 len = min(len, em->len - (start - em->start));
6851unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006852 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6853 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006854 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006855 bh_result->b_bdev = em->bdev;
6856 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006857 if (create) {
6858 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6859 set_buffer_new(bh_result);
6860
6861 /*
6862 * Need to update the i_size under the extent lock so buffered
6863 * readers will get the updated i_size when we unlock.
6864 */
6865 if (start + len > i_size_read(inode))
6866 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006867
Miao Xie172a5042013-02-21 02:48:22 -07006868 spin_lock(&BTRFS_I(inode)->lock);
6869 BTRFS_I(inode)->outstanding_extents++;
6870 spin_unlock(&BTRFS_I(inode)->lock);
6871
Miao Xie09348562013-02-07 10:12:07 +00006872 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6873 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6874 &cached_state, GFP_NOFS);
6875 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006876 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006877
Josef Bacikeb838e72012-07-31 16:28:48 -04006878 /*
6879 * In the case of write we need to clear and unlock the entire range,
6880 * in the case of read we need to unlock only the end area that we
6881 * aren't using if there is any left over space.
6882 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006883 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006884 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6885 lockend, unlock_bits, 1, 0,
6886 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006887 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006888 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006889 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006890
Josef Bacik4b46fce2010-05-23 11:00:55 -04006891 free_extent_map(em);
6892
6893 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006894
6895unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006896 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6897 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6898 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006899}
6900
6901struct btrfs_dio_private {
6902 struct inode *inode;
6903 u64 logical_offset;
6904 u64 disk_bytenr;
6905 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006906 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006907
6908 /* number of bios pending for this dio */
6909 atomic_t pending_bios;
6910
6911 /* IO errors */
6912 int errors;
6913
6914 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006915};
6916
6917static void btrfs_endio_direct_read(struct bio *bio, int err)
6918{
Miao Xiee65e1532010-11-22 03:04:43 +00006919 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006920 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6921 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006922 struct inode *inode = dip->inode;
6923 struct btrfs_root *root = BTRFS_I(inode)->root;
6924 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006925
6926 start = dip->logical_offset;
6927 do {
6928 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6929 struct page *page = bvec->bv_page;
6930 char *kaddr;
6931 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006932 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006933 unsigned long flags;
6934
Josef Bacikc3298612012-08-03 16:49:19 -04006935 if (get_state_private(&BTRFS_I(inode)->io_tree,
6936 start, &private))
6937 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006938 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006939 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006940 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6941 csum, bvec->bv_len);
6942 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006943 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006944 local_irq_restore(flags);
6945
6946 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006947 if (csum != private) {
6948failed:
Li Zefan33345d012011-04-20 10:31:50 +08006949 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006950 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006951 (unsigned long long)btrfs_ino(inode),
6952 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006953 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006954 err = -EIO;
6955 }
6956 }
6957
6958 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006959 bvec++;
6960 } while (bvec <= bvec_end);
6961
6962 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006963 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006964 bio->bi_private = dip->private;
6965
Josef Bacik4b46fce2010-05-23 11:00:55 -04006966 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006967
6968 /* If we had a csum failure make sure to clear the uptodate flag */
6969 if (err)
6970 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006971 dio_end_io(bio, err);
6972}
6973
6974static void btrfs_endio_direct_write(struct bio *bio, int err)
6975{
6976 struct btrfs_dio_private *dip = bio->bi_private;
6977 struct inode *inode = dip->inode;
6978 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006979 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006980 u64 ordered_offset = dip->logical_offset;
6981 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006982 int ret;
6983
6984 if (err)
6985 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006986again:
6987 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6988 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006989 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006990 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006991 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006992
Josef Bacik5fd02042012-05-02 14:00:54 -04006993 ordered->work.func = finish_ordered_fn;
6994 ordered->work.flags = 0;
6995 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6996 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006997out_test:
6998 /*
6999 * our bio might span multiple ordered extents. If we haven't
7000 * completed the accounting for the whole dio, go back and try again
7001 */
7002 if (ordered_offset < dip->logical_offset + dip->bytes) {
7003 ordered_bytes = dip->logical_offset + dip->bytes -
7004 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007005 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007006 goto again;
7007 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007008out_done:
7009 bio->bi_private = dip->private;
7010
Josef Bacik4b46fce2010-05-23 11:00:55 -04007011 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007012
7013 /* If we had an error make sure to clear the uptodate flag */
7014 if (err)
7015 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007016 dio_end_io(bio, err);
7017}
7018
Chris Masoneaf25d92010-05-25 09:48:28 -04007019static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7020 struct bio *bio, int mirror_num,
7021 unsigned long bio_flags, u64 offset)
7022{
7023 int ret;
7024 struct btrfs_root *root = BTRFS_I(inode)->root;
7025 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007026 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007027 return 0;
7028}
7029
Miao Xiee65e1532010-11-22 03:04:43 +00007030static void btrfs_end_dio_bio(struct bio *bio, int err)
7031{
7032 struct btrfs_dio_private *dip = bio->bi_private;
7033
7034 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007035 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007036 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007037 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007038 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007039 dip->errors = 1;
7040
7041 /*
7042 * before atomic variable goto zero, we must make sure
7043 * dip->errors is perceived to be set.
7044 */
7045 smp_mb__before_atomic_dec();
7046 }
7047
7048 /* if there are more bios still pending for this dio, just exit */
7049 if (!atomic_dec_and_test(&dip->pending_bios))
7050 goto out;
7051
7052 if (dip->errors)
7053 bio_io_error(dip->orig_bio);
7054 else {
7055 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
7056 bio_endio(dip->orig_bio, 0);
7057 }
7058out:
7059 bio_put(bio);
7060}
7061
7062static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7063 u64 first_sector, gfp_t gfp_flags)
7064{
7065 int nr_vecs = bio_get_nr_vecs(bdev);
7066 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7067}
7068
7069static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7070 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007071 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007072{
7073 int write = rw & REQ_WRITE;
7074 struct btrfs_root *root = BTRFS_I(inode)->root;
7075 int ret;
7076
Josef Bacikb812ce22012-11-16 13:56:32 -05007077 if (async_submit)
7078 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7079
Miao Xiee65e1532010-11-22 03:04:43 +00007080 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007081
7082 if (!write) {
7083 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7084 if (ret)
7085 goto err;
7086 }
Miao Xiee65e1532010-11-22 03:04:43 +00007087
Josef Bacik1ae39932011-04-06 14:41:34 -04007088 if (skip_sum)
7089 goto map;
7090
7091 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007092 ret = btrfs_wq_submit_bio(root->fs_info,
7093 inode, rw, bio, 0, 0,
7094 file_offset,
7095 __btrfs_submit_bio_start_direct_io,
7096 __btrfs_submit_bio_done);
7097 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007098 } else if (write) {
7099 /*
7100 * If we aren't doing async submit, calculate the csum of the
7101 * bio now.
7102 */
7103 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7104 if (ret)
7105 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007106 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007107 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007108 if (ret)
7109 goto err;
7110 }
Miao Xiee65e1532010-11-22 03:04:43 +00007111
Josef Bacik1ae39932011-04-06 14:41:34 -04007112map:
7113 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007114err:
7115 bio_put(bio);
7116 return ret;
7117}
7118
7119static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7120 int skip_sum)
7121{
7122 struct inode *inode = dip->inode;
7123 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007124 struct bio *bio;
7125 struct bio *orig_bio = dip->orig_bio;
7126 struct bio_vec *bvec = orig_bio->bi_io_vec;
7127 u64 start_sector = orig_bio->bi_sector;
7128 u64 file_offset = dip->logical_offset;
7129 u64 submit_len = 0;
7130 u64 map_length;
7131 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007132 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007133 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007134
Miao Xiee65e1532010-11-22 03:04:43 +00007135 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007136 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007137 &map_length, NULL, 0);
7138 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007139 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007140 return -EIO;
7141 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007142 if (map_length >= orig_bio->bi_size) {
7143 bio = orig_bio;
7144 goto submit;
7145 }
7146
David Woodhouse53b381b2013-01-29 18:40:14 -05007147 /* async crcs make it difficult to collect full stripe writes. */
7148 if (btrfs_get_alloc_profile(root, 1) &
7149 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7150 async_submit = 0;
7151 else
7152 async_submit = 1;
7153
Josef Bacik02f57c72011-04-06 14:25:44 -04007154 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7155 if (!bio)
7156 return -ENOMEM;
7157 bio->bi_private = dip;
7158 bio->bi_end_io = btrfs_end_dio_bio;
7159 atomic_inc(&dip->pending_bios);
7160
Miao Xiee65e1532010-11-22 03:04:43 +00007161 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7162 if (unlikely(map_length < submit_len + bvec->bv_len ||
7163 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7164 bvec->bv_offset) < bvec->bv_len)) {
7165 /*
7166 * inc the count before we submit the bio so
7167 * we know the end IO handler won't happen before
7168 * we inc the count. Otherwise, the dip might get freed
7169 * before we're done setting it up
7170 */
7171 atomic_inc(&dip->pending_bios);
7172 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7173 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007174 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007175 if (ret) {
7176 bio_put(bio);
7177 atomic_dec(&dip->pending_bios);
7178 goto out_err;
7179 }
7180
Miao Xiee65e1532010-11-22 03:04:43 +00007181 start_sector += submit_len >> 9;
7182 file_offset += submit_len;
7183
7184 submit_len = 0;
7185 nr_pages = 0;
7186
7187 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7188 start_sector, GFP_NOFS);
7189 if (!bio)
7190 goto out_err;
7191 bio->bi_private = dip;
7192 bio->bi_end_io = btrfs_end_dio_bio;
7193
7194 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007195 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007196 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007197 &map_length, NULL, 0);
7198 if (ret) {
7199 bio_put(bio);
7200 goto out_err;
7201 }
7202 } else {
7203 submit_len += bvec->bv_len;
7204 nr_pages ++;
7205 bvec++;
7206 }
7207 }
7208
Josef Bacik02f57c72011-04-06 14:25:44 -04007209submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007210 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007211 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007212 if (!ret)
7213 return 0;
7214
7215 bio_put(bio);
7216out_err:
7217 dip->errors = 1;
7218 /*
7219 * before atomic variable goto zero, we must
7220 * make sure dip->errors is perceived to be set.
7221 */
7222 smp_mb__before_atomic_dec();
7223 if (atomic_dec_and_test(&dip->pending_bios))
7224 bio_io_error(dip->orig_bio);
7225
7226 /* bio_end_io() will handle error, so we needn't return it */
7227 return 0;
7228}
7229
Josef Bacik4b46fce2010-05-23 11:00:55 -04007230static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
7231 loff_t file_offset)
7232{
7233 struct btrfs_root *root = BTRFS_I(inode)->root;
7234 struct btrfs_dio_private *dip;
7235 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007236 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007237 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007238 int ret = 0;
7239
7240 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7241
7242 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7243 if (!dip) {
7244 ret = -ENOMEM;
7245 goto free_ordered;
7246 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007247
7248 dip->private = bio->bi_private;
7249 dip->inode = inode;
7250 dip->logical_offset = file_offset;
7251
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252 dip->bytes = 0;
7253 do {
7254 dip->bytes += bvec->bv_len;
7255 bvec++;
7256 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
7257
Chris Mason46bfbb52010-05-26 11:04:10 -04007258 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007259 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007260 dip->errors = 0;
7261 dip->orig_bio = bio;
7262 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263
7264 if (write)
7265 bio->bi_end_io = btrfs_endio_direct_write;
7266 else
7267 bio->bi_end_io = btrfs_endio_direct_read;
7268
Miao Xiee65e1532010-11-22 03:04:43 +00007269 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7270 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007271 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007272free_ordered:
7273 /*
7274 * If this is a write, we need to clean up the reserved space and kill
7275 * the ordered extent.
7276 */
7277 if (write) {
7278 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007279 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007280 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7281 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7282 btrfs_free_reserved_extent(root, ordered->start,
7283 ordered->disk_len);
7284 btrfs_put_ordered_extent(ordered);
7285 btrfs_put_ordered_extent(ordered);
7286 }
7287 bio_endio(bio, ret);
7288}
7289
Chris Mason5a5f79b2010-05-26 21:33:37 -04007290static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7291 const struct iovec *iov, loff_t offset,
7292 unsigned long nr_segs)
7293{
7294 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007295 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007296 size_t size;
7297 unsigned long addr;
7298 unsigned blocksize_mask = root->sectorsize - 1;
7299 ssize_t retval = -EINVAL;
7300 loff_t end = offset;
7301
7302 if (offset & blocksize_mask)
7303 goto out;
7304
7305 /* Check the memory alignment. Blocks cannot straddle pages */
7306 for (seg = 0; seg < nr_segs; seg++) {
7307 addr = (unsigned long)iov[seg].iov_base;
7308 size = iov[seg].iov_len;
7309 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007310 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007311 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007312
7313 /* If this is a write we don't need to check anymore */
7314 if (rw & WRITE)
7315 continue;
7316
7317 /*
7318 * Check to make sure we don't have duplicate iov_base's in this
7319 * iovec, if so return EINVAL, otherwise we'll get csum errors
7320 * when reading back.
7321 */
7322 for (i = seg + 1; i < nr_segs; i++) {
7323 if (iov[seg].iov_base == iov[i].iov_base)
7324 goto out;
7325 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007326 }
7327 retval = 0;
7328out:
7329 return retval;
7330}
Josef Bacikeb838e72012-07-31 16:28:48 -04007331
Chris Mason16432982008-04-10 10:23:21 -04007332static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7333 const struct iovec *iov, loff_t offset,
7334 unsigned long nr_segs)
7335{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007336 struct file *file = iocb->ki_filp;
7337 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007338 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007339 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007340 bool wakeup = true;
7341 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007342 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007343
Chris Mason5a5f79b2010-05-26 21:33:37 -04007344 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007345 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007346 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007347
Miao Xie38851cc2013-02-08 07:04:11 +00007348 atomic_inc(&inode->i_dio_count);
7349 smp_mb__after_atomic_inc();
7350
Miao Xie09348562013-02-07 10:12:07 +00007351 if (rw & WRITE) {
7352 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007353 /*
7354 * If the write DIO is beyond the EOF, we need update
7355 * the isize, but it is protected by i_mutex. So we can
7356 * not unlock the i_mutex at this case.
7357 */
7358 if (offset + count <= inode->i_size) {
7359 mutex_unlock(&inode->i_mutex);
7360 relock = true;
7361 }
Miao Xie09348562013-02-07 10:12:07 +00007362 ret = btrfs_delalloc_reserve_space(inode, count);
7363 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007364 goto out;
7365 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7366 &BTRFS_I(inode)->runtime_flags))) {
7367 inode_dio_done(inode);
7368 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7369 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007370 }
7371
7372 ret = __blockdev_direct_IO(rw, iocb, inode,
7373 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7374 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007375 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007376 if (rw & WRITE) {
7377 if (ret < 0 && ret != -EIOCBQUEUED)
7378 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007379 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007380 btrfs_delalloc_release_space(inode,
7381 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007382 else
7383 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007384 }
Miao Xie38851cc2013-02-08 07:04:11 +00007385out:
Miao Xie2e60a512013-02-08 07:01:08 +00007386 if (wakeup)
7387 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007388 if (relock)
7389 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007390
7391 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007392}
7393
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007394#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7395
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007396static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7397 __u64 start, __u64 len)
7398{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007399 int ret;
7400
7401 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7402 if (ret)
7403 return ret;
7404
Chris Masonec29ed52011-02-23 16:23:20 -05007405 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007406}
7407
Chris Mason9ebefb182007-06-15 13:50:00 -04007408int btrfs_readpage(struct file *file, struct page *page)
7409{
Chris Masond1310b22008-01-24 16:13:08 -05007410 struct extent_io_tree *tree;
7411 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007412 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007413}
Chris Mason1832a6d2007-12-21 16:27:21 -05007414
Chris Mason39279cc2007-06-12 06:35:45 -04007415static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7416{
Chris Masond1310b22008-01-24 16:13:08 -05007417 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007418
7419
7420 if (current->flags & PF_MEMALLOC) {
7421 redirty_page_for_writepage(wbc, page);
7422 unlock_page(page);
7423 return 0;
7424 }
Chris Masond1310b22008-01-24 16:13:08 -05007425 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007426 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7427}
Chris Mason39279cc2007-06-12 06:35:45 -04007428
Chris Masonf4219502008-07-22 11:18:09 -04007429int btrfs_writepages(struct address_space *mapping,
7430 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007431{
Chris Masond1310b22008-01-24 16:13:08 -05007432 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007433
Chris Masond1310b22008-01-24 16:13:08 -05007434 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007435 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7436}
7437
Chris Mason3ab2fb52007-11-08 10:59:22 -05007438static int
7439btrfs_readpages(struct file *file, struct address_space *mapping,
7440 struct list_head *pages, unsigned nr_pages)
7441{
Chris Masond1310b22008-01-24 16:13:08 -05007442 struct extent_io_tree *tree;
7443 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007444 return extent_readpages(tree, mapping, pages, nr_pages,
7445 btrfs_get_extent);
7446}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007447static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007448{
Chris Masond1310b22008-01-24 16:13:08 -05007449 struct extent_io_tree *tree;
7450 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007451 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007452
Chris Masond1310b22008-01-24 16:13:08 -05007453 tree = &BTRFS_I(page->mapping->host)->io_tree;
7454 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007455 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007456 if (ret == 1) {
7457 ClearPagePrivate(page);
7458 set_page_private(page, 0);
7459 page_cache_release(page);
7460 }
7461 return ret;
7462}
Chris Mason39279cc2007-06-12 06:35:45 -04007463
Chris Masone6dcd2d2008-07-17 12:53:50 -04007464static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7465{
Chris Mason98509cf2008-09-11 15:51:43 -04007466 if (PageWriteback(page) || PageDirty(page))
7467 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007468 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007469}
7470
Chris Masona52d9a82007-08-27 16:49:44 -04007471static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7472{
Josef Bacik5fd02042012-05-02 14:00:54 -04007473 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007474 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007475 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007476 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007477 u64 page_start = page_offset(page);
7478 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007479
Chris Mason8b62b722009-09-02 16:53:46 -04007480 /*
7481 * we have the page locked, so new writeback can't start,
7482 * and the dirty bit won't be cleared while we are here.
7483 *
7484 * Wait for IO on this page so that we can safely clear
7485 * the PagePrivate2 bit and do ordered accounting
7486 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007487 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007488
Josef Bacik5fd02042012-05-02 14:00:54 -04007489 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007490 if (offset) {
7491 btrfs_releasepage(page, GFP_NOFS);
7492 return;
7493 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007494 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007495 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007496 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007497 /*
7498 * IO on this page will never be started, so we need
7499 * to account for any ordered extents now
7500 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007501 clear_extent_bit(tree, page_start, page_end,
7502 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007503 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7504 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007505 /*
7506 * whoever cleared the private bit is responsible
7507 * for the finish_ordered_io
7508 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007509 if (TestClearPagePrivate2(page) &&
7510 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7511 PAGE_CACHE_SIZE, 1)) {
7512 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007513 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007514 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007515 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007516 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007517 }
7518 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007519 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007520 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7521 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007522 __btrfs_releasepage(page, GFP_NOFS);
7523
Chris Mason4a096752008-07-21 10:29:44 -04007524 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007525 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007526 ClearPagePrivate(page);
7527 set_page_private(page, 0);
7528 page_cache_release(page);
7529 }
Chris Mason39279cc2007-06-12 06:35:45 -04007530}
7531
Chris Mason9ebefb182007-06-15 13:50:00 -04007532/*
7533 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7534 * called from a page fault handler when a page is first dirtied. Hence we must
7535 * be careful to check for EOF conditions here. We set the page up correctly
7536 * for a written page which means we get ENOSPC checking when writing into
7537 * holes and correct delalloc and unwritten extent mapping on filesystems that
7538 * support these features.
7539 *
7540 * We are not allowed to take the i_mutex here so we have to play games to
7541 * protect against truncate races as the page could now be beyond EOF. Because
7542 * vmtruncate() writes the inode size before removing pages, once we have the
7543 * page lock we can determine safely if the page is beyond EOF. If it is not
7544 * beyond EOF, then the page is guaranteed safe against truncation until we
7545 * unlock the page.
7546 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007547int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007548{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007549 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007550 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007551 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007552 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7553 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007554 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007555 char *kaddr;
7556 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007557 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007558 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007559 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007560 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007561 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007562
Jan Karab2b5ef52012-06-12 16:20:45 +02007563 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007564 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007565 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007566 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007567 reserved = 1;
7568 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007569 if (ret) {
7570 if (ret == -ENOMEM)
7571 ret = VM_FAULT_OOM;
7572 else /* -ENOSPC, -EIO, etc */
7573 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007574 if (reserved)
7575 goto out;
7576 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007577 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007578
Nick Piggin56a76f82009-03-31 15:23:23 -07007579 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007580again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007581 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007582 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007583 page_start = page_offset(page);
7584 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007585
Chris Mason9ebefb182007-06-15 13:50:00 -04007586 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007587 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007588 /* page got truncated out from underneath us */
7589 goto out_unlock;
7590 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007591 wait_on_page_writeback(page);
7592
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007593 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007594 set_page_extent_mapped(page);
7595
Chris Masoneb84ae02008-07-17 13:53:27 -04007596 /*
7597 * we can't set the delalloc bits if there are pending ordered
7598 * extents. Drop our locks and wait for them to finish
7599 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007600 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7601 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007602 unlock_extent_cached(io_tree, page_start, page_end,
7603 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007604 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007605 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007606 btrfs_put_ordered_extent(ordered);
7607 goto again;
7608 }
7609
Josef Bacikfbf19082009-10-01 17:10:23 -04007610 /*
7611 * XXX - page_mkwrite gets called every time the page is dirtied, even
7612 * if it was already dirty, so for space accounting reasons we need to
7613 * clear any delalloc bits for the range we are fixing to save. There
7614 * is probably a better way to do this, but for now keep consistent with
7615 * prepare_pages in the normal write path.
7616 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007617 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007618 EXTENT_DIRTY | EXTENT_DELALLOC |
7619 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007620 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007621
Josef Bacik2ac55d42010-02-03 19:33:23 +00007622 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7623 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007624 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007625 unlock_extent_cached(io_tree, page_start, page_end,
7626 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007627 ret = VM_FAULT_SIGBUS;
7628 goto out_unlock;
7629 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007630 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007631
7632 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007633 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007634 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007635 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007636 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007637
Chris Masone6dcd2d2008-07-17 12:53:50 -04007638 if (zero_start != PAGE_CACHE_SIZE) {
7639 kaddr = kmap(page);
7640 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7641 flush_dcache_page(page);
7642 kunmap(page);
7643 }
Chris Mason247e7432008-07-17 12:53:51 -04007644 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007645 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007646 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007647
Chris Mason257c62e2009-10-13 13:21:08 -04007648 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7649 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007650 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007651
Josef Bacik2ac55d42010-02-03 19:33:23 +00007652 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007653
7654out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007655 if (!ret) {
7656 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007657 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007658 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007659 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007660out:
Josef Bacikec39e182012-01-12 19:10:12 -05007661 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007662out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007663 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007664 return ret;
7665}
7666
Josef Bacika41ad392011-01-31 15:30:16 -05007667static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007668{
7669 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007670 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007671 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007672 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007673 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007674 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007675 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007676
Josef Bacik2aaa6652012-08-29 14:27:18 -04007677 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007678 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007679 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007680
Chris Mason4a096752008-07-21 10:29:44 -04007681 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007682 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007683
Josef Bacikfcb80c22011-05-03 10:40:22 -04007684 /*
7685 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7686 * 3 things going on here
7687 *
7688 * 1) We need to reserve space for our orphan item and the space to
7689 * delete our orphan item. Lord knows we don't want to have a dangling
7690 * orphan item because we didn't reserve space to remove it.
7691 *
7692 * 2) We need to reserve space to update our inode.
7693 *
7694 * 3) We need to have something to cache all the space that is going to
7695 * be free'd up by the truncate operation, but also have some slack
7696 * space reserved in case it uses space during the truncate (thank you
7697 * very much snapshotting).
7698 *
7699 * And we need these to all be seperate. The fact is we can use alot of
7700 * space doing the truncate, and we have no earthly idea how much space
7701 * we will use, so we need the truncate reservation to be seperate so it
7702 * doesn't end up using space reserved for updating the inode or
7703 * removing the orphan item. We also need to be able to stop the
7704 * transaction and start a new one, which means we need to be able to
7705 * update the inode several times, and we have no idea of knowing how
7706 * many times that will be, so we can't just reserve 1 item for the
7707 * entirety of the opration, so that has to be done seperately as well.
7708 * Then there is the orphan item, which does indeed need to be held on
7709 * to for the whole operation, and we need nobody to touch this reserved
7710 * space except the orphan code.
7711 *
7712 * So that leaves us with
7713 *
7714 * 1) root->orphan_block_rsv - for the orphan deletion.
7715 * 2) rsv - for the truncate reservation, which we will steal from the
7716 * transaction reservation.
7717 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7718 * updating the inode.
7719 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007720 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007721 if (!rsv)
7722 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007723 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007724 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007725
Josef Bacik907cbce2011-08-08 13:46:15 -04007726 /*
Josef Bacik07127182011-08-19 10:29:59 -04007727 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007728 * 1 for updating the inode.
7729 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007730 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007731 if (IS_ERR(trans)) {
7732 err = PTR_ERR(trans);
7733 goto out;
7734 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007735
Josef Bacik907cbce2011-08-08 13:46:15 -04007736 /* Migrate the slack space for the truncate to our reserve */
7737 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7738 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007739 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007740
Chris Mason5a3f23d2009-03-31 13:27:11 -04007741 /*
7742 * setattr is responsible for setting the ordered_data_close flag,
7743 * but that is only tested during the last file release. That
7744 * could happen well after the next commit, leaving a great big
7745 * window where new writes may get lost if someone chooses to write
7746 * to this file after truncating to zero
7747 *
7748 * The inode doesn't have any dirty data here, and so if we commit
7749 * this is a noop. If someone immediately starts writing to the inode
7750 * it is very likely we'll catch some of their writes in this
7751 * transaction, and the commit will find this file on the ordered
7752 * data list with good things to send down.
7753 *
7754 * This is a best effort solution, there is still a window where
7755 * using truncate to replace the contents of the file will
7756 * end up with a zero length file after a crash.
7757 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007758 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7759 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007760 btrfs_add_ordered_operation(trans, root, inode);
7761
Josef Bacik5dc562c2012-08-17 13:14:17 -04007762 /*
7763 * So if we truncate and then write and fsync we normally would just
7764 * write the extents that changed, which is a problem if we need to
7765 * first truncate that entire inode. So set this flag so we write out
7766 * all of the extents in the inode to the sync log so we're completely
7767 * safe.
7768 */
7769 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007770 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007771
Yan, Zheng80825102009-11-12 09:35:36 +00007772 while (1) {
7773 ret = btrfs_truncate_inode_items(trans, root, inode,
7774 inode->i_size,
7775 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007776 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007777 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007778 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007779 }
Chris Mason39279cc2007-06-12 06:35:45 -04007780
Josef Bacikfcb80c22011-05-03 10:40:22 -04007781 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007782 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007783 if (ret) {
7784 err = ret;
7785 break;
7786 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007787
Yan, Zheng80825102009-11-12 09:35:36 +00007788 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007789 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007790
7791 trans = btrfs_start_transaction(root, 2);
7792 if (IS_ERR(trans)) {
7793 ret = err = PTR_ERR(trans);
7794 trans = NULL;
7795 break;
7796 }
7797
7798 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7799 rsv, min_size);
7800 BUG_ON(ret); /* shouldn't happen */
7801 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007802 }
7803
7804 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007805 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007806 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007807 if (ret)
7808 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007809 }
7810
Chris Mason917c16b2011-11-08 14:49:59 -05007811 if (trans) {
7812 trans->block_rsv = &root->fs_info->trans_block_rsv;
7813 ret = btrfs_update_inode(trans, root, inode);
7814 if (ret && !err)
7815 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007816
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007817 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007818 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007819 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007820
7821out:
7822 btrfs_free_block_rsv(root, rsv);
7823
Josef Bacik3893e332011-01-31 16:03:11 -05007824 if (ret && !err)
7825 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007826
Josef Bacik3893e332011-01-31 16:03:11 -05007827 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007828}
7829
Sven Wegener3b963622008-06-09 21:57:42 -04007830/*
Chris Masond352ac62008-09-29 15:18:18 -04007831 * create a new subvolume directory/inode (helper for the ioctl).
7832 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007833int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007834 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007835{
Chris Mason39279cc2007-06-12 06:35:45 -04007836 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007837 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007838 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007839
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007840 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7841 new_dirid, new_dirid,
7842 S_IFDIR | (~current_umask() & S_IRWXUGO),
7843 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007844 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007845 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007846 inode->i_op = &btrfs_dir_inode_operations;
7847 inode->i_fop = &btrfs_dir_file_operations;
7848
Miklos Szeredibfe86842011-10-28 14:13:29 +02007849 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007850 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007851
Yan, Zheng76dda932009-09-21 16:00:26 -04007852 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007853
Yan, Zheng76dda932009-09-21 16:00:26 -04007854 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007855 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007856}
7857
Chris Mason39279cc2007-06-12 06:35:45 -04007858struct inode *btrfs_alloc_inode(struct super_block *sb)
7859{
7860 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007861 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007862
7863 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7864 if (!ei)
7865 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007866
7867 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007868 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007869 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007870 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007871 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007872 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007873 ei->disk_i_size = 0;
7874 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007875 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007876 ei->index_cnt = (u64)-1;
7877 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007878 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007879
Josef Bacik9e0baf62011-07-15 15:16:44 +00007880 spin_lock_init(&ei->lock);
7881 ei->outstanding_extents = 0;
7882 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007883
Josef Bacik72ac3c02012-05-23 14:13:11 -04007884 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007885 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007886
Miao Xie16cdcec2011-04-22 18:12:22 +08007887 ei->delayed_node = NULL;
7888
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007889 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007890 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007891 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7892 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007893 ei->io_tree.track_uptodate = 1;
7894 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007895 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007896 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007897 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007898 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007899 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007900 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007901 RB_CLEAR_NODE(&ei->rb_node);
7902
7903 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007904}
7905
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007906static void btrfs_i_callback(struct rcu_head *head)
7907{
7908 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007909 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7910}
7911
Chris Mason39279cc2007-06-12 06:35:45 -04007912void btrfs_destroy_inode(struct inode *inode)
7913{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007914 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007915 struct btrfs_root *root = BTRFS_I(inode)->root;
7916
Al Virob3d9b7a2012-06-09 13:51:19 -04007917 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007918 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007919 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7920 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007921 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7922 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007923
Chris Mason5a3f23d2009-03-31 13:27:11 -04007924 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007925 * This can happen where we create an inode, but somebody else also
7926 * created the same inode and we need to destroy the one we already
7927 * created.
7928 */
7929 if (!root)
7930 goto free;
7931
7932 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007933 * Make sure we're properly removed from the ordered operation
7934 * lists.
7935 */
7936 smp_mb();
7937 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7938 spin_lock(&root->fs_info->ordered_extent_lock);
7939 list_del_init(&BTRFS_I(inode)->ordered_operations);
7940 spin_unlock(&root->fs_info->ordered_extent_lock);
7941 }
7942
Josef Bacik8a35d952012-05-23 14:26:42 -04007943 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7944 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007945 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7946 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007947 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007948 }
Josef Bacik7b128762008-07-24 12:17:14 -04007949
Chris Masond3977122009-01-05 21:25:51 -05007950 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007951 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7952 if (!ordered)
7953 break;
7954 else {
Chris Masond3977122009-01-05 21:25:51 -05007955 printk(KERN_ERR "btrfs found ordered "
7956 "extent %llu %llu on inode cleanup\n",
7957 (unsigned long long)ordered->file_offset,
7958 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007959 btrfs_remove_ordered_extent(inode, ordered);
7960 btrfs_put_ordered_extent(ordered);
7961 btrfs_put_ordered_extent(ordered);
7962 }
7963 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007964 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007965 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007966free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007967 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007968 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007969}
7970
Al Viro45321ac2010-06-07 13:43:19 -04007971int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007972{
7973 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007974
Liu Bofa6ac872013-02-20 14:10:23 +00007975 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007976 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007977 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007978 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007979 else
Al Viro45321ac2010-06-07 13:43:19 -04007980 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007981}
7982
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007983static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007984{
7985 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7986
7987 inode_init_once(&ei->vfs_inode);
7988}
7989
7990void btrfs_destroy_cachep(void)
7991{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007992 /*
7993 * Make sure all delayed rcu free inodes are flushed before we
7994 * destroy cache.
7995 */
7996 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007997 if (btrfs_inode_cachep)
7998 kmem_cache_destroy(btrfs_inode_cachep);
7999 if (btrfs_trans_handle_cachep)
8000 kmem_cache_destroy(btrfs_trans_handle_cachep);
8001 if (btrfs_transaction_cachep)
8002 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008003 if (btrfs_path_cachep)
8004 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008005 if (btrfs_free_space_cachep)
8006 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008007 if (btrfs_delalloc_work_cachep)
8008 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008009}
8010
8011int btrfs_init_cachep(void)
8012{
David Sterba837e1972012-09-07 03:00:48 -06008013 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008014 sizeof(struct btrfs_inode), 0,
8015 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008016 if (!btrfs_inode_cachep)
8017 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008018
David Sterba837e1972012-09-07 03:00:48 -06008019 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008020 sizeof(struct btrfs_trans_handle), 0,
8021 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008022 if (!btrfs_trans_handle_cachep)
8023 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008024
David Sterba837e1972012-09-07 03:00:48 -06008025 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008026 sizeof(struct btrfs_transaction), 0,
8027 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008028 if (!btrfs_transaction_cachep)
8029 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008030
David Sterba837e1972012-09-07 03:00:48 -06008031 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008032 sizeof(struct btrfs_path), 0,
8033 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008034 if (!btrfs_path_cachep)
8035 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008036
David Sterba837e1972012-09-07 03:00:48 -06008037 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008038 sizeof(struct btrfs_free_space), 0,
8039 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8040 if (!btrfs_free_space_cachep)
8041 goto fail;
8042
Miao Xie8ccf6f192012-10-25 09:28:04 +00008043 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8044 sizeof(struct btrfs_delalloc_work), 0,
8045 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8046 NULL);
8047 if (!btrfs_delalloc_work_cachep)
8048 goto fail;
8049
Chris Mason39279cc2007-06-12 06:35:45 -04008050 return 0;
8051fail:
8052 btrfs_destroy_cachep();
8053 return -ENOMEM;
8054}
8055
8056static int btrfs_getattr(struct vfsmount *mnt,
8057 struct dentry *dentry, struct kstat *stat)
8058{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008059 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008060 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008061 u32 blocksize = inode->i_sb->s_blocksize;
8062
Chris Mason39279cc2007-06-12 06:35:45 -04008063 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008064 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008065 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008066
8067 spin_lock(&BTRFS_I(inode)->lock);
8068 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8069 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008070 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008071 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008072 return 0;
8073}
8074
Chris Masond3977122009-01-05 21:25:51 -05008075static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8076 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008077{
8078 struct btrfs_trans_handle *trans;
8079 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008080 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008081 struct inode *new_inode = new_dentry->d_inode;
8082 struct inode *old_inode = old_dentry->d_inode;
8083 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008084 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008085 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008086 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008087 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008088
Li Zefan33345d012011-04-20 10:31:50 +08008089 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008090 return -EPERM;
8091
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008092 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008093 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008094 return -EXDEV;
8095
Li Zefan33345d012011-04-20 10:31:50 +08008096 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8097 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008098 return -ENOTEMPTY;
8099
Chris Mason39279cc2007-06-12 06:35:45 -04008100 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008101 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008102 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008103
8104
8105 /* check for collisions, even if the name isn't there */
8106 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8107 new_dentry->d_name.name,
8108 new_dentry->d_name.len);
8109
8110 if (ret) {
8111 if (ret == -EEXIST) {
8112 /* we shouldn't get
8113 * eexist without a new_inode */
8114 if (!new_inode) {
8115 WARN_ON(1);
8116 return ret;
8117 }
8118 } else {
8119 /* maybe -EOVERFLOW */
8120 return ret;
8121 }
8122 }
8123 ret = 0;
8124
Chris Mason5a3f23d2009-03-31 13:27:11 -04008125 /*
8126 * we're using rename to replace one file with another.
8127 * and the replacement file is large. Start IO on it now so
8128 * we don't add too much work to the end of the transaction
8129 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008130 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008131 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8132 filemap_flush(old_inode->i_mapping);
8133
Yan, Zheng76dda932009-09-21 16:00:26 -04008134 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008135 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008136 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008137 /*
8138 * We want to reserve the absolute worst case amount of items. So if
8139 * both inodes are subvols and we need to unlink them then that would
8140 * require 4 item modifications, but if they are both normal inodes it
8141 * would require 5 item modifications, so we'll assume their normal
8142 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8143 * should cover the worst case number of items we'll modify.
8144 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008145 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008146 if (IS_ERR(trans)) {
8147 ret = PTR_ERR(trans);
8148 goto out_notrans;
8149 }
Chris Mason5f39d392007-10-15 16:14:19 -04008150
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008151 if (dest != root)
8152 btrfs_record_root_in_trans(trans, dest);
8153
Yan, Zhenga5719522009-09-24 09:17:31 -04008154 ret = btrfs_set_inode_index(new_dir, &index);
8155 if (ret)
8156 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008157
Li Zefan33345d012011-04-20 10:31:50 +08008158 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008159 /* force full log commit if subvolume involved. */
8160 root->fs_info->last_trans_log_full_commit = trans->transid;
8161 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008162 ret = btrfs_insert_inode_ref(trans, dest,
8163 new_dentry->d_name.name,
8164 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008165 old_ino,
8166 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008167 if (ret)
8168 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008169 /*
8170 * this is an ugly little race, but the rename is required
8171 * to make sure that if we crash, the inode is either at the
8172 * old name or the new one. pinning the log transaction lets
8173 * us make sure we don't allow a log commit to come in after
8174 * we unlink the name but before we add the new name back in.
8175 */
8176 btrfs_pin_log_trans(root);
8177 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008178 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008179 * make sure the inode gets flushed if it is replacing
8180 * something.
8181 */
Li Zefan33345d012011-04-20 10:31:50 +08008182 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008183 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008184
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008185 inode_inc_iversion(old_dir);
8186 inode_inc_iversion(new_dir);
8187 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008188 old_dir->i_ctime = old_dir->i_mtime = ctime;
8189 new_dir->i_ctime = new_dir->i_mtime = ctime;
8190 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008191
Chris Mason12fcfd22009-03-24 10:24:20 -04008192 if (old_dentry->d_parent != new_dentry->d_parent)
8193 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8194
Li Zefan33345d012011-04-20 10:31:50 +08008195 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008196 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8197 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8198 old_dentry->d_name.name,
8199 old_dentry->d_name.len);
8200 } else {
Al Viro92986792011-03-04 17:14:37 +00008201 ret = __btrfs_unlink_inode(trans, root, old_dir,
8202 old_dentry->d_inode,
8203 old_dentry->d_name.name,
8204 old_dentry->d_name.len);
8205 if (!ret)
8206 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008207 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008208 if (ret) {
8209 btrfs_abort_transaction(trans, root, ret);
8210 goto out_fail;
8211 }
Chris Mason39279cc2007-06-12 06:35:45 -04008212
8213 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008214 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008215 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008216 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008217 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8218 root_objectid = BTRFS_I(new_inode)->location.objectid;
8219 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8220 root_objectid,
8221 new_dentry->d_name.name,
8222 new_dentry->d_name.len);
8223 BUG_ON(new_inode->i_nlink == 0);
8224 } else {
8225 ret = btrfs_unlink_inode(trans, dest, new_dir,
8226 new_dentry->d_inode,
8227 new_dentry->d_name.name,
8228 new_dentry->d_name.len);
8229 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008230 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008231 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008232 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008233 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008234 if (ret) {
8235 btrfs_abort_transaction(trans, root, ret);
8236 goto out_fail;
8237 }
Chris Mason39279cc2007-06-12 06:35:45 -04008238 }
Josef Bacikaec74772008-07-24 12:12:38 -04008239
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008240 ret = btrfs_add_link(trans, new_dir, old_inode,
8241 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008242 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008243 if (ret) {
8244 btrfs_abort_transaction(trans, root, ret);
8245 goto out_fail;
8246 }
Chris Mason39279cc2007-06-12 06:35:45 -04008247
Li Zefan33345d012011-04-20 10:31:50 +08008248 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008249 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008250 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008251 btrfs_end_log_trans(root);
8252 }
Chris Mason39279cc2007-06-12 06:35:45 -04008253out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008254 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008255out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008256 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008257 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008258
Chris Mason39279cc2007-06-12 06:35:45 -04008259 return ret;
8260}
8261
Miao Xie8ccf6f192012-10-25 09:28:04 +00008262static void btrfs_run_delalloc_work(struct btrfs_work *work)
8263{
8264 struct btrfs_delalloc_work *delalloc_work;
8265
8266 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8267 work);
8268 if (delalloc_work->wait)
8269 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8270 else
8271 filemap_flush(delalloc_work->inode->i_mapping);
8272
8273 if (delalloc_work->delay_iput)
8274 btrfs_add_delayed_iput(delalloc_work->inode);
8275 else
8276 iput(delalloc_work->inode);
8277 complete(&delalloc_work->completion);
8278}
8279
8280struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8281 int wait, int delay_iput)
8282{
8283 struct btrfs_delalloc_work *work;
8284
8285 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8286 if (!work)
8287 return NULL;
8288
8289 init_completion(&work->completion);
8290 INIT_LIST_HEAD(&work->list);
8291 work->inode = inode;
8292 work->wait = wait;
8293 work->delay_iput = delay_iput;
8294 work->work.func = btrfs_run_delalloc_work;
8295
8296 return work;
8297}
8298
8299void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8300{
8301 wait_for_completion(&work->completion);
8302 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8303}
8304
Chris Masond352ac62008-09-29 15:18:18 -04008305/*
8306 * some fairly slow code that needs optimization. This walks the list
8307 * of all the inodes with pending delalloc and forces them to disk.
8308 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008309int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008310{
Chris Masonea8c2812008-08-04 23:17:27 -04008311 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008312 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008313 struct btrfs_delalloc_work *work, *next;
8314 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008315 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008316 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008317
Yan Zhengc146afa2008-11-12 14:34:12 -05008318 if (root->fs_info->sb->s_flags & MS_RDONLY)
8319 return -EROFS;
8320
Miao Xie8ccf6f192012-10-25 09:28:04 +00008321 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008322 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008323
Chris Mason75eff682008-12-15 15:54:40 -05008324 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008325 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8326 while (!list_empty(&splice)) {
8327 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008328 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008329
8330 list_del_init(&binode->delalloc_inodes);
8331
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008332 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008333 if (!inode) {
8334 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8335 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008336 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008337 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008338
8339 list_add_tail(&binode->delalloc_inodes,
8340 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008341 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008342
8343 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8344 if (unlikely(!work)) {
8345 ret = -ENOMEM;
8346 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008347 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008348 list_add_tail(&work->list, &works);
8349 btrfs_queue_worker(&root->fs_info->flush_workers,
8350 &work->work);
8351
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008352 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008353 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008354 }
Chris Mason75eff682008-12-15 15:54:40 -05008355 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008356
Miao Xie1eafa6c2013-01-22 10:49:00 +00008357 list_for_each_entry_safe(work, next, &works, list) {
8358 list_del_init(&work->list);
8359 btrfs_wait_and_free_delalloc_work(work);
8360 }
8361
Chris Mason8c8bee12008-09-29 11:19:10 -04008362 /* the filemap_flush will queue IO into the worker threads, but
8363 * we have to make sure the IO is actually started and that
8364 * ordered extents get created before we return
8365 */
8366 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008367 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008368 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008369 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008370 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8371 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008372 }
8373 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008374 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008375out:
8376 list_for_each_entry_safe(work, next, &works, list) {
8377 list_del_init(&work->list);
8378 btrfs_wait_and_free_delalloc_work(work);
8379 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008380
8381 if (!list_empty_careful(&splice)) {
8382 spin_lock(&root->fs_info->delalloc_lock);
8383 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8384 spin_unlock(&root->fs_info->delalloc_lock);
8385 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008386 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008387}
8388
Chris Mason39279cc2007-06-12 06:35:45 -04008389static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8390 const char *symname)
8391{
8392 struct btrfs_trans_handle *trans;
8393 struct btrfs_root *root = BTRFS_I(dir)->root;
8394 struct btrfs_path *path;
8395 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008396 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008397 int err;
8398 int drop_inode = 0;
8399 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008400 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008401 int name_len;
8402 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008403 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008404 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008405 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008406
8407 name_len = strlen(symname) + 1;
8408 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8409 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008410
Josef Bacik9ed74f22009-09-11 16:12:44 -04008411 /*
8412 * 2 items for inode item and ref
8413 * 2 items for dir items
8414 * 1 item for xattr if selinux is on
8415 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008416 trans = btrfs_start_transaction(root, 5);
8417 if (IS_ERR(trans))
8418 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008419
Li Zefan581bb052011-04-20 10:06:11 +08008420 err = btrfs_find_free_ino(root, &objectid);
8421 if (err)
8422 goto out_unlock;
8423
Josef Bacikaec74772008-07-24 12:12:38 -04008424 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008425 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008426 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008427 if (IS_ERR(inode)) {
8428 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008429 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008430 }
Chris Mason39279cc2007-06-12 06:35:45 -04008431
Eric Paris2a7dba32011-02-01 11:05:39 -05008432 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008433 if (err) {
8434 drop_inode = 1;
8435 goto out_unlock;
8436 }
8437
Casey Schauflerad19db72011-12-15 10:09:07 -05008438 /*
8439 * If the active LSM wants to access the inode during
8440 * d_instantiate it needs these. Smack checks to see
8441 * if the filesystem supports xattrs by looking at the
8442 * ops vector.
8443 */
8444 inode->i_fop = &btrfs_file_operations;
8445 inode->i_op = &btrfs_file_inode_operations;
8446
Josef Bacika1b075d2010-11-19 20:36:11 +00008447 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008448 if (err)
8449 drop_inode = 1;
8450 else {
8451 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008452 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008453 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008454 }
Chris Mason39279cc2007-06-12 06:35:45 -04008455 if (drop_inode)
8456 goto out_unlock;
8457
8458 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008459 if (!path) {
8460 err = -ENOMEM;
8461 drop_inode = 1;
8462 goto out_unlock;
8463 }
Li Zefan33345d012011-04-20 10:31:50 +08008464 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008465 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008466 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8467 datasize = btrfs_file_extent_calc_inline_size(name_len);
8468 err = btrfs_insert_empty_item(trans, root, path, &key,
8469 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008470 if (err) {
8471 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008472 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008473 goto out_unlock;
8474 }
Chris Mason5f39d392007-10-15 16:14:19 -04008475 leaf = path->nodes[0];
8476 ei = btrfs_item_ptr(leaf, path->slots[0],
8477 struct btrfs_file_extent_item);
8478 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8479 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008480 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008481 btrfs_set_file_extent_encryption(leaf, ei, 0);
8482 btrfs_set_file_extent_compression(leaf, ei, 0);
8483 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8484 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8485
Chris Mason39279cc2007-06-12 06:35:45 -04008486 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008487 write_extent_buffer(leaf, symname, ptr, name_len);
8488 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008489 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008490
Chris Mason39279cc2007-06-12 06:35:45 -04008491 inode->i_op = &btrfs_symlink_inode_operations;
8492 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008493 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008494 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008495 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008496 err = btrfs_update_inode(trans, root, inode);
8497 if (err)
8498 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008499
8500out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008501 if (!err)
8502 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008503 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008504 if (drop_inode) {
8505 inode_dec_link_count(inode);
8506 iput(inode);
8507 }
Liu Bob53d3f52012-11-14 14:34:34 +00008508 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008509 return err;
8510}
Chris Mason16432982008-04-10 10:23:21 -04008511
Josef Bacik0af3d002010-06-21 14:48:16 -04008512static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8513 u64 start, u64 num_bytes, u64 min_size,
8514 loff_t actual_len, u64 *alloc_hint,
8515 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008516{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008517 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8518 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008519 struct btrfs_root *root = BTRFS_I(inode)->root;
8520 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008521 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008522 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008523 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008524 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008525 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008526
Josef Bacik0af3d002010-06-21 14:48:16 -04008527 if (trans)
8528 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008529 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008530 if (own_trans) {
8531 trans = btrfs_start_transaction(root, 3);
8532 if (IS_ERR(trans)) {
8533 ret = PTR_ERR(trans);
8534 break;
8535 }
Yan Zhengd899e052008-10-30 14:25:28 -04008536 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008537
Chris Mason154ea282013-03-05 11:11:26 -05008538 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8539 cur_bytes = max(cur_bytes, min_size);
8540 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008541 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008542 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008543 if (own_trans)
8544 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008545 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008546 }
8547
Yan Zhengd899e052008-10-30 14:25:28 -04008548 ret = insert_reserved_file_extent(trans, inode,
8549 cur_offset, ins.objectid,
8550 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008551 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008552 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008553 if (ret) {
8554 btrfs_abort_transaction(trans, root, ret);
8555 if (own_trans)
8556 btrfs_end_transaction(trans, root);
8557 break;
8558 }
Chris Masona1ed8352009-09-11 12:27:37 -04008559 btrfs_drop_extent_cache(inode, cur_offset,
8560 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008561
Josef Bacik5dc562c2012-08-17 13:14:17 -04008562 em = alloc_extent_map();
8563 if (!em) {
8564 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8565 &BTRFS_I(inode)->runtime_flags);
8566 goto next;
8567 }
8568
8569 em->start = cur_offset;
8570 em->orig_start = cur_offset;
8571 em->len = ins.offset;
8572 em->block_start = ins.objectid;
8573 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008574 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008575 em->bdev = root->fs_info->fs_devices->latest_bdev;
8576 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8577 em->generation = trans->transid;
8578
8579 while (1) {
8580 write_lock(&em_tree->lock);
8581 ret = add_extent_mapping(em_tree, em);
8582 if (!ret)
8583 list_move(&em->list,
8584 &em_tree->modified_extents);
8585 write_unlock(&em_tree->lock);
8586 if (ret != -EEXIST)
8587 break;
8588 btrfs_drop_extent_cache(inode, cur_offset,
8589 cur_offset + ins.offset - 1,
8590 0);
8591 }
8592 free_extent_map(em);
8593next:
Yan Zhengd899e052008-10-30 14:25:28 -04008594 num_bytes -= ins.offset;
8595 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008596 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008597
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008598 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008599 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008600 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008601 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008602 (actual_len > inode->i_size) &&
8603 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008604 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008605 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008606 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008607 i_size = cur_offset;
8608 i_size_write(inode, i_size);
8609 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008610 }
8611
Yan Zhengd899e052008-10-30 14:25:28 -04008612 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008613
8614 if (ret) {
8615 btrfs_abort_transaction(trans, root, ret);
8616 if (own_trans)
8617 btrfs_end_transaction(trans, root);
8618 break;
8619 }
Yan Zhengd899e052008-10-30 14:25:28 -04008620
Josef Bacik0af3d002010-06-21 14:48:16 -04008621 if (own_trans)
8622 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008623 }
Yan Zhengd899e052008-10-30 14:25:28 -04008624 return ret;
8625}
8626
Josef Bacik0af3d002010-06-21 14:48:16 -04008627int btrfs_prealloc_file_range(struct inode *inode, int mode,
8628 u64 start, u64 num_bytes, u64 min_size,
8629 loff_t actual_len, u64 *alloc_hint)
8630{
8631 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8632 min_size, actual_len, alloc_hint,
8633 NULL);
8634}
8635
8636int btrfs_prealloc_file_range_trans(struct inode *inode,
8637 struct btrfs_trans_handle *trans, int mode,
8638 u64 start, u64 num_bytes, u64 min_size,
8639 loff_t actual_len, u64 *alloc_hint)
8640{
8641 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8642 min_size, actual_len, alloc_hint, trans);
8643}
8644
Chris Masone6dcd2d2008-07-17 12:53:50 -04008645static int btrfs_set_page_dirty(struct page *page)
8646{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008647 return __set_page_dirty_nobuffers(page);
8648}
8649
Al Viro10556cb2011-06-20 19:28:19 -04008650static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008651{
Li Zefanb83cc962010-12-20 16:04:08 +08008652 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008653 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008654
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008655 if (mask & MAY_WRITE &&
8656 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8657 if (btrfs_root_readonly(root))
8658 return -EROFS;
8659 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8660 return -EACCES;
8661 }
Al Viro2830ba72011-06-20 19:16:29 -04008662 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008663}
Chris Mason39279cc2007-06-12 06:35:45 -04008664
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008665static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008666 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008667 .lookup = btrfs_lookup,
8668 .create = btrfs_create,
8669 .unlink = btrfs_unlink,
8670 .link = btrfs_link,
8671 .mkdir = btrfs_mkdir,
8672 .rmdir = btrfs_rmdir,
8673 .rename = btrfs_rename,
8674 .symlink = btrfs_symlink,
8675 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008676 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008677 .setxattr = btrfs_setxattr,
8678 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008679 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008680 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008681 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008682 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008683};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008684static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008685 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008686 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008687 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008688};
Yan, Zheng76dda932009-09-21 16:00:26 -04008689
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008690static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008691 .llseek = generic_file_llseek,
8692 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008693 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008694 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008695#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008696 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008697#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008698 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008699 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008700};
8701
Chris Masond1310b22008-01-24 16:13:08 -05008702static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008703 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008704 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008705 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008706 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008707 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008708 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008709 .set_bit_hook = btrfs_set_bit_hook,
8710 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008711 .merge_extent_hook = btrfs_merge_extent_hook,
8712 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008713};
8714
Chris Mason35054392009-01-21 13:11:13 -05008715/*
8716 * btrfs doesn't support the bmap operation because swapfiles
8717 * use bmap to make a mapping of extents in the file. They assume
8718 * these extents won't change over the life of the file and they
8719 * use the bmap result to do IO directly to the drive.
8720 *
8721 * the btrfs bmap call would return logical addresses that aren't
8722 * suitable for IO and they also will change frequently as COW
8723 * operations happen. So, swapfile + btrfs == corruption.
8724 *
8725 * For now we're avoiding this by dropping bmap.
8726 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008727static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008728 .readpage = btrfs_readpage,
8729 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008730 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008731 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008732 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008733 .invalidatepage = btrfs_invalidatepage,
8734 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008735 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008736 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008737};
8738
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008739static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008740 .readpage = btrfs_readpage,
8741 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008742 .invalidatepage = btrfs_invalidatepage,
8743 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008744};
8745
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008746static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008747 .getattr = btrfs_getattr,
8748 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008749 .setxattr = btrfs_setxattr,
8750 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008751 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008752 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008753 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008754 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008755 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008756 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008757};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008758static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008759 .getattr = btrfs_getattr,
8760 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008761 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008762 .setxattr = btrfs_setxattr,
8763 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008764 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008765 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008766 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008767 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008768};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008769static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008770 .readlink = generic_readlink,
8771 .follow_link = page_follow_link_light,
8772 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008773 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008774 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008775 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008776 .setxattr = btrfs_setxattr,
8777 .getxattr = btrfs_getxattr,
8778 .listxattr = btrfs_listxattr,
8779 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008780 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008781 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008782};
Yan, Zheng76dda932009-09-21 16:00:26 -04008783
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008784const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008785 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008786 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008787};