blob: 3f3129173b2664f7faeea87e6a782bde59003a2d [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Chris Mason39279cc2007-06-12 06:35:45 -040059
60struct btrfs_iget_args {
61 u64 ino;
62 struct btrfs_root *root;
63};
64
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070065static const struct inode_operations btrfs_dir_inode_operations;
66static const struct inode_operations btrfs_symlink_inode_operations;
67static const struct inode_operations btrfs_dir_ro_inode_operations;
68static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
71static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050073static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040074
75static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000076static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_trans_handle_cachep;
78struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050080struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081
82#define S_SHIFT 12
83static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
84 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
85 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
86 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
87 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
88 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
89 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
90 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
91};
92
Eric Sandeen3972f262013-01-12 02:57:22 +000093static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050094static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040095static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050096static noinline int cow_file_range(struct inode *inode,
97 struct page *locked_page,
98 u64 start, u64 end, int *page_started,
99 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400100static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
101 u64 len, u64 orig_start,
102 u64 block_start, u64 block_len,
103 u64 orig_block_len, int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400104
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000105static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500106 struct inode *inode, struct inode *dir,
107 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500108{
109 int err;
110
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500112 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500113 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500114 return err;
115}
116
Chris Masond352ac62008-09-29 15:18:18 -0400117/*
Chris Masonc8b97812008-10-29 14:49:59 -0400118 * this does all the hard work for inserting an inline extent into
119 * the btree. The caller should have done a btrfs_drop_extents so that
120 * no overlapping inline items exist in the btree
121 */
Chris Masond3977122009-01-05 21:25:51 -0500122static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400123 struct btrfs_root *root, struct inode *inode,
124 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000125 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400126 struct page **compressed_pages)
127{
128 struct btrfs_key key;
129 struct btrfs_path *path;
130 struct extent_buffer *leaf;
131 struct page *page = NULL;
132 char *kaddr;
133 unsigned long ptr;
134 struct btrfs_file_extent_item *ei;
135 int err = 0;
136 int ret;
137 size_t cur_size = size;
138 size_t datasize;
139 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400140
Li Zefanfe3f5662011-03-28 08:30:38 +0000141 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400142 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400143
Chris Masond3977122009-01-05 21:25:51 -0500144 path = btrfs_alloc_path();
145 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400146 return -ENOMEM;
147
Chris Masonb9473432009-03-13 11:00:37 -0400148 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400149
Li Zefan33345d012011-04-20 10:31:50 +0800150 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400151 key.offset = start;
152 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400153 datasize = btrfs_file_extent_calc_inline_size(cur_size);
154
155 inode_add_bytes(inode, size);
156 ret = btrfs_insert_empty_item(trans, root, path, &key,
157 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400158 if (ret) {
159 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400160 goto fail;
161 }
162 leaf = path->nodes[0];
163 ei = btrfs_item_ptr(leaf, path->slots[0],
164 struct btrfs_file_extent_item);
165 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
166 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
167 btrfs_set_file_extent_encryption(leaf, ei, 0);
168 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
169 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
170 ptr = btrfs_file_extent_inline_start(ei);
171
Li Zefan261507a02010-12-17 14:21:50 +0800172 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400173 struct page *cpage;
174 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500175 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400176 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500177 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400178 PAGE_CACHE_SIZE);
179
Cong Wang7ac687d2011-11-25 23:14:28 +0800180 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400181 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800182 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400183
184 i++;
185 ptr += cur_size;
186 compressed_size -= cur_size;
187 }
188 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800189 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400190 } else {
191 page = find_get_page(inode->i_mapping,
192 start >> PAGE_CACHE_SHIFT);
193 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800194 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 offset = start & (PAGE_CACHE_SIZE - 1);
196 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800197 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400198 page_cache_release(page);
199 }
200 btrfs_mark_buffer_dirty(leaf);
201 btrfs_free_path(path);
202
Yan, Zhengc2167752009-11-12 09:34:21 +0000203 /*
204 * we're an inline extent, so nobody can
205 * extend the file past i_size without locking
206 * a page we already have locked.
207 *
208 * We must do any isize and inode updates
209 * before we unlock the pages. Otherwise we
210 * could end up racing with unlink.
211 */
Chris Masonc8b97812008-10-29 14:49:59 -0400212 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100213 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000214
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100215 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400216fail:
217 btrfs_free_path(path);
218 return err;
219}
220
221
222/*
223 * conditionally insert an inline extent into the file. This
224 * does the checks required to make sure the data is small enough
225 * to fit as an inline extent.
226 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400227static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400228 struct btrfs_root *root,
229 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000230 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400231 struct page **compressed_pages)
232{
233 u64 isize = i_size_read(inode);
234 u64 actual_end = min(end + 1, isize);
235 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000236 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 u64 data_len = inline_len;
238 int ret;
239
240 if (compressed_size)
241 data_len = compressed_size;
242
243 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400244 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400245 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
246 (!compressed_size &&
247 (actual_end & (root->sectorsize - 1)) == 0) ||
248 end + 1 < isize ||
249 data_len > root->fs_info->max_inline) {
250 return 1;
251 }
252
Josef Bacik26714852012-08-29 12:24:27 -0400253 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100254 if (ret)
255 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400256
257 if (isize > actual_end)
258 inline_len = min_t(u64, isize, actual_end);
259 ret = insert_inline_extent(trans, root, inode, start,
260 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000261 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400262 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100263 btrfs_abort_transaction(trans, root, ret);
264 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400265 } else if (ret == -ENOSPC) {
266 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400268
Josef Bacikbdc20e62013-02-28 13:23:38 -0500269 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400270 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400271 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400272 return 0;
273}
274
Chris Mason771ed682008-11-06 22:02:51 -0500275struct async_extent {
276 u64 start;
277 u64 ram_size;
278 u64 compressed_size;
279 struct page **pages;
280 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800281 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500282 struct list_head list;
283};
284
285struct async_cow {
286 struct inode *inode;
287 struct btrfs_root *root;
288 struct page *locked_page;
289 u64 start;
290 u64 end;
291 struct list_head extents;
292 struct btrfs_work work;
293};
294
295static noinline int add_async_extent(struct async_cow *cow,
296 u64 start, u64 ram_size,
297 u64 compressed_size,
298 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800299 unsigned long nr_pages,
300 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500301{
302 struct async_extent *async_extent;
303
304 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100305 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500306 async_extent->start = start;
307 async_extent->ram_size = ram_size;
308 async_extent->compressed_size = compressed_size;
309 async_extent->pages = pages;
310 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800311 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500312 list_add_tail(&async_extent->list, &cow->extents);
313 return 0;
314}
315
Chris Masonc8b97812008-10-29 14:49:59 -0400316/*
Chris Mason771ed682008-11-06 22:02:51 -0500317 * we create compressed extents in two phases. The first
318 * phase compresses a range of pages that have already been
319 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400320 *
Chris Mason771ed682008-11-06 22:02:51 -0500321 * This is done inside an ordered work queue, and the compression
322 * is spread across many cpus. The actual IO submission is step
323 * two, and the ordered work queue takes care of making sure that
324 * happens in the same order things were put onto the queue by
325 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400326 *
Chris Mason771ed682008-11-06 22:02:51 -0500327 * If this code finds it can't get good compression, it puts an
328 * entry onto the work queue to write the uncompressed bytes. This
329 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300330 * are written in the same order that the flusher thread sent them
331 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400332 */
Chris Mason771ed682008-11-06 22:02:51 -0500333static noinline int compress_file_range(struct inode *inode,
334 struct page *locked_page,
335 u64 start, u64 end,
336 struct async_cow *async_cow,
337 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400338{
339 struct btrfs_root *root = BTRFS_I(inode)->root;
340 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400341 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400342 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400343 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500344 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400345 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400346 struct page **pages = NULL;
347 unsigned long nr_pages;
348 unsigned long nr_pages_ret = 0;
349 unsigned long total_compressed = 0;
350 unsigned long total_in = 0;
351 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500352 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400353 int i;
354 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800355 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400356 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400357
Liu Bo4cb13e52012-03-29 09:57:45 -0400358 /* if this is a small write inside eof, kick off a defrag */
359 if ((end - start + 1) < 16 * 1024 &&
360 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400361 btrfs_add_inode_defrag(NULL, inode);
362
Chris Mason42dc7ba2008-12-15 11:44:56 -0500363 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400364again:
365 will_compress = 0;
366 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
367 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
368
Chris Masonf03d9301f2009-02-04 09:31:06 -0500369 /*
370 * we don't want to send crud past the end of i_size through
371 * compression, that's just a waste of CPU time. So, if the
372 * end of the file is before the start of our current
373 * requested range of bytes, we bail out to the uncompressed
374 * cleanup code that can deal with all of this.
375 *
376 * It isn't really the fastest way to fix things, but this is a
377 * very uncommon corner.
378 */
379 if (actual_end <= start)
380 goto cleanup_and_bail_uncompressed;
381
Chris Masonc8b97812008-10-29 14:49:59 -0400382 total_compressed = actual_end - start;
383
384 /* we want to make sure that amount of ram required to uncompress
385 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500386 * of a compressed extent to 128k. This is a crucial number
387 * because it also controls how easily we can spread reads across
388 * cpus for decompression.
389 *
390 * We also want to make sure the amount of IO required to do
391 * a random read is reasonably small, so we limit the size of
392 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400393 */
394 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000395 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500396 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400397 total_in = 0;
398 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400399
Chris Mason771ed682008-11-06 22:02:51 -0500400 /*
401 * we do compression for mount -o compress and when the
402 * inode has not been flagged as nocompress. This flag can
403 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400404 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200405 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500406 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000407 (BTRFS_I(inode)->force_compress) ||
408 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400409 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400410 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800411 if (!pages) {
412 /* just bail out to the uncompressed code */
413 goto cont;
414 }
Chris Mason179e29e2007-11-01 11:28:41 -0400415
Li Zefan261507a02010-12-17 14:21:50 +0800416 if (BTRFS_I(inode)->force_compress)
417 compress_type = BTRFS_I(inode)->force_compress;
418
Chris Mason4adaa612013-03-26 13:07:00 -0400419 /*
420 * we need to call clear_page_dirty_for_io on each
421 * page in the range. Otherwise applications with the file
422 * mmap'd can wander in and change the page contents while
423 * we are compressing them.
424 *
425 * If the compression fails for any reason, we set the pages
426 * dirty again later on.
427 */
428 extent_range_clear_dirty_for_io(inode, start, end);
429 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800430 ret = btrfs_compress_pages(compress_type,
431 inode->i_mapping, start,
432 total_compressed, pages,
433 nr_pages, &nr_pages_ret,
434 &total_in,
435 &total_compressed,
436 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400437
438 if (!ret) {
439 unsigned long offset = total_compressed &
440 (PAGE_CACHE_SIZE - 1);
441 struct page *page = pages[nr_pages_ret - 1];
442 char *kaddr;
443
444 /* zero the tail end of the last page, we might be
445 * sending it down to disk
446 */
447 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800448 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400449 memset(kaddr + offset, 0,
450 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800451 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400452 }
453 will_compress = 1;
454 }
455 }
Li Zefan560f7d72011-09-08 10:22:01 +0800456cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400457 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400458 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100459 if (IS_ERR(trans)) {
460 ret = PTR_ERR(trans);
461 trans = NULL;
462 goto cleanup_and_out;
463 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400464 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500465
Chris Masonc8b97812008-10-29 14:49:59 -0400466 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500467 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400468 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500469 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400470 */
471 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000472 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400473 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500474 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400475 ret = cow_file_range_inline(trans, root, inode,
476 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000477 total_compressed,
478 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100480 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500481 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100482 * inline extent creation worked or returned error,
483 * we don't need to create any more async work items.
484 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500485 */
Chris Masonc8b97812008-10-29 14:49:59 -0400486 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400487 &BTRFS_I(inode)->io_tree,
488 start, end, NULL,
489 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400490 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400491 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000492
493 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400494 goto free_pages_out;
495 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000496 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400497 }
498
499 if (will_compress) {
500 /*
501 * we aren't doing an inline extent round the compressed size
502 * up to a block size boundary so the allocator does sane
503 * things
504 */
Qu Wenruofda28322013-02-26 08:10:22 +0000505 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400506
507 /*
508 * one last check to make sure the compression is really a
509 * win, compare the page count read with the blocks on disk
510 */
Qu Wenruofda28322013-02-26 08:10:22 +0000511 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400512 if (total_compressed >= total_in) {
513 will_compress = 0;
514 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400515 num_bytes = total_in;
516 }
517 }
518 if (!will_compress && pages) {
519 /*
520 * the compression code ran but failed to make things smaller,
521 * free any pages it allocated and our page pointer array
522 */
523 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400524 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400525 page_cache_release(pages[i]);
526 }
527 kfree(pages);
528 pages = NULL;
529 total_compressed = 0;
530 nr_pages_ret = 0;
531
532 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500533 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
534 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500535 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500536 }
Chris Masonc8b97812008-10-29 14:49:59 -0400537 }
Chris Mason771ed682008-11-06 22:02:51 -0500538 if (will_compress) {
539 *num_added += 1;
540
541 /* the async work queues will take care of doing actual
542 * allocation on disk for these compressed pages,
543 * and will submit them to the elevator.
544 */
545 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800546 total_compressed, pages, nr_pages_ret,
547 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500548
Yan, Zheng24ae6362010-12-06 07:02:36 +0000549 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500550 start += num_bytes;
551 pages = NULL;
552 cond_resched();
553 goto again;
554 }
555 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500556cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500557 /*
558 * No compression, but we still need to write the pages in
559 * the file we've been given so far. redirty the locked
560 * page if it corresponds to our extent and set things up
561 * for the async work queue to run cow_file_range to do
562 * the normal delalloc dance
563 */
564 if (page_offset(locked_page) >= start &&
565 page_offset(locked_page) <= end) {
566 __set_page_dirty_nobuffers(locked_page);
567 /* unlocked later on in the async handlers */
568 }
Chris Mason4adaa612013-03-26 13:07:00 -0400569 if (redirty)
570 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800571 add_async_extent(async_cow, start, end - start + 1,
572 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500573 *num_added += 1;
574 }
575
576out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100577 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500578
579free_pages_out:
580 for (i = 0; i < nr_pages_ret; i++) {
581 WARN_ON(pages[i]->mapping);
582 page_cache_release(pages[i]);
583 }
Chris Masond3977122009-01-05 21:25:51 -0500584 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500585
586 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100587
588cleanup_and_out:
589 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
590 start, end, NULL,
591 EXTENT_CLEAR_UNLOCK_PAGE |
592 EXTENT_CLEAR_DIRTY |
593 EXTENT_CLEAR_DELALLOC |
594 EXTENT_SET_WRITEBACK |
595 EXTENT_END_WRITEBACK);
596 if (!trans || IS_ERR(trans))
597 btrfs_error(root->fs_info, ret, "Failed to join transaction");
598 else
599 btrfs_abort_transaction(trans, root, ret);
600 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500601}
602
603/*
604 * phase two of compressed writeback. This is the ordered portion
605 * of the code, which only gets called in the order the work was
606 * queued. We walk all the async extents created by compress_file_range
607 * and send them down to the disk.
608 */
609static noinline int submit_compressed_extents(struct inode *inode,
610 struct async_cow *async_cow)
611{
612 struct async_extent *async_extent;
613 u64 alloc_hint = 0;
614 struct btrfs_trans_handle *trans;
615 struct btrfs_key ins;
616 struct extent_map *em;
617 struct btrfs_root *root = BTRFS_I(inode)->root;
618 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
619 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500620 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500621
622 if (list_empty(&async_cow->extents))
623 return 0;
624
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500625again:
Chris Masond3977122009-01-05 21:25:51 -0500626 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500627 async_extent = list_entry(async_cow->extents.next,
628 struct async_extent, list);
629 list_del(&async_extent->list);
630
631 io_tree = &BTRFS_I(inode)->io_tree;
632
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500633retry:
Chris Mason771ed682008-11-06 22:02:51 -0500634 /* did the compression code fall back to uncompressed IO? */
635 if (!async_extent->pages) {
636 int page_started = 0;
637 unsigned long nr_written = 0;
638
639 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000640 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100641 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500642
643 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500644 ret = cow_file_range(inode, async_cow->locked_page,
645 async_extent->start,
646 async_extent->start +
647 async_extent->ram_size - 1,
648 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500649
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100650 /* JDM XXX */
651
Chris Mason771ed682008-11-06 22:02:51 -0500652 /*
653 * if page_started, cow_file_range inserted an
654 * inline extent and took care of all the unlocking
655 * and IO for us. Otherwise, we need to submit
656 * all those pages down to the drive.
657 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500658 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500659 extent_write_locked_range(io_tree,
660 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500661 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500662 async_extent->ram_size - 1,
663 btrfs_get_extent,
664 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500665 else if (ret)
666 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500667 kfree(async_extent);
668 cond_resched();
669 continue;
670 }
671
672 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100673 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500674
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400675 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100676 if (IS_ERR(trans)) {
677 ret = PTR_ERR(trans);
678 } else {
679 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
680 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500681 async_extent->compressed_size,
682 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500683 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600684 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100685 btrfs_abort_transaction(trans, root, ret);
686 btrfs_end_transaction(trans, root);
687 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000688
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500689 if (ret) {
690 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500691
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500692 for (i = 0; i < async_extent->nr_pages; i++) {
693 WARN_ON(async_extent->pages[i]->mapping);
694 page_cache_release(async_extent->pages[i]);
695 }
696 kfree(async_extent->pages);
697 async_extent->nr_pages = 0;
698 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500699
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100700 if (ret == -ENOSPC)
701 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500702 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500703 }
704
Yan, Zhengc2167752009-11-12 09:34:21 +0000705 /*
706 * here we're doing allocation and writeback of the
707 * compressed pages
708 */
709 btrfs_drop_extent_cache(inode, async_extent->start,
710 async_extent->start +
711 async_extent->ram_size - 1, 0);
712
David Sterba172ddd62011-04-21 00:48:27 +0200713 em = alloc_extent_map();
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500714 if (!em)
715 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500716 em->start = async_extent->start;
717 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500718 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400719 em->mod_start = em->start;
720 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500721
722 em->block_start = ins.objectid;
723 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500724 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500725 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800726 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500727 set_bit(EXTENT_FLAG_PINNED, &em->flags);
728 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400729 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500730
Chris Masond3977122009-01-05 21:25:51 -0500731 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400732 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500733 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400734 if (!ret)
735 list_move(&em->list,
736 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400737 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500738 if (ret != -EEXIST) {
739 free_extent_map(em);
740 break;
741 }
742 btrfs_drop_extent_cache(inode, async_extent->start,
743 async_extent->start +
744 async_extent->ram_size - 1, 0);
745 }
746
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500747 if (ret)
748 goto out_free_reserve;
749
Li Zefan261507a02010-12-17 14:21:50 +0800750 ret = btrfs_add_ordered_extent_compress(inode,
751 async_extent->start,
752 ins.objectid,
753 async_extent->ram_size,
754 ins.offset,
755 BTRFS_ORDERED_COMPRESSED,
756 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500757 if (ret)
758 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500759
Chris Mason771ed682008-11-06 22:02:51 -0500760 /*
761 * clear dirty, set writeback and unlock the pages.
762 */
763 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400764 &BTRFS_I(inode)->io_tree,
765 async_extent->start,
766 async_extent->start +
767 async_extent->ram_size - 1,
768 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
769 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400770 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400771 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500772
773 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500774 async_extent->start,
775 async_extent->ram_size,
776 ins.objectid,
777 ins.offset, async_extent->pages,
778 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500779 alloc_hint = ins.objectid + ins.offset;
780 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500781 if (ret)
782 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500783 cond_resched();
784 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100785 ret = 0;
786out:
787 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500788out_free_reserve:
789 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100790out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500791 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
792 async_extent->start,
793 async_extent->start +
794 async_extent->ram_size - 1,
795 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
796 EXTENT_CLEAR_UNLOCK |
797 EXTENT_CLEAR_DELALLOC |
798 EXTENT_CLEAR_DIRTY |
799 EXTENT_SET_WRITEBACK |
800 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100801 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500802 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500803}
804
Josef Bacik4b46fce2010-05-23 11:00:55 -0400805static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
806 u64 num_bytes)
807{
808 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
809 struct extent_map *em;
810 u64 alloc_hint = 0;
811
812 read_lock(&em_tree->lock);
813 em = search_extent_mapping(em_tree, start, num_bytes);
814 if (em) {
815 /*
816 * if block start isn't an actual block number then find the
817 * first block in this inode and use that as a hint. If that
818 * block is also bogus then just don't worry about it.
819 */
820 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
821 free_extent_map(em);
822 em = search_extent_mapping(em_tree, 0, 0);
823 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
824 alloc_hint = em->block_start;
825 if (em)
826 free_extent_map(em);
827 } else {
828 alloc_hint = em->block_start;
829 free_extent_map(em);
830 }
831 }
832 read_unlock(&em_tree->lock);
833
834 return alloc_hint;
835}
836
Chris Mason771ed682008-11-06 22:02:51 -0500837/*
838 * when extent_io.c finds a delayed allocation range in the file,
839 * the call backs end up in this code. The basic idea is to
840 * allocate extents on disk for the range, and create ordered data structs
841 * in ram to track those extents.
842 *
843 * locked_page is the page that writepage had locked already. We use
844 * it to make sure we don't do extra locks or unlocks.
845 *
846 * *page_started is set to one if we unlock locked_page and do everything
847 * required to start IO on it. It may be clean and already done with
848 * IO when we return.
849 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000850static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
851 struct inode *inode,
852 struct btrfs_root *root,
853 struct page *locked_page,
854 u64 start, u64 end, int *page_started,
855 unsigned long *nr_written,
856 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500857{
Chris Mason771ed682008-11-06 22:02:51 -0500858 u64 alloc_hint = 0;
859 u64 num_bytes;
860 unsigned long ram_size;
861 u64 disk_num_bytes;
862 u64 cur_alloc_size;
863 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500864 struct btrfs_key ins;
865 struct extent_map *em;
866 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
867 int ret = 0;
868
Liu Bo83eea1f2012-07-10 05:28:39 -0600869 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500870
Qu Wenruofda28322013-02-26 08:10:22 +0000871 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500872 num_bytes = max(blocksize, num_bytes);
873 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500874
Chris Mason4cb53002011-05-24 15:35:30 -0400875 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400876 if (num_bytes < 64 * 1024 &&
877 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400878 btrfs_add_inode_defrag(trans, inode);
879
Chris Mason771ed682008-11-06 22:02:51 -0500880 if (start == 0) {
881 /* lets try to make an inline extent */
882 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000883 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500884 if (ret == 0) {
885 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400886 &BTRFS_I(inode)->io_tree,
887 start, end, NULL,
888 EXTENT_CLEAR_UNLOCK_PAGE |
889 EXTENT_CLEAR_UNLOCK |
890 EXTENT_CLEAR_DELALLOC |
891 EXTENT_CLEAR_DIRTY |
892 EXTENT_SET_WRITEBACK |
893 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000894
Chris Mason771ed682008-11-06 22:02:51 -0500895 *nr_written = *nr_written +
896 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
897 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500898 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100899 } else if (ret < 0) {
900 btrfs_abort_transaction(trans, root, ret);
901 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500902 }
903 }
Chris Masonc8b97812008-10-29 14:49:59 -0400904
905 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200906 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400907
Josef Bacik4b46fce2010-05-23 11:00:55 -0400908 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400909 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400910
Chris Masond3977122009-01-05 21:25:51 -0500911 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400912 unsigned long op;
913
Josef Bacik287a0ab2010-03-19 18:07:23 +0000914 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400915 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500916 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500917 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100918 if (ret < 0) {
919 btrfs_abort_transaction(trans, root, ret);
920 goto out_unlock;
921 }
Chris Masond3977122009-01-05 21:25:51 -0500922
David Sterba172ddd62011-04-21 00:48:27 +0200923 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100924 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400925 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500926 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500927 ram_size = ins.offset;
928 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400929 em->mod_start = em->start;
930 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400931
Chris Masone6dcd2d2008-07-17 12:53:50 -0400932 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400933 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500934 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400935 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400936 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400937 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400938
Chris Masond3977122009-01-05 21:25:51 -0500939 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400940 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400941 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400942 if (!ret)
943 list_move(&em->list,
944 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400945 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400946 if (ret != -EEXIST) {
947 free_extent_map(em);
948 break;
949 }
950 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400951 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400952 }
953
Chris Mason98d20f62008-04-14 09:46:10 -0400954 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400955 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500956 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100957 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400958
Yan Zheng17d217f2008-12-12 10:03:38 -0500959 if (root->root_key.objectid ==
960 BTRFS_DATA_RELOC_TREE_OBJECTID) {
961 ret = btrfs_reloc_clone_csums(inode, start,
962 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100963 if (ret) {
964 btrfs_abort_transaction(trans, root, ret);
965 goto out_unlock;
966 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500967 }
968
Chris Masond3977122009-01-05 21:25:51 -0500969 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400970 break;
Chris Masond3977122009-01-05 21:25:51 -0500971
Chris Masonc8b97812008-10-29 14:49:59 -0400972 /* we're not doing compressed IO, don't unlock the first
973 * page (which the caller expects to stay locked), don't
974 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400975 *
976 * Do set the Private2 bit so we know this page was properly
977 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400978 */
Chris Masona791e352009-10-08 11:27:10 -0400979 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
980 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
981 EXTENT_SET_PRIVATE2;
982
Chris Masonc8b97812008-10-29 14:49:59 -0400983 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
984 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400985 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400986 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500987 num_bytes -= cur_alloc_size;
988 alloc_hint = ins.objectid + ins.offset;
989 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400990 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100991out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500992 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000993
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994out_unlock:
995 extent_clear_unlock_delalloc(inode,
996 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400997 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100998 EXTENT_CLEAR_UNLOCK_PAGE |
999 EXTENT_CLEAR_UNLOCK |
1000 EXTENT_CLEAR_DELALLOC |
1001 EXTENT_CLEAR_DIRTY |
1002 EXTENT_SET_WRITEBACK |
1003 EXTENT_END_WRITEBACK);
1004
1005 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001006}
Chris Masonc8b97812008-10-29 14:49:59 -04001007
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001008static noinline int cow_file_range(struct inode *inode,
1009 struct page *locked_page,
1010 u64 start, u64 end, int *page_started,
1011 unsigned long *nr_written,
1012 int unlock)
1013{
1014 struct btrfs_trans_handle *trans;
1015 struct btrfs_root *root = BTRFS_I(inode)->root;
1016 int ret;
1017
1018 trans = btrfs_join_transaction(root);
1019 if (IS_ERR(trans)) {
1020 extent_clear_unlock_delalloc(inode,
1021 &BTRFS_I(inode)->io_tree,
1022 start, end, locked_page,
1023 EXTENT_CLEAR_UNLOCK_PAGE |
1024 EXTENT_CLEAR_UNLOCK |
1025 EXTENT_CLEAR_DELALLOC |
1026 EXTENT_CLEAR_DIRTY |
1027 EXTENT_SET_WRITEBACK |
1028 EXTENT_END_WRITEBACK);
1029 return PTR_ERR(trans);
1030 }
1031 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1032
1033 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1034 page_started, nr_written, unlock);
1035
1036 btrfs_end_transaction(trans, root);
1037
1038 return ret;
1039}
1040
Chris Mason771ed682008-11-06 22:02:51 -05001041/*
1042 * work queue call back to started compression on a file and pages
1043 */
1044static noinline void async_cow_start(struct btrfs_work *work)
1045{
1046 struct async_cow *async_cow;
1047 int num_added = 0;
1048 async_cow = container_of(work, struct async_cow, work);
1049
1050 compress_file_range(async_cow->inode, async_cow->locked_page,
1051 async_cow->start, async_cow->end, async_cow,
1052 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001053 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001054 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001055 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001056 }
Chris Mason771ed682008-11-06 22:02:51 -05001057}
1058
1059/*
1060 * work queue call back to submit previously compressed pages
1061 */
1062static noinline void async_cow_submit(struct btrfs_work *work)
1063{
1064 struct async_cow *async_cow;
1065 struct btrfs_root *root;
1066 unsigned long nr_pages;
1067
1068 async_cow = container_of(work, struct async_cow, work);
1069
1070 root = async_cow->root;
1071 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1072 PAGE_CACHE_SHIFT;
1073
Josef Bacik66657b32012-08-01 15:36:24 -04001074 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001075 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001076 waitqueue_active(&root->fs_info->async_submit_wait))
1077 wake_up(&root->fs_info->async_submit_wait);
1078
Chris Masond3977122009-01-05 21:25:51 -05001079 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001080 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001081}
Chris Masonc8b97812008-10-29 14:49:59 -04001082
Chris Mason771ed682008-11-06 22:02:51 -05001083static noinline void async_cow_free(struct btrfs_work *work)
1084{
1085 struct async_cow *async_cow;
1086 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001087 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001088 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001089 kfree(async_cow);
1090}
1091
1092static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1093 u64 start, u64 end, int *page_started,
1094 unsigned long *nr_written)
1095{
1096 struct async_cow *async_cow;
1097 struct btrfs_root *root = BTRFS_I(inode)->root;
1098 unsigned long nr_pages;
1099 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001100 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001101
Chris Masona3429ab2009-10-08 12:30:20 -04001102 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1103 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001104 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001105 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001106 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001107 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001108 async_cow->root = root;
1109 async_cow->locked_page = locked_page;
1110 async_cow->start = start;
1111
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001112 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001113 cur_end = end;
1114 else
1115 cur_end = min(end, start + 512 * 1024 - 1);
1116
1117 async_cow->end = cur_end;
1118 INIT_LIST_HEAD(&async_cow->extents);
1119
1120 async_cow->work.func = async_cow_start;
1121 async_cow->work.ordered_func = async_cow_submit;
1122 async_cow->work.ordered_free = async_cow_free;
1123 async_cow->work.flags = 0;
1124
Chris Mason771ed682008-11-06 22:02:51 -05001125 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1126 PAGE_CACHE_SHIFT;
1127 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1128
1129 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1130 &async_cow->work);
1131
1132 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1133 wait_event(root->fs_info->async_submit_wait,
1134 (atomic_read(&root->fs_info->async_delalloc_pages) <
1135 limit));
1136 }
1137
Chris Masond3977122009-01-05 21:25:51 -05001138 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001139 atomic_read(&root->fs_info->async_delalloc_pages)) {
1140 wait_event(root->fs_info->async_submit_wait,
1141 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1142 0));
1143 }
1144
1145 *nr_written += nr_pages;
1146 start = cur_end + 1;
1147 }
1148 *page_started = 1;
1149 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001150}
1151
Chris Masond3977122009-01-05 21:25:51 -05001152static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001153 u64 bytenr, u64 num_bytes)
1154{
1155 int ret;
1156 struct btrfs_ordered_sum *sums;
1157 LIST_HEAD(list);
1158
Yan Zheng07d400a2009-01-06 11:42:00 -05001159 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001160 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001161 if (ret == 0 && list_empty(&list))
1162 return 0;
1163
1164 while (!list_empty(&list)) {
1165 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1166 list_del(&sums->list);
1167 kfree(sums);
1168 }
1169 return 1;
1170}
1171
Chris Masond352ac62008-09-29 15:18:18 -04001172/*
1173 * when nowcow writeback call back. This checks for snapshots or COW copies
1174 * of the extents that exist in the file, and COWs the file as required.
1175 *
1176 * If no cow copies or snapshots exist, we write directly to the existing
1177 * blocks on disk
1178 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001179static noinline int run_delalloc_nocow(struct inode *inode,
1180 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001181 u64 start, u64 end, int *page_started, int force,
1182 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001183{
Chris Masonbe20aa92007-12-17 20:14:01 -05001184 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001185 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001186 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001187 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001188 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001189 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001190 u64 cow_start;
1191 u64 cur_offset;
1192 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001193 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001194 u64 disk_bytenr;
1195 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001196 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001197 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001198 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001199 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 int nocow;
1201 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001202 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001203 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001204
1205 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001206 if (!path) {
1207 extent_clear_unlock_delalloc(inode,
1208 &BTRFS_I(inode)->io_tree,
1209 start, end, locked_page,
1210 EXTENT_CLEAR_UNLOCK_PAGE |
1211 EXTENT_CLEAR_UNLOCK |
1212 EXTENT_CLEAR_DELALLOC |
1213 EXTENT_CLEAR_DIRTY |
1214 EXTENT_SET_WRITEBACK |
1215 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001216 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001217 }
Li Zefan82d59022011-04-20 10:33:24 +08001218
Liu Bo83eea1f2012-07-10 05:28:39 -06001219 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001220
1221 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001222 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001223 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001224 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001225
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001226 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001227 extent_clear_unlock_delalloc(inode,
1228 &BTRFS_I(inode)->io_tree,
1229 start, end, locked_page,
1230 EXTENT_CLEAR_UNLOCK_PAGE |
1231 EXTENT_CLEAR_UNLOCK |
1232 EXTENT_CLEAR_DELALLOC |
1233 EXTENT_CLEAR_DIRTY |
1234 EXTENT_SET_WRITEBACK |
1235 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001236 btrfs_free_path(path);
1237 return PTR_ERR(trans);
1238 }
1239
Josef Bacik74b21072011-04-13 12:02:53 -04001240 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001241
Yan Zheng80ff3852008-10-30 14:20:02 -04001242 cow_start = (u64)-1;
1243 cur_offset = start;
1244 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001245 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001247 if (ret < 0) {
1248 btrfs_abort_transaction(trans, root, ret);
1249 goto error;
1250 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1252 leaf = path->nodes[0];
1253 btrfs_item_key_to_cpu(leaf, &found_key,
1254 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001255 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001256 found_key.type == BTRFS_EXTENT_DATA_KEY)
1257 path->slots[0]--;
1258 }
1259 check_prev = 0;
1260next_slot:
1261 leaf = path->nodes[0];
1262 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1263 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001264 if (ret < 0) {
1265 btrfs_abort_transaction(trans, root, ret);
1266 goto error;
1267 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 if (ret > 0)
1269 break;
1270 leaf = path->nodes[0];
1271 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001272
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 nocow = 0;
1274 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001275 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001277
Li Zefan33345d012011-04-20 10:31:50 +08001278 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1280 found_key.offset > end)
1281 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001282
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 if (found_key.offset > cur_offset) {
1284 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001285 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 goto out_check;
1287 }
Chris Masonc31f8832008-01-08 15:46:31 -05001288
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 fi = btrfs_item_ptr(leaf, path->slots[0],
1290 struct btrfs_file_extent_item);
1291 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001292
Yan Zhengd899e052008-10-30 14:25:28 -04001293 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1294 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001296 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 extent_end = found_key.offset +
1298 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001299 disk_num_bytes =
1300 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 if (extent_end <= start) {
1302 path->slots[0]++;
1303 goto next_slot;
1304 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001305 if (disk_bytenr == 0)
1306 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 if (btrfs_file_extent_compression(leaf, fi) ||
1308 btrfs_file_extent_encryption(leaf, fi) ||
1309 btrfs_file_extent_other_encoding(leaf, fi))
1310 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001311 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1312 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001313 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001315 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001316 found_key.offset -
1317 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001318 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001319 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001320 disk_bytenr += cur_offset - found_key.offset;
1321 num_bytes = min(end + 1, extent_end) - cur_offset;
1322 /*
1323 * force cow if csum exists in the range.
1324 * this ensure that csum for a given extent are
1325 * either valid or do not exist.
1326 */
1327 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1328 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 nocow = 1;
1330 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1331 extent_end = found_key.offset +
1332 btrfs_file_extent_inline_len(leaf, fi);
1333 extent_end = ALIGN(extent_end, root->sectorsize);
1334 } else {
1335 BUG_ON(1);
1336 }
1337out_check:
1338 if (extent_end <= start) {
1339 path->slots[0]++;
1340 goto next_slot;
1341 }
1342 if (!nocow) {
1343 if (cow_start == (u64)-1)
1344 cow_start = cur_offset;
1345 cur_offset = extent_end;
1346 if (cur_offset > end)
1347 break;
1348 path->slots[0]++;
1349 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001350 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001351
David Sterbab3b4aa72011-04-21 01:20:15 +02001352 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001354 ret = __cow_file_range(trans, inode, root, locked_page,
1355 cow_start, found_key.offset - 1,
1356 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001357 if (ret) {
1358 btrfs_abort_transaction(trans, root, ret);
1359 goto error;
1360 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001362 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001363
Yan Zhengd899e052008-10-30 14:25:28 -04001364 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1365 struct extent_map *em;
1366 struct extent_map_tree *em_tree;
1367 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001368 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001369 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001370 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001371 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001372 em->len = num_bytes;
1373 em->block_len = num_bytes;
1374 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001375 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001376 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001377 em->mod_start = em->start;
1378 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001379 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001380 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001381 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001382 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001383 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001384 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001385 if (!ret)
1386 list_move(&em->list,
1387 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001388 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001389 if (ret != -EEXIST) {
1390 free_extent_map(em);
1391 break;
1392 }
1393 btrfs_drop_extent_cache(inode, em->start,
1394 em->start + em->len - 1, 0);
1395 }
1396 type = BTRFS_ORDERED_PREALLOC;
1397 } else {
1398 type = BTRFS_ORDERED_NOCOW;
1399 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001400
1401 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001402 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001404
Yan, Zhengefa56462010-05-16 10:49:59 -04001405 if (root->root_key.objectid ==
1406 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1407 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1408 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001409 if (ret) {
1410 btrfs_abort_transaction(trans, root, ret);
1411 goto error;
1412 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001413 }
1414
Yan Zhengd899e052008-10-30 14:25:28 -04001415 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001416 cur_offset, cur_offset + num_bytes - 1,
1417 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1418 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1419 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001420 cur_offset = extent_end;
1421 if (cur_offset > end)
1422 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001423 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001424 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001425
Josef Bacik17ca04a2012-05-31 15:58:55 -04001426 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001427 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001428 cur_offset = end;
1429 }
1430
Yan Zheng80ff3852008-10-30 14:20:02 -04001431 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001432 ret = __cow_file_range(trans, inode, root, locked_page,
1433 cow_start, end,
1434 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001435 if (ret) {
1436 btrfs_abort_transaction(trans, root, ret);
1437 goto error;
1438 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001439 }
1440
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001441error:
Miao Xiea698d0752012-09-20 01:51:59 -06001442 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001443 if (!ret)
1444 ret = err;
1445
Josef Bacik17ca04a2012-05-31 15:58:55 -04001446 if (ret && cur_offset < end)
1447 extent_clear_unlock_delalloc(inode,
1448 &BTRFS_I(inode)->io_tree,
1449 cur_offset, end, locked_page,
1450 EXTENT_CLEAR_UNLOCK_PAGE |
1451 EXTENT_CLEAR_UNLOCK |
1452 EXTENT_CLEAR_DELALLOC |
1453 EXTENT_CLEAR_DIRTY |
1454 EXTENT_SET_WRITEBACK |
1455 EXTENT_END_WRITEBACK);
1456
Yan Zheng7ea394f2008-08-05 13:05:02 -04001457 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001458 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001459}
1460
Chris Masond352ac62008-09-29 15:18:18 -04001461/*
1462 * extent_io.c call back to do delayed allocation processing
1463 */
Chris Masonc8b97812008-10-29 14:49:59 -04001464static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001465 u64 start, u64 end, int *page_started,
1466 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001467{
Chris Masonbe20aa92007-12-17 20:14:01 -05001468 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001469 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001470
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001471 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001472 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001473 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001474 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001475 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001476 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001477 } else if (!btrfs_test_opt(root, COMPRESS) &&
1478 !(BTRFS_I(inode)->force_compress) &&
1479 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001480 ret = cow_file_range(inode, locked_page, start, end,
1481 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001482 } else {
1483 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1484 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001485 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001486 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001487 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001488 return ret;
1489}
1490
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001491static void btrfs_split_extent_hook(struct inode *inode,
1492 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001493{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001494 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001495 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001496 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001497
Josef Bacik9e0baf62011-07-15 15:16:44 +00001498 spin_lock(&BTRFS_I(inode)->lock);
1499 BTRFS_I(inode)->outstanding_extents++;
1500 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001501}
1502
1503/*
1504 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1505 * extents so we can keep track of new extents that are just merged onto old
1506 * extents, such as when we are doing sequential writes, so we can properly
1507 * account for the metadata space we'll need.
1508 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001509static void btrfs_merge_extent_hook(struct inode *inode,
1510 struct extent_state *new,
1511 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001512{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001513 /* not delalloc, ignore it */
1514 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001515 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001516
Josef Bacik9e0baf62011-07-15 15:16:44 +00001517 spin_lock(&BTRFS_I(inode)->lock);
1518 BTRFS_I(inode)->outstanding_extents--;
1519 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001520}
1521
Chris Masond352ac62008-09-29 15:18:18 -04001522/*
1523 * extent_io.c set_bit_hook, used to track delayed allocation
1524 * bytes in this file, and to maintain the list of inodes that
1525 * have pending delalloc work to be done.
1526 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001527static void btrfs_set_bit_hook(struct inode *inode,
1528 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001529{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001530
Chris Mason75eff682008-12-15 15:54:40 -05001531 /*
1532 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001533 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001534 * bit, which is only set or cleared with irqs on
1535 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001536 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001537 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001538 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001539 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001540
Josef Bacik9e0baf62011-07-15 15:16:44 +00001541 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001542 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001543 } else {
1544 spin_lock(&BTRFS_I(inode)->lock);
1545 BTRFS_I(inode)->outstanding_extents++;
1546 spin_unlock(&BTRFS_I(inode)->lock);
1547 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001548
Miao Xie963d6782013-01-29 10:10:51 +00001549 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1550 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001551 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001552 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001553 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1554 &BTRFS_I(inode)->runtime_flags)) {
1555 spin_lock(&root->fs_info->delalloc_lock);
1556 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1557 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1558 &root->fs_info->delalloc_inodes);
1559 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1560 &BTRFS_I(inode)->runtime_flags);
1561 }
1562 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001563 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001564 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001565 }
Chris Mason291d6732008-01-29 15:55:23 -05001566}
1567
Chris Masond352ac62008-09-29 15:18:18 -04001568/*
1569 * extent_io.c clear_bit_hook, see set_bit_hook for why
1570 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001571static void btrfs_clear_bit_hook(struct inode *inode,
1572 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001573{
Chris Mason75eff682008-12-15 15:54:40 -05001574 /*
1575 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001576 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001577 * bit, which is only set or cleared with irqs on
1578 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001579 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001580 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001581 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001582 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001583
Josef Bacik9e0baf62011-07-15 15:16:44 +00001584 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001585 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001586 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1587 spin_lock(&BTRFS_I(inode)->lock);
1588 BTRFS_I(inode)->outstanding_extents--;
1589 spin_unlock(&BTRFS_I(inode)->lock);
1590 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001591
1592 if (*bits & EXTENT_DO_ACCOUNTING)
1593 btrfs_delalloc_release_metadata(inode, len);
1594
Josef Bacik0cb59c92010-07-02 12:14:14 -04001595 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1596 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001597 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001598
Miao Xie963d6782013-01-29 10:10:51 +00001599 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1600 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001601 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001602 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001603 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001604 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1605 &BTRFS_I(inode)->runtime_flags)) {
1606 spin_lock(&root->fs_info->delalloc_lock);
1607 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1608 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1609 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1610 &BTRFS_I(inode)->runtime_flags);
1611 }
1612 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001613 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001614 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001615 }
Chris Mason291d6732008-01-29 15:55:23 -05001616}
1617
Chris Masond352ac62008-09-29 15:18:18 -04001618/*
1619 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1620 * we don't create bios that span stripes or chunks
1621 */
David Woodhouse64a16702009-07-15 23:29:37 +01001622int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001623 size_t size, struct bio *bio,
1624 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001625{
1626 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001627 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001628 u64 length = 0;
1629 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001630 int ret;
1631
Chris Mason771ed682008-11-06 22:02:51 -05001632 if (bio_flags & EXTENT_BIO_COMPRESSED)
1633 return 0;
1634
Chris Masonf2d8d742008-04-21 10:03:05 -04001635 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001636 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001637 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001638 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001639 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001640 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001641 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001642 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001643 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001644}
1645
Chris Masond352ac62008-09-29 15:18:18 -04001646/*
1647 * in order to insert checksums into the metadata in large chunks,
1648 * we wait until bio submission time. All the pages in the bio are
1649 * checksummed and sums are attached onto the ordered extent record.
1650 *
1651 * At IO completion time the cums attached on the ordered extent record
1652 * are inserted into the btree
1653 */
Chris Masond3977122009-01-05 21:25:51 -05001654static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1655 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001656 unsigned long bio_flags,
1657 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001658{
Chris Mason065631f2008-02-20 12:07:25 -05001659 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001660 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001661
Chris Masond20f7042008-12-08 16:58:54 -05001662 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001663 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001664 return 0;
1665}
Chris Masone0156402008-04-16 11:15:20 -04001666
Chris Mason4a69a412008-11-06 22:03:00 -05001667/*
1668 * in order to insert checksums into the metadata in large chunks,
1669 * we wait until bio submission time. All the pages in the bio are
1670 * checksummed and sums are attached onto the ordered extent record.
1671 *
1672 * At IO completion time the cums attached on the ordered extent record
1673 * are inserted into the btree
1674 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001675static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001676 int mirror_num, unsigned long bio_flags,
1677 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001678{
1679 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001680 int ret;
1681
1682 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1683 if (ret)
1684 bio_endio(bio, ret);
1685 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001686}
1687
Chris Masond352ac62008-09-29 15:18:18 -04001688/*
Chris Masoncad321a2008-12-17 14:51:42 -05001689 * extent_io.c submission hook. This does the right thing for csum calculation
1690 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001691 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001692static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001693 int mirror_num, unsigned long bio_flags,
1694 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001695{
1696 struct btrfs_root *root = BTRFS_I(inode)->root;
1697 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001698 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001699 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001700 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001701
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001702 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001703
Liu Bo83eea1f2012-07-10 05:28:39 -06001704 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001705 metadata = 2;
1706
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001707 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001708 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1709 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001710 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001711
Chris Masond20f7042008-12-08 16:58:54 -05001712 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001713 ret = btrfs_submit_compressed_read(inode, bio,
1714 mirror_num,
1715 bio_flags);
1716 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001717 } else if (!skip_sum) {
1718 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1719 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001720 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001721 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001722 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001723 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001724 /* csum items have already been cloned */
1725 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1726 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001727 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001728 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001729 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001730 bio_flags, bio_offset,
1731 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001732 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001733 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001734 } else if (!skip_sum) {
1735 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1736 if (ret)
1737 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001738 }
1739
Chris Mason0b86a832008-03-24 15:01:56 -04001740mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001741 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1742
1743out:
1744 if (ret < 0)
1745 bio_endio(bio, ret);
1746 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001747}
Chris Mason6885f302008-02-20 16:11:05 -05001748
Chris Masond352ac62008-09-29 15:18:18 -04001749/*
1750 * given a list of ordered sums record them in the inode. This happens
1751 * at IO completion time based on sums calculated at bio submission time.
1752 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001753static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001754 struct inode *inode, u64 file_offset,
1755 struct list_head *list)
1756{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001757 struct btrfs_ordered_sum *sum;
1758
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001759 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001760 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001761 btrfs_csum_file_blocks(trans,
1762 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001763 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001764 }
1765 return 0;
1766}
1767
Josef Bacik2ac55d42010-02-03 19:33:23 +00001768int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1769 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001770{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001771 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001772 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001773 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001774}
1775
Chris Masond352ac62008-09-29 15:18:18 -04001776/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001777struct btrfs_writepage_fixup {
1778 struct page *page;
1779 struct btrfs_work work;
1780};
1781
Christoph Hellwigb2950862008-12-02 09:54:17 -05001782static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001783{
1784 struct btrfs_writepage_fixup *fixup;
1785 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001786 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001787 struct page *page;
1788 struct inode *inode;
1789 u64 page_start;
1790 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001791 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001792
1793 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1794 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001795again:
Chris Mason247e7432008-07-17 12:53:51 -04001796 lock_page(page);
1797 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1798 ClearPageChecked(page);
1799 goto out_page;
1800 }
1801
1802 inode = page->mapping->host;
1803 page_start = page_offset(page);
1804 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1805
Josef Bacik2ac55d42010-02-03 19:33:23 +00001806 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001807 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001808
1809 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001810 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001811 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001812
1813 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1814 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001815 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1816 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001817 unlock_page(page);
1818 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001819 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001820 goto again;
1821 }
Chris Mason247e7432008-07-17 12:53:51 -04001822
Jeff Mahoney87826df2012-02-15 16:23:57 +01001823 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1824 if (ret) {
1825 mapping_set_error(page->mapping, ret);
1826 end_extent_writepage(page, ret, page_start, page_end);
1827 ClearPageChecked(page);
1828 goto out;
1829 }
1830
Josef Bacik2ac55d42010-02-03 19:33:23 +00001831 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001832 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001833 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001834out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001835 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1836 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001837out_page:
1838 unlock_page(page);
1839 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001840 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001841}
1842
1843/*
1844 * There are a few paths in the higher layers of the kernel that directly
1845 * set the page dirty bit without asking the filesystem if it is a
1846 * good idea. This causes problems because we want to make sure COW
1847 * properly happens and the data=ordered rules are followed.
1848 *
Chris Masonc8b97812008-10-29 14:49:59 -04001849 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001850 * hasn't been properly setup for IO. We kick off an async process
1851 * to fix it up. The async helper will wait for ordered extents, set
1852 * the delalloc bit and make it safe to write the page.
1853 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001854static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001855{
1856 struct inode *inode = page->mapping->host;
1857 struct btrfs_writepage_fixup *fixup;
1858 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001859
Chris Mason8b62b722009-09-02 16:53:46 -04001860 /* this page is properly in the ordered list */
1861 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001862 return 0;
1863
1864 if (PageChecked(page))
1865 return -EAGAIN;
1866
1867 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1868 if (!fixup)
1869 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001870
Chris Mason247e7432008-07-17 12:53:51 -04001871 SetPageChecked(page);
1872 page_cache_get(page);
1873 fixup->work.func = btrfs_writepage_fixup_worker;
1874 fixup->page = page;
1875 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001876 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001877}
1878
Yan Zhengd899e052008-10-30 14:25:28 -04001879static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1880 struct inode *inode, u64 file_pos,
1881 u64 disk_bytenr, u64 disk_num_bytes,
1882 u64 num_bytes, u64 ram_bytes,
1883 u8 compression, u8 encryption,
1884 u16 other_encoding, int extent_type)
1885{
1886 struct btrfs_root *root = BTRFS_I(inode)->root;
1887 struct btrfs_file_extent_item *fi;
1888 struct btrfs_path *path;
1889 struct extent_buffer *leaf;
1890 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001891 int ret;
1892
1893 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001894 if (!path)
1895 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001896
Chris Masonb9473432009-03-13 11:00:37 -04001897 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001898
1899 /*
1900 * we may be replacing one extent in the tree with another.
1901 * The new extent is pinned in the extent map, and we don't want
1902 * to drop it from the cache until it is completely in the btree.
1903 *
1904 * So, tell btrfs_drop_extents to leave this extent in the cache.
1905 * the caller is expected to unpin it and allow it to be merged
1906 * with the others.
1907 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001908 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001909 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001910 if (ret)
1911 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001912
Li Zefan33345d012011-04-20 10:31:50 +08001913 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001914 ins.offset = file_pos;
1915 ins.type = BTRFS_EXTENT_DATA_KEY;
1916 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001917 if (ret)
1918 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001919 leaf = path->nodes[0];
1920 fi = btrfs_item_ptr(leaf, path->slots[0],
1921 struct btrfs_file_extent_item);
1922 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1923 btrfs_set_file_extent_type(leaf, fi, extent_type);
1924 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1925 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1926 btrfs_set_file_extent_offset(leaf, fi, 0);
1927 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1928 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1929 btrfs_set_file_extent_compression(leaf, fi, compression);
1930 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1931 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001932
Yan Zhengd899e052008-10-30 14:25:28 -04001933 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001934 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001935
1936 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001937
1938 ins.objectid = disk_bytenr;
1939 ins.offset = disk_num_bytes;
1940 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001941 ret = btrfs_alloc_reserved_file_extent(trans, root,
1942 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001943 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001944out:
Yan Zhengd899e052008-10-30 14:25:28 -04001945 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001946
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001947 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001948}
1949
Liu Bo38c227d2013-01-29 03:18:40 +00001950/* snapshot-aware defrag */
1951struct sa_defrag_extent_backref {
1952 struct rb_node node;
1953 struct old_sa_defrag_extent *old;
1954 u64 root_id;
1955 u64 inum;
1956 u64 file_pos;
1957 u64 extent_offset;
1958 u64 num_bytes;
1959 u64 generation;
1960};
1961
1962struct old_sa_defrag_extent {
1963 struct list_head list;
1964 struct new_sa_defrag_extent *new;
1965
1966 u64 extent_offset;
1967 u64 bytenr;
1968 u64 offset;
1969 u64 len;
1970 int count;
1971};
1972
1973struct new_sa_defrag_extent {
1974 struct rb_root root;
1975 struct list_head head;
1976 struct btrfs_path *path;
1977 struct inode *inode;
1978 u64 file_pos;
1979 u64 len;
1980 u64 bytenr;
1981 u64 disk_len;
1982 u8 compress_type;
1983};
1984
1985static int backref_comp(struct sa_defrag_extent_backref *b1,
1986 struct sa_defrag_extent_backref *b2)
1987{
1988 if (b1->root_id < b2->root_id)
1989 return -1;
1990 else if (b1->root_id > b2->root_id)
1991 return 1;
1992
1993 if (b1->inum < b2->inum)
1994 return -1;
1995 else if (b1->inum > b2->inum)
1996 return 1;
1997
1998 if (b1->file_pos < b2->file_pos)
1999 return -1;
2000 else if (b1->file_pos > b2->file_pos)
2001 return 1;
2002
2003 /*
2004 * [------------------------------] ===> (a range of space)
2005 * |<--->| |<---->| =============> (fs/file tree A)
2006 * |<---------------------------->| ===> (fs/file tree B)
2007 *
2008 * A range of space can refer to two file extents in one tree while
2009 * refer to only one file extent in another tree.
2010 *
2011 * So we may process a disk offset more than one time(two extents in A)
2012 * and locate at the same extent(one extent in B), then insert two same
2013 * backrefs(both refer to the extent in B).
2014 */
2015 return 0;
2016}
2017
2018static void backref_insert(struct rb_root *root,
2019 struct sa_defrag_extent_backref *backref)
2020{
2021 struct rb_node **p = &root->rb_node;
2022 struct rb_node *parent = NULL;
2023 struct sa_defrag_extent_backref *entry;
2024 int ret;
2025
2026 while (*p) {
2027 parent = *p;
2028 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2029
2030 ret = backref_comp(backref, entry);
2031 if (ret < 0)
2032 p = &(*p)->rb_left;
2033 else
2034 p = &(*p)->rb_right;
2035 }
2036
2037 rb_link_node(&backref->node, parent, p);
2038 rb_insert_color(&backref->node, root);
2039}
2040
2041/*
2042 * Note the backref might has changed, and in this case we just return 0.
2043 */
2044static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2045 void *ctx)
2046{
2047 struct btrfs_file_extent_item *extent;
2048 struct btrfs_fs_info *fs_info;
2049 struct old_sa_defrag_extent *old = ctx;
2050 struct new_sa_defrag_extent *new = old->new;
2051 struct btrfs_path *path = new->path;
2052 struct btrfs_key key;
2053 struct btrfs_root *root;
2054 struct sa_defrag_extent_backref *backref;
2055 struct extent_buffer *leaf;
2056 struct inode *inode = new->inode;
2057 int slot;
2058 int ret;
2059 u64 extent_offset;
2060 u64 num_bytes;
2061
2062 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2063 inum == btrfs_ino(inode))
2064 return 0;
2065
2066 key.objectid = root_id;
2067 key.type = BTRFS_ROOT_ITEM_KEY;
2068 key.offset = (u64)-1;
2069
2070 fs_info = BTRFS_I(inode)->root->fs_info;
2071 root = btrfs_read_fs_root_no_name(fs_info, &key);
2072 if (IS_ERR(root)) {
2073 if (PTR_ERR(root) == -ENOENT)
2074 return 0;
2075 WARN_ON(1);
2076 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2077 inum, offset, root_id);
2078 return PTR_ERR(root);
2079 }
2080
2081 key.objectid = inum;
2082 key.type = BTRFS_EXTENT_DATA_KEY;
2083 if (offset > (u64)-1 << 32)
2084 key.offset = 0;
2085 else
2086 key.offset = offset;
2087
2088 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2089 if (ret < 0) {
2090 WARN_ON(1);
2091 return ret;
2092 }
2093
2094 while (1) {
2095 cond_resched();
2096
2097 leaf = path->nodes[0];
2098 slot = path->slots[0];
2099
2100 if (slot >= btrfs_header_nritems(leaf)) {
2101 ret = btrfs_next_leaf(root, path);
2102 if (ret < 0) {
2103 goto out;
2104 } else if (ret > 0) {
2105 ret = 0;
2106 goto out;
2107 }
2108 continue;
2109 }
2110
2111 path->slots[0]++;
2112
2113 btrfs_item_key_to_cpu(leaf, &key, slot);
2114
2115 if (key.objectid > inum)
2116 goto out;
2117
2118 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2119 continue;
2120
2121 extent = btrfs_item_ptr(leaf, slot,
2122 struct btrfs_file_extent_item);
2123
2124 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2125 continue;
2126
2127 extent_offset = btrfs_file_extent_offset(leaf, extent);
2128 if (key.offset - extent_offset != offset)
2129 continue;
2130
2131 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2132 if (extent_offset >= old->extent_offset + old->offset +
2133 old->len || extent_offset + num_bytes <=
2134 old->extent_offset + old->offset)
2135 continue;
2136
2137 break;
2138 }
2139
2140 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2141 if (!backref) {
2142 ret = -ENOENT;
2143 goto out;
2144 }
2145
2146 backref->root_id = root_id;
2147 backref->inum = inum;
2148 backref->file_pos = offset + extent_offset;
2149 backref->num_bytes = num_bytes;
2150 backref->extent_offset = extent_offset;
2151 backref->generation = btrfs_file_extent_generation(leaf, extent);
2152 backref->old = old;
2153 backref_insert(&new->root, backref);
2154 old->count++;
2155out:
2156 btrfs_release_path(path);
2157 WARN_ON(ret);
2158 return ret;
2159}
2160
2161static noinline bool record_extent_backrefs(struct btrfs_path *path,
2162 struct new_sa_defrag_extent *new)
2163{
2164 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2165 struct old_sa_defrag_extent *old, *tmp;
2166 int ret;
2167
2168 new->path = path;
2169
2170 list_for_each_entry_safe(old, tmp, &new->head, list) {
2171 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2172 path, record_one_backref,
2173 old);
2174 BUG_ON(ret < 0 && ret != -ENOENT);
2175
2176 /* no backref to be processed for this extent */
2177 if (!old->count) {
2178 list_del(&old->list);
2179 kfree(old);
2180 }
2181 }
2182
2183 if (list_empty(&new->head))
2184 return false;
2185
2186 return true;
2187}
2188
2189static int relink_is_mergable(struct extent_buffer *leaf,
2190 struct btrfs_file_extent_item *fi,
2191 u64 disk_bytenr)
2192{
2193 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2194 return 0;
2195
2196 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2197 return 0;
2198
2199 if (btrfs_file_extent_compression(leaf, fi) ||
2200 btrfs_file_extent_encryption(leaf, fi) ||
2201 btrfs_file_extent_other_encoding(leaf, fi))
2202 return 0;
2203
2204 return 1;
2205}
2206
2207/*
2208 * Note the backref might has changed, and in this case we just return 0.
2209 */
2210static noinline int relink_extent_backref(struct btrfs_path *path,
2211 struct sa_defrag_extent_backref *prev,
2212 struct sa_defrag_extent_backref *backref)
2213{
2214 struct btrfs_file_extent_item *extent;
2215 struct btrfs_file_extent_item *item;
2216 struct btrfs_ordered_extent *ordered;
2217 struct btrfs_trans_handle *trans;
2218 struct btrfs_fs_info *fs_info;
2219 struct btrfs_root *root;
2220 struct btrfs_key key;
2221 struct extent_buffer *leaf;
2222 struct old_sa_defrag_extent *old = backref->old;
2223 struct new_sa_defrag_extent *new = old->new;
2224 struct inode *src_inode = new->inode;
2225 struct inode *inode;
2226 struct extent_state *cached = NULL;
2227 int ret = 0;
2228 u64 start;
2229 u64 len;
2230 u64 lock_start;
2231 u64 lock_end;
2232 bool merge = false;
2233 int index;
2234
2235 if (prev && prev->root_id == backref->root_id &&
2236 prev->inum == backref->inum &&
2237 prev->file_pos + prev->num_bytes == backref->file_pos)
2238 merge = true;
2239
2240 /* step 1: get root */
2241 key.objectid = backref->root_id;
2242 key.type = BTRFS_ROOT_ITEM_KEY;
2243 key.offset = (u64)-1;
2244
2245 fs_info = BTRFS_I(src_inode)->root->fs_info;
2246 index = srcu_read_lock(&fs_info->subvol_srcu);
2247
2248 root = btrfs_read_fs_root_no_name(fs_info, &key);
2249 if (IS_ERR(root)) {
2250 srcu_read_unlock(&fs_info->subvol_srcu, index);
2251 if (PTR_ERR(root) == -ENOENT)
2252 return 0;
2253 return PTR_ERR(root);
2254 }
2255 if (btrfs_root_refs(&root->root_item) == 0) {
2256 srcu_read_unlock(&fs_info->subvol_srcu, index);
2257 /* parse ENOENT to 0 */
2258 return 0;
2259 }
2260
2261 /* step 2: get inode */
2262 key.objectid = backref->inum;
2263 key.type = BTRFS_INODE_ITEM_KEY;
2264 key.offset = 0;
2265
2266 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2267 if (IS_ERR(inode)) {
2268 srcu_read_unlock(&fs_info->subvol_srcu, index);
2269 return 0;
2270 }
2271
2272 srcu_read_unlock(&fs_info->subvol_srcu, index);
2273
2274 /* step 3: relink backref */
2275 lock_start = backref->file_pos;
2276 lock_end = backref->file_pos + backref->num_bytes - 1;
2277 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2278 0, &cached);
2279
2280 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2281 if (ordered) {
2282 btrfs_put_ordered_extent(ordered);
2283 goto out_unlock;
2284 }
2285
2286 trans = btrfs_join_transaction(root);
2287 if (IS_ERR(trans)) {
2288 ret = PTR_ERR(trans);
2289 goto out_unlock;
2290 }
2291
2292 key.objectid = backref->inum;
2293 key.type = BTRFS_EXTENT_DATA_KEY;
2294 key.offset = backref->file_pos;
2295
2296 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2297 if (ret < 0) {
2298 goto out_free_path;
2299 } else if (ret > 0) {
2300 ret = 0;
2301 goto out_free_path;
2302 }
2303
2304 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2305 struct btrfs_file_extent_item);
2306
2307 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2308 backref->generation)
2309 goto out_free_path;
2310
2311 btrfs_release_path(path);
2312
2313 start = backref->file_pos;
2314 if (backref->extent_offset < old->extent_offset + old->offset)
2315 start += old->extent_offset + old->offset -
2316 backref->extent_offset;
2317
2318 len = min(backref->extent_offset + backref->num_bytes,
2319 old->extent_offset + old->offset + old->len);
2320 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2321
2322 ret = btrfs_drop_extents(trans, root, inode, start,
2323 start + len, 1);
2324 if (ret)
2325 goto out_free_path;
2326again:
2327 key.objectid = btrfs_ino(inode);
2328 key.type = BTRFS_EXTENT_DATA_KEY;
2329 key.offset = start;
2330
Liu Boa09a0a72013-03-11 09:20:58 +00002331 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002332 if (merge) {
2333 struct btrfs_file_extent_item *fi;
2334 u64 extent_len;
2335 struct btrfs_key found_key;
2336
2337 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2338 if (ret < 0)
2339 goto out_free_path;
2340
2341 path->slots[0]--;
2342 leaf = path->nodes[0];
2343 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2344
2345 fi = btrfs_item_ptr(leaf, path->slots[0],
2346 struct btrfs_file_extent_item);
2347 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2348
2349 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2350 extent_len + found_key.offset == start) {
2351 btrfs_set_file_extent_num_bytes(leaf, fi,
2352 extent_len + len);
2353 btrfs_mark_buffer_dirty(leaf);
2354 inode_add_bytes(inode, len);
2355
2356 ret = 1;
2357 goto out_free_path;
2358 } else {
2359 merge = false;
2360 btrfs_release_path(path);
2361 goto again;
2362 }
2363 }
2364
2365 ret = btrfs_insert_empty_item(trans, root, path, &key,
2366 sizeof(*extent));
2367 if (ret) {
2368 btrfs_abort_transaction(trans, root, ret);
2369 goto out_free_path;
2370 }
2371
2372 leaf = path->nodes[0];
2373 item = btrfs_item_ptr(leaf, path->slots[0],
2374 struct btrfs_file_extent_item);
2375 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2376 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2377 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2378 btrfs_set_file_extent_num_bytes(leaf, item, len);
2379 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2380 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2381 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2382 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2383 btrfs_set_file_extent_encryption(leaf, item, 0);
2384 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2385
2386 btrfs_mark_buffer_dirty(leaf);
2387 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002388 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002389
2390 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2391 new->disk_len, 0,
2392 backref->root_id, backref->inum,
2393 new->file_pos, 0); /* start - extent_offset */
2394 if (ret) {
2395 btrfs_abort_transaction(trans, root, ret);
2396 goto out_free_path;
2397 }
2398
2399 ret = 1;
2400out_free_path:
2401 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002402 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002403 btrfs_end_transaction(trans, root);
2404out_unlock:
2405 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2406 &cached, GFP_NOFS);
2407 iput(inode);
2408 return ret;
2409}
2410
2411static void relink_file_extents(struct new_sa_defrag_extent *new)
2412{
2413 struct btrfs_path *path;
2414 struct old_sa_defrag_extent *old, *tmp;
2415 struct sa_defrag_extent_backref *backref;
2416 struct sa_defrag_extent_backref *prev = NULL;
2417 struct inode *inode;
2418 struct btrfs_root *root;
2419 struct rb_node *node;
2420 int ret;
2421
2422 inode = new->inode;
2423 root = BTRFS_I(inode)->root;
2424
2425 path = btrfs_alloc_path();
2426 if (!path)
2427 return;
2428
2429 if (!record_extent_backrefs(path, new)) {
2430 btrfs_free_path(path);
2431 goto out;
2432 }
2433 btrfs_release_path(path);
2434
2435 while (1) {
2436 node = rb_first(&new->root);
2437 if (!node)
2438 break;
2439 rb_erase(node, &new->root);
2440
2441 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2442
2443 ret = relink_extent_backref(path, prev, backref);
2444 WARN_ON(ret < 0);
2445
2446 kfree(prev);
2447
2448 if (ret == 1)
2449 prev = backref;
2450 else
2451 prev = NULL;
2452 cond_resched();
2453 }
2454 kfree(prev);
2455
2456 btrfs_free_path(path);
2457
2458 list_for_each_entry_safe(old, tmp, &new->head, list) {
2459 list_del(&old->list);
2460 kfree(old);
2461 }
2462out:
2463 atomic_dec(&root->fs_info->defrag_running);
2464 wake_up(&root->fs_info->transaction_wait);
2465
2466 kfree(new);
2467}
2468
2469static struct new_sa_defrag_extent *
2470record_old_file_extents(struct inode *inode,
2471 struct btrfs_ordered_extent *ordered)
2472{
2473 struct btrfs_root *root = BTRFS_I(inode)->root;
2474 struct btrfs_path *path;
2475 struct btrfs_key key;
2476 struct old_sa_defrag_extent *old, *tmp;
2477 struct new_sa_defrag_extent *new;
2478 int ret;
2479
2480 new = kmalloc(sizeof(*new), GFP_NOFS);
2481 if (!new)
2482 return NULL;
2483
2484 new->inode = inode;
2485 new->file_pos = ordered->file_offset;
2486 new->len = ordered->len;
2487 new->bytenr = ordered->start;
2488 new->disk_len = ordered->disk_len;
2489 new->compress_type = ordered->compress_type;
2490 new->root = RB_ROOT;
2491 INIT_LIST_HEAD(&new->head);
2492
2493 path = btrfs_alloc_path();
2494 if (!path)
2495 goto out_kfree;
2496
2497 key.objectid = btrfs_ino(inode);
2498 key.type = BTRFS_EXTENT_DATA_KEY;
2499 key.offset = new->file_pos;
2500
2501 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2502 if (ret < 0)
2503 goto out_free_path;
2504 if (ret > 0 && path->slots[0] > 0)
2505 path->slots[0]--;
2506
2507 /* find out all the old extents for the file range */
2508 while (1) {
2509 struct btrfs_file_extent_item *extent;
2510 struct extent_buffer *l;
2511 int slot;
2512 u64 num_bytes;
2513 u64 offset;
2514 u64 end;
2515 u64 disk_bytenr;
2516 u64 extent_offset;
2517
2518 l = path->nodes[0];
2519 slot = path->slots[0];
2520
2521 if (slot >= btrfs_header_nritems(l)) {
2522 ret = btrfs_next_leaf(root, path);
2523 if (ret < 0)
2524 goto out_free_list;
2525 else if (ret > 0)
2526 break;
2527 continue;
2528 }
2529
2530 btrfs_item_key_to_cpu(l, &key, slot);
2531
2532 if (key.objectid != btrfs_ino(inode))
2533 break;
2534 if (key.type != BTRFS_EXTENT_DATA_KEY)
2535 break;
2536 if (key.offset >= new->file_pos + new->len)
2537 break;
2538
2539 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2540
2541 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2542 if (key.offset + num_bytes < new->file_pos)
2543 goto next;
2544
2545 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2546 if (!disk_bytenr)
2547 goto next;
2548
2549 extent_offset = btrfs_file_extent_offset(l, extent);
2550
2551 old = kmalloc(sizeof(*old), GFP_NOFS);
2552 if (!old)
2553 goto out_free_list;
2554
2555 offset = max(new->file_pos, key.offset);
2556 end = min(new->file_pos + new->len, key.offset + num_bytes);
2557
2558 old->bytenr = disk_bytenr;
2559 old->extent_offset = extent_offset;
2560 old->offset = offset - key.offset;
2561 old->len = end - offset;
2562 old->new = new;
2563 old->count = 0;
2564 list_add_tail(&old->list, &new->head);
2565next:
2566 path->slots[0]++;
2567 cond_resched();
2568 }
2569
2570 btrfs_free_path(path);
2571 atomic_inc(&root->fs_info->defrag_running);
2572
2573 return new;
2574
2575out_free_list:
2576 list_for_each_entry_safe(old, tmp, &new->head, list) {
2577 list_del(&old->list);
2578 kfree(old);
2579 }
2580out_free_path:
2581 btrfs_free_path(path);
2582out_kfree:
2583 kfree(new);
2584 return NULL;
2585}
2586
Chris Mason5d13a982009-03-13 11:41:46 -04002587/*
2588 * helper function for btrfs_finish_ordered_io, this
2589 * just reads in some of the csum leaves to prime them into ram
2590 * before we start the transaction. It limits the amount of btree
2591 * reads required while inside the transaction.
2592 */
Chris Masond352ac62008-09-29 15:18:18 -04002593/* as ordered data IO finishes, this gets called so we can finish
2594 * an ordered extent if the range of bytes in the file it covers are
2595 * fully written.
2596 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002597static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002598{
Josef Bacik5fd02042012-05-02 14:00:54 -04002599 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002600 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002601 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002602 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002603 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002604 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002605 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002606 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002607 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002608
Liu Bo83eea1f2012-07-10 05:28:39 -06002609 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002610
Josef Bacik5fd02042012-05-02 14:00:54 -04002611 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2612 ret = -EIO;
2613 goto out;
2614 }
2615
Yan, Zhengc2167752009-11-12 09:34:21 +00002616 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002617 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002618 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2619 if (nolock)
2620 trans = btrfs_join_transaction_nolock(root);
2621 else
2622 trans = btrfs_join_transaction(root);
2623 if (IS_ERR(trans)) {
2624 ret = PTR_ERR(trans);
2625 trans = NULL;
2626 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002627 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002628 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2629 ret = btrfs_update_inode_fallback(trans, root, inode);
2630 if (ret) /* -ENOMEM or corruption */
2631 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002632 goto out;
2633 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002634
Josef Bacik2ac55d42010-02-03 19:33:23 +00002635 lock_extent_bits(io_tree, ordered_extent->file_offset,
2636 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002637 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002638
Liu Bo38c227d2013-01-29 03:18:40 +00002639 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2640 ordered_extent->file_offset + ordered_extent->len - 1,
2641 EXTENT_DEFRAG, 1, cached_state);
2642 if (ret) {
2643 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2644 if (last_snapshot >= BTRFS_I(inode)->generation)
2645 /* the inode is shared */
2646 new = record_old_file_extents(inode, ordered_extent);
2647
2648 clear_extent_bit(io_tree, ordered_extent->file_offset,
2649 ordered_extent->file_offset + ordered_extent->len - 1,
2650 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2651 }
2652
Josef Bacik0cb59c92010-07-02 12:14:14 -04002653 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002654 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002655 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002656 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002657 if (IS_ERR(trans)) {
2658 ret = PTR_ERR(trans);
2659 trans = NULL;
2660 goto out_unlock;
2661 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002662 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002663
Chris Masonc8b97812008-10-29 14:49:59 -04002664 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002665 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002666 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002667 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002668 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002669 ordered_extent->file_offset,
2670 ordered_extent->file_offset +
2671 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002672 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002673 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002674 ret = insert_reserved_file_extent(trans, inode,
2675 ordered_extent->file_offset,
2676 ordered_extent->start,
2677 ordered_extent->disk_len,
2678 ordered_extent->len,
2679 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002680 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002681 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002682 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002683 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2684 ordered_extent->file_offset, ordered_extent->len,
2685 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002686 if (ret < 0) {
2687 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002688 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002689 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002690
Chris Masone6dcd2d2008-07-17 12:53:50 -04002691 add_pending_csums(trans, inode, ordered_extent->file_offset,
2692 &ordered_extent->list);
2693
Josef Bacik6c760c02012-11-09 10:53:21 -05002694 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2695 ret = btrfs_update_inode_fallback(trans, root, inode);
2696 if (ret) { /* -ENOMEM or corruption */
2697 btrfs_abort_transaction(trans, root, ret);
2698 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002699 }
2700 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002701out_unlock:
2702 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2703 ordered_extent->file_offset +
2704 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002705out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002706 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002707 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002708 if (trans)
2709 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002710
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002711 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002712 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2713 ordered_extent->file_offset +
2714 ordered_extent->len - 1, NULL, GFP_NOFS);
2715
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002716 /*
2717 * If the ordered extent had an IOERR or something else went
2718 * wrong we need to return the space for this ordered extent
2719 * back to the allocator.
2720 */
2721 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2722 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2723 btrfs_free_reserved_extent(root, ordered_extent->start,
2724 ordered_extent->disk_len);
2725 }
2726
2727
Josef Bacik5fd02042012-05-02 14:00:54 -04002728 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002729 * This needs to be done to make sure anybody waiting knows we are done
2730 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002731 */
2732 btrfs_remove_ordered_extent(inode, ordered_extent);
2733
Liu Bo38c227d2013-01-29 03:18:40 +00002734 /* for snapshot-aware defrag */
2735 if (new)
2736 relink_file_extents(new);
2737
Chris Masone6dcd2d2008-07-17 12:53:50 -04002738 /* once for us */
2739 btrfs_put_ordered_extent(ordered_extent);
2740 /* once for the tree */
2741 btrfs_put_ordered_extent(ordered_extent);
2742
Josef Bacik5fd02042012-05-02 14:00:54 -04002743 return ret;
2744}
2745
2746static void finish_ordered_fn(struct btrfs_work *work)
2747{
2748 struct btrfs_ordered_extent *ordered_extent;
2749 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2750 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002751}
2752
Christoph Hellwigb2950862008-12-02 09:54:17 -05002753static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002754 struct extent_state *state, int uptodate)
2755{
Josef Bacik5fd02042012-05-02 14:00:54 -04002756 struct inode *inode = page->mapping->host;
2757 struct btrfs_root *root = BTRFS_I(inode)->root;
2758 struct btrfs_ordered_extent *ordered_extent = NULL;
2759 struct btrfs_workers *workers;
2760
liubo1abe9b82011-03-24 11:18:59 +00002761 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2762
Chris Mason8b62b722009-09-02 16:53:46 -04002763 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002764 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2765 end - start + 1, uptodate))
2766 return 0;
2767
2768 ordered_extent->work.func = finish_ordered_fn;
2769 ordered_extent->work.flags = 0;
2770
Liu Bo83eea1f2012-07-10 05:28:39 -06002771 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002772 workers = &root->fs_info->endio_freespace_worker;
2773 else
2774 workers = &root->fs_info->endio_write_workers;
2775 btrfs_queue_worker(workers, &ordered_extent->work);
2776
2777 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002778}
2779
Chris Masond352ac62008-09-29 15:18:18 -04002780/*
Chris Masond352ac62008-09-29 15:18:18 -04002781 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002782 * if there's a match, we allow the bio to finish. If not, the code in
2783 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002784 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002785static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002786 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002787{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002788 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002789 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002790 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002791 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002792 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002793 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002794 struct btrfs_root *root = BTRFS_I(inode)->root;
2795 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002796 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2797 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002798
Chris Masond20f7042008-12-08 16:58:54 -05002799 if (PageChecked(page)) {
2800 ClearPageChecked(page);
2801 goto good;
2802 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002803
2804 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002805 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002806
Yan Zheng17d217f2008-12-12 10:03:38 -05002807 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002808 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002809 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2810 GFP_NOFS);
2811 return 0;
2812 }
2813
Yanc2e639f2008-02-04 08:57:25 -05002814 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002815 private = state->private;
2816 ret = 0;
2817 } else {
2818 ret = get_state_private(io_tree, start, &private);
2819 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002820 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002821 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002822 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002823
Liu Bob0496682013-03-14 14:57:45 +00002824 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002825 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002826 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002827 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002828
Cong Wang7ac687d2011-11-25 23:14:28 +08002829 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002830good:
Chris Mason07157aa2007-08-30 08:50:51 -04002831 return 0;
2832
2833zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002834 if (__ratelimit(&_rs))
2835 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2836 (unsigned long long)btrfs_ino(page->mapping->host),
2837 (unsigned long long)start, csum,
2838 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002839 memset(kaddr + offset, 1, end - start + 1);
2840 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002841 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002842 if (private == 0)
2843 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002844 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002845}
Chris Masonb888db2b2007-08-27 16:49:44 -04002846
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002847struct delayed_iput {
2848 struct list_head list;
2849 struct inode *inode;
2850};
2851
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002852/* JDM: If this is fs-wide, why can't we add a pointer to
2853 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002854void btrfs_add_delayed_iput(struct inode *inode)
2855{
2856 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2857 struct delayed_iput *delayed;
2858
2859 if (atomic_add_unless(&inode->i_count, -1, 1))
2860 return;
2861
2862 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2863 delayed->inode = inode;
2864
2865 spin_lock(&fs_info->delayed_iput_lock);
2866 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2867 spin_unlock(&fs_info->delayed_iput_lock);
2868}
2869
2870void btrfs_run_delayed_iputs(struct btrfs_root *root)
2871{
2872 LIST_HEAD(list);
2873 struct btrfs_fs_info *fs_info = root->fs_info;
2874 struct delayed_iput *delayed;
2875 int empty;
2876
2877 spin_lock(&fs_info->delayed_iput_lock);
2878 empty = list_empty(&fs_info->delayed_iputs);
2879 spin_unlock(&fs_info->delayed_iput_lock);
2880 if (empty)
2881 return;
2882
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002883 spin_lock(&fs_info->delayed_iput_lock);
2884 list_splice_init(&fs_info->delayed_iputs, &list);
2885 spin_unlock(&fs_info->delayed_iput_lock);
2886
2887 while (!list_empty(&list)) {
2888 delayed = list_entry(list.next, struct delayed_iput, list);
2889 list_del(&delayed->list);
2890 iput(delayed->inode);
2891 kfree(delayed);
2892 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002893}
2894
Yan, Zhengd68fc572010-05-16 10:49:58 -04002895/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002896 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002897 * files in the subvolume, it removes orphan item and frees block_rsv
2898 * structure.
2899 */
2900void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2901 struct btrfs_root *root)
2902{
Josef Bacik90290e12011-12-02 15:44:12 -05002903 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002904 int ret;
2905
Josef Bacik8a35d952012-05-23 14:26:42 -04002906 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002907 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2908 return;
2909
Josef Bacik90290e12011-12-02 15:44:12 -05002910 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002911 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002912 spin_unlock(&root->orphan_lock);
2913 return;
2914 }
2915
2916 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2917 spin_unlock(&root->orphan_lock);
2918 return;
2919 }
2920
2921 block_rsv = root->orphan_block_rsv;
2922 root->orphan_block_rsv = NULL;
2923 spin_unlock(&root->orphan_lock);
2924
Yan, Zhengd68fc572010-05-16 10:49:58 -04002925 if (root->orphan_item_inserted &&
2926 btrfs_root_refs(&root->root_item) > 0) {
2927 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2928 root->root_key.objectid);
2929 BUG_ON(ret);
2930 root->orphan_item_inserted = 0;
2931 }
2932
Josef Bacik90290e12011-12-02 15:44:12 -05002933 if (block_rsv) {
2934 WARN_ON(block_rsv->size > 0);
2935 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002936 }
2937}
2938
2939/*
Josef Bacik7b128762008-07-24 12:17:14 -04002940 * This creates an orphan entry for the given inode in case something goes
2941 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002942 *
2943 * NOTE: caller of this function should reserve 5 units of metadata for
2944 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002945 */
2946int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2947{
2948 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002949 struct btrfs_block_rsv *block_rsv = NULL;
2950 int reserve = 0;
2951 int insert = 0;
2952 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002953
Yan, Zhengd68fc572010-05-16 10:49:58 -04002954 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002955 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002956 if (!block_rsv)
2957 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002958 }
2959
Yan, Zhengd68fc572010-05-16 10:49:58 -04002960 spin_lock(&root->orphan_lock);
2961 if (!root->orphan_block_rsv) {
2962 root->orphan_block_rsv = block_rsv;
2963 } else if (block_rsv) {
2964 btrfs_free_block_rsv(root, block_rsv);
2965 block_rsv = NULL;
2966 }
Josef Bacik7b128762008-07-24 12:17:14 -04002967
Josef Bacik8a35d952012-05-23 14:26:42 -04002968 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2969 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002970#if 0
2971 /*
2972 * For proper ENOSPC handling, we should do orphan
2973 * cleanup when mounting. But this introduces backward
2974 * compatibility issue.
2975 */
2976 if (!xchg(&root->orphan_item_inserted, 1))
2977 insert = 2;
2978 else
2979 insert = 1;
2980#endif
2981 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002982 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002983 }
Josef Bacik7b128762008-07-24 12:17:14 -04002984
Josef Bacik72ac3c02012-05-23 14:13:11 -04002985 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2986 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002987 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002988 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002989
Yan, Zhengd68fc572010-05-16 10:49:58 -04002990 /* grab metadata reservation from transaction handle */
2991 if (reserve) {
2992 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002993 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002994 }
2995
2996 /* insert an orphan item to track this unlinked/truncated file */
2997 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002998 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002999 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003000 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3001 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003002 btrfs_abort_transaction(trans, root, ret);
3003 return ret;
3004 }
3005 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003006 }
3007
3008 /* insert an orphan item to track subvolume contains orphan files */
3009 if (insert >= 2) {
3010 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3011 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003012 if (ret && ret != -EEXIST) {
3013 btrfs_abort_transaction(trans, root, ret);
3014 return ret;
3015 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003016 }
3017 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003018}
3019
3020/*
3021 * We have done the truncate/delete so we can go ahead and remove the orphan
3022 * item for this particular inode.
3023 */
3024int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
3025{
3026 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003027 int delete_item = 0;
3028 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003029 int ret = 0;
3030
Yan, Zhengd68fc572010-05-16 10:49:58 -04003031 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003032 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3033 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003034 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003035
Josef Bacik72ac3c02012-05-23 14:13:11 -04003036 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3037 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003038 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003039 spin_unlock(&root->orphan_lock);
3040
3041 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003042 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003043 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003044 }
3045
Josef Bacik8a35d952012-05-23 14:26:42 -04003046 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003047 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003048 atomic_dec(&root->orphan_inodes);
3049 }
Josef Bacik7b128762008-07-24 12:17:14 -04003050
Yan, Zhengd68fc572010-05-16 10:49:58 -04003051 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003052}
3053
3054/*
3055 * this cleans up any orphans that may be left on the list from the last use
3056 * of this root.
3057 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003058int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003059{
3060 struct btrfs_path *path;
3061 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003062 struct btrfs_key key, found_key;
3063 struct btrfs_trans_handle *trans;
3064 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003065 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003066 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3067
Yan, Zhengd68fc572010-05-16 10:49:58 -04003068 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003069 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003070
3071 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003072 if (!path) {
3073 ret = -ENOMEM;
3074 goto out;
3075 }
Josef Bacik7b128762008-07-24 12:17:14 -04003076 path->reada = -1;
3077
3078 key.objectid = BTRFS_ORPHAN_OBJECTID;
3079 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3080 key.offset = (u64)-1;
3081
Josef Bacik7b128762008-07-24 12:17:14 -04003082 while (1) {
3083 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003084 if (ret < 0)
3085 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003086
3087 /*
3088 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003089 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003090 * find the key and see if we have stuff that matches
3091 */
3092 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003093 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003094 if (path->slots[0] == 0)
3095 break;
3096 path->slots[0]--;
3097 }
3098
3099 /* pull out the item */
3100 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003101 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3102
3103 /* make sure the item matches what we want */
3104 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3105 break;
3106 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3107 break;
3108
3109 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003110 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003111
3112 /*
3113 * this is where we are basically btrfs_lookup, without the
3114 * crossing root thing. we store the inode number in the
3115 * offset of the orphan item.
3116 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003117
3118 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003119 btrfs_err(root->fs_info,
3120 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003121 ret = -EINVAL;
3122 goto out;
3123 }
3124
3125 last_objectid = found_key.offset;
3126
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003127 found_key.objectid = found_key.offset;
3128 found_key.type = BTRFS_INODE_ITEM_KEY;
3129 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003130 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003131 ret = PTR_RET(inode);
3132 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003133 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003134
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003135 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3136 struct btrfs_root *dead_root;
3137 struct btrfs_fs_info *fs_info = root->fs_info;
3138 int is_dead_root = 0;
3139
3140 /*
3141 * this is an orphan in the tree root. Currently these
3142 * could come from 2 sources:
3143 * a) a snapshot deletion in progress
3144 * b) a free space cache inode
3145 * We need to distinguish those two, as the snapshot
3146 * orphan must not get deleted.
3147 * find_dead_roots already ran before us, so if this
3148 * is a snapshot deletion, we should find the root
3149 * in the dead_roots list
3150 */
3151 spin_lock(&fs_info->trans_lock);
3152 list_for_each_entry(dead_root, &fs_info->dead_roots,
3153 root_list) {
3154 if (dead_root->root_key.objectid ==
3155 found_key.objectid) {
3156 is_dead_root = 1;
3157 break;
3158 }
3159 }
3160 spin_unlock(&fs_info->trans_lock);
3161 if (is_dead_root) {
3162 /* prevent this orphan from being found again */
3163 key.offset = found_key.objectid - 1;
3164 continue;
3165 }
3166 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003167 /*
3168 * Inode is already gone but the orphan item is still there,
3169 * kill the orphan item.
3170 */
3171 if (ret == -ESTALE) {
3172 trans = btrfs_start_transaction(root, 1);
3173 if (IS_ERR(trans)) {
3174 ret = PTR_ERR(trans);
3175 goto out;
3176 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003177 btrfs_debug(root->fs_info, "auto deleting %Lu",
3178 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003179 ret = btrfs_del_orphan_item(trans, root,
3180 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003181 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003182 btrfs_end_transaction(trans, root);
3183 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003184 }
Josef Bacik7b128762008-07-24 12:17:14 -04003185
Josef Bacik7b128762008-07-24 12:17:14 -04003186 /*
3187 * add this inode to the orphan list so btrfs_orphan_del does
3188 * the proper thing when we hit it
3189 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003190 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3191 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003192 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003193
Josef Bacik7b128762008-07-24 12:17:14 -04003194 /* if we have links, this was a truncate, lets do that */
3195 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003196 if (!S_ISREG(inode->i_mode)) {
3197 WARN_ON(1);
3198 iput(inode);
3199 continue;
3200 }
Josef Bacik7b128762008-07-24 12:17:14 -04003201 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003202
3203 /* 1 for the orphan item deletion. */
3204 trans = btrfs_start_transaction(root, 1);
3205 if (IS_ERR(trans)) {
3206 ret = PTR_ERR(trans);
3207 goto out;
3208 }
3209 ret = btrfs_orphan_add(trans, inode);
3210 btrfs_end_transaction(trans, root);
3211 if (ret)
3212 goto out;
3213
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003214 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003215 if (ret)
3216 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003217 } else {
3218 nr_unlink++;
3219 }
3220
3221 /* this will do delete_inode and everything for us */
3222 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003223 if (ret)
3224 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003225 }
Miao Xie3254c872011-11-10 20:45:05 -05003226 /* release the path since we're done with it */
3227 btrfs_release_path(path);
3228
Yan, Zhengd68fc572010-05-16 10:49:58 -04003229 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3230
3231 if (root->orphan_block_rsv)
3232 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3233 (u64)-1);
3234
3235 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003236 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003237 if (!IS_ERR(trans))
3238 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003239 }
Josef Bacik7b128762008-07-24 12:17:14 -04003240
3241 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003242 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003243 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003244 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003245
3246out:
3247 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003248 btrfs_crit(root->fs_info,
3249 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003250 btrfs_free_path(path);
3251 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003252}
3253
Chris Masond352ac62008-09-29 15:18:18 -04003254/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003255 * very simple check to peek ahead in the leaf looking for xattrs. If we
3256 * don't find any xattrs, we know there can't be any acls.
3257 *
3258 * slot is the slot the inode is in, objectid is the objectid of the inode
3259 */
3260static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3261 int slot, u64 objectid)
3262{
3263 u32 nritems = btrfs_header_nritems(leaf);
3264 struct btrfs_key found_key;
3265 int scanned = 0;
3266
3267 slot++;
3268 while (slot < nritems) {
3269 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3270
3271 /* we found a different objectid, there must not be acls */
3272 if (found_key.objectid != objectid)
3273 return 0;
3274
3275 /* we found an xattr, assume we've got an acl */
3276 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3277 return 1;
3278
3279 /*
3280 * we found a key greater than an xattr key, there can't
3281 * be any acls later on
3282 */
3283 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3284 return 0;
3285
3286 slot++;
3287 scanned++;
3288
3289 /*
3290 * it goes inode, inode backrefs, xattrs, extents,
3291 * so if there are a ton of hard links to an inode there can
3292 * be a lot of backrefs. Don't waste time searching too hard,
3293 * this is just an optimization
3294 */
3295 if (scanned >= 8)
3296 break;
3297 }
3298 /* we hit the end of the leaf before we found an xattr or
3299 * something larger than an xattr. We have to assume the inode
3300 * has acls
3301 */
3302 return 1;
3303}
3304
3305/*
Chris Masond352ac62008-09-29 15:18:18 -04003306 * read an inode from the btree into the in-memory inode
3307 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003308static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003309{
3310 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003311 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003312 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003313 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003314 struct btrfs_root *root = BTRFS_I(inode)->root;
3315 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003316 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003317 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003318 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003319 bool filled = false;
3320
3321 ret = btrfs_fill_inode(inode, &rdev);
3322 if (!ret)
3323 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003324
3325 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003326 if (!path)
3327 goto make_bad;
3328
Josef Bacikd90c7322011-05-17 09:50:54 -04003329 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003330 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003331
Chris Mason39279cc2007-06-12 06:35:45 -04003332 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003333 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003334 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003335
Chris Mason5f39d392007-10-15 16:14:19 -04003336 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003337
3338 if (filled)
3339 goto cache_acl;
3340
Chris Mason5f39d392007-10-15 16:14:19 -04003341 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3342 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003343 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003344 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003345 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3346 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003347 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003348
3349 tspec = btrfs_inode_atime(inode_item);
3350 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3351 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3352
3353 tspec = btrfs_inode_mtime(inode_item);
3354 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3355 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3356
3357 tspec = btrfs_inode_ctime(inode_item);
3358 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3359 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3360
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003361 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003362 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003363 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3364
3365 /*
3366 * If we were modified in the current generation and evicted from memory
3367 * and then re-read we need to do a full sync since we don't have any
3368 * idea about which extents were modified before we were evicted from
3369 * cache.
3370 */
3371 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3372 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3373 &BTRFS_I(inode)->runtime_flags);
3374
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003375 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003376 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003377 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003378 rdev = btrfs_inode_rdev(leaf, inode_item);
3379
Josef Bacikaec74772008-07-24 12:12:38 -04003380 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003381 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003382cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003383 /*
3384 * try to precache a NULL acl entry for files that don't have
3385 * any xattrs or acls
3386 */
Li Zefan33345d012011-04-20 10:31:50 +08003387 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3388 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003389 if (!maybe_acls)
3390 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003391
Chris Mason39279cc2007-06-12 06:35:45 -04003392 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003393
Chris Mason39279cc2007-06-12 06:35:45 -04003394 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003395 case S_IFREG:
3396 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003397 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003398 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003399 inode->i_fop = &btrfs_file_operations;
3400 inode->i_op = &btrfs_file_inode_operations;
3401 break;
3402 case S_IFDIR:
3403 inode->i_fop = &btrfs_dir_file_operations;
3404 if (root == root->fs_info->tree_root)
3405 inode->i_op = &btrfs_dir_ro_inode_operations;
3406 else
3407 inode->i_op = &btrfs_dir_inode_operations;
3408 break;
3409 case S_IFLNK:
3410 inode->i_op = &btrfs_symlink_inode_operations;
3411 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003412 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003413 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003414 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003415 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003416 init_special_inode(inode, inode->i_mode, rdev);
3417 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003418 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003419
3420 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003421 return;
3422
3423make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003424 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003425 make_bad_inode(inode);
3426}
3427
Chris Masond352ac62008-09-29 15:18:18 -04003428/*
3429 * given a leaf and an inode, copy the inode fields into the leaf
3430 */
Chris Masone02119d2008-09-05 16:13:11 -04003431static void fill_inode_item(struct btrfs_trans_handle *trans,
3432 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003433 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003434 struct inode *inode)
3435{
Liu Bo51fab692012-12-27 09:01:21 +00003436 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003437
Liu Bo51fab692012-12-27 09:01:21 +00003438 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003439
Liu Bo51fab692012-12-27 09:01:21 +00003440 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3441 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3442 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3443 &token);
3444 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3445 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003446
Liu Bo51fab692012-12-27 09:01:21 +00003447 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3448 inode->i_atime.tv_sec, &token);
3449 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3450 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003451
Liu Bo51fab692012-12-27 09:01:21 +00003452 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3453 inode->i_mtime.tv_sec, &token);
3454 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3455 inode->i_mtime.tv_nsec, &token);
3456
3457 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3458 inode->i_ctime.tv_sec, &token);
3459 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3460 inode->i_ctime.tv_nsec, &token);
3461
3462 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3463 &token);
3464 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3465 &token);
3466 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3467 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3468 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3469 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3470 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003471}
3472
Chris Masond352ac62008-09-29 15:18:18 -04003473/*
3474 * copy everything in the in-memory inode into the btree.
3475 */
Chris Mason21151332011-11-10 20:39:08 -05003476static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003477 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003478{
3479 struct btrfs_inode_item *inode_item;
3480 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003481 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003482 int ret;
3483
3484 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003485 if (!path)
3486 return -ENOMEM;
3487
Chris Masonb9473432009-03-13 11:00:37 -04003488 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003489 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3490 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003491 if (ret) {
3492 if (ret > 0)
3493 ret = -ENOENT;
3494 goto failed;
3495 }
3496
Chris Masonb4ce94d2009-02-04 09:25:08 -05003497 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003498 leaf = path->nodes[0];
3499 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003500 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003501
Chris Masone02119d2008-09-05 16:13:11 -04003502 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003503 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003504 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003505 ret = 0;
3506failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003507 btrfs_free_path(path);
3508 return ret;
3509}
3510
Chris Masond352ac62008-09-29 15:18:18 -04003511/*
Chris Mason21151332011-11-10 20:39:08 -05003512 * copy everything in the in-memory inode into the btree.
3513 */
3514noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3515 struct btrfs_root *root, struct inode *inode)
3516{
3517 int ret;
3518
3519 /*
3520 * If the inode is a free space inode, we can deadlock during commit
3521 * if we put it into the delayed code.
3522 *
3523 * The data relocation inode should also be directly updated
3524 * without delay
3525 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003526 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003527 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003528 btrfs_update_root_times(trans, root);
3529
Chris Mason21151332011-11-10 20:39:08 -05003530 ret = btrfs_delayed_update_inode(trans, root, inode);
3531 if (!ret)
3532 btrfs_set_inode_last_trans(trans, inode);
3533 return ret;
3534 }
3535
3536 return btrfs_update_inode_item(trans, root, inode);
3537}
3538
Josef Bacikbe6aef62012-10-22 15:43:12 -04003539noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3540 struct btrfs_root *root,
3541 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003542{
3543 int ret;
3544
3545 ret = btrfs_update_inode(trans, root, inode);
3546 if (ret == -ENOSPC)
3547 return btrfs_update_inode_item(trans, root, inode);
3548 return ret;
3549}
3550
3551/*
Chris Masond352ac62008-09-29 15:18:18 -04003552 * unlink helper that gets used here in inode.c and in the tree logging
3553 * recovery code. It remove a link in a directory with a given name, and
3554 * also drops the back refs in the inode to the directory
3555 */
Al Viro92986792011-03-04 17:14:37 +00003556static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3557 struct btrfs_root *root,
3558 struct inode *dir, struct inode *inode,
3559 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003560{
3561 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003562 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003563 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003564 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003565 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003566 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003567 u64 ino = btrfs_ino(inode);
3568 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003569
3570 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003571 if (!path) {
3572 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003573 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003574 }
3575
Chris Masonb9473432009-03-13 11:00:37 -04003576 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003577 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003578 name, name_len, -1);
3579 if (IS_ERR(di)) {
3580 ret = PTR_ERR(di);
3581 goto err;
3582 }
3583 if (!di) {
3584 ret = -ENOENT;
3585 goto err;
3586 }
Chris Mason5f39d392007-10-15 16:14:19 -04003587 leaf = path->nodes[0];
3588 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003589 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003590 if (ret)
3591 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003592 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003593
Li Zefan33345d012011-04-20 10:31:50 +08003594 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3595 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003596 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003597 btrfs_info(root->fs_info,
3598 "failed to delete reference to %.*s, inode %llu parent %llu",
3599 name_len, name,
3600 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003601 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003602 goto err;
3603 }
3604
Miao Xie16cdcec2011-04-22 18:12:22 +08003605 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003606 if (ret) {
3607 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003608 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003609 }
Chris Mason39279cc2007-06-12 06:35:45 -04003610
Chris Masone02119d2008-09-05 16:13:11 -04003611 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003612 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003613 if (ret != 0 && ret != -ENOENT) {
3614 btrfs_abort_transaction(trans, root, ret);
3615 goto err;
3616 }
Chris Masone02119d2008-09-05 16:13:11 -04003617
3618 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3619 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003620 if (ret == -ENOENT)
3621 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003622err:
3623 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003624 if (ret)
3625 goto out;
3626
3627 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003628 inode_inc_iversion(inode);
3629 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003630 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003631 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003632out:
Chris Mason39279cc2007-06-12 06:35:45 -04003633 return ret;
3634}
3635
Al Viro92986792011-03-04 17:14:37 +00003636int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3637 struct btrfs_root *root,
3638 struct inode *dir, struct inode *inode,
3639 const char *name, int name_len)
3640{
3641 int ret;
3642 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3643 if (!ret) {
3644 btrfs_drop_nlink(inode);
3645 ret = btrfs_update_inode(trans, root, inode);
3646 }
3647 return ret;
3648}
3649
3650
Yan, Zhenga22285a2010-05-16 10:48:46 -04003651/* helper to check if there is any shared block in the path */
3652static int check_path_shared(struct btrfs_root *root,
3653 struct btrfs_path *path)
3654{
3655 struct extent_buffer *eb;
3656 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00003657 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003658
3659 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003660 int ret;
3661
Yan, Zhenga22285a2010-05-16 10:48:46 -04003662 if (!path->nodes[level])
3663 break;
3664 eb = path->nodes[level];
3665 if (!btrfs_block_can_be_shared(root, eb))
3666 continue;
Josef Bacik3173a182013-03-07 14:22:04 -05003667 ret = btrfs_lookup_extent_info(NULL, root, eb->start, level, 1,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668 &refs, NULL);
3669 if (refs > 1)
3670 return 1;
3671 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003672 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003673}
3674
3675/*
3676 * helper to start transaction for unlink and rmdir.
3677 *
3678 * unlink and rmdir are special in btrfs, they do not always free space.
3679 * so in enospc case, we should make sure they will free space before
3680 * allowing them to use the global metadata reservation.
3681 */
3682static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3683 struct dentry *dentry)
3684{
3685 struct btrfs_trans_handle *trans;
3686 struct btrfs_root *root = BTRFS_I(dir)->root;
3687 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003688 struct btrfs_dir_item *di;
3689 struct inode *inode = dentry->d_inode;
3690 u64 index;
3691 int check_link = 1;
3692 int err = -ENOSPC;
3693 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003694 u64 ino = btrfs_ino(inode);
3695 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003696
Josef Bacike70bea52011-10-11 14:18:24 -04003697 /*
3698 * 1 for the possible orphan item
3699 * 1 for the dir item
3700 * 1 for the dir index
3701 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003702 * 1 for the inode
3703 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003704 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003705 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3706 return trans;
3707
Li Zefan33345d012011-04-20 10:31:50 +08003708 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003709 return ERR_PTR(-ENOSPC);
3710
3711 /* check if there is someone else holds reference */
3712 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3713 return ERR_PTR(-ENOSPC);
3714
3715 if (atomic_read(&inode->i_count) > 2)
3716 return ERR_PTR(-ENOSPC);
3717
3718 if (xchg(&root->fs_info->enospc_unlink, 1))
3719 return ERR_PTR(-ENOSPC);
3720
3721 path = btrfs_alloc_path();
3722 if (!path) {
3723 root->fs_info->enospc_unlink = 0;
3724 return ERR_PTR(-ENOMEM);
3725 }
3726
Josef Bacik3880a1b2011-10-14 14:46:51 -04003727 /* 1 for the orphan item */
3728 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003729 if (IS_ERR(trans)) {
3730 btrfs_free_path(path);
3731 root->fs_info->enospc_unlink = 0;
3732 return trans;
3733 }
3734
3735 path->skip_locking = 1;
3736 path->search_commit_root = 1;
3737
3738 ret = btrfs_lookup_inode(trans, root, path,
3739 &BTRFS_I(dir)->location, 0);
3740 if (ret < 0) {
3741 err = ret;
3742 goto out;
3743 }
3744 if (ret == 0) {
3745 if (check_path_shared(root, path))
3746 goto out;
3747 } else {
3748 check_link = 0;
3749 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003750 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003751
3752 ret = btrfs_lookup_inode(trans, root, path,
3753 &BTRFS_I(inode)->location, 0);
3754 if (ret < 0) {
3755 err = ret;
3756 goto out;
3757 }
3758 if (ret == 0) {
3759 if (check_path_shared(root, path))
3760 goto out;
3761 } else {
3762 check_link = 0;
3763 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003764 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003765
3766 if (ret == 0 && S_ISREG(inode->i_mode)) {
3767 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003768 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003769 if (ret < 0) {
3770 err = ret;
3771 goto out;
3772 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003773 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003774 if (check_path_shared(root, path))
3775 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003776 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003777 }
3778
3779 if (!check_link) {
3780 err = 0;
3781 goto out;
3782 }
3783
Li Zefan33345d012011-04-20 10:31:50 +08003784 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003785 dentry->d_name.name, dentry->d_name.len, 0);
3786 if (IS_ERR(di)) {
3787 err = PTR_ERR(di);
3788 goto out;
3789 }
3790 if (di) {
3791 if (check_path_shared(root, path))
3792 goto out;
3793 } else {
3794 err = 0;
3795 goto out;
3796 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003797 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003798
Mark Fashehf1863732012-08-08 11:32:27 -07003799 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3800 dentry->d_name.len, ino, dir_ino, 0,
3801 &index);
3802 if (ret) {
3803 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003804 goto out;
3805 }
Mark Fashehf1863732012-08-08 11:32:27 -07003806
Yan, Zhenga22285a2010-05-16 10:48:46 -04003807 if (check_path_shared(root, path))
3808 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003809
David Sterbab3b4aa72011-04-21 01:20:15 +02003810 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003811
Miao Xie16cdcec2011-04-22 18:12:22 +08003812 /*
3813 * This is a commit root search, if we can lookup inode item and other
3814 * relative items in the commit root, it means the transaction of
3815 * dir/file creation has been committed, and the dir index item that we
3816 * delay to insert has also been inserted into the commit root. So
3817 * we needn't worry about the delayed insertion of the dir index item
3818 * here.
3819 */
Li Zefan33345d012011-04-20 10:31:50 +08003820 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003821 dentry->d_name.name, dentry->d_name.len, 0);
3822 if (IS_ERR(di)) {
3823 err = PTR_ERR(di);
3824 goto out;
3825 }
3826 BUG_ON(ret == -ENOENT);
3827 if (check_path_shared(root, path))
3828 goto out;
3829
3830 err = 0;
3831out:
3832 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003833 /* Migrate the orphan reservation over */
3834 if (!err)
3835 err = btrfs_block_rsv_migrate(trans->block_rsv,
3836 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003837 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003838
Yan, Zhenga22285a2010-05-16 10:48:46 -04003839 if (err) {
3840 btrfs_end_transaction(trans, root);
3841 root->fs_info->enospc_unlink = 0;
3842 return ERR_PTR(err);
3843 }
3844
3845 trans->block_rsv = &root->fs_info->global_block_rsv;
3846 return trans;
3847}
3848
3849static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3850 struct btrfs_root *root)
3851{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003852 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003853 btrfs_block_rsv_release(root, trans->block_rsv,
3854 trans->bytes_reserved);
3855 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003856 BUG_ON(!root->fs_info->enospc_unlink);
3857 root->fs_info->enospc_unlink = 0;
3858 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003859 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003860}
3861
Chris Mason39279cc2007-06-12 06:35:45 -04003862static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3863{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003864 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003865 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003866 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003867 int ret;
3868
Yan, Zhenga22285a2010-05-16 10:48:46 -04003869 trans = __unlink_start_trans(dir, dentry);
3870 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003871 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003872
Chris Mason12fcfd22009-03-24 10:24:20 -04003873 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3874
Chris Masone02119d2008-09-05 16:13:11 -04003875 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3876 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003877 if (ret)
3878 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003879
Yan, Zhenga22285a2010-05-16 10:48:46 -04003880 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003881 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003882 if (ret)
3883 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003884 }
Josef Bacik7b128762008-07-24 12:17:14 -04003885
Tsutomu Itohb5324022011-07-19 07:27:20 +00003886out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003887 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003888 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003889 return ret;
3890}
3891
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003892int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3893 struct btrfs_root *root,
3894 struct inode *dir, u64 objectid,
3895 const char *name, int name_len)
3896{
3897 struct btrfs_path *path;
3898 struct extent_buffer *leaf;
3899 struct btrfs_dir_item *di;
3900 struct btrfs_key key;
3901 u64 index;
3902 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003903 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003904
3905 path = btrfs_alloc_path();
3906 if (!path)
3907 return -ENOMEM;
3908
Li Zefan33345d012011-04-20 10:31:50 +08003909 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003910 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003911 if (IS_ERR_OR_NULL(di)) {
3912 if (!di)
3913 ret = -ENOENT;
3914 else
3915 ret = PTR_ERR(di);
3916 goto out;
3917 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003918
3919 leaf = path->nodes[0];
3920 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3921 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3922 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003923 if (ret) {
3924 btrfs_abort_transaction(trans, root, ret);
3925 goto out;
3926 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003927 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003928
3929 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3930 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003931 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003932 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003933 if (ret != -ENOENT) {
3934 btrfs_abort_transaction(trans, root, ret);
3935 goto out;
3936 }
Li Zefan33345d012011-04-20 10:31:50 +08003937 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003938 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003939 if (IS_ERR_OR_NULL(di)) {
3940 if (!di)
3941 ret = -ENOENT;
3942 else
3943 ret = PTR_ERR(di);
3944 btrfs_abort_transaction(trans, root, ret);
3945 goto out;
3946 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003947
3948 leaf = path->nodes[0];
3949 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003950 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003951 index = key.offset;
3952 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003953 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003954
Miao Xie16cdcec2011-04-22 18:12:22 +08003955 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003956 if (ret) {
3957 btrfs_abort_transaction(trans, root, ret);
3958 goto out;
3959 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003960
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003961 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003962 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003963 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003964 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003965 if (ret)
3966 btrfs_abort_transaction(trans, root, ret);
3967out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003968 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003969 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003970}
3971
Chris Mason39279cc2007-06-12 06:35:45 -04003972static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3973{
3974 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003975 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003976 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003977 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003978
David Sterbab3ae2442012-09-13 16:04:34 -06003979 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003980 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003981 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3982 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003983
Yan, Zhenga22285a2010-05-16 10:48:46 -04003984 trans = __unlink_start_trans(dir, dentry);
3985 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003986 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003987
Li Zefan33345d012011-04-20 10:31:50 +08003988 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003989 err = btrfs_unlink_subvol(trans, root, dir,
3990 BTRFS_I(inode)->location.objectid,
3991 dentry->d_name.name,
3992 dentry->d_name.len);
3993 goto out;
3994 }
3995
Josef Bacik7b128762008-07-24 12:17:14 -04003996 err = btrfs_orphan_add(trans, inode);
3997 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003998 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003999
Chris Mason39279cc2007-06-12 06:35:45 -04004000 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004001 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4002 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004003 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004004 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004005out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04004006 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004007 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004008
Chris Mason39279cc2007-06-12 06:35:45 -04004009 return err;
4010}
4011
Chris Mason323ac952008-10-01 19:05:46 -04004012/*
Chris Mason39279cc2007-06-12 06:35:45 -04004013 * this can truncate away extent items, csum items and directory items.
4014 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004015 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004016 *
4017 * csum items that cross the new i_size are truncated to the new size
4018 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004019 *
4020 * min_type is the minimum key type to truncate down to. If set to 0, this
4021 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004022 */
Yan, Zheng80825102009-11-12 09:35:36 +00004023int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4024 struct btrfs_root *root,
4025 struct inode *inode,
4026 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004027{
Chris Mason39279cc2007-06-12 06:35:45 -04004028 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004029 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004030 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004031 struct btrfs_key key;
4032 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004033 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004034 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004035 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004036 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004037 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004038 int found_extent;
4039 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004040 int pending_del_nr = 0;
4041 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004042 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004043 int ret;
4044 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004045 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004046
4047 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004048
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004049 path = btrfs_alloc_path();
4050 if (!path)
4051 return -ENOMEM;
4052 path->reada = -1;
4053
Josef Bacik5dc562c2012-08-17 13:14:17 -04004054 /*
4055 * We want to drop from the next block forward in case this new size is
4056 * not block aligned since we will be keeping the last block of the
4057 * extent just the way it is.
4058 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004059 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004060 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4061 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004062
Miao Xie16cdcec2011-04-22 18:12:22 +08004063 /*
4064 * This function is also used to drop the items in the log tree before
4065 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4066 * it is used to drop the loged items. So we shouldn't kill the delayed
4067 * items.
4068 */
4069 if (min_type == 0 && root == BTRFS_I(inode)->root)
4070 btrfs_kill_delayed_inode_items(inode);
4071
Li Zefan33345d012011-04-20 10:31:50 +08004072 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004073 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004074 key.type = (u8)-1;
4075
Chris Mason85e21ba2008-01-29 15:11:36 -05004076search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004077 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004078 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004079 if (ret < 0) {
4080 err = ret;
4081 goto out;
4082 }
Chris Masond3977122009-01-05 21:25:51 -05004083
Chris Mason85e21ba2008-01-29 15:11:36 -05004084 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004085 /* there are no items in the tree for us to truncate, we're
4086 * done
4087 */
Yan, Zheng80825102009-11-12 09:35:36 +00004088 if (path->slots[0] == 0)
4089 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004090 path->slots[0]--;
4091 }
4092
Chris Masond3977122009-01-05 21:25:51 -05004093 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004094 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004095 leaf = path->nodes[0];
4096 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4097 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004098
Li Zefan33345d012011-04-20 10:31:50 +08004099 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004100 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004101
Chris Mason85e21ba2008-01-29 15:11:36 -05004102 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004103 break;
4104
Chris Mason5f39d392007-10-15 16:14:19 -04004105 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004106 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004107 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004108 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004109 extent_type = btrfs_file_extent_type(leaf, fi);
4110 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004111 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004112 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004113 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004114 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004115 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004116 }
Yan008630c2007-11-07 13:31:09 -05004117 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004118 }
Yan, Zheng80825102009-11-12 09:35:36 +00004119 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004120 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004121 } else {
4122 if (item_end < new_size)
4123 break;
4124 if (found_key.offset >= new_size)
4125 del_item = 1;
4126 else
4127 del_item = 0;
4128 }
Chris Mason39279cc2007-06-12 06:35:45 -04004129 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004130 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004131 if (found_type != BTRFS_EXTENT_DATA_KEY)
4132 goto delete;
4133
4134 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004135 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004136 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004137 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004138 u64 orig_num_bytes =
4139 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004140 extent_num_bytes = ALIGN(new_size -
4141 found_key.offset,
4142 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004143 btrfs_set_file_extent_num_bytes(leaf, fi,
4144 extent_num_bytes);
4145 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004146 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004147 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004148 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004149 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004150 } else {
Chris Masondb945352007-10-15 16:15:53 -04004151 extent_num_bytes =
4152 btrfs_file_extent_disk_num_bytes(leaf,
4153 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004154 extent_offset = found_key.offset -
4155 btrfs_file_extent_offset(leaf, fi);
4156
Chris Mason39279cc2007-06-12 06:35:45 -04004157 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004158 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004159 if (extent_start != 0) {
4160 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004161 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004162 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004163 }
4164 }
Chris Mason90692182008-02-08 13:49:28 -05004165 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004166 /*
4167 * we can't truncate inline items that have had
4168 * special encodings
4169 */
4170 if (!del_item &&
4171 btrfs_file_extent_compression(leaf, fi) == 0 &&
4172 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4173 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004174 u32 size = new_size - found_key.offset;
4175
4176 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004177 inode_sub_bytes(inode, item_end + 1 -
4178 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004179 }
4180 size =
4181 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004182 btrfs_truncate_item(trans, root, path,
4183 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004184 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004185 inode_sub_bytes(inode, item_end + 1 -
4186 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004187 }
Chris Mason39279cc2007-06-12 06:35:45 -04004188 }
Chris Mason179e29e2007-11-01 11:28:41 -04004189delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004190 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004191 if (!pending_del_nr) {
4192 /* no pending yet, add ourselves */
4193 pending_del_slot = path->slots[0];
4194 pending_del_nr = 1;
4195 } else if (pending_del_nr &&
4196 path->slots[0] + 1 == pending_del_slot) {
4197 /* hop on the pending chunk */
4198 pending_del_nr++;
4199 pending_del_slot = path->slots[0];
4200 } else {
Chris Masond3977122009-01-05 21:25:51 -05004201 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004202 }
Chris Mason39279cc2007-06-12 06:35:45 -04004203 } else {
4204 break;
4205 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004206 if (found_extent && (root->ref_cows ||
4207 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004208 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004209 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004210 extent_num_bytes, 0,
4211 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004212 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004213 BUG_ON(ret);
4214 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004215
Yan, Zheng80825102009-11-12 09:35:36 +00004216 if (found_type == BTRFS_INODE_ITEM_KEY)
4217 break;
4218
4219 if (path->slots[0] == 0 ||
4220 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004221 if (pending_del_nr) {
4222 ret = btrfs_del_items(trans, root, path,
4223 pending_del_slot,
4224 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004225 if (ret) {
4226 btrfs_abort_transaction(trans,
4227 root, ret);
4228 goto error;
4229 }
Yan, Zheng80825102009-11-12 09:35:36 +00004230 pending_del_nr = 0;
4231 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004232 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004233 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004234 } else {
4235 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004236 }
Chris Mason39279cc2007-06-12 06:35:45 -04004237 }
Yan, Zheng80825102009-11-12 09:35:36 +00004238out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004239 if (pending_del_nr) {
4240 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4241 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004242 if (ret)
4243 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004244 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004245error:
Chris Mason39279cc2007-06-12 06:35:45 -04004246 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004247 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004248}
4249
Chris Masona52d9a82007-08-27 16:49:44 -04004250/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004251 * btrfs_truncate_page - read, zero a chunk and write a page
4252 * @inode - inode that we're zeroing
4253 * @from - the offset to start zeroing
4254 * @len - the length to zero, 0 to zero the entire range respective to the
4255 * offset
4256 * @front - zero up to the offset instead of from the offset on
4257 *
4258 * This will find the page for the "from" offset and cow the page and zero the
4259 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004260 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004261int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4262 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004263{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004264 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004265 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004266 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4267 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004268 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004269 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004270 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004271 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4272 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4273 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004274 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004275 int ret = 0;
4276 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004277 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004278
Josef Bacik2aaa6652012-08-29 14:27:18 -04004279 if ((offset & (blocksize - 1)) == 0 &&
4280 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004281 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004282 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004283 if (ret)
4284 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004285
Chris Mason211c17f2008-05-15 09:13:45 -04004286again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004287 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004288 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004289 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004290 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004291 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004292 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004293
4294 page_start = page_offset(page);
4295 page_end = page_start + PAGE_CACHE_SIZE - 1;
4296
Chris Masona52d9a82007-08-27 16:49:44 -04004297 if (!PageUptodate(page)) {
4298 ret = btrfs_readpage(NULL, page);
4299 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004300 if (page->mapping != mapping) {
4301 unlock_page(page);
4302 page_cache_release(page);
4303 goto again;
4304 }
Chris Masona52d9a82007-08-27 16:49:44 -04004305 if (!PageUptodate(page)) {
4306 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004307 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004308 }
4309 }
Chris Mason211c17f2008-05-15 09:13:45 -04004310 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004311
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004312 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004313 set_page_extent_mapped(page);
4314
4315 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4316 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004317 unlock_extent_cached(io_tree, page_start, page_end,
4318 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004319 unlock_page(page);
4320 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004321 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004322 btrfs_put_ordered_extent(ordered);
4323 goto again;
4324 }
4325
Josef Bacik2ac55d42010-02-03 19:33:23 +00004326 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004327 EXTENT_DIRTY | EXTENT_DELALLOC |
4328 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004329 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004330
Josef Bacik2ac55d42010-02-03 19:33:23 +00004331 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4332 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004333 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004334 unlock_extent_cached(io_tree, page_start, page_end,
4335 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004336 goto out_unlock;
4337 }
4338
Chris Masone6dcd2d2008-07-17 12:53:50 -04004339 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004340 if (!len)
4341 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004342 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004343 if (front)
4344 memset(kaddr, 0, offset);
4345 else
4346 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004347 flush_dcache_page(page);
4348 kunmap(page);
4349 }
Chris Mason247e7432008-07-17 12:53:51 -04004350 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004351 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004352 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4353 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004354
Chris Mason89642222008-07-24 09:41:53 -04004355out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004356 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004357 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004358 unlock_page(page);
4359 page_cache_release(page);
4360out:
4361 return ret;
4362}
4363
Josef Bacik695a0d02011-03-04 15:46:53 -05004364/*
4365 * This function puts in dummy file extents for the area we're creating a hole
4366 * for. So if we are truncating this file to a larger size we need to insert
4367 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4368 * the range between oldsize and size
4369 */
Josef Bacika41ad392011-01-31 15:30:16 -05004370int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004371{
4372 struct btrfs_trans_handle *trans;
4373 struct btrfs_root *root = BTRFS_I(inode)->root;
4374 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004375 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004376 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004377 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004378 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4379 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004380 u64 last_byte;
4381 u64 cur_offset;
4382 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004383 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004384
4385 if (size <= hole_start)
4386 return 0;
4387
Yan Zheng9036c102008-10-30 14:19:41 -04004388 while (1) {
4389 struct btrfs_ordered_extent *ordered;
4390 btrfs_wait_ordered_range(inode, hole_start,
4391 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004392 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004393 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004394 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4395 if (!ordered)
4396 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004397 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4398 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004399 btrfs_put_ordered_extent(ordered);
4400 }
4401
Yan Zheng9036c102008-10-30 14:19:41 -04004402 cur_offset = hole_start;
4403 while (1) {
4404 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4405 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004406 if (IS_ERR(em)) {
4407 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004408 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004409 break;
4410 }
Yan Zheng9036c102008-10-30 14:19:41 -04004411 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004412 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004413 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004414 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004415 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004416
Miao Xie36423202011-12-14 20:12:02 -05004417 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004418 if (IS_ERR(trans)) {
4419 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004420 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004421 }
Yan, Zheng80825102009-11-12 09:35:36 +00004422
Josef Bacik5dc562c2012-08-17 13:14:17 -04004423 err = btrfs_drop_extents(trans, root, inode,
4424 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004425 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004426 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004427 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004428 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004429 break;
Miao Xie5b397372011-09-11 10:52:24 -04004430 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004431
Yan Zheng9036c102008-10-30 14:19:41 -04004432 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004433 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004434 0, hole_size, 0, hole_size,
4435 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004436 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004437 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004438 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004439 break;
Miao Xie5b397372011-09-11 10:52:24 -04004440 }
Yan, Zheng80825102009-11-12 09:35:36 +00004441
Josef Bacik5dc562c2012-08-17 13:14:17 -04004442 btrfs_drop_extent_cache(inode, cur_offset,
4443 cur_offset + hole_size - 1, 0);
4444 hole_em = alloc_extent_map();
4445 if (!hole_em) {
4446 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4447 &BTRFS_I(inode)->runtime_flags);
4448 goto next;
4449 }
4450 hole_em->start = cur_offset;
4451 hole_em->len = hole_size;
4452 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004453
Josef Bacik5dc562c2012-08-17 13:14:17 -04004454 hole_em->block_start = EXTENT_MAP_HOLE;
4455 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004456 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004457 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4458 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4459 hole_em->generation = trans->transid;
4460
4461 while (1) {
4462 write_lock(&em_tree->lock);
4463 err = add_extent_mapping(em_tree, hole_em);
4464 if (!err)
4465 list_move(&hole_em->list,
4466 &em_tree->modified_extents);
4467 write_unlock(&em_tree->lock);
4468 if (err != -EEXIST)
4469 break;
4470 btrfs_drop_extent_cache(inode, cur_offset,
4471 cur_offset +
4472 hole_size - 1, 0);
4473 }
4474 free_extent_map(hole_em);
4475next:
Miao Xie36423202011-12-14 20:12:02 -05004476 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004477 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004478 }
4479 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004480 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004481 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004482 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004483 break;
4484 }
4485
Yan, Zhenga22285a2010-05-16 10:48:46 -04004486 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004487 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4488 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004489 return err;
4490}
4491
Eric Sandeen3972f262013-01-12 02:57:22 +00004492static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004493{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004494 struct btrfs_root *root = BTRFS_I(inode)->root;
4495 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004496 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004497 loff_t newsize = attr->ia_size;
4498 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004499 int ret;
4500
Josef Bacika41ad392011-01-31 15:30:16 -05004501 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004502 return 0;
4503
Eric Sandeen3972f262013-01-12 02:57:22 +00004504 /*
4505 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4506 * special case where we need to update the times despite not having
4507 * these flags set. For all other operations the VFS set these flags
4508 * explicitly if it wants a timestamp update.
4509 */
4510 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4511 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4512
Josef Bacika41ad392011-01-31 15:30:16 -05004513 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004514 truncate_pagecache(inode, oldsize, newsize);
4515 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004516 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004517 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004518
Miao Xief4a2f4c2011-12-14 20:12:01 -05004519 trans = btrfs_start_transaction(root, 1);
4520 if (IS_ERR(trans))
4521 return PTR_ERR(trans);
4522
4523 i_size_write(inode, newsize);
4524 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4525 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004526 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004527 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004528
Josef Bacika41ad392011-01-31 15:30:16 -05004529 /*
4530 * We're truncating a file that used to have good data down to
4531 * zero. Make sure it gets into the ordered flush list so that
4532 * any new writes get down to disk quickly.
4533 */
4534 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004535 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4536 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004537
Josef Bacikf3fe8202013-01-07 17:03:21 -05004538 /*
4539 * 1 for the orphan item we're going to add
4540 * 1 for the orphan item deletion.
4541 */
4542 trans = btrfs_start_transaction(root, 2);
4543 if (IS_ERR(trans))
4544 return PTR_ERR(trans);
4545
4546 /*
4547 * We need to do this in case we fail at _any_ point during the
4548 * actual truncate. Once we do the truncate_setsize we could
4549 * invalidate pages which forces any outstanding ordered io to
4550 * be instantly completed which will give us extents that need
4551 * to be truncated. If we fail to get an orphan inode down we
4552 * could have left over extents that were never meant to live,
4553 * so we need to garuntee from this point on that everything
4554 * will be consistent.
4555 */
4556 ret = btrfs_orphan_add(trans, inode);
4557 btrfs_end_transaction(trans, root);
4558 if (ret)
4559 return ret;
4560
Josef Bacika41ad392011-01-31 15:30:16 -05004561 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4562 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004563
4564 /* Disable nonlocked read DIO to avoid the end less truncate */
4565 btrfs_inode_block_unlocked_dio(inode);
4566 inode_dio_wait(inode);
4567 btrfs_inode_resume_unlocked_dio(inode);
4568
Josef Bacika41ad392011-01-31 15:30:16 -05004569 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004570 if (ret && inode->i_nlink)
4571 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004572 }
4573
Josef Bacika41ad392011-01-31 15:30:16 -05004574 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004575}
4576
Chris Mason39279cc2007-06-12 06:35:45 -04004577static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4578{
4579 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004580 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004581 int err;
4582
Li Zefanb83cc962010-12-20 16:04:08 +08004583 if (btrfs_root_readonly(root))
4584 return -EROFS;
4585
Chris Mason39279cc2007-06-12 06:35:45 -04004586 err = inode_change_ok(inode, attr);
4587 if (err)
4588 return err;
4589
Chris Mason5a3f23d2009-03-31 13:27:11 -04004590 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004591 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004592 if (err)
4593 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004594 }
Yan Zheng9036c102008-10-30 14:19:41 -04004595
Christoph Hellwig10257742010-06-04 11:30:02 +02004596 if (attr->ia_valid) {
4597 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004598 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004599 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004600
Josef Bacik22c44fe2011-11-30 10:45:38 -05004601 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004602 err = btrfs_acl_chmod(inode);
4603 }
4604
Chris Mason39279cc2007-06-12 06:35:45 -04004605 return err;
4606}
Chris Mason61295eb2008-01-14 16:24:38 -05004607
Al Virobd555972010-06-07 11:35:40 -04004608void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004609{
4610 struct btrfs_trans_handle *trans;
4611 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004612 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004613 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004614 int ret;
4615
liubo1abe9b82011-03-24 11:18:59 +00004616 trace_btrfs_inode_evict(inode);
4617
Chris Mason39279cc2007-06-12 06:35:45 -04004618 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004619 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004620 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004621 goto no_delete;
4622
Chris Mason39279cc2007-06-12 06:35:45 -04004623 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004624 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004625 goto no_delete;
4626 }
Al Virobd555972010-06-07 11:35:40 -04004627 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004628 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004629
Yan, Zhengc71bf092009-11-12 09:34:40 +00004630 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004631 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004632 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004633 goto no_delete;
4634 }
4635
Yan, Zheng76dda932009-09-21 16:00:26 -04004636 if (inode->i_nlink > 0) {
4637 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4638 goto no_delete;
4639 }
4640
Miao Xie0e8c36a2012-12-19 06:59:51 +00004641 ret = btrfs_commit_inode_delayed_inode(inode);
4642 if (ret) {
4643 btrfs_orphan_del(NULL, inode);
4644 goto no_delete;
4645 }
4646
Miao Xie66d8f3d2012-09-06 04:02:28 -06004647 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004648 if (!rsv) {
4649 btrfs_orphan_del(NULL, inode);
4650 goto no_delete;
4651 }
Josef Bacik4a338542011-08-29 11:01:31 -04004652 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004653 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004654 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004655
Chris Masondbe674a2008-07-17 12:54:05 -04004656 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004657
Josef Bacik4289a662011-08-05 13:22:24 -04004658 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004659 * This is a bit simpler than btrfs_truncate since we've already
4660 * reserved our space for our orphan item in the unlink, so we just
4661 * need to reserve some slack space in case we add bytes and update
4662 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004663 */
Yan, Zheng80825102009-11-12 09:35:36 +00004664 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004665 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4666 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004667
Josef Bacik726c35f2011-09-26 15:46:06 -04004668 /*
4669 * Try and steal from the global reserve since we will
4670 * likely not use this space anyway, we want to try as
4671 * hard as possible to get this to work.
4672 */
4673 if (ret)
4674 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4675
Yan, Zhengd68fc572010-05-16 10:49:58 -04004676 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004677 btrfs_warn(root->fs_info,
4678 "Could not get space for a delete, will truncate on mount %d",
4679 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004680 btrfs_orphan_del(NULL, inode);
4681 btrfs_free_block_rsv(root, rsv);
4682 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004683 }
4684
Miao Xie0e8c36a2012-12-19 06:59:51 +00004685 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004686 if (IS_ERR(trans)) {
4687 btrfs_orphan_del(NULL, inode);
4688 btrfs_free_block_rsv(root, rsv);
4689 goto no_delete;
4690 }
4691
4692 trans->block_rsv = rsv;
4693
Yan, Zhengd68fc572010-05-16 10:49:58 -04004694 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004695 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004696 break;
4697
Miao Xie8407aa42012-09-07 01:43:32 -06004698 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004699 btrfs_end_transaction(trans, root);
4700 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004701 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004702 }
4703
Josef Bacik4289a662011-08-05 13:22:24 -04004704 btrfs_free_block_rsv(root, rsv);
4705
Yan, Zheng80825102009-11-12 09:35:36 +00004706 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004707 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004708 ret = btrfs_orphan_del(trans, inode);
4709 BUG_ON(ret);
4710 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004711
Josef Bacik4289a662011-08-05 13:22:24 -04004712 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004713 if (!(root == root->fs_info->tree_root ||
4714 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004715 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004716
Chris Mason54aa1f42007-06-22 14:16:25 -04004717 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004718 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004719no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004720 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004721 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004722}
4723
4724/*
4725 * this returns the key found in the dir entry in the location pointer.
4726 * If no dir entries were found, location->objectid is 0.
4727 */
4728static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4729 struct btrfs_key *location)
4730{
4731 const char *name = dentry->d_name.name;
4732 int namelen = dentry->d_name.len;
4733 struct btrfs_dir_item *di;
4734 struct btrfs_path *path;
4735 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004736 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004737
4738 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004739 if (!path)
4740 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004741
Li Zefan33345d012011-04-20 10:31:50 +08004742 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004743 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004744 if (IS_ERR(di))
4745 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004746
David Sterbac7040052011-04-19 18:00:01 +02004747 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004748 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004749
Chris Mason5f39d392007-10-15 16:14:19 -04004750 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004751out:
Chris Mason39279cc2007-06-12 06:35:45 -04004752 btrfs_free_path(path);
4753 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004754out_err:
4755 location->objectid = 0;
4756 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004757}
4758
4759/*
4760 * when we hit a tree root in a directory, the btrfs part of the inode
4761 * needs to be changed to reflect the root directory of the tree root. This
4762 * is kind of like crossing a mount point.
4763 */
4764static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004765 struct inode *dir,
4766 struct dentry *dentry,
4767 struct btrfs_key *location,
4768 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004769{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004770 struct btrfs_path *path;
4771 struct btrfs_root *new_root;
4772 struct btrfs_root_ref *ref;
4773 struct extent_buffer *leaf;
4774 int ret;
4775 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004776
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004777 path = btrfs_alloc_path();
4778 if (!path) {
4779 err = -ENOMEM;
4780 goto out;
4781 }
Chris Mason39279cc2007-06-12 06:35:45 -04004782
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004783 err = -ENOENT;
4784 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4785 BTRFS_I(dir)->root->root_key.objectid,
4786 location->objectid);
4787 if (ret) {
4788 if (ret < 0)
4789 err = ret;
4790 goto out;
4791 }
Chris Mason39279cc2007-06-12 06:35:45 -04004792
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004793 leaf = path->nodes[0];
4794 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004795 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004796 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4797 goto out;
4798
4799 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4800 (unsigned long)(ref + 1),
4801 dentry->d_name.len);
4802 if (ret)
4803 goto out;
4804
David Sterbab3b4aa72011-04-21 01:20:15 +02004805 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004806
4807 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4808 if (IS_ERR(new_root)) {
4809 err = PTR_ERR(new_root);
4810 goto out;
4811 }
4812
4813 if (btrfs_root_refs(&new_root->root_item) == 0) {
4814 err = -ENOENT;
4815 goto out;
4816 }
4817
4818 *sub_root = new_root;
4819 location->objectid = btrfs_root_dirid(&new_root->root_item);
4820 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004821 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004822 err = 0;
4823out:
4824 btrfs_free_path(path);
4825 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004826}
4827
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004828static void inode_tree_add(struct inode *inode)
4829{
4830 struct btrfs_root *root = BTRFS_I(inode)->root;
4831 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004832 struct rb_node **p;
4833 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004834 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004835again:
4836 p = &root->inode_tree.rb_node;
4837 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004838
Al Viro1d3382cb2010-10-23 15:19:20 -04004839 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004840 return;
4841
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004842 spin_lock(&root->inode_lock);
4843 while (*p) {
4844 parent = *p;
4845 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4846
Li Zefan33345d012011-04-20 10:31:50 +08004847 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004848 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004849 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004850 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004851 else {
4852 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004853 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004854 rb_erase(parent, &root->inode_tree);
4855 RB_CLEAR_NODE(parent);
4856 spin_unlock(&root->inode_lock);
4857 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004858 }
4859 }
4860 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4861 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4862 spin_unlock(&root->inode_lock);
4863}
4864
4865static void inode_tree_del(struct inode *inode)
4866{
4867 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004868 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004869
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004870 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004871 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004872 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004873 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004874 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004875 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004876 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004877
Josef Bacik0af3d002010-06-21 14:48:16 -04004878 /*
4879 * Free space cache has inodes in the tree root, but the tree root has a
4880 * root_refs of 0, so this could end up dropping the tree root as a
4881 * snapshot, so we need the extra !root->fs_info->tree_root check to
4882 * make sure we don't drop it.
4883 */
4884 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4885 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004886 synchronize_srcu(&root->fs_info->subvol_srcu);
4887 spin_lock(&root->inode_lock);
4888 empty = RB_EMPTY_ROOT(&root->inode_tree);
4889 spin_unlock(&root->inode_lock);
4890 if (empty)
4891 btrfs_add_dead_root(root);
4892 }
4893}
4894
Jeff Mahoney143bede2012-03-01 14:56:26 +01004895void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004896{
4897 struct rb_node *node;
4898 struct rb_node *prev;
4899 struct btrfs_inode *entry;
4900 struct inode *inode;
4901 u64 objectid = 0;
4902
4903 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4904
4905 spin_lock(&root->inode_lock);
4906again:
4907 node = root->inode_tree.rb_node;
4908 prev = NULL;
4909 while (node) {
4910 prev = node;
4911 entry = rb_entry(node, struct btrfs_inode, rb_node);
4912
Li Zefan33345d012011-04-20 10:31:50 +08004913 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004914 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004915 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004916 node = node->rb_right;
4917 else
4918 break;
4919 }
4920 if (!node) {
4921 while (prev) {
4922 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004923 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004924 node = prev;
4925 break;
4926 }
4927 prev = rb_next(prev);
4928 }
4929 }
4930 while (node) {
4931 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004932 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004933 inode = igrab(&entry->vfs_inode);
4934 if (inode) {
4935 spin_unlock(&root->inode_lock);
4936 if (atomic_read(&inode->i_count) > 1)
4937 d_prune_aliases(inode);
4938 /*
Al Viro45321ac2010-06-07 13:43:19 -04004939 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004940 * the inode cache when its usage count
4941 * hits zero.
4942 */
4943 iput(inode);
4944 cond_resched();
4945 spin_lock(&root->inode_lock);
4946 goto again;
4947 }
4948
4949 if (cond_resched_lock(&root->inode_lock))
4950 goto again;
4951
4952 node = rb_next(node);
4953 }
4954 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004955}
4956
Chris Masone02119d2008-09-05 16:13:11 -04004957static int btrfs_init_locked_inode(struct inode *inode, void *p)
4958{
4959 struct btrfs_iget_args *args = p;
4960 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004961 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004962 return 0;
4963}
4964
4965static int btrfs_find_actor(struct inode *inode, void *opaque)
4966{
4967 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004968 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004969 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004970}
4971
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004972static struct inode *btrfs_iget_locked(struct super_block *s,
4973 u64 objectid,
4974 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004975{
4976 struct inode *inode;
4977 struct btrfs_iget_args args;
4978 args.ino = objectid;
4979 args.root = root;
4980
4981 inode = iget5_locked(s, objectid, btrfs_find_actor,
4982 btrfs_init_locked_inode,
4983 (void *)&args);
4984 return inode;
4985}
4986
Balaji Rao1a54ef82008-07-21 02:01:04 +05304987/* Get an inode object given its location and corresponding root.
4988 * Returns in *is_new if the inode was read from disk
4989 */
4990struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004991 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304992{
4993 struct inode *inode;
4994
4995 inode = btrfs_iget_locked(s, location->objectid, root);
4996 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004997 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304998
4999 if (inode->i_state & I_NEW) {
5000 BTRFS_I(inode)->root = root;
5001 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
5002 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005003 if (!is_bad_inode(inode)) {
5004 inode_tree_add(inode);
5005 unlock_new_inode(inode);
5006 if (new)
5007 *new = 1;
5008 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005009 unlock_new_inode(inode);
5010 iput(inode);
5011 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005012 }
5013 }
5014
Balaji Rao1a54ef82008-07-21 02:01:04 +05305015 return inode;
5016}
5017
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005018static struct inode *new_simple_dir(struct super_block *s,
5019 struct btrfs_key *key,
5020 struct btrfs_root *root)
5021{
5022 struct inode *inode = new_inode(s);
5023
5024 if (!inode)
5025 return ERR_PTR(-ENOMEM);
5026
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005027 BTRFS_I(inode)->root = root;
5028 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005029 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005030
5031 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005032 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005033 inode->i_fop = &simple_dir_operations;
5034 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5035 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5036
5037 return inode;
5038}
5039
Chris Mason3de45862008-11-17 21:02:50 -05005040struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005041{
Chris Masond3977122009-01-05 21:25:51 -05005042 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005043 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005044 struct btrfs_root *sub_root = root;
5045 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005046 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005047 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005048
5049 if (dentry->d_name.len > BTRFS_NAME_LEN)
5050 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005051
Jeff Layton39e3c952012-11-28 11:30:53 -05005052 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005053 if (ret < 0)
5054 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005055
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005056 if (location.objectid == 0)
5057 return NULL;
5058
5059 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005060 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005061 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005062 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005063
5064 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5065
Yan, Zheng76dda932009-09-21 16:00:26 -04005066 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005067 ret = fixup_tree_root_location(root, dir, dentry,
5068 &location, &sub_root);
5069 if (ret < 0) {
5070 if (ret != -ENOENT)
5071 inode = ERR_PTR(ret);
5072 else
5073 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5074 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005075 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005076 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005077 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5078
Julia Lawall34d19ba2011-01-24 19:55:19 +00005079 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005080 down_read(&root->fs_info->cleanup_work_sem);
5081 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005082 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005083 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005084 if (ret)
5085 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005086 }
5087
Chris Mason3de45862008-11-17 21:02:50 -05005088 return inode;
5089}
5090
Nick Pigginfe15ce42011-01-07 17:49:23 +11005091static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005092{
5093 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005094 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005095
Li Zefan848cce02012-02-21 17:04:28 +08005096 if (!inode && !IS_ROOT(dentry))
5097 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005098
Li Zefan848cce02012-02-21 17:04:28 +08005099 if (inode) {
5100 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005101 if (btrfs_root_refs(&root->root_item) == 0)
5102 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005103
5104 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5105 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005106 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005107 return 0;
5108}
5109
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005110static void btrfs_dentry_release(struct dentry *dentry)
5111{
5112 if (dentry->d_fsdata)
5113 kfree(dentry->d_fsdata);
5114}
5115
Chris Mason3de45862008-11-17 21:02:50 -05005116static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005117 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005118{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005119 struct dentry *ret;
5120
5121 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005122 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005123}
5124
Miao Xie16cdcec2011-04-22 18:12:22 +08005125unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005126 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5127};
5128
David Woodhousecbdf5a22008-08-06 19:42:33 +01005129static int btrfs_real_readdir(struct file *filp, void *dirent,
5130 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005131{
Al Viro496ad9a2013-01-23 17:07:38 -05005132 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005133 struct btrfs_root *root = BTRFS_I(inode)->root;
5134 struct btrfs_item *item;
5135 struct btrfs_dir_item *di;
5136 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005137 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005138 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005139 struct list_head ins_list;
5140 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005141 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005142 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005143 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005144 unsigned char d_type;
5145 int over = 0;
5146 u32 di_cur;
5147 u32 di_total;
5148 u32 di_len;
5149 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005150 char tmp_name[32];
5151 char *name_ptr;
5152 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005153 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005154
5155 /* FIXME, use a real flag for deciding about the key type */
5156 if (root->fs_info->tree_root == root)
5157 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005158
Chris Mason39544012007-12-12 14:38:19 -05005159 /* special case for "." */
5160 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005161 over = filldir(dirent, ".", 1,
5162 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005163 if (over)
5164 return 0;
5165 filp->f_pos = 1;
5166 }
Chris Mason39544012007-12-12 14:38:19 -05005167 /* special case for .., just use the back ref */
5168 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005169 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005170 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005171 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005172 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005173 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005174 filp->f_pos = 2;
5175 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005176 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005177 if (!path)
5178 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005179
Josef Bacik026fd312011-05-13 10:32:11 -04005180 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005181
Miao Xie16cdcec2011-04-22 18:12:22 +08005182 if (key_type == BTRFS_DIR_INDEX_KEY) {
5183 INIT_LIST_HEAD(&ins_list);
5184 INIT_LIST_HEAD(&del_list);
5185 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5186 }
5187
Chris Mason39279cc2007-06-12 06:35:45 -04005188 btrfs_set_key_type(&key, key_type);
5189 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005190 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005191
Chris Mason39279cc2007-06-12 06:35:45 -04005192 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5193 if (ret < 0)
5194 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005195
5196 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005197 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005198 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005199 if (slot >= btrfs_header_nritems(leaf)) {
5200 ret = btrfs_next_leaf(root, path);
5201 if (ret < 0)
5202 goto err;
5203 else if (ret > 0)
5204 break;
5205 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005206 }
Chris Mason3de45862008-11-17 21:02:50 -05005207
Chris Mason5f39d392007-10-15 16:14:19 -04005208 item = btrfs_item_nr(leaf, slot);
5209 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5210
5211 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005212 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005213 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005214 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005215 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005216 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005217 if (key_type == BTRFS_DIR_INDEX_KEY &&
5218 btrfs_should_delete_dir_index(&del_list,
5219 found_key.offset))
5220 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005221
5222 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005223 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005224
Chris Mason39279cc2007-06-12 06:35:45 -04005225 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5226 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005227 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005228
5229 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005230 struct btrfs_key location;
5231
Josef Bacik22a94d42011-03-16 16:47:17 -04005232 if (verify_dir_item(root, leaf, di))
5233 break;
5234
Chris Mason5f39d392007-10-15 16:14:19 -04005235 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005236 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005237 name_ptr = tmp_name;
5238 } else {
5239 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005240 if (!name_ptr) {
5241 ret = -ENOMEM;
5242 goto err;
5243 }
Chris Mason5f39d392007-10-15 16:14:19 -04005244 }
5245 read_extent_buffer(leaf, name_ptr,
5246 (unsigned long)(di + 1), name_len);
5247
5248 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5249 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005250
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005251
Chris Mason3de45862008-11-17 21:02:50 -05005252 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005253 * skip it.
5254 *
5255 * In contrast to old kernels, we insert the snapshot's
5256 * dir item and dir index after it has been created, so
5257 * we won't find a reference to our own snapshot. We
5258 * still keep the following code for backward
5259 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005260 */
5261 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5262 location.objectid == root->root_key.objectid) {
5263 over = 0;
5264 goto skip;
5265 }
Chris Mason5f39d392007-10-15 16:14:19 -04005266 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005267 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005268 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005269
Chris Mason3de45862008-11-17 21:02:50 -05005270skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005271 if (name_ptr != tmp_name)
5272 kfree(name_ptr);
5273
Chris Mason39279cc2007-06-12 06:35:45 -04005274 if (over)
5275 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005276 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005277 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005278 di_cur += di_len;
5279 di = (struct btrfs_dir_item *)((char *)di + di_len);
5280 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005281next:
5282 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005283 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005284
Miao Xie16cdcec2011-04-22 18:12:22 +08005285 if (key_type == BTRFS_DIR_INDEX_KEY) {
5286 if (is_curr)
5287 filp->f_pos++;
5288 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5289 &ins_list);
5290 if (ret)
5291 goto nopos;
5292 }
5293
David Woodhouse49593bf2008-08-17 17:08:36 +01005294 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005295 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005296 /*
5297 * 32-bit glibc will use getdents64, but then strtol -
5298 * so the last number we can serve is this.
5299 */
5300 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005301 else
5302 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005303nopos:
5304 ret = 0;
5305err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005306 if (key_type == BTRFS_DIR_INDEX_KEY)
5307 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005308 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005309 return ret;
5310}
5311
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005312int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005313{
5314 struct btrfs_root *root = BTRFS_I(inode)->root;
5315 struct btrfs_trans_handle *trans;
5316 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005317 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005318
Josef Bacik72ac3c02012-05-23 14:13:11 -04005319 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005320 return 0;
5321
Liu Bo83eea1f2012-07-10 05:28:39 -06005322 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005323 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005324
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005325 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005326 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005327 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005328 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005329 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005330 if (IS_ERR(trans))
5331 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005332 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005333 }
5334 return ret;
5335}
5336
5337/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005338 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005339 * inode changes. But, it is most likely to find the inode in cache.
5340 * FIXME, needs more benchmarking...there are no reasons other than performance
5341 * to keep or drop this code.
5342 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05005343int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005344{
5345 struct btrfs_root *root = BTRFS_I(inode)->root;
5346 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005347 int ret;
5348
Josef Bacik72ac3c02012-05-23 14:13:11 -04005349 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005350 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005351
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005352 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005353 if (IS_ERR(trans))
5354 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005355
5356 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005357 if (ret && ret == -ENOSPC) {
5358 /* whoops, lets try again with the full transaction */
5359 btrfs_end_transaction(trans, root);
5360 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005361 if (IS_ERR(trans))
5362 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005363
Chris Mason94b60442010-05-26 11:02:00 -04005364 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005365 }
Chris Mason39279cc2007-06-12 06:35:45 -04005366 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005367 if (BTRFS_I(inode)->delayed_node)
5368 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005369
5370 return ret;
5371}
5372
5373/*
5374 * This is a copy of file_update_time. We need this so we can return error on
5375 * ENOSPC for updating the inode in the case of file write and mmap writes.
5376 */
Josef Bacike41f9412012-03-26 09:46:47 -04005377static int btrfs_update_time(struct inode *inode, struct timespec *now,
5378 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005379{
Alexander Block2bc5565282012-06-15 09:49:33 +02005380 struct btrfs_root *root = BTRFS_I(inode)->root;
5381
5382 if (btrfs_root_readonly(root))
5383 return -EROFS;
5384
Josef Bacike41f9412012-03-26 09:46:47 -04005385 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005386 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005387 if (flags & S_CTIME)
5388 inode->i_ctime = *now;
5389 if (flags & S_MTIME)
5390 inode->i_mtime = *now;
5391 if (flags & S_ATIME)
5392 inode->i_atime = *now;
5393 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005394}
5395
Chris Masond352ac62008-09-29 15:18:18 -04005396/*
5397 * find the highest existing sequence number in a directory
5398 * and then set the in-memory index_cnt variable to reflect
5399 * free sequence numbers
5400 */
Josef Bacikaec74772008-07-24 12:12:38 -04005401static int btrfs_set_inode_index_count(struct inode *inode)
5402{
5403 struct btrfs_root *root = BTRFS_I(inode)->root;
5404 struct btrfs_key key, found_key;
5405 struct btrfs_path *path;
5406 struct extent_buffer *leaf;
5407 int ret;
5408
Li Zefan33345d012011-04-20 10:31:50 +08005409 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005410 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5411 key.offset = (u64)-1;
5412
5413 path = btrfs_alloc_path();
5414 if (!path)
5415 return -ENOMEM;
5416
5417 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5418 if (ret < 0)
5419 goto out;
5420 /* FIXME: we should be able to handle this */
5421 if (ret == 0)
5422 goto out;
5423 ret = 0;
5424
5425 /*
5426 * MAGIC NUMBER EXPLANATION:
5427 * since we search a directory based on f_pos we have to start at 2
5428 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5429 * else has to start at 2
5430 */
5431 if (path->slots[0] == 0) {
5432 BTRFS_I(inode)->index_cnt = 2;
5433 goto out;
5434 }
5435
5436 path->slots[0]--;
5437
5438 leaf = path->nodes[0];
5439 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5440
Li Zefan33345d012011-04-20 10:31:50 +08005441 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005442 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5443 BTRFS_I(inode)->index_cnt = 2;
5444 goto out;
5445 }
5446
5447 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5448out:
5449 btrfs_free_path(path);
5450 return ret;
5451}
5452
Chris Masond352ac62008-09-29 15:18:18 -04005453/*
5454 * helper to find a free sequence number in a given directory. This current
5455 * code is very simple, later versions will do smarter things in the btree
5456 */
Chris Mason3de45862008-11-17 21:02:50 -05005457int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005458{
5459 int ret = 0;
5460
5461 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005462 ret = btrfs_inode_delayed_dir_index_count(dir);
5463 if (ret) {
5464 ret = btrfs_set_inode_index_count(dir);
5465 if (ret)
5466 return ret;
5467 }
Josef Bacikaec74772008-07-24 12:12:38 -04005468 }
5469
Chris Mason00e4e6b2008-08-05 11:18:09 -04005470 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005471 BTRFS_I(dir)->index_cnt++;
5472
5473 return ret;
5474}
5475
Chris Mason39279cc2007-06-12 06:35:45 -04005476static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5477 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005478 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005479 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005480 u64 ref_objectid, u64 objectid,
5481 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005482{
5483 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005484 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005485 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005486 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005487 struct btrfs_inode_ref *ref;
5488 struct btrfs_key key[2];
5489 u32 sizes[2];
5490 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005491 int ret;
5492 int owner;
5493
Chris Mason5f39d392007-10-15 16:14:19 -04005494 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005495 if (!path)
5496 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005497
Chris Mason39279cc2007-06-12 06:35:45 -04005498 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005499 if (!inode) {
5500 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005501 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005502 }
Chris Mason39279cc2007-06-12 06:35:45 -04005503
Li Zefan581bb052011-04-20 10:06:11 +08005504 /*
5505 * we have to initialize this early, so we can reclaim the inode
5506 * number if we fail afterwards in this function.
5507 */
5508 inode->i_ino = objectid;
5509
Josef Bacikaec74772008-07-24 12:12:38 -04005510 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005511 trace_btrfs_inode_request(dir);
5512
Chris Mason3de45862008-11-17 21:02:50 -05005513 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005514 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005515 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005516 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005517 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005518 }
Josef Bacikaec74772008-07-24 12:12:38 -04005519 }
5520 /*
5521 * index_cnt is ignored for everything but a dir,
5522 * btrfs_get_inode_index_count has an explanation for the magic
5523 * number
5524 */
5525 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005526 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005527 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005528 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005529
Josef Bacik5dc562c2012-08-17 13:14:17 -04005530 /*
5531 * We could have gotten an inode number from somebody who was fsynced
5532 * and then removed in this same transaction, so let's just set full
5533 * sync since it will be a full sync anyway and this will blow away the
5534 * old info in the log.
5535 */
5536 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5537
Al Viro569254b2011-07-24 17:08:40 -04005538 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005539 owner = 0;
5540 else
5541 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005542
5543 key[0].objectid = objectid;
5544 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5545 key[0].offset = 0;
5546
Mark Fashehf1863732012-08-08 11:32:27 -07005547 /*
5548 * Start new inodes with an inode_ref. This is slightly more
5549 * efficient for small numbers of hard links since they will
5550 * be packed into one item. Extended refs will kick in if we
5551 * add more hard links than can fit in the ref item.
5552 */
Chris Mason9c583092008-01-29 15:15:18 -05005553 key[1].objectid = objectid;
5554 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5555 key[1].offset = ref_objectid;
5556
5557 sizes[0] = sizeof(struct btrfs_inode_item);
5558 sizes[1] = name_len + sizeof(*ref);
5559
Chris Masonb9473432009-03-13 11:00:37 -04005560 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005561 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5562 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005563 goto fail;
5564
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005565 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005566 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005567 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005568 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5569 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005570 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5571 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005572 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005573
5574 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5575 struct btrfs_inode_ref);
5576 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005577 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005578 ptr = (unsigned long)(ref + 1);
5579 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5580
Chris Mason5f39d392007-10-15 16:14:19 -04005581 btrfs_mark_buffer_dirty(path->nodes[0]);
5582 btrfs_free_path(path);
5583
Chris Mason39279cc2007-06-12 06:35:45 -04005584 location = &BTRFS_I(inode)->location;
5585 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005586 location->offset = 0;
5587 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5588
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005589 btrfs_inherit_iflags(inode, dir);
5590
Al Viro569254b2011-07-24 17:08:40 -04005591 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005592 if (btrfs_test_opt(root, NODATASUM))
5593 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005594 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005595 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5596 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005597 }
5598
Chris Mason39279cc2007-06-12 06:35:45 -04005599 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005601
5602 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005603 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005604
Alexander Block8ea05e32012-07-25 17:35:53 +02005605 btrfs_update_root_times(trans, root);
5606
Chris Mason39279cc2007-06-12 06:35:45 -04005607 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005608fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005609 if (dir)
5610 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005611 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005612 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005613 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005614}
5615
5616static inline u8 btrfs_inode_type(struct inode *inode)
5617{
5618 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5619}
5620
Chris Masond352ac62008-09-29 15:18:18 -04005621/*
5622 * utility function to add 'inode' into 'parent_inode' with
5623 * a give name and a given sequence number.
5624 * if 'add_backref' is true, also insert a backref from the
5625 * inode to the parent directory.
5626 */
Chris Masone02119d2008-09-05 16:13:11 -04005627int btrfs_add_link(struct btrfs_trans_handle *trans,
5628 struct inode *parent_inode, struct inode *inode,
5629 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005630{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005631 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005632 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005633 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005634 u64 ino = btrfs_ino(inode);
5635 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005636
Li Zefan33345d012011-04-20 10:31:50 +08005637 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5639 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005640 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005641 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5642 key.offset = 0;
5643 }
Chris Mason39279cc2007-06-12 06:35:45 -04005644
Li Zefan33345d012011-04-20 10:31:50 +08005645 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005646 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5647 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005648 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005649 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005650 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5651 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005652 }
5653
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005654 /* Nothing to clean up yet */
5655 if (ret)
5656 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005657
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005658 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5659 parent_inode, &key,
5660 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005661 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005662 goto fail_dir_item;
5663 else if (ret) {
5664 btrfs_abort_transaction(trans, root, ret);
5665 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005666 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005667
5668 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5669 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005670 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005671 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5672 ret = btrfs_update_inode(trans, root, parent_inode);
5673 if (ret)
5674 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005675 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005676
5677fail_dir_item:
5678 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5679 u64 local_index;
5680 int err;
5681 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5682 key.objectid, root->root_key.objectid,
5683 parent_ino, &local_index, name, name_len);
5684
5685 } else if (add_backref) {
5686 u64 local_index;
5687 int err;
5688
5689 err = btrfs_del_inode_ref(trans, root, name, name_len,
5690 ino, parent_ino, &local_index);
5691 }
5692 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005693}
5694
5695static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005696 struct inode *dir, struct dentry *dentry,
5697 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005698{
Josef Bacika1b075d2010-11-19 20:36:11 +00005699 int err = btrfs_add_link(trans, dir, inode,
5700 dentry->d_name.name, dentry->d_name.len,
5701 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005702 if (err > 0)
5703 err = -EEXIST;
5704 return err;
5705}
5706
Josef Bacik618e21d2007-07-11 10:18:17 -04005707static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005708 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005709{
5710 struct btrfs_trans_handle *trans;
5711 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005712 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005713 int err;
5714 int drop_inode = 0;
5715 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005716 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005717
5718 if (!new_valid_dev(rdev))
5719 return -EINVAL;
5720
Josef Bacik9ed74f22009-09-11 16:12:44 -04005721 /*
5722 * 2 for inode item and ref
5723 * 2 for dir items
5724 * 1 for xattr if selinux is on
5725 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005726 trans = btrfs_start_transaction(root, 5);
5727 if (IS_ERR(trans))
5728 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005729
Li Zefan581bb052011-04-20 10:06:11 +08005730 err = btrfs_find_free_ino(root, &objectid);
5731 if (err)
5732 goto out_unlock;
5733
Josef Bacikaec74772008-07-24 12:12:38 -04005734 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005735 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005736 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005737 if (IS_ERR(inode)) {
5738 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005739 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005740 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005741
Eric Paris2a7dba32011-02-01 11:05:39 -05005742 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005743 if (err) {
5744 drop_inode = 1;
5745 goto out_unlock;
5746 }
5747
Casey Schauflerad19db72011-12-15 10:09:07 -05005748 /*
5749 * If the active LSM wants to access the inode during
5750 * d_instantiate it needs these. Smack checks to see
5751 * if the filesystem supports xattrs by looking at the
5752 * ops vector.
5753 */
5754
5755 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005756 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005757 if (err)
5758 drop_inode = 1;
5759 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005760 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005761 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005762 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005763 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005764out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005765 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005766 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005767 if (drop_inode) {
5768 inode_dec_link_count(inode);
5769 iput(inode);
5770 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005771 return err;
5772}
5773
Chris Mason39279cc2007-06-12 06:35:45 -04005774static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005775 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005776{
5777 struct btrfs_trans_handle *trans;
5778 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005779 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005780 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005781 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005782 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005783 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005784
Josef Bacik9ed74f22009-09-11 16:12:44 -04005785 /*
5786 * 2 for inode item and ref
5787 * 2 for dir items
5788 * 1 for xattr if selinux is on
5789 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005790 trans = btrfs_start_transaction(root, 5);
5791 if (IS_ERR(trans))
5792 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005793
Li Zefan581bb052011-04-20 10:06:11 +08005794 err = btrfs_find_free_ino(root, &objectid);
5795 if (err)
5796 goto out_unlock;
5797
Josef Bacikaec74772008-07-24 12:12:38 -04005798 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005799 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005800 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005801 if (IS_ERR(inode)) {
5802 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005803 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005804 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005805 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005806
Eric Paris2a7dba32011-02-01 11:05:39 -05005807 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005808 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005809 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005810
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005811 err = btrfs_update_inode(trans, root, inode);
5812 if (err)
5813 goto out_unlock;
5814
Casey Schauflerad19db72011-12-15 10:09:07 -05005815 /*
5816 * If the active LSM wants to access the inode during
5817 * d_instantiate it needs these. Smack checks to see
5818 * if the filesystem supports xattrs by looking at the
5819 * ops vector.
5820 */
5821 inode->i_fop = &btrfs_file_operations;
5822 inode->i_op = &btrfs_file_inode_operations;
5823
Josef Bacika1b075d2010-11-19 20:36:11 +00005824 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005825 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005826 goto out_unlock;
5827
5828 inode->i_mapping->a_ops = &btrfs_aops;
5829 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5830 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5831 d_instantiate(dentry, inode);
5832
Chris Mason39279cc2007-06-12 06:35:45 -04005833out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005834 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005835 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005836 inode_dec_link_count(inode);
5837 iput(inode);
5838 }
Liu Bob53d3f52012-11-14 14:34:34 +00005839 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005840 return err;
5841}
5842
5843static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5844 struct dentry *dentry)
5845{
5846 struct btrfs_trans_handle *trans;
5847 struct btrfs_root *root = BTRFS_I(dir)->root;
5848 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005849 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005850 int err;
5851 int drop_inode = 0;
5852
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005853 /* do not allow sys_link's with other subvols of the same device */
5854 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005855 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005856
Mark Fashehf1863732012-08-08 11:32:27 -07005857 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005858 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005859
Chris Mason3de45862008-11-17 21:02:50 -05005860 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005861 if (err)
5862 goto fail;
5863
Yan, Zhenga22285a2010-05-16 10:48:46 -04005864 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005865 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005866 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005867 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005868 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005869 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005870 if (IS_ERR(trans)) {
5871 err = PTR_ERR(trans);
5872 goto fail;
5873 }
Chris Mason5f39d392007-10-15 16:14:19 -04005874
Miao Xie31534952011-04-13 13:19:21 +08005875 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005876 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005877 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005878 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005879 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005880
Josef Bacika1b075d2010-11-19 20:36:11 +00005881 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005882
Yan, Zhenga5719522009-09-24 09:17:31 -04005883 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005884 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005885 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005886 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005887 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005888 if (err)
5889 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005890 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005891 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005892 }
Chris Mason39279cc2007-06-12 06:35:45 -04005893
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005894 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005895fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005896 if (drop_inode) {
5897 inode_dec_link_count(inode);
5898 iput(inode);
5899 }
Liu Bob53d3f52012-11-14 14:34:34 +00005900 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005901 return err;
5902}
5903
Al Viro18bb1db2011-07-26 01:41:39 -04005904static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005905{
Chris Masonb9d86662008-05-02 16:13:49 -04005906 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005907 struct btrfs_trans_handle *trans;
5908 struct btrfs_root *root = BTRFS_I(dir)->root;
5909 int err = 0;
5910 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005911 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005912 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005913
Josef Bacik9ed74f22009-09-11 16:12:44 -04005914 /*
5915 * 2 items for inode and ref
5916 * 2 items for dir items
5917 * 1 for xattr if selinux is on
5918 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005919 trans = btrfs_start_transaction(root, 5);
5920 if (IS_ERR(trans))
5921 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005922
Li Zefan581bb052011-04-20 10:06:11 +08005923 err = btrfs_find_free_ino(root, &objectid);
5924 if (err)
5925 goto out_fail;
5926
Josef Bacikaec74772008-07-24 12:12:38 -04005927 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005928 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005929 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005930 if (IS_ERR(inode)) {
5931 err = PTR_ERR(inode);
5932 goto out_fail;
5933 }
Chris Mason5f39d392007-10-15 16:14:19 -04005934
Chris Mason39279cc2007-06-12 06:35:45 -04005935 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005936
Eric Paris2a7dba32011-02-01 11:05:39 -05005937 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005938 if (err)
5939 goto out_fail;
5940
Chris Mason39279cc2007-06-12 06:35:45 -04005941 inode->i_op = &btrfs_dir_inode_operations;
5942 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005943
Chris Masondbe674a2008-07-17 12:54:05 -04005944 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005945 err = btrfs_update_inode(trans, root, inode);
5946 if (err)
5947 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005948
Josef Bacika1b075d2010-11-19 20:36:11 +00005949 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5950 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005951 if (err)
5952 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005953
Chris Mason39279cc2007-06-12 06:35:45 -04005954 d_instantiate(dentry, inode);
5955 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005956
5957out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005958 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005959 if (drop_on_err)
5960 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005961 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005962 return err;
5963}
5964
Chris Masond352ac62008-09-29 15:18:18 -04005965/* helper for btfs_get_extent. Given an existing extent in the tree,
5966 * and an extent that you want to insert, deal with overlap and insert
5967 * the new extent into the tree.
5968 */
Chris Mason3b951512008-04-17 11:29:12 -04005969static int merge_extent_mapping(struct extent_map_tree *em_tree,
5970 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005971 struct extent_map *em,
5972 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005973{
5974 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005975
Chris Masone6dcd2d2008-07-17 12:53:50 -04005976 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5977 start_diff = map_start - em->start;
5978 em->start = map_start;
5979 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005980 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5981 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005982 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005983 em->block_len -= start_diff;
5984 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005985 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005986}
5987
Chris Masonc8b97812008-10-29 14:49:59 -04005988static noinline int uncompress_inline(struct btrfs_path *path,
5989 struct inode *inode, struct page *page,
5990 size_t pg_offset, u64 extent_offset,
5991 struct btrfs_file_extent_item *item)
5992{
5993 int ret;
5994 struct extent_buffer *leaf = path->nodes[0];
5995 char *tmp;
5996 size_t max_size;
5997 unsigned long inline_size;
5998 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005999 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006000
6001 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006002 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006003 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6004 inline_size = btrfs_file_extent_inline_item_len(leaf,
6005 btrfs_item_nr(leaf, path->slots[0]));
6006 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006007 if (!tmp)
6008 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006009 ptr = btrfs_file_extent_inline_start(item);
6010
6011 read_extent_buffer(leaf, tmp, ptr, inline_size);
6012
Chris Mason5b050f02008-11-11 09:34:41 -05006013 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006014 ret = btrfs_decompress(compress_type, tmp, page,
6015 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006016 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006017 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006018 unsigned long copy_size = min_t(u64,
6019 PAGE_CACHE_SIZE - pg_offset,
6020 max_size - extent_offset);
6021 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006022 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006023 }
6024 kfree(tmp);
6025 return 0;
6026}
6027
Chris Masond352ac62008-09-29 15:18:18 -04006028/*
6029 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006030 * the ugly parts come from merging extents from the disk with the in-ram
6031 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006032 * where the in-ram extents might be locked pending data=ordered completion.
6033 *
6034 * This also copies inline extents directly into the page.
6035 */
Chris Masond3977122009-01-05 21:25:51 -05006036
Chris Masona52d9a82007-08-27 16:49:44 -04006037struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006038 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006039 int create)
6040{
6041 int ret;
6042 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006043 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006044 u64 extent_start = 0;
6045 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006046 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006047 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006048 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006049 struct btrfs_root *root = BTRFS_I(inode)->root;
6050 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006051 struct extent_buffer *leaf;
6052 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006053 struct extent_map *em = NULL;
6054 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006055 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006056 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006057 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006058
Chris Masona52d9a82007-08-27 16:49:44 -04006059again:
Chris Mason890871b2009-09-02 16:24:52 -04006060 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006061 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006062 if (em)
6063 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006064 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006065
Chris Masona52d9a82007-08-27 16:49:44 -04006066 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006067 if (em->start > start || em->start + em->len <= start)
6068 free_extent_map(em);
6069 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006070 free_extent_map(em);
6071 else
6072 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006073 }
David Sterba172ddd62011-04-21 00:48:27 +02006074 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006075 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006076 err = -ENOMEM;
6077 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006078 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006079 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006080 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006081 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006082 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006083 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006084
6085 if (!path) {
6086 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006087 if (!path) {
6088 err = -ENOMEM;
6089 goto out;
6090 }
6091 /*
6092 * Chances are we'll be called again, so go ahead and do
6093 * readahead
6094 */
6095 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006096 }
6097
Chris Mason179e29e2007-11-01 11:28:41 -04006098 ret = btrfs_lookup_file_extent(trans, root, path,
6099 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006100 if (ret < 0) {
6101 err = ret;
6102 goto out;
6103 }
6104
6105 if (ret != 0) {
6106 if (path->slots[0] == 0)
6107 goto not_found;
6108 path->slots[0]--;
6109 }
6110
Chris Mason5f39d392007-10-15 16:14:19 -04006111 leaf = path->nodes[0];
6112 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006113 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006114 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006115 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6116 found_type = btrfs_key_type(&found_key);
6117 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006118 found_type != BTRFS_EXTENT_DATA_KEY) {
6119 goto not_found;
6120 }
6121
Chris Mason5f39d392007-10-15 16:14:19 -04006122 found_type = btrfs_file_extent_type(leaf, item);
6123 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006124 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006125 if (found_type == BTRFS_FILE_EXTENT_REG ||
6126 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006127 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006128 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006129 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6130 size_t size;
6131 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006132 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006133 }
6134
6135 if (start >= extent_end) {
6136 path->slots[0]++;
6137 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6138 ret = btrfs_next_leaf(root, path);
6139 if (ret < 0) {
6140 err = ret;
6141 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006142 }
Yan Zheng9036c102008-10-30 14:19:41 -04006143 if (ret > 0)
6144 goto not_found;
6145 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006146 }
Yan Zheng9036c102008-10-30 14:19:41 -04006147 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6148 if (found_key.objectid != objectid ||
6149 found_key.type != BTRFS_EXTENT_DATA_KEY)
6150 goto not_found;
6151 if (start + len <= found_key.offset)
6152 goto not_found;
6153 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006154 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006155 em->len = found_key.offset - start;
6156 goto not_found_em;
6157 }
6158
Yan Zhengd899e052008-10-30 14:25:28 -04006159 if (found_type == BTRFS_FILE_EXTENT_REG ||
6160 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006161 em->start = extent_start;
6162 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006163 em->orig_start = extent_start -
6164 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006165 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6166 item);
Chris Masondb945352007-10-15 16:15:53 -04006167 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6168 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006169 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006170 goto insert;
6171 }
Li Zefan261507a02010-12-17 14:21:50 +08006172 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006173 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006174 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006175 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006176 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006177 } else {
6178 bytenr += btrfs_file_extent_offset(leaf, item);
6179 em->block_start = bytenr;
6180 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006181 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6182 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006183 }
Chris Masona52d9a82007-08-27 16:49:44 -04006184 goto insert;
6185 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006186 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006187 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006188 size_t size;
6189 size_t extent_offset;
6190 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006191
Chris Masona52d9a82007-08-27 16:49:44 -04006192 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006193 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006194 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006195 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006196 goto out;
6197 }
6198
Yan Zheng9036c102008-10-30 14:19:41 -04006199 size = btrfs_file_extent_inline_len(leaf, item);
6200 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006201 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006202 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006203 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006204 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006205 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006206 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006207 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006208 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006209 em->compress_type = compress_type;
6210 }
Yan689f9342007-10-29 11:41:07 -04006211 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006212 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006213 if (btrfs_file_extent_compression(leaf, item) !=
6214 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006215 ret = uncompress_inline(path, inode, page,
6216 pg_offset,
6217 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006218 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006219 } else {
6220 map = kmap(page);
6221 read_extent_buffer(leaf, map + pg_offset, ptr,
6222 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006223 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6224 memset(map + pg_offset + copy_size, 0,
6225 PAGE_CACHE_SIZE - pg_offset -
6226 copy_size);
6227 }
Chris Masonc8b97812008-10-29 14:49:59 -04006228 kunmap(page);
6229 }
Chris Mason179e29e2007-11-01 11:28:41 -04006230 flush_dcache_page(page);
6231 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006232 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006233 if (!trans) {
6234 kunmap(page);
6235 free_extent_map(em);
6236 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006237
David Sterbab3b4aa72011-04-21 01:20:15 +02006238 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006239 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006240
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006241 if (IS_ERR(trans))
6242 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006243 goto again;
6244 }
Chris Masonc8b97812008-10-29 14:49:59 -04006245 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006246 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006247 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006248 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006249 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006250 }
Chris Masond1310b22008-01-24 16:13:08 -05006251 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006252 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006253 goto insert;
6254 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006255 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006256 }
6257not_found:
6258 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006259 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006260 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006261not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006262 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006263 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006264insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006265 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006266 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006267 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6268 (unsigned long long)em->start,
6269 (unsigned long long)em->len,
6270 (unsigned long long)start,
6271 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006272 err = -EIO;
6273 goto out;
6274 }
Chris Masond1310b22008-01-24 16:13:08 -05006275
6276 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006277 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006278 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04006279 /* it is possible that someone inserted the extent into the tree
6280 * while we had the lock dropped. It is also possible that
6281 * an overlapping map exists in the tree
6282 */
Chris Masona52d9a82007-08-27 16:49:44 -04006283 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006284 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006285
6286 ret = 0;
6287
Chris Mason3b951512008-04-17 11:29:12 -04006288 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006289 if (existing && (existing->start > start ||
6290 existing->start + existing->len <= start)) {
6291 free_extent_map(existing);
6292 existing = NULL;
6293 }
Chris Mason3b951512008-04-17 11:29:12 -04006294 if (!existing) {
6295 existing = lookup_extent_mapping(em_tree, em->start,
6296 em->len);
6297 if (existing) {
6298 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006299 em, start,
6300 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006301 free_extent_map(existing);
6302 if (err) {
6303 free_extent_map(em);
6304 em = NULL;
6305 }
6306 } else {
6307 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006308 free_extent_map(em);
6309 em = NULL;
6310 }
6311 } else {
6312 free_extent_map(em);
6313 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006314 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006315 }
Chris Masona52d9a82007-08-27 16:49:44 -04006316 }
Chris Mason890871b2009-09-02 16:24:52 -04006317 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006318out:
liubo1abe9b82011-03-24 11:18:59 +00006319
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006320 if (em)
6321 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006322
Chris Masonf4219502008-07-22 11:18:09 -04006323 if (path)
6324 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006325 if (trans) {
6326 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006327 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006328 err = ret;
6329 }
Chris Masona52d9a82007-08-27 16:49:44 -04006330 if (err) {
6331 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006332 return ERR_PTR(err);
6333 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006334 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006335 return em;
6336}
6337
Chris Masonec29ed52011-02-23 16:23:20 -05006338struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6339 size_t pg_offset, u64 start, u64 len,
6340 int create)
6341{
6342 struct extent_map *em;
6343 struct extent_map *hole_em = NULL;
6344 u64 range_start = start;
6345 u64 end;
6346 u64 found;
6347 u64 found_end;
6348 int err = 0;
6349
6350 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6351 if (IS_ERR(em))
6352 return em;
6353 if (em) {
6354 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006355 * if our em maps to
6356 * - a hole or
6357 * - a pre-alloc extent,
6358 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006359 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006360 if (em->block_start != EXTENT_MAP_HOLE &&
6361 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006362 return em;
6363 else
6364 hole_em = em;
6365 }
6366
6367 /* check to see if we've wrapped (len == -1 or similar) */
6368 end = start + len;
6369 if (end < start)
6370 end = (u64)-1;
6371 else
6372 end -= 1;
6373
6374 em = NULL;
6375
6376 /* ok, we didn't find anything, lets look for delalloc */
6377 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6378 end, len, EXTENT_DELALLOC, 1);
6379 found_end = range_start + found;
6380 if (found_end < range_start)
6381 found_end = (u64)-1;
6382
6383 /*
6384 * we didn't find anything useful, return
6385 * the original results from get_extent()
6386 */
6387 if (range_start > end || found_end <= start) {
6388 em = hole_em;
6389 hole_em = NULL;
6390 goto out;
6391 }
6392
6393 /* adjust the range_start to make sure it doesn't
6394 * go backwards from the start they passed in
6395 */
6396 range_start = max(start,range_start);
6397 found = found_end - range_start;
6398
6399 if (found > 0) {
6400 u64 hole_start = start;
6401 u64 hole_len = len;
6402
David Sterba172ddd62011-04-21 00:48:27 +02006403 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006404 if (!em) {
6405 err = -ENOMEM;
6406 goto out;
6407 }
6408 /*
6409 * when btrfs_get_extent can't find anything it
6410 * returns one huge hole
6411 *
6412 * make sure what it found really fits our range, and
6413 * adjust to make sure it is based on the start from
6414 * the caller
6415 */
6416 if (hole_em) {
6417 u64 calc_end = extent_map_end(hole_em);
6418
6419 if (calc_end <= start || (hole_em->start > end)) {
6420 free_extent_map(hole_em);
6421 hole_em = NULL;
6422 } else {
6423 hole_start = max(hole_em->start, start);
6424 hole_len = calc_end - hole_start;
6425 }
6426 }
6427 em->bdev = NULL;
6428 if (hole_em && range_start > hole_start) {
6429 /* our hole starts before our delalloc, so we
6430 * have to return just the parts of the hole
6431 * that go until the delalloc starts
6432 */
6433 em->len = min(hole_len,
6434 range_start - hole_start);
6435 em->start = hole_start;
6436 em->orig_start = hole_start;
6437 /*
6438 * don't adjust block start at all,
6439 * it is fixed at EXTENT_MAP_HOLE
6440 */
6441 em->block_start = hole_em->block_start;
6442 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006443 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6444 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006445 } else {
6446 em->start = range_start;
6447 em->len = found;
6448 em->orig_start = range_start;
6449 em->block_start = EXTENT_MAP_DELALLOC;
6450 em->block_len = found;
6451 }
6452 } else if (hole_em) {
6453 return hole_em;
6454 }
6455out:
6456
6457 free_extent_map(hole_em);
6458 if (err) {
6459 free_extent_map(em);
6460 return ERR_PTR(err);
6461 }
6462 return em;
6463}
6464
Josef Bacik4b46fce2010-05-23 11:00:55 -04006465static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6466 u64 start, u64 len)
6467{
6468 struct btrfs_root *root = BTRFS_I(inode)->root;
6469 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006470 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006471 struct btrfs_key ins;
6472 u64 alloc_hint;
6473 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006474
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006475 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006476 if (IS_ERR(trans))
6477 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006478
6479 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6480
6481 alloc_hint = get_extent_allocation_hint(inode, start, len);
6482 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006483 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006484 if (ret) {
6485 em = ERR_PTR(ret);
6486 goto out;
6487 }
6488
Josef Bacik70c8a912012-10-11 16:54:30 -04006489 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
6490 ins.offset, ins.offset, 0);
6491 if (IS_ERR(em))
6492 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006493
6494 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6495 ins.offset, ins.offset, 0);
6496 if (ret) {
6497 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6498 em = ERR_PTR(ret);
6499 }
6500out:
6501 btrfs_end_transaction(trans, root);
6502 return em;
6503}
6504
Chris Mason46bfbb52010-05-26 11:04:10 -04006505/*
6506 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6507 * block must be cow'd
6508 */
6509static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
6510 struct inode *inode, u64 offset, u64 len)
6511{
6512 struct btrfs_path *path;
6513 int ret;
6514 struct extent_buffer *leaf;
6515 struct btrfs_root *root = BTRFS_I(inode)->root;
6516 struct btrfs_file_extent_item *fi;
6517 struct btrfs_key key;
6518 u64 disk_bytenr;
6519 u64 backref_offset;
6520 u64 extent_end;
6521 u64 num_bytes;
6522 int slot;
6523 int found_type;
6524
6525 path = btrfs_alloc_path();
6526 if (!path)
6527 return -ENOMEM;
6528
Li Zefan33345d012011-04-20 10:31:50 +08006529 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006530 offset, 0);
6531 if (ret < 0)
6532 goto out;
6533
6534 slot = path->slots[0];
6535 if (ret == 1) {
6536 if (slot == 0) {
6537 /* can't find the item, must cow */
6538 ret = 0;
6539 goto out;
6540 }
6541 slot--;
6542 }
6543 ret = 0;
6544 leaf = path->nodes[0];
6545 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006546 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006547 key.type != BTRFS_EXTENT_DATA_KEY) {
6548 /* not our file or wrong item type, must cow */
6549 goto out;
6550 }
6551
6552 if (key.offset > offset) {
6553 /* Wrong offset, must cow */
6554 goto out;
6555 }
6556
6557 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6558 found_type = btrfs_file_extent_type(leaf, fi);
6559 if (found_type != BTRFS_FILE_EXTENT_REG &&
6560 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6561 /* not a regular extent, must cow */
6562 goto out;
6563 }
6564 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6565 backref_offset = btrfs_file_extent_offset(leaf, fi);
6566
6567 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6568 if (extent_end < offset + len) {
6569 /* extent doesn't include our full range, must cow */
6570 goto out;
6571 }
6572
6573 if (btrfs_extent_readonly(root, disk_bytenr))
6574 goto out;
6575
6576 /*
6577 * look for other files referencing this extent, if we
6578 * find any we must cow
6579 */
Li Zefan33345d012011-04-20 10:31:50 +08006580 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006581 key.offset - backref_offset, disk_bytenr))
6582 goto out;
6583
6584 /*
6585 * adjust disk_bytenr and num_bytes to cover just the bytes
6586 * in this extent we are about to write. If there
6587 * are any csums in that range we have to cow in order
6588 * to keep the csums correct
6589 */
6590 disk_bytenr += backref_offset;
6591 disk_bytenr += offset - key.offset;
6592 num_bytes = min(offset + len, extent_end) - offset;
6593 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6594 goto out;
6595 /*
6596 * all of the above have passed, it is safe to overwrite this extent
6597 * without cow
6598 */
6599 ret = 1;
6600out:
6601 btrfs_free_path(path);
6602 return ret;
6603}
6604
Josef Bacikeb838e72012-07-31 16:28:48 -04006605static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6606 struct extent_state **cached_state, int writing)
6607{
6608 struct btrfs_ordered_extent *ordered;
6609 int ret = 0;
6610
6611 while (1) {
6612 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6613 0, cached_state);
6614 /*
6615 * We're concerned with the entire range that we're going to be
6616 * doing DIO to, so we need to make sure theres no ordered
6617 * extents in this range.
6618 */
6619 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6620 lockend - lockstart + 1);
6621
6622 /*
6623 * We need to make sure there are no buffered pages in this
6624 * range either, we could have raced between the invalidate in
6625 * generic_file_direct_write and locking the extent. The
6626 * invalidate needs to happen so that reads after a write do not
6627 * get stale data.
6628 */
6629 if (!ordered && (!writing ||
6630 !test_range_bit(&BTRFS_I(inode)->io_tree,
6631 lockstart, lockend, EXTENT_UPTODATE, 0,
6632 *cached_state)))
6633 break;
6634
6635 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6636 cached_state, GFP_NOFS);
6637
6638 if (ordered) {
6639 btrfs_start_ordered_extent(inode, ordered, 1);
6640 btrfs_put_ordered_extent(ordered);
6641 } else {
6642 /* Screw you mmap */
6643 ret = filemap_write_and_wait_range(inode->i_mapping,
6644 lockstart,
6645 lockend);
6646 if (ret)
6647 break;
6648
6649 /*
6650 * If we found a page that couldn't be invalidated just
6651 * fall back to buffered.
6652 */
6653 ret = invalidate_inode_pages2_range(inode->i_mapping,
6654 lockstart >> PAGE_CACHE_SHIFT,
6655 lockend >> PAGE_CACHE_SHIFT);
6656 if (ret)
6657 break;
6658 }
6659
6660 cond_resched();
6661 }
6662
6663 return ret;
6664}
6665
Josef Bacik69ffb542012-09-11 15:40:07 -04006666static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6667 u64 len, u64 orig_start,
6668 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05006669 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006670{
6671 struct extent_map_tree *em_tree;
6672 struct extent_map *em;
6673 struct btrfs_root *root = BTRFS_I(inode)->root;
6674 int ret;
6675
6676 em_tree = &BTRFS_I(inode)->extent_tree;
6677 em = alloc_extent_map();
6678 if (!em)
6679 return ERR_PTR(-ENOMEM);
6680
6681 em->start = start;
6682 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006683 em->mod_start = start;
6684 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006685 em->len = len;
6686 em->block_len = block_len;
6687 em->block_start = block_start;
6688 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006689 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006690 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006691 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6692 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006693 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006694
6695 do {
6696 btrfs_drop_extent_cache(inode, em->start,
6697 em->start + em->len - 1, 0);
6698 write_lock(&em_tree->lock);
6699 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04006700 if (!ret)
6701 list_move(&em->list,
6702 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006703 write_unlock(&em_tree->lock);
6704 } while (ret == -EEXIST);
6705
6706 if (ret) {
6707 free_extent_map(em);
6708 return ERR_PTR(ret);
6709 }
6710
6711 return em;
6712}
6713
6714
Josef Bacik4b46fce2010-05-23 11:00:55 -04006715static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6716 struct buffer_head *bh_result, int create)
6717{
6718 struct extent_map *em;
6719 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006720 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006721 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006722 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006723 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006724 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006725 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006726 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006727
Miao Xie172a5042013-02-21 02:48:22 -07006728 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006729 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006730 else
Josef Bacikc3298612012-08-03 16:49:19 -04006731 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006732
Josef Bacikc3298612012-08-03 16:49:19 -04006733 lockstart = start;
6734 lockend = start + len - 1;
6735
Josef Bacikeb838e72012-07-31 16:28:48 -04006736 /*
6737 * If this errors out it's because we couldn't invalidate pagecache for
6738 * this range and we need to fallback to buffered.
6739 */
6740 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6741 return -ENOTBLK;
6742
Josef Bacik4b46fce2010-05-23 11:00:55 -04006743 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006744 if (IS_ERR(em)) {
6745 ret = PTR_ERR(em);
6746 goto unlock_err;
6747 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006748
6749 /*
6750 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6751 * io. INLINE is special, and we could probably kludge it in here, but
6752 * it's still buffered so for safety lets just fall back to the generic
6753 * buffered path.
6754 *
6755 * For COMPRESSED we _have_ to read the entire extent in so we can
6756 * decompress it, so there will be buffering required no matter what we
6757 * do, so go ahead and fallback to buffered.
6758 *
6759 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6760 * to buffered IO. Don't blame me, this is the price we pay for using
6761 * the generic code.
6762 */
6763 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6764 em->block_start == EXTENT_MAP_INLINE) {
6765 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006766 ret = -ENOTBLK;
6767 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006768 }
6769
6770 /* Just a good old fashioned hole, return */
6771 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6772 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6773 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006774 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006775 }
6776
6777 /*
6778 * We don't allocate a new extent in the following cases
6779 *
6780 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6781 * existing extent.
6782 * 2) The extent is marked as PREALLOC. We're good to go here and can
6783 * just use the extent.
6784 *
6785 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006786 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006787 len = min(len, em->len - (start - em->start));
6788 lockstart = start + len;
6789 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006790 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006791
6792 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6793 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6794 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006795 int type;
6796 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006797 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006798
6799 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6800 type = BTRFS_ORDERED_PREALLOC;
6801 else
6802 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006803 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006804 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006805
6806 /*
6807 * we're not going to log anything, but we do need
6808 * to make sure the current transaction stays open
6809 * while we look for nocow cross refs
6810 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006811 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006812 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006813 goto must_cow;
6814
6815 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006816 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006817 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006818
6819 if (type == BTRFS_ORDERED_PREALLOC) {
6820 free_extent_map(em);
6821 em = create_pinned_em(inode, start, len,
6822 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006823 block_start, len,
6824 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006825 if (IS_ERR(em)) {
6826 btrfs_end_transaction(trans, root);
6827 goto unlock_err;
6828 }
6829 }
6830
Chris Mason46bfbb52010-05-26 11:04:10 -04006831 ret = btrfs_add_ordered_extent_dio(inode, start,
6832 block_start, len, len, type);
6833 btrfs_end_transaction(trans, root);
6834 if (ret) {
6835 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006836 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006837 }
6838 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006839 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006840 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006841 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006842must_cow:
6843 /*
6844 * this will cow the extent, reset the len in case we changed
6845 * it above
6846 */
6847 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006848 free_extent_map(em);
6849 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006850 if (IS_ERR(em)) {
6851 ret = PTR_ERR(em);
6852 goto unlock_err;
6853 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006854 len = min(len, em->len - (start - em->start));
6855unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006856 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6857 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006858 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006859 bh_result->b_bdev = em->bdev;
6860 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006861 if (create) {
6862 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6863 set_buffer_new(bh_result);
6864
6865 /*
6866 * Need to update the i_size under the extent lock so buffered
6867 * readers will get the updated i_size when we unlock.
6868 */
6869 if (start + len > i_size_read(inode))
6870 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006871
Miao Xie172a5042013-02-21 02:48:22 -07006872 spin_lock(&BTRFS_I(inode)->lock);
6873 BTRFS_I(inode)->outstanding_extents++;
6874 spin_unlock(&BTRFS_I(inode)->lock);
6875
Miao Xie09348562013-02-07 10:12:07 +00006876 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6877 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6878 &cached_state, GFP_NOFS);
6879 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006880 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006881
Josef Bacikeb838e72012-07-31 16:28:48 -04006882 /*
6883 * In the case of write we need to clear and unlock the entire range,
6884 * in the case of read we need to unlock only the end area that we
6885 * aren't using if there is any left over space.
6886 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006887 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006888 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6889 lockend, unlock_bits, 1, 0,
6890 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006891 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006892 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006893 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006894
Josef Bacik4b46fce2010-05-23 11:00:55 -04006895 free_extent_map(em);
6896
6897 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006898
6899unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006900 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6901 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6902 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006903}
6904
6905struct btrfs_dio_private {
6906 struct inode *inode;
6907 u64 logical_offset;
6908 u64 disk_bytenr;
6909 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006910 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006911
6912 /* number of bios pending for this dio */
6913 atomic_t pending_bios;
6914
6915 /* IO errors */
6916 int errors;
6917
6918 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006919};
6920
6921static void btrfs_endio_direct_read(struct bio *bio, int err)
6922{
Miao Xiee65e1532010-11-22 03:04:43 +00006923 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006924 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6925 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006926 struct inode *inode = dip->inode;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006927 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006928 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006929
6930 start = dip->logical_offset;
6931 do {
6932 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6933 struct page *page = bvec->bv_page;
6934 char *kaddr;
6935 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006936 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006937 unsigned long flags;
6938
Josef Bacikc3298612012-08-03 16:49:19 -04006939 if (get_state_private(&BTRFS_I(inode)->io_tree,
6940 start, &private))
6941 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006942 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006943 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006944 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006945 csum, bvec->bv_len);
6946 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006947 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006948 local_irq_restore(flags);
6949
6950 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006951 if (csum != private) {
6952failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006953 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6954 (unsigned long long)btrfs_ino(inode),
6955 (unsigned long long)start,
6956 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006957 err = -EIO;
6958 }
6959 }
6960
6961 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006962 bvec++;
6963 } while (bvec <= bvec_end);
6964
6965 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006966 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006967 bio->bi_private = dip->private;
6968
Josef Bacik4b46fce2010-05-23 11:00:55 -04006969 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006970
6971 /* If we had a csum failure make sure to clear the uptodate flag */
6972 if (err)
6973 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006974 dio_end_io(bio, err);
6975}
6976
6977static void btrfs_endio_direct_write(struct bio *bio, int err)
6978{
6979 struct btrfs_dio_private *dip = bio->bi_private;
6980 struct inode *inode = dip->inode;
6981 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006982 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006983 u64 ordered_offset = dip->logical_offset;
6984 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006985 int ret;
6986
6987 if (err)
6988 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006989again:
6990 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6991 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006992 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006993 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006994 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006995
Josef Bacik5fd02042012-05-02 14:00:54 -04006996 ordered->work.func = finish_ordered_fn;
6997 ordered->work.flags = 0;
6998 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6999 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007000out_test:
7001 /*
7002 * our bio might span multiple ordered extents. If we haven't
7003 * completed the accounting for the whole dio, go back and try again
7004 */
7005 if (ordered_offset < dip->logical_offset + dip->bytes) {
7006 ordered_bytes = dip->logical_offset + dip->bytes -
7007 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007008 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007009 goto again;
7010 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007011out_done:
7012 bio->bi_private = dip->private;
7013
Josef Bacik4b46fce2010-05-23 11:00:55 -04007014 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007015
7016 /* If we had an error make sure to clear the uptodate flag */
7017 if (err)
7018 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007019 dio_end_io(bio, err);
7020}
7021
Chris Masoneaf25d92010-05-25 09:48:28 -04007022static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7023 struct bio *bio, int mirror_num,
7024 unsigned long bio_flags, u64 offset)
7025{
7026 int ret;
7027 struct btrfs_root *root = BTRFS_I(inode)->root;
7028 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007029 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007030 return 0;
7031}
7032
Miao Xiee65e1532010-11-22 03:04:43 +00007033static void btrfs_end_dio_bio(struct bio *bio, int err)
7034{
7035 struct btrfs_dio_private *dip = bio->bi_private;
7036
7037 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007038 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007039 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007040 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007041 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007042 dip->errors = 1;
7043
7044 /*
7045 * before atomic variable goto zero, we must make sure
7046 * dip->errors is perceived to be set.
7047 */
7048 smp_mb__before_atomic_dec();
7049 }
7050
7051 /* if there are more bios still pending for this dio, just exit */
7052 if (!atomic_dec_and_test(&dip->pending_bios))
7053 goto out;
7054
7055 if (dip->errors)
7056 bio_io_error(dip->orig_bio);
7057 else {
7058 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
7059 bio_endio(dip->orig_bio, 0);
7060 }
7061out:
7062 bio_put(bio);
7063}
7064
7065static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7066 u64 first_sector, gfp_t gfp_flags)
7067{
7068 int nr_vecs = bio_get_nr_vecs(bdev);
7069 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7070}
7071
7072static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7073 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007074 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007075{
7076 int write = rw & REQ_WRITE;
7077 struct btrfs_root *root = BTRFS_I(inode)->root;
7078 int ret;
7079
Josef Bacikb812ce22012-11-16 13:56:32 -05007080 if (async_submit)
7081 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7082
Miao Xiee65e1532010-11-22 03:04:43 +00007083 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007084
7085 if (!write) {
7086 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7087 if (ret)
7088 goto err;
7089 }
Miao Xiee65e1532010-11-22 03:04:43 +00007090
Josef Bacik1ae39932011-04-06 14:41:34 -04007091 if (skip_sum)
7092 goto map;
7093
7094 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007095 ret = btrfs_wq_submit_bio(root->fs_info,
7096 inode, rw, bio, 0, 0,
7097 file_offset,
7098 __btrfs_submit_bio_start_direct_io,
7099 __btrfs_submit_bio_done);
7100 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007101 } else if (write) {
7102 /*
7103 * If we aren't doing async submit, calculate the csum of the
7104 * bio now.
7105 */
7106 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7107 if (ret)
7108 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007109 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007110 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007111 if (ret)
7112 goto err;
7113 }
Miao Xiee65e1532010-11-22 03:04:43 +00007114
Josef Bacik1ae39932011-04-06 14:41:34 -04007115map:
7116 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007117err:
7118 bio_put(bio);
7119 return ret;
7120}
7121
7122static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7123 int skip_sum)
7124{
7125 struct inode *inode = dip->inode;
7126 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007127 struct bio *bio;
7128 struct bio *orig_bio = dip->orig_bio;
7129 struct bio_vec *bvec = orig_bio->bi_io_vec;
7130 u64 start_sector = orig_bio->bi_sector;
7131 u64 file_offset = dip->logical_offset;
7132 u64 submit_len = 0;
7133 u64 map_length;
7134 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007135 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007136 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007137
Miao Xiee65e1532010-11-22 03:04:43 +00007138 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007139 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007140 &map_length, NULL, 0);
7141 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007142 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007143 return -EIO;
7144 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007145 if (map_length >= orig_bio->bi_size) {
7146 bio = orig_bio;
7147 goto submit;
7148 }
7149
David Woodhouse53b381b2013-01-29 18:40:14 -05007150 /* async crcs make it difficult to collect full stripe writes. */
7151 if (btrfs_get_alloc_profile(root, 1) &
7152 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7153 async_submit = 0;
7154 else
7155 async_submit = 1;
7156
Josef Bacik02f57c72011-04-06 14:25:44 -04007157 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7158 if (!bio)
7159 return -ENOMEM;
7160 bio->bi_private = dip;
7161 bio->bi_end_io = btrfs_end_dio_bio;
7162 atomic_inc(&dip->pending_bios);
7163
Miao Xiee65e1532010-11-22 03:04:43 +00007164 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7165 if (unlikely(map_length < submit_len + bvec->bv_len ||
7166 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7167 bvec->bv_offset) < bvec->bv_len)) {
7168 /*
7169 * inc the count before we submit the bio so
7170 * we know the end IO handler won't happen before
7171 * we inc the count. Otherwise, the dip might get freed
7172 * before we're done setting it up
7173 */
7174 atomic_inc(&dip->pending_bios);
7175 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7176 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007177 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007178 if (ret) {
7179 bio_put(bio);
7180 atomic_dec(&dip->pending_bios);
7181 goto out_err;
7182 }
7183
Miao Xiee65e1532010-11-22 03:04:43 +00007184 start_sector += submit_len >> 9;
7185 file_offset += submit_len;
7186
7187 submit_len = 0;
7188 nr_pages = 0;
7189
7190 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7191 start_sector, GFP_NOFS);
7192 if (!bio)
7193 goto out_err;
7194 bio->bi_private = dip;
7195 bio->bi_end_io = btrfs_end_dio_bio;
7196
7197 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007198 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007199 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007200 &map_length, NULL, 0);
7201 if (ret) {
7202 bio_put(bio);
7203 goto out_err;
7204 }
7205 } else {
7206 submit_len += bvec->bv_len;
7207 nr_pages ++;
7208 bvec++;
7209 }
7210 }
7211
Josef Bacik02f57c72011-04-06 14:25:44 -04007212submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007213 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007214 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007215 if (!ret)
7216 return 0;
7217
7218 bio_put(bio);
7219out_err:
7220 dip->errors = 1;
7221 /*
7222 * before atomic variable goto zero, we must
7223 * make sure dip->errors is perceived to be set.
7224 */
7225 smp_mb__before_atomic_dec();
7226 if (atomic_dec_and_test(&dip->pending_bios))
7227 bio_io_error(dip->orig_bio);
7228
7229 /* bio_end_io() will handle error, so we needn't return it */
7230 return 0;
7231}
7232
Josef Bacik4b46fce2010-05-23 11:00:55 -04007233static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
7234 loff_t file_offset)
7235{
7236 struct btrfs_root *root = BTRFS_I(inode)->root;
7237 struct btrfs_dio_private *dip;
7238 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007239 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007240 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007241 int ret = 0;
7242
7243 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7244
7245 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7246 if (!dip) {
7247 ret = -ENOMEM;
7248 goto free_ordered;
7249 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007250
7251 dip->private = bio->bi_private;
7252 dip->inode = inode;
7253 dip->logical_offset = file_offset;
7254
Josef Bacik4b46fce2010-05-23 11:00:55 -04007255 dip->bytes = 0;
7256 do {
7257 dip->bytes += bvec->bv_len;
7258 bvec++;
7259 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
7260
Chris Mason46bfbb52010-05-26 11:04:10 -04007261 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007262 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007263 dip->errors = 0;
7264 dip->orig_bio = bio;
7265 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266
7267 if (write)
7268 bio->bi_end_io = btrfs_endio_direct_write;
7269 else
7270 bio->bi_end_io = btrfs_endio_direct_read;
7271
Miao Xiee65e1532010-11-22 03:04:43 +00007272 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7273 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007274 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007275free_ordered:
7276 /*
7277 * If this is a write, we need to clean up the reserved space and kill
7278 * the ordered extent.
7279 */
7280 if (write) {
7281 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007282 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7284 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7285 btrfs_free_reserved_extent(root, ordered->start,
7286 ordered->disk_len);
7287 btrfs_put_ordered_extent(ordered);
7288 btrfs_put_ordered_extent(ordered);
7289 }
7290 bio_endio(bio, ret);
7291}
7292
Chris Mason5a5f79b2010-05-26 21:33:37 -04007293static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7294 const struct iovec *iov, loff_t offset,
7295 unsigned long nr_segs)
7296{
7297 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007298 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007299 size_t size;
7300 unsigned long addr;
7301 unsigned blocksize_mask = root->sectorsize - 1;
7302 ssize_t retval = -EINVAL;
7303 loff_t end = offset;
7304
7305 if (offset & blocksize_mask)
7306 goto out;
7307
7308 /* Check the memory alignment. Blocks cannot straddle pages */
7309 for (seg = 0; seg < nr_segs; seg++) {
7310 addr = (unsigned long)iov[seg].iov_base;
7311 size = iov[seg].iov_len;
7312 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007313 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007314 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007315
7316 /* If this is a write we don't need to check anymore */
7317 if (rw & WRITE)
7318 continue;
7319
7320 /*
7321 * Check to make sure we don't have duplicate iov_base's in this
7322 * iovec, if so return EINVAL, otherwise we'll get csum errors
7323 * when reading back.
7324 */
7325 for (i = seg + 1; i < nr_segs; i++) {
7326 if (iov[seg].iov_base == iov[i].iov_base)
7327 goto out;
7328 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007329 }
7330 retval = 0;
7331out:
7332 return retval;
7333}
Josef Bacikeb838e72012-07-31 16:28:48 -04007334
Chris Mason16432982008-04-10 10:23:21 -04007335static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7336 const struct iovec *iov, loff_t offset,
7337 unsigned long nr_segs)
7338{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007339 struct file *file = iocb->ki_filp;
7340 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007341 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007342 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007343 bool wakeup = true;
7344 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007345 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007346
Chris Mason5a5f79b2010-05-26 21:33:37 -04007347 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007348 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007349 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007350
Miao Xie38851cc2013-02-08 07:04:11 +00007351 atomic_inc(&inode->i_dio_count);
7352 smp_mb__after_atomic_inc();
7353
Miao Xie09348562013-02-07 10:12:07 +00007354 if (rw & WRITE) {
7355 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007356 /*
7357 * If the write DIO is beyond the EOF, we need update
7358 * the isize, but it is protected by i_mutex. So we can
7359 * not unlock the i_mutex at this case.
7360 */
7361 if (offset + count <= inode->i_size) {
7362 mutex_unlock(&inode->i_mutex);
7363 relock = true;
7364 }
Miao Xie09348562013-02-07 10:12:07 +00007365 ret = btrfs_delalloc_reserve_space(inode, count);
7366 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007367 goto out;
7368 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7369 &BTRFS_I(inode)->runtime_flags))) {
7370 inode_dio_done(inode);
7371 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7372 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007373 }
7374
7375 ret = __blockdev_direct_IO(rw, iocb, inode,
7376 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7377 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007378 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007379 if (rw & WRITE) {
7380 if (ret < 0 && ret != -EIOCBQUEUED)
7381 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007382 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007383 btrfs_delalloc_release_space(inode,
7384 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007385 else
7386 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007387 }
Miao Xie38851cc2013-02-08 07:04:11 +00007388out:
Miao Xie2e60a512013-02-08 07:01:08 +00007389 if (wakeup)
7390 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007391 if (relock)
7392 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007393
7394 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007395}
7396
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007397#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7398
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007399static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7400 __u64 start, __u64 len)
7401{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007402 int ret;
7403
7404 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7405 if (ret)
7406 return ret;
7407
Chris Masonec29ed52011-02-23 16:23:20 -05007408 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007409}
7410
Chris Mason9ebefb182007-06-15 13:50:00 -04007411int btrfs_readpage(struct file *file, struct page *page)
7412{
Chris Masond1310b22008-01-24 16:13:08 -05007413 struct extent_io_tree *tree;
7414 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007415 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007416}
Chris Mason1832a6d2007-12-21 16:27:21 -05007417
Chris Mason39279cc2007-06-12 06:35:45 -04007418static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7419{
Chris Masond1310b22008-01-24 16:13:08 -05007420 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04007421
7422
7423 if (current->flags & PF_MEMALLOC) {
7424 redirty_page_for_writepage(wbc, page);
7425 unlock_page(page);
7426 return 0;
7427 }
Chris Masond1310b22008-01-24 16:13:08 -05007428 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007429 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7430}
Chris Mason39279cc2007-06-12 06:35:45 -04007431
Chris Masonf4219502008-07-22 11:18:09 -04007432int btrfs_writepages(struct address_space *mapping,
7433 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007434{
Chris Masond1310b22008-01-24 16:13:08 -05007435 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007436
Chris Masond1310b22008-01-24 16:13:08 -05007437 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007438 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7439}
7440
Chris Mason3ab2fb52007-11-08 10:59:22 -05007441static int
7442btrfs_readpages(struct file *file, struct address_space *mapping,
7443 struct list_head *pages, unsigned nr_pages)
7444{
Chris Masond1310b22008-01-24 16:13:08 -05007445 struct extent_io_tree *tree;
7446 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007447 return extent_readpages(tree, mapping, pages, nr_pages,
7448 btrfs_get_extent);
7449}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007450static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007451{
Chris Masond1310b22008-01-24 16:13:08 -05007452 struct extent_io_tree *tree;
7453 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007454 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007455
Chris Masond1310b22008-01-24 16:13:08 -05007456 tree = &BTRFS_I(page->mapping->host)->io_tree;
7457 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007458 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007459 if (ret == 1) {
7460 ClearPagePrivate(page);
7461 set_page_private(page, 0);
7462 page_cache_release(page);
7463 }
7464 return ret;
7465}
Chris Mason39279cc2007-06-12 06:35:45 -04007466
Chris Masone6dcd2d2008-07-17 12:53:50 -04007467static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7468{
Chris Mason98509cf2008-09-11 15:51:43 -04007469 if (PageWriteback(page) || PageDirty(page))
7470 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007471 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007472}
7473
Chris Masona52d9a82007-08-27 16:49:44 -04007474static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7475{
Josef Bacik5fd02042012-05-02 14:00:54 -04007476 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007477 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007478 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007479 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007480 u64 page_start = page_offset(page);
7481 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007482
Chris Mason8b62b722009-09-02 16:53:46 -04007483 /*
7484 * we have the page locked, so new writeback can't start,
7485 * and the dirty bit won't be cleared while we are here.
7486 *
7487 * Wait for IO on this page so that we can safely clear
7488 * the PagePrivate2 bit and do ordered accounting
7489 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007490 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007491
Josef Bacik5fd02042012-05-02 14:00:54 -04007492 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007493 if (offset) {
7494 btrfs_releasepage(page, GFP_NOFS);
7495 return;
7496 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007497 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007498 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007499 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007500 /*
7501 * IO on this page will never be started, so we need
7502 * to account for any ordered extents now
7503 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007504 clear_extent_bit(tree, page_start, page_end,
7505 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007506 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7507 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007508 /*
7509 * whoever cleared the private bit is responsible
7510 * for the finish_ordered_io
7511 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007512 if (TestClearPagePrivate2(page) &&
7513 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7514 PAGE_CACHE_SIZE, 1)) {
7515 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007516 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007517 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007518 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007519 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007520 }
7521 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007522 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007523 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7524 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007525 __btrfs_releasepage(page, GFP_NOFS);
7526
Chris Mason4a096752008-07-21 10:29:44 -04007527 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04007528 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04007529 ClearPagePrivate(page);
7530 set_page_private(page, 0);
7531 page_cache_release(page);
7532 }
Chris Mason39279cc2007-06-12 06:35:45 -04007533}
7534
Chris Mason9ebefb182007-06-15 13:50:00 -04007535/*
7536 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7537 * called from a page fault handler when a page is first dirtied. Hence we must
7538 * be careful to check for EOF conditions here. We set the page up correctly
7539 * for a written page which means we get ENOSPC checking when writing into
7540 * holes and correct delalloc and unwritten extent mapping on filesystems that
7541 * support these features.
7542 *
7543 * We are not allowed to take the i_mutex here so we have to play games to
7544 * protect against truncate races as the page could now be beyond EOF. Because
7545 * vmtruncate() writes the inode size before removing pages, once we have the
7546 * page lock we can determine safely if the page is beyond EOF. If it is not
7547 * beyond EOF, then the page is guaranteed safe against truncation until we
7548 * unlock the page.
7549 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007550int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007551{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007552 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007553 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007554 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007555 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7556 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007557 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007558 char *kaddr;
7559 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007560 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007561 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007562 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007563 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007564 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007565
Jan Karab2b5ef52012-06-12 16:20:45 +02007566 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007567 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007568 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007569 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007570 reserved = 1;
7571 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007572 if (ret) {
7573 if (ret == -ENOMEM)
7574 ret = VM_FAULT_OOM;
7575 else /* -ENOSPC, -EIO, etc */
7576 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007577 if (reserved)
7578 goto out;
7579 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007580 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007581
Nick Piggin56a76f82009-03-31 15:23:23 -07007582 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007583again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007584 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007585 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007586 page_start = page_offset(page);
7587 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007588
Chris Mason9ebefb182007-06-15 13:50:00 -04007589 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007590 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007591 /* page got truncated out from underneath us */
7592 goto out_unlock;
7593 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007594 wait_on_page_writeback(page);
7595
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007596 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007597 set_page_extent_mapped(page);
7598
Chris Masoneb84ae02008-07-17 13:53:27 -04007599 /*
7600 * we can't set the delalloc bits if there are pending ordered
7601 * extents. Drop our locks and wait for them to finish
7602 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007603 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7604 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007605 unlock_extent_cached(io_tree, page_start, page_end,
7606 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007607 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007608 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007609 btrfs_put_ordered_extent(ordered);
7610 goto again;
7611 }
7612
Josef Bacikfbf19082009-10-01 17:10:23 -04007613 /*
7614 * XXX - page_mkwrite gets called every time the page is dirtied, even
7615 * if it was already dirty, so for space accounting reasons we need to
7616 * clear any delalloc bits for the range we are fixing to save. There
7617 * is probably a better way to do this, but for now keep consistent with
7618 * prepare_pages in the normal write path.
7619 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007620 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007621 EXTENT_DIRTY | EXTENT_DELALLOC |
7622 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007623 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007624
Josef Bacik2ac55d42010-02-03 19:33:23 +00007625 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7626 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007627 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007628 unlock_extent_cached(io_tree, page_start, page_end,
7629 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007630 ret = VM_FAULT_SIGBUS;
7631 goto out_unlock;
7632 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007633 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007634
7635 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007636 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007637 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007638 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007639 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007640
Chris Masone6dcd2d2008-07-17 12:53:50 -04007641 if (zero_start != PAGE_CACHE_SIZE) {
7642 kaddr = kmap(page);
7643 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7644 flush_dcache_page(page);
7645 kunmap(page);
7646 }
Chris Mason247e7432008-07-17 12:53:51 -04007647 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007648 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007649 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007650
Chris Mason257c62e2009-10-13 13:21:08 -04007651 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7652 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007653 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007654
Josef Bacik2ac55d42010-02-03 19:33:23 +00007655 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007656
7657out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007658 if (!ret) {
7659 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007660 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007661 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007662 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007663out:
Josef Bacikec39e182012-01-12 19:10:12 -05007664 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007665out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007666 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007667 return ret;
7668}
7669
Josef Bacika41ad392011-01-31 15:30:16 -05007670static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007671{
7672 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007673 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007674 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007675 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007676 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007677 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007678 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007679
Josef Bacik2aaa6652012-08-29 14:27:18 -04007680 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007681 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007682 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007683
Chris Mason4a096752008-07-21 10:29:44 -04007684 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007685 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007686
Josef Bacikfcb80c22011-05-03 10:40:22 -04007687 /*
7688 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7689 * 3 things going on here
7690 *
7691 * 1) We need to reserve space for our orphan item and the space to
7692 * delete our orphan item. Lord knows we don't want to have a dangling
7693 * orphan item because we didn't reserve space to remove it.
7694 *
7695 * 2) We need to reserve space to update our inode.
7696 *
7697 * 3) We need to have something to cache all the space that is going to
7698 * be free'd up by the truncate operation, but also have some slack
7699 * space reserved in case it uses space during the truncate (thank you
7700 * very much snapshotting).
7701 *
7702 * And we need these to all be seperate. The fact is we can use alot of
7703 * space doing the truncate, and we have no earthly idea how much space
7704 * we will use, so we need the truncate reservation to be seperate so it
7705 * doesn't end up using space reserved for updating the inode or
7706 * removing the orphan item. We also need to be able to stop the
7707 * transaction and start a new one, which means we need to be able to
7708 * update the inode several times, and we have no idea of knowing how
7709 * many times that will be, so we can't just reserve 1 item for the
7710 * entirety of the opration, so that has to be done seperately as well.
7711 * Then there is the orphan item, which does indeed need to be held on
7712 * to for the whole operation, and we need nobody to touch this reserved
7713 * space except the orphan code.
7714 *
7715 * So that leaves us with
7716 *
7717 * 1) root->orphan_block_rsv - for the orphan deletion.
7718 * 2) rsv - for the truncate reservation, which we will steal from the
7719 * transaction reservation.
7720 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7721 * updating the inode.
7722 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007723 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007724 if (!rsv)
7725 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007726 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007727 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007728
Josef Bacik907cbce2011-08-08 13:46:15 -04007729 /*
Josef Bacik07127182011-08-19 10:29:59 -04007730 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007731 * 1 for updating the inode.
7732 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007733 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007734 if (IS_ERR(trans)) {
7735 err = PTR_ERR(trans);
7736 goto out;
7737 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007738
Josef Bacik907cbce2011-08-08 13:46:15 -04007739 /* Migrate the slack space for the truncate to our reserve */
7740 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7741 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007742 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007743
Chris Mason5a3f23d2009-03-31 13:27:11 -04007744 /*
7745 * setattr is responsible for setting the ordered_data_close flag,
7746 * but that is only tested during the last file release. That
7747 * could happen well after the next commit, leaving a great big
7748 * window where new writes may get lost if someone chooses to write
7749 * to this file after truncating to zero
7750 *
7751 * The inode doesn't have any dirty data here, and so if we commit
7752 * this is a noop. If someone immediately starts writing to the inode
7753 * it is very likely we'll catch some of their writes in this
7754 * transaction, and the commit will find this file on the ordered
7755 * data list with good things to send down.
7756 *
7757 * This is a best effort solution, there is still a window where
7758 * using truncate to replace the contents of the file will
7759 * end up with a zero length file after a crash.
7760 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007761 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7762 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007763 btrfs_add_ordered_operation(trans, root, inode);
7764
Josef Bacik5dc562c2012-08-17 13:14:17 -04007765 /*
7766 * So if we truncate and then write and fsync we normally would just
7767 * write the extents that changed, which is a problem if we need to
7768 * first truncate that entire inode. So set this flag so we write out
7769 * all of the extents in the inode to the sync log so we're completely
7770 * safe.
7771 */
7772 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007773 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007774
Yan, Zheng80825102009-11-12 09:35:36 +00007775 while (1) {
7776 ret = btrfs_truncate_inode_items(trans, root, inode,
7777 inode->i_size,
7778 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007779 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007780 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007781 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007782 }
Chris Mason39279cc2007-06-12 06:35:45 -04007783
Josef Bacikfcb80c22011-05-03 10:40:22 -04007784 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007785 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007786 if (ret) {
7787 err = ret;
7788 break;
7789 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007790
Yan, Zheng80825102009-11-12 09:35:36 +00007791 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007792 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007793
7794 trans = btrfs_start_transaction(root, 2);
7795 if (IS_ERR(trans)) {
7796 ret = err = PTR_ERR(trans);
7797 trans = NULL;
7798 break;
7799 }
7800
7801 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7802 rsv, min_size);
7803 BUG_ON(ret); /* shouldn't happen */
7804 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007805 }
7806
7807 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007808 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007809 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007810 if (ret)
7811 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007812 }
7813
Chris Mason917c16b2011-11-08 14:49:59 -05007814 if (trans) {
7815 trans->block_rsv = &root->fs_info->trans_block_rsv;
7816 ret = btrfs_update_inode(trans, root, inode);
7817 if (ret && !err)
7818 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007819
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007820 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007821 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007822 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007823
7824out:
7825 btrfs_free_block_rsv(root, rsv);
7826
Josef Bacik3893e332011-01-31 16:03:11 -05007827 if (ret && !err)
7828 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007829
Josef Bacik3893e332011-01-31 16:03:11 -05007830 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007831}
7832
Sven Wegener3b963622008-06-09 21:57:42 -04007833/*
Chris Masond352ac62008-09-29 15:18:18 -04007834 * create a new subvolume directory/inode (helper for the ioctl).
7835 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007836int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007837 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007838{
Chris Mason39279cc2007-06-12 06:35:45 -04007839 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007840 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007841 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007842
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007843 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7844 new_dirid, new_dirid,
7845 S_IFDIR | (~current_umask() & S_IRWXUGO),
7846 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007847 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007848 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007849 inode->i_op = &btrfs_dir_inode_operations;
7850 inode->i_fop = &btrfs_dir_file_operations;
7851
Miklos Szeredibfe86842011-10-28 14:13:29 +02007852 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007853 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007854
Yan, Zheng76dda932009-09-21 16:00:26 -04007855 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007856
Yan, Zheng76dda932009-09-21 16:00:26 -04007857 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007858 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007859}
7860
Chris Mason39279cc2007-06-12 06:35:45 -04007861struct inode *btrfs_alloc_inode(struct super_block *sb)
7862{
7863 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007864 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007865
7866 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7867 if (!ei)
7868 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007869
7870 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007871 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007872 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007873 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007874 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007875 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007876 ei->disk_i_size = 0;
7877 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007878 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007879 ei->index_cnt = (u64)-1;
7880 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007881 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007882
Josef Bacik9e0baf62011-07-15 15:16:44 +00007883 spin_lock_init(&ei->lock);
7884 ei->outstanding_extents = 0;
7885 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007886
Josef Bacik72ac3c02012-05-23 14:13:11 -04007887 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007888 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007889
Miao Xie16cdcec2011-04-22 18:12:22 +08007890 ei->delayed_node = NULL;
7891
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007892 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007893 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007894 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7895 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007896 ei->io_tree.track_uptodate = 1;
7897 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007898 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007899 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007900 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007901 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007902 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007903 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007904 RB_CLEAR_NODE(&ei->rb_node);
7905
7906 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007907}
7908
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007909static void btrfs_i_callback(struct rcu_head *head)
7910{
7911 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007912 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7913}
7914
Chris Mason39279cc2007-06-12 06:35:45 -04007915void btrfs_destroy_inode(struct inode *inode)
7916{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007917 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007918 struct btrfs_root *root = BTRFS_I(inode)->root;
7919
Al Virob3d9b7a2012-06-09 13:51:19 -04007920 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007921 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007922 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7923 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007924 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7925 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007926
Chris Mason5a3f23d2009-03-31 13:27:11 -04007927 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007928 * This can happen where we create an inode, but somebody else also
7929 * created the same inode and we need to destroy the one we already
7930 * created.
7931 */
7932 if (!root)
7933 goto free;
7934
7935 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007936 * Make sure we're properly removed from the ordered operation
7937 * lists.
7938 */
7939 smp_mb();
7940 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7941 spin_lock(&root->fs_info->ordered_extent_lock);
7942 list_del_init(&BTRFS_I(inode)->ordered_operations);
7943 spin_unlock(&root->fs_info->ordered_extent_lock);
7944 }
7945
Josef Bacik8a35d952012-05-23 14:26:42 -04007946 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7947 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007948 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7949 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007950 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007951 }
Josef Bacik7b128762008-07-24 12:17:14 -04007952
Chris Masond3977122009-01-05 21:25:51 -05007953 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007954 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7955 if (!ordered)
7956 break;
7957 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007958 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7959 (unsigned long long)ordered->file_offset,
7960 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007961 btrfs_remove_ordered_extent(inode, ordered);
7962 btrfs_put_ordered_extent(ordered);
7963 btrfs_put_ordered_extent(ordered);
7964 }
7965 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007966 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007967 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007968free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007969 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007970 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007971}
7972
Al Viro45321ac2010-06-07 13:43:19 -04007973int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007974{
7975 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007976
Liu Bofa6ac872013-02-20 14:10:23 +00007977 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007978 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007979 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007980 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007981 else
Al Viro45321ac2010-06-07 13:43:19 -04007982 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007983}
7984
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007985static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007986{
7987 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7988
7989 inode_init_once(&ei->vfs_inode);
7990}
7991
7992void btrfs_destroy_cachep(void)
7993{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007994 /*
7995 * Make sure all delayed rcu free inodes are flushed before we
7996 * destroy cache.
7997 */
7998 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007999 if (btrfs_inode_cachep)
8000 kmem_cache_destroy(btrfs_inode_cachep);
8001 if (btrfs_trans_handle_cachep)
8002 kmem_cache_destroy(btrfs_trans_handle_cachep);
8003 if (btrfs_transaction_cachep)
8004 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008005 if (btrfs_path_cachep)
8006 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008007 if (btrfs_free_space_cachep)
8008 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008009 if (btrfs_delalloc_work_cachep)
8010 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008011}
8012
8013int btrfs_init_cachep(void)
8014{
David Sterba837e1972012-09-07 03:00:48 -06008015 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008016 sizeof(struct btrfs_inode), 0,
8017 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008018 if (!btrfs_inode_cachep)
8019 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008020
David Sterba837e1972012-09-07 03:00:48 -06008021 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008022 sizeof(struct btrfs_trans_handle), 0,
8023 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008024 if (!btrfs_trans_handle_cachep)
8025 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008026
David Sterba837e1972012-09-07 03:00:48 -06008027 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008028 sizeof(struct btrfs_transaction), 0,
8029 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008030 if (!btrfs_transaction_cachep)
8031 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008032
David Sterba837e1972012-09-07 03:00:48 -06008033 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008034 sizeof(struct btrfs_path), 0,
8035 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008036 if (!btrfs_path_cachep)
8037 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008038
David Sterba837e1972012-09-07 03:00:48 -06008039 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008040 sizeof(struct btrfs_free_space), 0,
8041 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8042 if (!btrfs_free_space_cachep)
8043 goto fail;
8044
Miao Xie8ccf6f192012-10-25 09:28:04 +00008045 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8046 sizeof(struct btrfs_delalloc_work), 0,
8047 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8048 NULL);
8049 if (!btrfs_delalloc_work_cachep)
8050 goto fail;
8051
Chris Mason39279cc2007-06-12 06:35:45 -04008052 return 0;
8053fail:
8054 btrfs_destroy_cachep();
8055 return -ENOMEM;
8056}
8057
8058static int btrfs_getattr(struct vfsmount *mnt,
8059 struct dentry *dentry, struct kstat *stat)
8060{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008061 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008062 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008063 u32 blocksize = inode->i_sb->s_blocksize;
8064
Chris Mason39279cc2007-06-12 06:35:45 -04008065 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008066 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008067 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008068
8069 spin_lock(&BTRFS_I(inode)->lock);
8070 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8071 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008072 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008073 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008074 return 0;
8075}
8076
Chris Masond3977122009-01-05 21:25:51 -05008077static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8078 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008079{
8080 struct btrfs_trans_handle *trans;
8081 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008082 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008083 struct inode *new_inode = new_dentry->d_inode;
8084 struct inode *old_inode = old_dentry->d_inode;
8085 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008086 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008087 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008088 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008089 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008090
Li Zefan33345d012011-04-20 10:31:50 +08008091 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008092 return -EPERM;
8093
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008094 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008095 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008096 return -EXDEV;
8097
Li Zefan33345d012011-04-20 10:31:50 +08008098 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8099 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008100 return -ENOTEMPTY;
8101
Chris Mason39279cc2007-06-12 06:35:45 -04008102 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008103 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008104 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008105
8106
8107 /* check for collisions, even if the name isn't there */
8108 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8109 new_dentry->d_name.name,
8110 new_dentry->d_name.len);
8111
8112 if (ret) {
8113 if (ret == -EEXIST) {
8114 /* we shouldn't get
8115 * eexist without a new_inode */
8116 if (!new_inode) {
8117 WARN_ON(1);
8118 return ret;
8119 }
8120 } else {
8121 /* maybe -EOVERFLOW */
8122 return ret;
8123 }
8124 }
8125 ret = 0;
8126
Chris Mason5a3f23d2009-03-31 13:27:11 -04008127 /*
8128 * we're using rename to replace one file with another.
8129 * and the replacement file is large. Start IO on it now so
8130 * we don't add too much work to the end of the transaction
8131 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008132 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008133 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8134 filemap_flush(old_inode->i_mapping);
8135
Yan, Zheng76dda932009-09-21 16:00:26 -04008136 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008137 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008138 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008139 /*
8140 * We want to reserve the absolute worst case amount of items. So if
8141 * both inodes are subvols and we need to unlink them then that would
8142 * require 4 item modifications, but if they are both normal inodes it
8143 * would require 5 item modifications, so we'll assume their normal
8144 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8145 * should cover the worst case number of items we'll modify.
8146 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008147 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008148 if (IS_ERR(trans)) {
8149 ret = PTR_ERR(trans);
8150 goto out_notrans;
8151 }
Chris Mason5f39d392007-10-15 16:14:19 -04008152
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008153 if (dest != root)
8154 btrfs_record_root_in_trans(trans, dest);
8155
Yan, Zhenga5719522009-09-24 09:17:31 -04008156 ret = btrfs_set_inode_index(new_dir, &index);
8157 if (ret)
8158 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008159
Li Zefan33345d012011-04-20 10:31:50 +08008160 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008161 /* force full log commit if subvolume involved. */
8162 root->fs_info->last_trans_log_full_commit = trans->transid;
8163 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008164 ret = btrfs_insert_inode_ref(trans, dest,
8165 new_dentry->d_name.name,
8166 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008167 old_ino,
8168 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008169 if (ret)
8170 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008171 /*
8172 * this is an ugly little race, but the rename is required
8173 * to make sure that if we crash, the inode is either at the
8174 * old name or the new one. pinning the log transaction lets
8175 * us make sure we don't allow a log commit to come in after
8176 * we unlink the name but before we add the new name back in.
8177 */
8178 btrfs_pin_log_trans(root);
8179 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008180 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008181 * make sure the inode gets flushed if it is replacing
8182 * something.
8183 */
Li Zefan33345d012011-04-20 10:31:50 +08008184 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008185 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008186
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008187 inode_inc_iversion(old_dir);
8188 inode_inc_iversion(new_dir);
8189 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008190 old_dir->i_ctime = old_dir->i_mtime = ctime;
8191 new_dir->i_ctime = new_dir->i_mtime = ctime;
8192 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008193
Chris Mason12fcfd22009-03-24 10:24:20 -04008194 if (old_dentry->d_parent != new_dentry->d_parent)
8195 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8196
Li Zefan33345d012011-04-20 10:31:50 +08008197 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008198 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8199 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8200 old_dentry->d_name.name,
8201 old_dentry->d_name.len);
8202 } else {
Al Viro92986792011-03-04 17:14:37 +00008203 ret = __btrfs_unlink_inode(trans, root, old_dir,
8204 old_dentry->d_inode,
8205 old_dentry->d_name.name,
8206 old_dentry->d_name.len);
8207 if (!ret)
8208 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008209 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008210 if (ret) {
8211 btrfs_abort_transaction(trans, root, ret);
8212 goto out_fail;
8213 }
Chris Mason39279cc2007-06-12 06:35:45 -04008214
8215 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008216 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008217 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008218 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008219 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8220 root_objectid = BTRFS_I(new_inode)->location.objectid;
8221 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8222 root_objectid,
8223 new_dentry->d_name.name,
8224 new_dentry->d_name.len);
8225 BUG_ON(new_inode->i_nlink == 0);
8226 } else {
8227 ret = btrfs_unlink_inode(trans, dest, new_dir,
8228 new_dentry->d_inode,
8229 new_dentry->d_name.name,
8230 new_dentry->d_name.len);
8231 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008232 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008233 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008234 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008235 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008236 if (ret) {
8237 btrfs_abort_transaction(trans, root, ret);
8238 goto out_fail;
8239 }
Chris Mason39279cc2007-06-12 06:35:45 -04008240 }
Josef Bacikaec74772008-07-24 12:12:38 -04008241
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008242 ret = btrfs_add_link(trans, new_dir, old_inode,
8243 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008244 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008245 if (ret) {
8246 btrfs_abort_transaction(trans, root, ret);
8247 goto out_fail;
8248 }
Chris Mason39279cc2007-06-12 06:35:45 -04008249
Li Zefan33345d012011-04-20 10:31:50 +08008250 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008251 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008252 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008253 btrfs_end_log_trans(root);
8254 }
Chris Mason39279cc2007-06-12 06:35:45 -04008255out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008256 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008257out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008258 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008259 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008260
Chris Mason39279cc2007-06-12 06:35:45 -04008261 return ret;
8262}
8263
Miao Xie8ccf6f192012-10-25 09:28:04 +00008264static void btrfs_run_delalloc_work(struct btrfs_work *work)
8265{
8266 struct btrfs_delalloc_work *delalloc_work;
8267
8268 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8269 work);
8270 if (delalloc_work->wait)
8271 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8272 else
8273 filemap_flush(delalloc_work->inode->i_mapping);
8274
8275 if (delalloc_work->delay_iput)
8276 btrfs_add_delayed_iput(delalloc_work->inode);
8277 else
8278 iput(delalloc_work->inode);
8279 complete(&delalloc_work->completion);
8280}
8281
8282struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8283 int wait, int delay_iput)
8284{
8285 struct btrfs_delalloc_work *work;
8286
8287 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8288 if (!work)
8289 return NULL;
8290
8291 init_completion(&work->completion);
8292 INIT_LIST_HEAD(&work->list);
8293 work->inode = inode;
8294 work->wait = wait;
8295 work->delay_iput = delay_iput;
8296 work->work.func = btrfs_run_delalloc_work;
8297
8298 return work;
8299}
8300
8301void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8302{
8303 wait_for_completion(&work->completion);
8304 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8305}
8306
Chris Masond352ac62008-09-29 15:18:18 -04008307/*
8308 * some fairly slow code that needs optimization. This walks the list
8309 * of all the inodes with pending delalloc and forces them to disk.
8310 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008311int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008312{
Chris Masonea8c2812008-08-04 23:17:27 -04008313 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008314 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008315 struct btrfs_delalloc_work *work, *next;
8316 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008317 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008318 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008319
Yan Zhengc146afa2008-11-12 14:34:12 -05008320 if (root->fs_info->sb->s_flags & MS_RDONLY)
8321 return -EROFS;
8322
Miao Xie8ccf6f192012-10-25 09:28:04 +00008323 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008324 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008325
Chris Mason75eff682008-12-15 15:54:40 -05008326 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008327 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8328 while (!list_empty(&splice)) {
8329 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008330 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008331
8332 list_del_init(&binode->delalloc_inodes);
8333
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008334 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008335 if (!inode) {
8336 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8337 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008338 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008339 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008340
8341 list_add_tail(&binode->delalloc_inodes,
8342 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008343 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008344
8345 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8346 if (unlikely(!work)) {
8347 ret = -ENOMEM;
8348 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008349 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008350 list_add_tail(&work->list, &works);
8351 btrfs_queue_worker(&root->fs_info->flush_workers,
8352 &work->work);
8353
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008354 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008355 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008356 }
Chris Mason75eff682008-12-15 15:54:40 -05008357 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008358
Miao Xie1eafa6c2013-01-22 10:49:00 +00008359 list_for_each_entry_safe(work, next, &works, list) {
8360 list_del_init(&work->list);
8361 btrfs_wait_and_free_delalloc_work(work);
8362 }
8363
Chris Mason8c8bee12008-09-29 11:19:10 -04008364 /* the filemap_flush will queue IO into the worker threads, but
8365 * we have to make sure the IO is actually started and that
8366 * ordered extents get created before we return
8367 */
8368 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008369 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008370 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008371 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008372 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8373 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008374 }
8375 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008376 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008377out:
8378 list_for_each_entry_safe(work, next, &works, list) {
8379 list_del_init(&work->list);
8380 btrfs_wait_and_free_delalloc_work(work);
8381 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008382
8383 if (!list_empty_careful(&splice)) {
8384 spin_lock(&root->fs_info->delalloc_lock);
8385 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8386 spin_unlock(&root->fs_info->delalloc_lock);
8387 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008388 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008389}
8390
Chris Mason39279cc2007-06-12 06:35:45 -04008391static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8392 const char *symname)
8393{
8394 struct btrfs_trans_handle *trans;
8395 struct btrfs_root *root = BTRFS_I(dir)->root;
8396 struct btrfs_path *path;
8397 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008398 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008399 int err;
8400 int drop_inode = 0;
8401 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008402 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008403 int name_len;
8404 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008405 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008406 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008407 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008408
8409 name_len = strlen(symname) + 1;
8410 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8411 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008412
Josef Bacik9ed74f22009-09-11 16:12:44 -04008413 /*
8414 * 2 items for inode item and ref
8415 * 2 items for dir items
8416 * 1 item for xattr if selinux is on
8417 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008418 trans = btrfs_start_transaction(root, 5);
8419 if (IS_ERR(trans))
8420 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008421
Li Zefan581bb052011-04-20 10:06:11 +08008422 err = btrfs_find_free_ino(root, &objectid);
8423 if (err)
8424 goto out_unlock;
8425
Josef Bacikaec74772008-07-24 12:12:38 -04008426 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008427 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008428 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008429 if (IS_ERR(inode)) {
8430 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008431 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008432 }
Chris Mason39279cc2007-06-12 06:35:45 -04008433
Eric Paris2a7dba32011-02-01 11:05:39 -05008434 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008435 if (err) {
8436 drop_inode = 1;
8437 goto out_unlock;
8438 }
8439
Casey Schauflerad19db72011-12-15 10:09:07 -05008440 /*
8441 * If the active LSM wants to access the inode during
8442 * d_instantiate it needs these. Smack checks to see
8443 * if the filesystem supports xattrs by looking at the
8444 * ops vector.
8445 */
8446 inode->i_fop = &btrfs_file_operations;
8447 inode->i_op = &btrfs_file_inode_operations;
8448
Josef Bacika1b075d2010-11-19 20:36:11 +00008449 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008450 if (err)
8451 drop_inode = 1;
8452 else {
8453 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008454 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008455 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008456 }
Chris Mason39279cc2007-06-12 06:35:45 -04008457 if (drop_inode)
8458 goto out_unlock;
8459
8460 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008461 if (!path) {
8462 err = -ENOMEM;
8463 drop_inode = 1;
8464 goto out_unlock;
8465 }
Li Zefan33345d012011-04-20 10:31:50 +08008466 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008467 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008468 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8469 datasize = btrfs_file_extent_calc_inline_size(name_len);
8470 err = btrfs_insert_empty_item(trans, root, path, &key,
8471 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008472 if (err) {
8473 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008474 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008475 goto out_unlock;
8476 }
Chris Mason5f39d392007-10-15 16:14:19 -04008477 leaf = path->nodes[0];
8478 ei = btrfs_item_ptr(leaf, path->slots[0],
8479 struct btrfs_file_extent_item);
8480 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8481 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008482 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008483 btrfs_set_file_extent_encryption(leaf, ei, 0);
8484 btrfs_set_file_extent_compression(leaf, ei, 0);
8485 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8486 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8487
Chris Mason39279cc2007-06-12 06:35:45 -04008488 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008489 write_extent_buffer(leaf, symname, ptr, name_len);
8490 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008491 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008492
Chris Mason39279cc2007-06-12 06:35:45 -04008493 inode->i_op = &btrfs_symlink_inode_operations;
8494 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008495 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008496 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008497 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008498 err = btrfs_update_inode(trans, root, inode);
8499 if (err)
8500 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008501
8502out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008503 if (!err)
8504 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008505 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008506 if (drop_inode) {
8507 inode_dec_link_count(inode);
8508 iput(inode);
8509 }
Liu Bob53d3f52012-11-14 14:34:34 +00008510 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008511 return err;
8512}
Chris Mason16432982008-04-10 10:23:21 -04008513
Josef Bacik0af3d002010-06-21 14:48:16 -04008514static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8515 u64 start, u64 num_bytes, u64 min_size,
8516 loff_t actual_len, u64 *alloc_hint,
8517 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008518{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008519 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8520 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008521 struct btrfs_root *root = BTRFS_I(inode)->root;
8522 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008523 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008524 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008525 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008526 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008527 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008528
Josef Bacik0af3d002010-06-21 14:48:16 -04008529 if (trans)
8530 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008531 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008532 if (own_trans) {
8533 trans = btrfs_start_transaction(root, 3);
8534 if (IS_ERR(trans)) {
8535 ret = PTR_ERR(trans);
8536 break;
8537 }
Yan Zhengd899e052008-10-30 14:25:28 -04008538 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008539
Chris Mason154ea282013-03-05 11:11:26 -05008540 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8541 cur_bytes = max(cur_bytes, min_size);
8542 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008543 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008544 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008545 if (own_trans)
8546 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008547 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008548 }
8549
Yan Zhengd899e052008-10-30 14:25:28 -04008550 ret = insert_reserved_file_extent(trans, inode,
8551 cur_offset, ins.objectid,
8552 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008553 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008554 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008555 if (ret) {
8556 btrfs_abort_transaction(trans, root, ret);
8557 if (own_trans)
8558 btrfs_end_transaction(trans, root);
8559 break;
8560 }
Chris Masona1ed8352009-09-11 12:27:37 -04008561 btrfs_drop_extent_cache(inode, cur_offset,
8562 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008563
Josef Bacik5dc562c2012-08-17 13:14:17 -04008564 em = alloc_extent_map();
8565 if (!em) {
8566 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8567 &BTRFS_I(inode)->runtime_flags);
8568 goto next;
8569 }
8570
8571 em->start = cur_offset;
8572 em->orig_start = cur_offset;
8573 em->len = ins.offset;
8574 em->block_start = ins.objectid;
8575 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008576 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008577 em->bdev = root->fs_info->fs_devices->latest_bdev;
8578 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8579 em->generation = trans->transid;
8580
8581 while (1) {
8582 write_lock(&em_tree->lock);
8583 ret = add_extent_mapping(em_tree, em);
8584 if (!ret)
8585 list_move(&em->list,
8586 &em_tree->modified_extents);
8587 write_unlock(&em_tree->lock);
8588 if (ret != -EEXIST)
8589 break;
8590 btrfs_drop_extent_cache(inode, cur_offset,
8591 cur_offset + ins.offset - 1,
8592 0);
8593 }
8594 free_extent_map(em);
8595next:
Yan Zhengd899e052008-10-30 14:25:28 -04008596 num_bytes -= ins.offset;
8597 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008598 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008599
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008600 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008601 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008602 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008603 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008604 (actual_len > inode->i_size) &&
8605 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008606 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008607 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008608 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008609 i_size = cur_offset;
8610 i_size_write(inode, i_size);
8611 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008612 }
8613
Yan Zhengd899e052008-10-30 14:25:28 -04008614 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008615
8616 if (ret) {
8617 btrfs_abort_transaction(trans, root, ret);
8618 if (own_trans)
8619 btrfs_end_transaction(trans, root);
8620 break;
8621 }
Yan Zhengd899e052008-10-30 14:25:28 -04008622
Josef Bacik0af3d002010-06-21 14:48:16 -04008623 if (own_trans)
8624 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008625 }
Yan Zhengd899e052008-10-30 14:25:28 -04008626 return ret;
8627}
8628
Josef Bacik0af3d002010-06-21 14:48:16 -04008629int btrfs_prealloc_file_range(struct inode *inode, int mode,
8630 u64 start, u64 num_bytes, u64 min_size,
8631 loff_t actual_len, u64 *alloc_hint)
8632{
8633 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8634 min_size, actual_len, alloc_hint,
8635 NULL);
8636}
8637
8638int btrfs_prealloc_file_range_trans(struct inode *inode,
8639 struct btrfs_trans_handle *trans, int mode,
8640 u64 start, u64 num_bytes, u64 min_size,
8641 loff_t actual_len, u64 *alloc_hint)
8642{
8643 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8644 min_size, actual_len, alloc_hint, trans);
8645}
8646
Chris Masone6dcd2d2008-07-17 12:53:50 -04008647static int btrfs_set_page_dirty(struct page *page)
8648{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008649 return __set_page_dirty_nobuffers(page);
8650}
8651
Al Viro10556cb22011-06-20 19:28:19 -04008652static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008653{
Li Zefanb83cc962010-12-20 16:04:08 +08008654 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008655 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008656
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008657 if (mask & MAY_WRITE &&
8658 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8659 if (btrfs_root_readonly(root))
8660 return -EROFS;
8661 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8662 return -EACCES;
8663 }
Al Viro2830ba72011-06-20 19:16:29 -04008664 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008665}
Chris Mason39279cc2007-06-12 06:35:45 -04008666
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008667static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008668 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008669 .lookup = btrfs_lookup,
8670 .create = btrfs_create,
8671 .unlink = btrfs_unlink,
8672 .link = btrfs_link,
8673 .mkdir = btrfs_mkdir,
8674 .rmdir = btrfs_rmdir,
8675 .rename = btrfs_rename,
8676 .symlink = btrfs_symlink,
8677 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008678 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008679 .setxattr = btrfs_setxattr,
8680 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008681 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008682 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008683 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008684 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008685};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008686static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008687 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008688 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008689 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008690};
Yan, Zheng76dda932009-09-21 16:00:26 -04008691
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008692static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008693 .llseek = generic_file_llseek,
8694 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008695 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008696 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008697#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008698 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008699#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008700 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008701 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008702};
8703
Chris Masond1310b22008-01-24 16:13:08 -05008704static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008705 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008706 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008707 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008708 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008709 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008710 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008711 .set_bit_hook = btrfs_set_bit_hook,
8712 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008713 .merge_extent_hook = btrfs_merge_extent_hook,
8714 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008715};
8716
Chris Mason35054392009-01-21 13:11:13 -05008717/*
8718 * btrfs doesn't support the bmap operation because swapfiles
8719 * use bmap to make a mapping of extents in the file. They assume
8720 * these extents won't change over the life of the file and they
8721 * use the bmap result to do IO directly to the drive.
8722 *
8723 * the btrfs bmap call would return logical addresses that aren't
8724 * suitable for IO and they also will change frequently as COW
8725 * operations happen. So, swapfile + btrfs == corruption.
8726 *
8727 * For now we're avoiding this by dropping bmap.
8728 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008729static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008730 .readpage = btrfs_readpage,
8731 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008732 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008733 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008734 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008735 .invalidatepage = btrfs_invalidatepage,
8736 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008737 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008738 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008739};
8740
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008741static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008742 .readpage = btrfs_readpage,
8743 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008744 .invalidatepage = btrfs_invalidatepage,
8745 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008746};
8747
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008748static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008749 .getattr = btrfs_getattr,
8750 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008751 .setxattr = btrfs_setxattr,
8752 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008753 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008754 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008755 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008756 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008757 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008758 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008759};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008760static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008761 .getattr = btrfs_getattr,
8762 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008763 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008764 .setxattr = btrfs_setxattr,
8765 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008766 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008767 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008768 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008769 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008770};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008771static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008772 .readlink = generic_readlink,
8773 .follow_link = page_follow_link_light,
8774 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008775 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008776 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008777 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008778 .setxattr = btrfs_setxattr,
8779 .getxattr = btrfs_getxattr,
8780 .listxattr = btrfs_listxattr,
8781 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008782 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008783 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008784};
Yan, Zheng76dda932009-09-21 16:00:26 -04008785
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008786const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008787 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008788 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008789};