blob: c226daefd65d20a7c635723926f978172f53aaa6 [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 Masonb888db22007-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 Masonb888db22007-08-27 16:49:44 -0400356
Liu Bo4cb13e52012-03-29 09:57:45 -0400357 /* if this is a small write inside eof, kick off a defrag */
358 if ((end - start + 1) < 16 * 1024 &&
359 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400360 btrfs_add_inode_defrag(NULL, inode);
361
Chris Mason42dc7ba2008-12-15 11:44:56 -0500362 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400363again:
364 will_compress = 0;
365 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
366 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
367
Chris Masonf03d9302009-02-04 09:31:06 -0500368 /*
369 * we don't want to send crud past the end of i_size through
370 * compression, that's just a waste of CPU time. So, if the
371 * end of the file is before the start of our current
372 * requested range of bytes, we bail out to the uncompressed
373 * cleanup code that can deal with all of this.
374 *
375 * It isn't really the fastest way to fix things, but this is a
376 * very uncommon corner.
377 */
378 if (actual_end <= start)
379 goto cleanup_and_bail_uncompressed;
380
Chris Masonc8b97812008-10-29 14:49:59 -0400381 total_compressed = actual_end - start;
382
383 /* we want to make sure that amount of ram required to uncompress
384 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500385 * of a compressed extent to 128k. This is a crucial number
386 * because it also controls how easily we can spread reads across
387 * cpus for decompression.
388 *
389 * We also want to make sure the amount of IO required to do
390 * a random read is reasonably small, so we limit the size of
391 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400392 */
393 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000394 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500395 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400396 total_in = 0;
397 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400398
Chris Mason771ed682008-11-06 22:02:51 -0500399 /*
400 * we do compression for mount -o compress and when the
401 * inode has not been flagged as nocompress. This flag can
402 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400403 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200404 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500405 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000406 (BTRFS_I(inode)->force_compress) ||
407 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400408 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400409 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800410 if (!pages) {
411 /* just bail out to the uncompressed code */
412 goto cont;
413 }
Chris Mason179e29e2007-11-01 11:28:41 -0400414
Li Zefan261507a02010-12-17 14:21:50 +0800415 if (BTRFS_I(inode)->force_compress)
416 compress_type = BTRFS_I(inode)->force_compress;
417
418 ret = btrfs_compress_pages(compress_type,
419 inode->i_mapping, start,
420 total_compressed, pages,
421 nr_pages, &nr_pages_ret,
422 &total_in,
423 &total_compressed,
424 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400425
426 if (!ret) {
427 unsigned long offset = total_compressed &
428 (PAGE_CACHE_SIZE - 1);
429 struct page *page = pages[nr_pages_ret - 1];
430 char *kaddr;
431
432 /* zero the tail end of the last page, we might be
433 * sending it down to disk
434 */
435 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800436 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400437 memset(kaddr + offset, 0,
438 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800439 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400440 }
441 will_compress = 1;
442 }
443 }
Li Zefan560f7d72011-09-08 10:22:01 +0800444cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400445 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400446 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100447 if (IS_ERR(trans)) {
448 ret = PTR_ERR(trans);
449 trans = NULL;
450 goto cleanup_and_out;
451 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400452 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500453
Chris Masonc8b97812008-10-29 14:49:59 -0400454 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500455 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400456 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500457 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400458 */
459 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000460 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400461 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500462 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400463 ret = cow_file_range_inline(trans, root, inode,
464 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000465 total_compressed,
466 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400467 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100468 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500469 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100470 * inline extent creation worked or returned error,
471 * we don't need to create any more async work items.
472 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500473 */
Chris Masonc8b97812008-10-29 14:49:59 -0400474 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400475 &BTRFS_I(inode)->io_tree,
476 start, end, NULL,
477 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400478 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400479 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000480
481 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400482 goto free_pages_out;
483 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000484 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400485 }
486
487 if (will_compress) {
488 /*
489 * we aren't doing an inline extent round the compressed size
490 * up to a block size boundary so the allocator does sane
491 * things
492 */
Qu Wenruofda28322013-02-26 08:10:22 +0000493 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400494
495 /*
496 * one last check to make sure the compression is really a
497 * win, compare the page count read with the blocks on disk
498 */
Qu Wenruofda28322013-02-26 08:10:22 +0000499 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400500 if (total_compressed >= total_in) {
501 will_compress = 0;
502 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400503 num_bytes = total_in;
504 }
505 }
506 if (!will_compress && pages) {
507 /*
508 * the compression code ran but failed to make things smaller,
509 * free any pages it allocated and our page pointer array
510 */
511 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400512 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400513 page_cache_release(pages[i]);
514 }
515 kfree(pages);
516 pages = NULL;
517 total_compressed = 0;
518 nr_pages_ret = 0;
519
520 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500521 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
522 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500523 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500524 }
Chris Masonc8b97812008-10-29 14:49:59 -0400525 }
Chris Mason771ed682008-11-06 22:02:51 -0500526 if (will_compress) {
527 *num_added += 1;
528
529 /* the async work queues will take care of doing actual
530 * allocation on disk for these compressed pages,
531 * and will submit them to the elevator.
532 */
533 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800534 total_compressed, pages, nr_pages_ret,
535 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500536
Yan, Zheng24ae6362010-12-06 07:02:36 +0000537 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500538 start += num_bytes;
539 pages = NULL;
540 cond_resched();
541 goto again;
542 }
543 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500544cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500545 /*
546 * No compression, but we still need to write the pages in
547 * the file we've been given so far. redirty the locked
548 * page if it corresponds to our extent and set things up
549 * for the async work queue to run cow_file_range to do
550 * the normal delalloc dance
551 */
552 if (page_offset(locked_page) >= start &&
553 page_offset(locked_page) <= end) {
554 __set_page_dirty_nobuffers(locked_page);
555 /* unlocked later on in the async handlers */
556 }
Li Zefan261507a02010-12-17 14:21:50 +0800557 add_async_extent(async_cow, start, end - start + 1,
558 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500559 *num_added += 1;
560 }
561
562out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100563 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500564
565free_pages_out:
566 for (i = 0; i < nr_pages_ret; i++) {
567 WARN_ON(pages[i]->mapping);
568 page_cache_release(pages[i]);
569 }
Chris Masond3977122009-01-05 21:25:51 -0500570 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500571
572 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100573
574cleanup_and_out:
575 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
576 start, end, NULL,
577 EXTENT_CLEAR_UNLOCK_PAGE |
578 EXTENT_CLEAR_DIRTY |
579 EXTENT_CLEAR_DELALLOC |
580 EXTENT_SET_WRITEBACK |
581 EXTENT_END_WRITEBACK);
582 if (!trans || IS_ERR(trans))
583 btrfs_error(root->fs_info, ret, "Failed to join transaction");
584 else
585 btrfs_abort_transaction(trans, root, ret);
586 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500587}
588
589/*
590 * phase two of compressed writeback. This is the ordered portion
591 * of the code, which only gets called in the order the work was
592 * queued. We walk all the async extents created by compress_file_range
593 * and send them down to the disk.
594 */
595static noinline int submit_compressed_extents(struct inode *inode,
596 struct async_cow *async_cow)
597{
598 struct async_extent *async_extent;
599 u64 alloc_hint = 0;
600 struct btrfs_trans_handle *trans;
601 struct btrfs_key ins;
602 struct extent_map *em;
603 struct btrfs_root *root = BTRFS_I(inode)->root;
604 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
605 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500606 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500607
608 if (list_empty(&async_cow->extents))
609 return 0;
610
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500611again:
Chris Masond3977122009-01-05 21:25:51 -0500612 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500613 async_extent = list_entry(async_cow->extents.next,
614 struct async_extent, list);
615 list_del(&async_extent->list);
616
617 io_tree = &BTRFS_I(inode)->io_tree;
618
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500619retry:
Chris Mason771ed682008-11-06 22:02:51 -0500620 /* did the compression code fall back to uncompressed IO? */
621 if (!async_extent->pages) {
622 int page_started = 0;
623 unsigned long nr_written = 0;
624
625 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000626 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100627 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500628
629 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500630 ret = cow_file_range(inode, async_cow->locked_page,
631 async_extent->start,
632 async_extent->start +
633 async_extent->ram_size - 1,
634 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500635
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100636 /* JDM XXX */
637
Chris Mason771ed682008-11-06 22:02:51 -0500638 /*
639 * if page_started, cow_file_range inserted an
640 * inline extent and took care of all the unlocking
641 * and IO for us. Otherwise, we need to submit
642 * all those pages down to the drive.
643 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500644 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500645 extent_write_locked_range(io_tree,
646 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500647 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500648 async_extent->ram_size - 1,
649 btrfs_get_extent,
650 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500651 else if (ret)
652 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500653 kfree(async_extent);
654 cond_resched();
655 continue;
656 }
657
658 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100659 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500660
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400661 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100662 if (IS_ERR(trans)) {
663 ret = PTR_ERR(trans);
664 } else {
665 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
666 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500667 async_extent->compressed_size,
668 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500669 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600670 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100671 btrfs_abort_transaction(trans, root, ret);
672 btrfs_end_transaction(trans, root);
673 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000674
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500675 if (ret) {
676 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500677
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500678 for (i = 0; i < async_extent->nr_pages; i++) {
679 WARN_ON(async_extent->pages[i]->mapping);
680 page_cache_release(async_extent->pages[i]);
681 }
682 kfree(async_extent->pages);
683 async_extent->nr_pages = 0;
684 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500685
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100686 if (ret == -ENOSPC)
687 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500688 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500689 }
690
Yan, Zhengc2167752009-11-12 09:34:21 +0000691 /*
692 * here we're doing allocation and writeback of the
693 * compressed pages
694 */
695 btrfs_drop_extent_cache(inode, async_extent->start,
696 async_extent->start +
697 async_extent->ram_size - 1, 0);
698
David Sterba172ddd62011-04-21 00:48:27 +0200699 em = alloc_extent_map();
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500700 if (!em)
701 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500702 em->start = async_extent->start;
703 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500704 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400705 em->mod_start = em->start;
706 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500707
708 em->block_start = ins.objectid;
709 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500710 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500711 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800712 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500713 set_bit(EXTENT_FLAG_PINNED, &em->flags);
714 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400715 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500716
Chris Masond3977122009-01-05 21:25:51 -0500717 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400718 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500719 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400720 if (!ret)
721 list_move(&em->list,
722 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400723 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500724 if (ret != -EEXIST) {
725 free_extent_map(em);
726 break;
727 }
728 btrfs_drop_extent_cache(inode, async_extent->start,
729 async_extent->start +
730 async_extent->ram_size - 1, 0);
731 }
732
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500733 if (ret)
734 goto out_free_reserve;
735
Li Zefan261507a02010-12-17 14:21:50 +0800736 ret = btrfs_add_ordered_extent_compress(inode,
737 async_extent->start,
738 ins.objectid,
739 async_extent->ram_size,
740 ins.offset,
741 BTRFS_ORDERED_COMPRESSED,
742 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500743 if (ret)
744 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500745
Chris Mason771ed682008-11-06 22:02:51 -0500746 /*
747 * clear dirty, set writeback and unlock the pages.
748 */
749 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400750 &BTRFS_I(inode)->io_tree,
751 async_extent->start,
752 async_extent->start +
753 async_extent->ram_size - 1,
754 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
755 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400756 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400757 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500758
759 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500760 async_extent->start,
761 async_extent->ram_size,
762 ins.objectid,
763 ins.offset, async_extent->pages,
764 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500765 alloc_hint = ins.objectid + ins.offset;
766 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500767 if (ret)
768 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500769 cond_resched();
770 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100771 ret = 0;
772out:
773 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500774out_free_reserve:
775 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100776out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500777 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
778 async_extent->start,
779 async_extent->start +
780 async_extent->ram_size - 1,
781 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
782 EXTENT_CLEAR_UNLOCK |
783 EXTENT_CLEAR_DELALLOC |
784 EXTENT_CLEAR_DIRTY |
785 EXTENT_SET_WRITEBACK |
786 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100787 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500788 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500789}
790
Josef Bacik4b46fce2010-05-23 11:00:55 -0400791static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
792 u64 num_bytes)
793{
794 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
795 struct extent_map *em;
796 u64 alloc_hint = 0;
797
798 read_lock(&em_tree->lock);
799 em = search_extent_mapping(em_tree, start, num_bytes);
800 if (em) {
801 /*
802 * if block start isn't an actual block number then find the
803 * first block in this inode and use that as a hint. If that
804 * block is also bogus then just don't worry about it.
805 */
806 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
807 free_extent_map(em);
808 em = search_extent_mapping(em_tree, 0, 0);
809 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
810 alloc_hint = em->block_start;
811 if (em)
812 free_extent_map(em);
813 } else {
814 alloc_hint = em->block_start;
815 free_extent_map(em);
816 }
817 }
818 read_unlock(&em_tree->lock);
819
820 return alloc_hint;
821}
822
Chris Mason771ed682008-11-06 22:02:51 -0500823/*
824 * when extent_io.c finds a delayed allocation range in the file,
825 * the call backs end up in this code. The basic idea is to
826 * allocate extents on disk for the range, and create ordered data structs
827 * in ram to track those extents.
828 *
829 * locked_page is the page that writepage had locked already. We use
830 * it to make sure we don't do extra locks or unlocks.
831 *
832 * *page_started is set to one if we unlock locked_page and do everything
833 * required to start IO on it. It may be clean and already done with
834 * IO when we return.
835 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000836static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
837 struct inode *inode,
838 struct btrfs_root *root,
839 struct page *locked_page,
840 u64 start, u64 end, int *page_started,
841 unsigned long *nr_written,
842 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500843{
Chris Mason771ed682008-11-06 22:02:51 -0500844 u64 alloc_hint = 0;
845 u64 num_bytes;
846 unsigned long ram_size;
847 u64 disk_num_bytes;
848 u64 cur_alloc_size;
849 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500850 struct btrfs_key ins;
851 struct extent_map *em;
852 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
853 int ret = 0;
854
Liu Bo83eea1f2012-07-10 05:28:39 -0600855 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500856
Qu Wenruofda28322013-02-26 08:10:22 +0000857 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500858 num_bytes = max(blocksize, num_bytes);
859 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500860
Chris Mason4cb53002011-05-24 15:35:30 -0400861 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400862 if (num_bytes < 64 * 1024 &&
863 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400864 btrfs_add_inode_defrag(trans, inode);
865
Chris Mason771ed682008-11-06 22:02:51 -0500866 if (start == 0) {
867 /* lets try to make an inline extent */
868 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000869 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500870 if (ret == 0) {
871 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400872 &BTRFS_I(inode)->io_tree,
873 start, end, NULL,
874 EXTENT_CLEAR_UNLOCK_PAGE |
875 EXTENT_CLEAR_UNLOCK |
876 EXTENT_CLEAR_DELALLOC |
877 EXTENT_CLEAR_DIRTY |
878 EXTENT_SET_WRITEBACK |
879 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000880
Chris Mason771ed682008-11-06 22:02:51 -0500881 *nr_written = *nr_written +
882 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
883 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500884 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100885 } else if (ret < 0) {
886 btrfs_abort_transaction(trans, root, ret);
887 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500888 }
889 }
Chris Masonc8b97812008-10-29 14:49:59 -0400890
891 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200892 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400893
Josef Bacik4b46fce2010-05-23 11:00:55 -0400894 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400895 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400896
Chris Masond3977122009-01-05 21:25:51 -0500897 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400898 unsigned long op;
899
Josef Bacik287a0ab2010-03-19 18:07:23 +0000900 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400901 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500902 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500903 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100904 if (ret < 0) {
905 btrfs_abort_transaction(trans, root, ret);
906 goto out_unlock;
907 }
Chris Masond3977122009-01-05 21:25:51 -0500908
David Sterba172ddd62011-04-21 00:48:27 +0200909 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100910 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400911 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500912 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500913 ram_size = ins.offset;
914 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400915 em->mod_start = em->start;
916 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400917
Chris Masone6dcd2d2008-07-17 12:53:50 -0400918 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400919 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500920 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400921 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400922 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400923 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400924
Chris Masond3977122009-01-05 21:25:51 -0500925 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400926 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400927 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400928 if (!ret)
929 list_move(&em->list,
930 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400931 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400932 if (ret != -EEXIST) {
933 free_extent_map(em);
934 break;
935 }
936 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400937 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400938 }
939
Chris Mason98d20f62008-04-14 09:46:10 -0400940 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400941 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500942 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100943 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400944
Yan Zheng17d217f2008-12-12 10:03:38 -0500945 if (root->root_key.objectid ==
946 BTRFS_DATA_RELOC_TREE_OBJECTID) {
947 ret = btrfs_reloc_clone_csums(inode, start,
948 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100949 if (ret) {
950 btrfs_abort_transaction(trans, root, ret);
951 goto out_unlock;
952 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500953 }
954
Chris Masond3977122009-01-05 21:25:51 -0500955 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400956 break;
Chris Masond3977122009-01-05 21:25:51 -0500957
Chris Masonc8b97812008-10-29 14:49:59 -0400958 /* we're not doing compressed IO, don't unlock the first
959 * page (which the caller expects to stay locked), don't
960 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400961 *
962 * Do set the Private2 bit so we know this page was properly
963 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400964 */
Chris Masona791e352009-10-08 11:27:10 -0400965 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
966 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
967 EXTENT_SET_PRIVATE2;
968
Chris Masonc8b97812008-10-29 14:49:59 -0400969 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
970 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400971 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400972 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500973 num_bytes -= cur_alloc_size;
974 alloc_hint = ins.objectid + ins.offset;
975 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400976 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500978 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000979
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100980out_unlock:
981 extent_clear_unlock_delalloc(inode,
982 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400983 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100984 EXTENT_CLEAR_UNLOCK_PAGE |
985 EXTENT_CLEAR_UNLOCK |
986 EXTENT_CLEAR_DELALLOC |
987 EXTENT_CLEAR_DIRTY |
988 EXTENT_SET_WRITEBACK |
989 EXTENT_END_WRITEBACK);
990
991 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500992}
Chris Masonc8b97812008-10-29 14:49:59 -0400993
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000994static noinline int cow_file_range(struct inode *inode,
995 struct page *locked_page,
996 u64 start, u64 end, int *page_started,
997 unsigned long *nr_written,
998 int unlock)
999{
1000 struct btrfs_trans_handle *trans;
1001 struct btrfs_root *root = BTRFS_I(inode)->root;
1002 int ret;
1003
1004 trans = btrfs_join_transaction(root);
1005 if (IS_ERR(trans)) {
1006 extent_clear_unlock_delalloc(inode,
1007 &BTRFS_I(inode)->io_tree,
1008 start, end, locked_page,
1009 EXTENT_CLEAR_UNLOCK_PAGE |
1010 EXTENT_CLEAR_UNLOCK |
1011 EXTENT_CLEAR_DELALLOC |
1012 EXTENT_CLEAR_DIRTY |
1013 EXTENT_SET_WRITEBACK |
1014 EXTENT_END_WRITEBACK);
1015 return PTR_ERR(trans);
1016 }
1017 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1018
1019 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1020 page_started, nr_written, unlock);
1021
1022 btrfs_end_transaction(trans, root);
1023
1024 return ret;
1025}
1026
Chris Mason771ed682008-11-06 22:02:51 -05001027/*
1028 * work queue call back to started compression on a file and pages
1029 */
1030static noinline void async_cow_start(struct btrfs_work *work)
1031{
1032 struct async_cow *async_cow;
1033 int num_added = 0;
1034 async_cow = container_of(work, struct async_cow, work);
1035
1036 compress_file_range(async_cow->inode, async_cow->locked_page,
1037 async_cow->start, async_cow->end, async_cow,
1038 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001039 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001040 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001041 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001042 }
Chris Mason771ed682008-11-06 22:02:51 -05001043}
1044
1045/*
1046 * work queue call back to submit previously compressed pages
1047 */
1048static noinline void async_cow_submit(struct btrfs_work *work)
1049{
1050 struct async_cow *async_cow;
1051 struct btrfs_root *root;
1052 unsigned long nr_pages;
1053
1054 async_cow = container_of(work, struct async_cow, work);
1055
1056 root = async_cow->root;
1057 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1058 PAGE_CACHE_SHIFT;
1059
Josef Bacik66657b32012-08-01 15:36:24 -04001060 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001061 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001062 waitqueue_active(&root->fs_info->async_submit_wait))
1063 wake_up(&root->fs_info->async_submit_wait);
1064
Chris Masond3977122009-01-05 21:25:51 -05001065 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001066 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001067}
Chris Masonc8b97812008-10-29 14:49:59 -04001068
Chris Mason771ed682008-11-06 22:02:51 -05001069static noinline void async_cow_free(struct btrfs_work *work)
1070{
1071 struct async_cow *async_cow;
1072 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001073 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001074 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001075 kfree(async_cow);
1076}
1077
1078static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1079 u64 start, u64 end, int *page_started,
1080 unsigned long *nr_written)
1081{
1082 struct async_cow *async_cow;
1083 struct btrfs_root *root = BTRFS_I(inode)->root;
1084 unsigned long nr_pages;
1085 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001086 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001087
Chris Masona3429ab2009-10-08 12:30:20 -04001088 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1089 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001090 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001091 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001092 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001093 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001094 async_cow->root = root;
1095 async_cow->locked_page = locked_page;
1096 async_cow->start = start;
1097
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001098 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001099 cur_end = end;
1100 else
1101 cur_end = min(end, start + 512 * 1024 - 1);
1102
1103 async_cow->end = cur_end;
1104 INIT_LIST_HEAD(&async_cow->extents);
1105
1106 async_cow->work.func = async_cow_start;
1107 async_cow->work.ordered_func = async_cow_submit;
1108 async_cow->work.ordered_free = async_cow_free;
1109 async_cow->work.flags = 0;
1110
Chris Mason771ed682008-11-06 22:02:51 -05001111 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1112 PAGE_CACHE_SHIFT;
1113 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1114
1115 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1116 &async_cow->work);
1117
1118 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1119 wait_event(root->fs_info->async_submit_wait,
1120 (atomic_read(&root->fs_info->async_delalloc_pages) <
1121 limit));
1122 }
1123
Chris Masond3977122009-01-05 21:25:51 -05001124 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001125 atomic_read(&root->fs_info->async_delalloc_pages)) {
1126 wait_event(root->fs_info->async_submit_wait,
1127 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1128 0));
1129 }
1130
1131 *nr_written += nr_pages;
1132 start = cur_end + 1;
1133 }
1134 *page_started = 1;
1135 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001136}
1137
Chris Masond3977122009-01-05 21:25:51 -05001138static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001139 u64 bytenr, u64 num_bytes)
1140{
1141 int ret;
1142 struct btrfs_ordered_sum *sums;
1143 LIST_HEAD(list);
1144
Yan Zheng07d400a2009-01-06 11:42:00 -05001145 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001146 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001147 if (ret == 0 && list_empty(&list))
1148 return 0;
1149
1150 while (!list_empty(&list)) {
1151 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1152 list_del(&sums->list);
1153 kfree(sums);
1154 }
1155 return 1;
1156}
1157
Chris Masond352ac62008-09-29 15:18:18 -04001158/*
1159 * when nowcow writeback call back. This checks for snapshots or COW copies
1160 * of the extents that exist in the file, and COWs the file as required.
1161 *
1162 * If no cow copies or snapshots exist, we write directly to the existing
1163 * blocks on disk
1164 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001165static noinline int run_delalloc_nocow(struct inode *inode,
1166 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001167 u64 start, u64 end, int *page_started, int force,
1168 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001169{
Chris Masonbe20aa92007-12-17 20:14:01 -05001170 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001171 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001172 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001173 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001174 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001175 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001176 u64 cow_start;
1177 u64 cur_offset;
1178 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001179 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001180 u64 disk_bytenr;
1181 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001182 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001183 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001184 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001185 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001186 int nocow;
1187 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001188 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001189 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001190
1191 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001192 if (!path) {
1193 extent_clear_unlock_delalloc(inode,
1194 &BTRFS_I(inode)->io_tree,
1195 start, end, locked_page,
1196 EXTENT_CLEAR_UNLOCK_PAGE |
1197 EXTENT_CLEAR_UNLOCK |
1198 EXTENT_CLEAR_DELALLOC |
1199 EXTENT_CLEAR_DIRTY |
1200 EXTENT_SET_WRITEBACK |
1201 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001202 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001203 }
Li Zefan82d59022011-04-20 10:33:24 +08001204
Liu Bo83eea1f2012-07-10 05:28:39 -06001205 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001206
1207 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001208 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001209 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001210 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001211
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001212 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001213 extent_clear_unlock_delalloc(inode,
1214 &BTRFS_I(inode)->io_tree,
1215 start, end, locked_page,
1216 EXTENT_CLEAR_UNLOCK_PAGE |
1217 EXTENT_CLEAR_UNLOCK |
1218 EXTENT_CLEAR_DELALLOC |
1219 EXTENT_CLEAR_DIRTY |
1220 EXTENT_SET_WRITEBACK |
1221 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001222 btrfs_free_path(path);
1223 return PTR_ERR(trans);
1224 }
1225
Josef Bacik74b21072011-04-13 12:02:53 -04001226 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001227
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 cow_start = (u64)-1;
1229 cur_offset = start;
1230 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001231 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001232 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001233 if (ret < 0) {
1234 btrfs_abort_transaction(trans, root, ret);
1235 goto error;
1236 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1238 leaf = path->nodes[0];
1239 btrfs_item_key_to_cpu(leaf, &found_key,
1240 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001241 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001242 found_key.type == BTRFS_EXTENT_DATA_KEY)
1243 path->slots[0]--;
1244 }
1245 check_prev = 0;
1246next_slot:
1247 leaf = path->nodes[0];
1248 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1249 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001250 if (ret < 0) {
1251 btrfs_abort_transaction(trans, root, ret);
1252 goto error;
1253 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 if (ret > 0)
1255 break;
1256 leaf = path->nodes[0];
1257 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001258
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 nocow = 0;
1260 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001261 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001263
Li Zefan33345d012011-04-20 10:31:50 +08001264 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1266 found_key.offset > end)
1267 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001268
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 if (found_key.offset > cur_offset) {
1270 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001271 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 goto out_check;
1273 }
Chris Masonc31f8832008-01-08 15:46:31 -05001274
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 fi = btrfs_item_ptr(leaf, path->slots[0],
1276 struct btrfs_file_extent_item);
1277 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001278
Yan Zhengd899e052008-10-30 14:25:28 -04001279 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1280 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001282 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 extent_end = found_key.offset +
1284 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001285 disk_num_bytes =
1286 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 if (extent_end <= start) {
1288 path->slots[0]++;
1289 goto next_slot;
1290 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001291 if (disk_bytenr == 0)
1292 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 if (btrfs_file_extent_compression(leaf, fi) ||
1294 btrfs_file_extent_encryption(leaf, fi) ||
1295 btrfs_file_extent_other_encoding(leaf, fi))
1296 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001297 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1298 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001299 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001301 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001302 found_key.offset -
1303 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001304 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001305 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001306 disk_bytenr += cur_offset - found_key.offset;
1307 num_bytes = min(end + 1, extent_end) - cur_offset;
1308 /*
1309 * force cow if csum exists in the range.
1310 * this ensure that csum for a given extent are
1311 * either valid or do not exist.
1312 */
1313 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1314 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 nocow = 1;
1316 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1317 extent_end = found_key.offset +
1318 btrfs_file_extent_inline_len(leaf, fi);
1319 extent_end = ALIGN(extent_end, root->sectorsize);
1320 } else {
1321 BUG_ON(1);
1322 }
1323out_check:
1324 if (extent_end <= start) {
1325 path->slots[0]++;
1326 goto next_slot;
1327 }
1328 if (!nocow) {
1329 if (cow_start == (u64)-1)
1330 cow_start = cur_offset;
1331 cur_offset = extent_end;
1332 if (cur_offset > end)
1333 break;
1334 path->slots[0]++;
1335 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001336 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001337
David Sterbab3b4aa72011-04-21 01:20:15 +02001338 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001340 ret = __cow_file_range(trans, inode, root, locked_page,
1341 cow_start, found_key.offset - 1,
1342 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001343 if (ret) {
1344 btrfs_abort_transaction(trans, root, ret);
1345 goto error;
1346 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001348 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001349
Yan Zhengd899e052008-10-30 14:25:28 -04001350 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1351 struct extent_map *em;
1352 struct extent_map_tree *em_tree;
1353 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001354 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001355 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001356 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001357 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001358 em->len = num_bytes;
1359 em->block_len = num_bytes;
1360 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001361 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001362 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001363 em->mod_start = em->start;
1364 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001365 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001366 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001367 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001368 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001369 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001370 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001371 if (!ret)
1372 list_move(&em->list,
1373 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001374 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001375 if (ret != -EEXIST) {
1376 free_extent_map(em);
1377 break;
1378 }
1379 btrfs_drop_extent_cache(inode, em->start,
1380 em->start + em->len - 1, 0);
1381 }
1382 type = BTRFS_ORDERED_PREALLOC;
1383 } else {
1384 type = BTRFS_ORDERED_NOCOW;
1385 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001386
1387 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001388 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001389 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001390
Yan, Zhengefa56462010-05-16 10:49:59 -04001391 if (root->root_key.objectid ==
1392 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1393 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1394 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001395 if (ret) {
1396 btrfs_abort_transaction(trans, root, ret);
1397 goto error;
1398 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001399 }
1400
Yan Zhengd899e052008-10-30 14:25:28 -04001401 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001402 cur_offset, cur_offset + num_bytes - 1,
1403 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1404 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1405 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001406 cur_offset = extent_end;
1407 if (cur_offset > end)
1408 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001409 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001410 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001411
Josef Bacik17ca04a2012-05-31 15:58:55 -04001412 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001413 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001414 cur_offset = end;
1415 }
1416
Yan Zheng80ff3852008-10-30 14:20:02 -04001417 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001418 ret = __cow_file_range(trans, inode, root, locked_page,
1419 cow_start, end,
1420 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001421 if (ret) {
1422 btrfs_abort_transaction(trans, root, ret);
1423 goto error;
1424 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 }
1426
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001427error:
Miao Xiea698d0752012-09-20 01:51:59 -06001428 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001429 if (!ret)
1430 ret = err;
1431
Josef Bacik17ca04a2012-05-31 15:58:55 -04001432 if (ret && cur_offset < end)
1433 extent_clear_unlock_delalloc(inode,
1434 &BTRFS_I(inode)->io_tree,
1435 cur_offset, end, locked_page,
1436 EXTENT_CLEAR_UNLOCK_PAGE |
1437 EXTENT_CLEAR_UNLOCK |
1438 EXTENT_CLEAR_DELALLOC |
1439 EXTENT_CLEAR_DIRTY |
1440 EXTENT_SET_WRITEBACK |
1441 EXTENT_END_WRITEBACK);
1442
Yan Zheng7ea394f2008-08-05 13:05:02 -04001443 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001445}
1446
Chris Masond352ac62008-09-29 15:18:18 -04001447/*
1448 * extent_io.c call back to do delayed allocation processing
1449 */
Chris Masonc8b97812008-10-29 14:49:59 -04001450static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001451 u64 start, u64 end, int *page_started,
1452 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001453{
Chris Masonbe20aa92007-12-17 20:14:01 -05001454 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001455 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001456
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001457 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001458 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001459 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001460 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001461 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001462 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001463 } else if (!btrfs_test_opt(root, COMPRESS) &&
1464 !(BTRFS_I(inode)->force_compress) &&
1465 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001466 ret = cow_file_range(inode, locked_page, start, end,
1467 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001468 } else {
1469 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1470 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001471 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001472 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001473 }
Chris Masonb888db22007-08-27 16:49:44 -04001474 return ret;
1475}
1476
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001477static void btrfs_split_extent_hook(struct inode *inode,
1478 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001479{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001480 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001481 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001482 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001483
Josef Bacik9e0baf62011-07-15 15:16:44 +00001484 spin_lock(&BTRFS_I(inode)->lock);
1485 BTRFS_I(inode)->outstanding_extents++;
1486 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001487}
1488
1489/*
1490 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1491 * extents so we can keep track of new extents that are just merged onto old
1492 * extents, such as when we are doing sequential writes, so we can properly
1493 * account for the metadata space we'll need.
1494 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001495static void btrfs_merge_extent_hook(struct inode *inode,
1496 struct extent_state *new,
1497 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001498{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001499 /* not delalloc, ignore it */
1500 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001501 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001502
Josef Bacik9e0baf62011-07-15 15:16:44 +00001503 spin_lock(&BTRFS_I(inode)->lock);
1504 BTRFS_I(inode)->outstanding_extents--;
1505 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001506}
1507
Chris Masond352ac62008-09-29 15:18:18 -04001508/*
1509 * extent_io.c set_bit_hook, used to track delayed allocation
1510 * bytes in this file, and to maintain the list of inodes that
1511 * have pending delalloc work to be done.
1512 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001513static void btrfs_set_bit_hook(struct inode *inode,
1514 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001515{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001516
Chris Mason75eff682008-12-15 15:54:40 -05001517 /*
1518 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001519 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001520 * bit, which is only set or cleared with irqs on
1521 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001522 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001523 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001524 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001525 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001526
Josef Bacik9e0baf62011-07-15 15:16:44 +00001527 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001528 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001529 } else {
1530 spin_lock(&BTRFS_I(inode)->lock);
1531 BTRFS_I(inode)->outstanding_extents++;
1532 spin_unlock(&BTRFS_I(inode)->lock);
1533 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001534
Miao Xie963d6782013-01-29 10:10:51 +00001535 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1536 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001537 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001538 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001539 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1540 &BTRFS_I(inode)->runtime_flags)) {
1541 spin_lock(&root->fs_info->delalloc_lock);
1542 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1543 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1544 &root->fs_info->delalloc_inodes);
1545 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1546 &BTRFS_I(inode)->runtime_flags);
1547 }
1548 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001549 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001550 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001551 }
Chris Mason291d6732008-01-29 15:55:23 -05001552}
1553
Chris Masond352ac62008-09-29 15:18:18 -04001554/*
1555 * extent_io.c clear_bit_hook, see set_bit_hook for why
1556 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001557static void btrfs_clear_bit_hook(struct inode *inode,
1558 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001559{
Chris Mason75eff682008-12-15 15:54:40 -05001560 /*
1561 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001562 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001563 * bit, which is only set or cleared with irqs on
1564 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001565 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001566 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001567 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001568 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001569
Josef Bacik9e0baf62011-07-15 15:16:44 +00001570 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001571 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001572 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1573 spin_lock(&BTRFS_I(inode)->lock);
1574 BTRFS_I(inode)->outstanding_extents--;
1575 spin_unlock(&BTRFS_I(inode)->lock);
1576 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001577
1578 if (*bits & EXTENT_DO_ACCOUNTING)
1579 btrfs_delalloc_release_metadata(inode, len);
1580
Josef Bacik0cb59c92010-07-02 12:14:14 -04001581 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1582 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001583 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001584
Miao Xie963d6782013-01-29 10:10:51 +00001585 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1586 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001587 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001588 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001589 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001590 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1591 &BTRFS_I(inode)->runtime_flags)) {
1592 spin_lock(&root->fs_info->delalloc_lock);
1593 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1594 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1595 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1596 &BTRFS_I(inode)->runtime_flags);
1597 }
1598 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001599 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001600 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001601 }
Chris Mason291d6732008-01-29 15:55:23 -05001602}
1603
Chris Masond352ac62008-09-29 15:18:18 -04001604/*
1605 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1606 * we don't create bios that span stripes or chunks
1607 */
David Woodhouse64a16702009-07-15 23:29:37 +01001608int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001609 size_t size, struct bio *bio,
1610 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001611{
1612 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001613 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001614 u64 length = 0;
1615 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001616 int ret;
1617
Chris Mason771ed682008-11-06 22:02:51 -05001618 if (bio_flags & EXTENT_BIO_COMPRESSED)
1619 return 0;
1620
Chris Masonf2d8d742008-04-21 10:03:05 -04001621 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001622 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001623 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001624 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001625 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001626 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001627 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001628 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001629 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001630}
1631
Chris Masond352ac62008-09-29 15:18:18 -04001632/*
1633 * in order to insert checksums into the metadata in large chunks,
1634 * we wait until bio submission time. All the pages in the bio are
1635 * checksummed and sums are attached onto the ordered extent record.
1636 *
1637 * At IO completion time the cums attached on the ordered extent record
1638 * are inserted into the btree
1639 */
Chris Masond3977122009-01-05 21:25:51 -05001640static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1641 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001642 unsigned long bio_flags,
1643 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001644{
Chris Mason065631f2008-02-20 12:07:25 -05001645 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001646 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001647
Chris Masond20f7042008-12-08 16:58:54 -05001648 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001649 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001650 return 0;
1651}
Chris Masone0156402008-04-16 11:15:20 -04001652
Chris Mason4a69a412008-11-06 22:03:00 -05001653/*
1654 * in order to insert checksums into the metadata in large chunks,
1655 * we wait until bio submission time. All the pages in the bio are
1656 * checksummed and sums are attached onto the ordered extent record.
1657 *
1658 * At IO completion time the cums attached on the ordered extent record
1659 * are inserted into the btree
1660 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001661static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001662 int mirror_num, unsigned long bio_flags,
1663 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001664{
1665 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001666 int ret;
1667
1668 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1669 if (ret)
1670 bio_endio(bio, ret);
1671 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001672}
1673
Chris Masond352ac62008-09-29 15:18:18 -04001674/*
Chris Masoncad321a2008-12-17 14:51:42 -05001675 * extent_io.c submission hook. This does the right thing for csum calculation
1676 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001677 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001678static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001679 int mirror_num, unsigned long bio_flags,
1680 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001681{
1682 struct btrfs_root *root = BTRFS_I(inode)->root;
1683 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001684 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001685 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001686 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001687
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001688 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001689
Liu Bo83eea1f2012-07-10 05:28:39 -06001690 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001691 metadata = 2;
1692
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001693 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001694 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1695 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001696 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001697
Chris Masond20f7042008-12-08 16:58:54 -05001698 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001699 ret = btrfs_submit_compressed_read(inode, bio,
1700 mirror_num,
1701 bio_flags);
1702 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001703 } else if (!skip_sum) {
1704 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1705 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001706 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001707 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001708 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001709 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001710 /* csum items have already been cloned */
1711 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1712 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001713 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001714 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001715 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001716 bio_flags, bio_offset,
1717 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001718 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001719 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001720 } else if (!skip_sum) {
1721 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1722 if (ret)
1723 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001724 }
1725
Chris Mason0b86a832008-03-24 15:01:56 -04001726mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001727 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1728
1729out:
1730 if (ret < 0)
1731 bio_endio(bio, ret);
1732 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001733}
Chris Mason6885f302008-02-20 16:11:05 -05001734
Chris Masond352ac62008-09-29 15:18:18 -04001735/*
1736 * given a list of ordered sums record them in the inode. This happens
1737 * at IO completion time based on sums calculated at bio submission time.
1738 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001739static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001740 struct inode *inode, u64 file_offset,
1741 struct list_head *list)
1742{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001743 struct btrfs_ordered_sum *sum;
1744
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001745 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001746 btrfs_csum_file_blocks(trans,
1747 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001748 }
1749 return 0;
1750}
1751
Josef Bacik2ac55d42010-02-03 19:33:23 +00001752int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1753 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001754{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001755 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001756 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001757 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001758}
1759
Chris Masond352ac62008-09-29 15:18:18 -04001760/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001761struct btrfs_writepage_fixup {
1762 struct page *page;
1763 struct btrfs_work work;
1764};
1765
Christoph Hellwigb2950862008-12-02 09:54:17 -05001766static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001767{
1768 struct btrfs_writepage_fixup *fixup;
1769 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001770 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001771 struct page *page;
1772 struct inode *inode;
1773 u64 page_start;
1774 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001775 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001776
1777 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1778 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001779again:
Chris Mason247e7432008-07-17 12:53:51 -04001780 lock_page(page);
1781 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1782 ClearPageChecked(page);
1783 goto out_page;
1784 }
1785
1786 inode = page->mapping->host;
1787 page_start = page_offset(page);
1788 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1789
Josef Bacik2ac55d42010-02-03 19:33:23 +00001790 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001791 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001792
1793 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001794 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001795 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001796
1797 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1798 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001799 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1800 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001801 unlock_page(page);
1802 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001803 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001804 goto again;
1805 }
Chris Mason247e7432008-07-17 12:53:51 -04001806
Jeff Mahoney87826df2012-02-15 16:23:57 +01001807 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1808 if (ret) {
1809 mapping_set_error(page->mapping, ret);
1810 end_extent_writepage(page, ret, page_start, page_end);
1811 ClearPageChecked(page);
1812 goto out;
1813 }
1814
Josef Bacik2ac55d42010-02-03 19:33:23 +00001815 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001816 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001817 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001818out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001819 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1820 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001821out_page:
1822 unlock_page(page);
1823 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001824 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001825}
1826
1827/*
1828 * There are a few paths in the higher layers of the kernel that directly
1829 * set the page dirty bit without asking the filesystem if it is a
1830 * good idea. This causes problems because we want to make sure COW
1831 * properly happens and the data=ordered rules are followed.
1832 *
Chris Masonc8b97812008-10-29 14:49:59 -04001833 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001834 * hasn't been properly setup for IO. We kick off an async process
1835 * to fix it up. The async helper will wait for ordered extents, set
1836 * the delalloc bit and make it safe to write the page.
1837 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001838static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001839{
1840 struct inode *inode = page->mapping->host;
1841 struct btrfs_writepage_fixup *fixup;
1842 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001843
Chris Mason8b62b722009-09-02 16:53:46 -04001844 /* this page is properly in the ordered list */
1845 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001846 return 0;
1847
1848 if (PageChecked(page))
1849 return -EAGAIN;
1850
1851 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1852 if (!fixup)
1853 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001854
Chris Mason247e7432008-07-17 12:53:51 -04001855 SetPageChecked(page);
1856 page_cache_get(page);
1857 fixup->work.func = btrfs_writepage_fixup_worker;
1858 fixup->page = page;
1859 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001860 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001861}
1862
Yan Zhengd899e052008-10-30 14:25:28 -04001863static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1864 struct inode *inode, u64 file_pos,
1865 u64 disk_bytenr, u64 disk_num_bytes,
1866 u64 num_bytes, u64 ram_bytes,
1867 u8 compression, u8 encryption,
1868 u16 other_encoding, int extent_type)
1869{
1870 struct btrfs_root *root = BTRFS_I(inode)->root;
1871 struct btrfs_file_extent_item *fi;
1872 struct btrfs_path *path;
1873 struct extent_buffer *leaf;
1874 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001875 int ret;
1876
1877 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001878 if (!path)
1879 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001880
Chris Masonb9473432009-03-13 11:00:37 -04001881 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001882
1883 /*
1884 * we may be replacing one extent in the tree with another.
1885 * The new extent is pinned in the extent map, and we don't want
1886 * to drop it from the cache until it is completely in the btree.
1887 *
1888 * So, tell btrfs_drop_extents to leave this extent in the cache.
1889 * the caller is expected to unpin it and allow it to be merged
1890 * with the others.
1891 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001892 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001893 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001894 if (ret)
1895 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001896
Li Zefan33345d012011-04-20 10:31:50 +08001897 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001898 ins.offset = file_pos;
1899 ins.type = BTRFS_EXTENT_DATA_KEY;
1900 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001901 if (ret)
1902 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001903 leaf = path->nodes[0];
1904 fi = btrfs_item_ptr(leaf, path->slots[0],
1905 struct btrfs_file_extent_item);
1906 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1907 btrfs_set_file_extent_type(leaf, fi, extent_type);
1908 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1909 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1910 btrfs_set_file_extent_offset(leaf, fi, 0);
1911 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1912 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1913 btrfs_set_file_extent_compression(leaf, fi, compression);
1914 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1915 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001916
Yan Zhengd899e052008-10-30 14:25:28 -04001917 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001918 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001919
1920 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001921
1922 ins.objectid = disk_bytenr;
1923 ins.offset = disk_num_bytes;
1924 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001925 ret = btrfs_alloc_reserved_file_extent(trans, root,
1926 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001927 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928out:
Yan Zhengd899e052008-10-30 14:25:28 -04001929 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001930
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001931 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001932}
1933
Liu Bo38c227d2013-01-29 03:18:40 +00001934/* snapshot-aware defrag */
1935struct sa_defrag_extent_backref {
1936 struct rb_node node;
1937 struct old_sa_defrag_extent *old;
1938 u64 root_id;
1939 u64 inum;
1940 u64 file_pos;
1941 u64 extent_offset;
1942 u64 num_bytes;
1943 u64 generation;
1944};
1945
1946struct old_sa_defrag_extent {
1947 struct list_head list;
1948 struct new_sa_defrag_extent *new;
1949
1950 u64 extent_offset;
1951 u64 bytenr;
1952 u64 offset;
1953 u64 len;
1954 int count;
1955};
1956
1957struct new_sa_defrag_extent {
1958 struct rb_root root;
1959 struct list_head head;
1960 struct btrfs_path *path;
1961 struct inode *inode;
1962 u64 file_pos;
1963 u64 len;
1964 u64 bytenr;
1965 u64 disk_len;
1966 u8 compress_type;
1967};
1968
1969static int backref_comp(struct sa_defrag_extent_backref *b1,
1970 struct sa_defrag_extent_backref *b2)
1971{
1972 if (b1->root_id < b2->root_id)
1973 return -1;
1974 else if (b1->root_id > b2->root_id)
1975 return 1;
1976
1977 if (b1->inum < b2->inum)
1978 return -1;
1979 else if (b1->inum > b2->inum)
1980 return 1;
1981
1982 if (b1->file_pos < b2->file_pos)
1983 return -1;
1984 else if (b1->file_pos > b2->file_pos)
1985 return 1;
1986
1987 /*
1988 * [------------------------------] ===> (a range of space)
1989 * |<--->| |<---->| =============> (fs/file tree A)
1990 * |<---------------------------->| ===> (fs/file tree B)
1991 *
1992 * A range of space can refer to two file extents in one tree while
1993 * refer to only one file extent in another tree.
1994 *
1995 * So we may process a disk offset more than one time(two extents in A)
1996 * and locate at the same extent(one extent in B), then insert two same
1997 * backrefs(both refer to the extent in B).
1998 */
1999 return 0;
2000}
2001
2002static void backref_insert(struct rb_root *root,
2003 struct sa_defrag_extent_backref *backref)
2004{
2005 struct rb_node **p = &root->rb_node;
2006 struct rb_node *parent = NULL;
2007 struct sa_defrag_extent_backref *entry;
2008 int ret;
2009
2010 while (*p) {
2011 parent = *p;
2012 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2013
2014 ret = backref_comp(backref, entry);
2015 if (ret < 0)
2016 p = &(*p)->rb_left;
2017 else
2018 p = &(*p)->rb_right;
2019 }
2020
2021 rb_link_node(&backref->node, parent, p);
2022 rb_insert_color(&backref->node, root);
2023}
2024
2025/*
2026 * Note the backref might has changed, and in this case we just return 0.
2027 */
2028static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2029 void *ctx)
2030{
2031 struct btrfs_file_extent_item *extent;
2032 struct btrfs_fs_info *fs_info;
2033 struct old_sa_defrag_extent *old = ctx;
2034 struct new_sa_defrag_extent *new = old->new;
2035 struct btrfs_path *path = new->path;
2036 struct btrfs_key key;
2037 struct btrfs_root *root;
2038 struct sa_defrag_extent_backref *backref;
2039 struct extent_buffer *leaf;
2040 struct inode *inode = new->inode;
2041 int slot;
2042 int ret;
2043 u64 extent_offset;
2044 u64 num_bytes;
2045
2046 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2047 inum == btrfs_ino(inode))
2048 return 0;
2049
2050 key.objectid = root_id;
2051 key.type = BTRFS_ROOT_ITEM_KEY;
2052 key.offset = (u64)-1;
2053
2054 fs_info = BTRFS_I(inode)->root->fs_info;
2055 root = btrfs_read_fs_root_no_name(fs_info, &key);
2056 if (IS_ERR(root)) {
2057 if (PTR_ERR(root) == -ENOENT)
2058 return 0;
2059 WARN_ON(1);
2060 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2061 inum, offset, root_id);
2062 return PTR_ERR(root);
2063 }
2064
2065 key.objectid = inum;
2066 key.type = BTRFS_EXTENT_DATA_KEY;
2067 if (offset > (u64)-1 << 32)
2068 key.offset = 0;
2069 else
2070 key.offset = offset;
2071
2072 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2073 if (ret < 0) {
2074 WARN_ON(1);
2075 return ret;
2076 }
2077
2078 while (1) {
2079 cond_resched();
2080
2081 leaf = path->nodes[0];
2082 slot = path->slots[0];
2083
2084 if (slot >= btrfs_header_nritems(leaf)) {
2085 ret = btrfs_next_leaf(root, path);
2086 if (ret < 0) {
2087 goto out;
2088 } else if (ret > 0) {
2089 ret = 0;
2090 goto out;
2091 }
2092 continue;
2093 }
2094
2095 path->slots[0]++;
2096
2097 btrfs_item_key_to_cpu(leaf, &key, slot);
2098
2099 if (key.objectid > inum)
2100 goto out;
2101
2102 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2103 continue;
2104
2105 extent = btrfs_item_ptr(leaf, slot,
2106 struct btrfs_file_extent_item);
2107
2108 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2109 continue;
2110
2111 extent_offset = btrfs_file_extent_offset(leaf, extent);
2112 if (key.offset - extent_offset != offset)
2113 continue;
2114
2115 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2116 if (extent_offset >= old->extent_offset + old->offset +
2117 old->len || extent_offset + num_bytes <=
2118 old->extent_offset + old->offset)
2119 continue;
2120
2121 break;
2122 }
2123
2124 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2125 if (!backref) {
2126 ret = -ENOENT;
2127 goto out;
2128 }
2129
2130 backref->root_id = root_id;
2131 backref->inum = inum;
2132 backref->file_pos = offset + extent_offset;
2133 backref->num_bytes = num_bytes;
2134 backref->extent_offset = extent_offset;
2135 backref->generation = btrfs_file_extent_generation(leaf, extent);
2136 backref->old = old;
2137 backref_insert(&new->root, backref);
2138 old->count++;
2139out:
2140 btrfs_release_path(path);
2141 WARN_ON(ret);
2142 return ret;
2143}
2144
2145static noinline bool record_extent_backrefs(struct btrfs_path *path,
2146 struct new_sa_defrag_extent *new)
2147{
2148 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2149 struct old_sa_defrag_extent *old, *tmp;
2150 int ret;
2151
2152 new->path = path;
2153
2154 list_for_each_entry_safe(old, tmp, &new->head, list) {
2155 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2156 path, record_one_backref,
2157 old);
2158 BUG_ON(ret < 0 && ret != -ENOENT);
2159
2160 /* no backref to be processed for this extent */
2161 if (!old->count) {
2162 list_del(&old->list);
2163 kfree(old);
2164 }
2165 }
2166
2167 if (list_empty(&new->head))
2168 return false;
2169
2170 return true;
2171}
2172
2173static int relink_is_mergable(struct extent_buffer *leaf,
2174 struct btrfs_file_extent_item *fi,
2175 u64 disk_bytenr)
2176{
2177 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2178 return 0;
2179
2180 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2181 return 0;
2182
2183 if (btrfs_file_extent_compression(leaf, fi) ||
2184 btrfs_file_extent_encryption(leaf, fi) ||
2185 btrfs_file_extent_other_encoding(leaf, fi))
2186 return 0;
2187
2188 return 1;
2189}
2190
2191/*
2192 * Note the backref might has changed, and in this case we just return 0.
2193 */
2194static noinline int relink_extent_backref(struct btrfs_path *path,
2195 struct sa_defrag_extent_backref *prev,
2196 struct sa_defrag_extent_backref *backref)
2197{
2198 struct btrfs_file_extent_item *extent;
2199 struct btrfs_file_extent_item *item;
2200 struct btrfs_ordered_extent *ordered;
2201 struct btrfs_trans_handle *trans;
2202 struct btrfs_fs_info *fs_info;
2203 struct btrfs_root *root;
2204 struct btrfs_key key;
2205 struct extent_buffer *leaf;
2206 struct old_sa_defrag_extent *old = backref->old;
2207 struct new_sa_defrag_extent *new = old->new;
2208 struct inode *src_inode = new->inode;
2209 struct inode *inode;
2210 struct extent_state *cached = NULL;
2211 int ret = 0;
2212 u64 start;
2213 u64 len;
2214 u64 lock_start;
2215 u64 lock_end;
2216 bool merge = false;
2217 int index;
2218
2219 if (prev && prev->root_id == backref->root_id &&
2220 prev->inum == backref->inum &&
2221 prev->file_pos + prev->num_bytes == backref->file_pos)
2222 merge = true;
2223
2224 /* step 1: get root */
2225 key.objectid = backref->root_id;
2226 key.type = BTRFS_ROOT_ITEM_KEY;
2227 key.offset = (u64)-1;
2228
2229 fs_info = BTRFS_I(src_inode)->root->fs_info;
2230 index = srcu_read_lock(&fs_info->subvol_srcu);
2231
2232 root = btrfs_read_fs_root_no_name(fs_info, &key);
2233 if (IS_ERR(root)) {
2234 srcu_read_unlock(&fs_info->subvol_srcu, index);
2235 if (PTR_ERR(root) == -ENOENT)
2236 return 0;
2237 return PTR_ERR(root);
2238 }
2239 if (btrfs_root_refs(&root->root_item) == 0) {
2240 srcu_read_unlock(&fs_info->subvol_srcu, index);
2241 /* parse ENOENT to 0 */
2242 return 0;
2243 }
2244
2245 /* step 2: get inode */
2246 key.objectid = backref->inum;
2247 key.type = BTRFS_INODE_ITEM_KEY;
2248 key.offset = 0;
2249
2250 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2251 if (IS_ERR(inode)) {
2252 srcu_read_unlock(&fs_info->subvol_srcu, index);
2253 return 0;
2254 }
2255
2256 srcu_read_unlock(&fs_info->subvol_srcu, index);
2257
2258 /* step 3: relink backref */
2259 lock_start = backref->file_pos;
2260 lock_end = backref->file_pos + backref->num_bytes - 1;
2261 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2262 0, &cached);
2263
2264 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2265 if (ordered) {
2266 btrfs_put_ordered_extent(ordered);
2267 goto out_unlock;
2268 }
2269
2270 trans = btrfs_join_transaction(root);
2271 if (IS_ERR(trans)) {
2272 ret = PTR_ERR(trans);
2273 goto out_unlock;
2274 }
2275
2276 key.objectid = backref->inum;
2277 key.type = BTRFS_EXTENT_DATA_KEY;
2278 key.offset = backref->file_pos;
2279
2280 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2281 if (ret < 0) {
2282 goto out_free_path;
2283 } else if (ret > 0) {
2284 ret = 0;
2285 goto out_free_path;
2286 }
2287
2288 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2289 struct btrfs_file_extent_item);
2290
2291 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2292 backref->generation)
2293 goto out_free_path;
2294
2295 btrfs_release_path(path);
2296
2297 start = backref->file_pos;
2298 if (backref->extent_offset < old->extent_offset + old->offset)
2299 start += old->extent_offset + old->offset -
2300 backref->extent_offset;
2301
2302 len = min(backref->extent_offset + backref->num_bytes,
2303 old->extent_offset + old->offset + old->len);
2304 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2305
2306 ret = btrfs_drop_extents(trans, root, inode, start,
2307 start + len, 1);
2308 if (ret)
2309 goto out_free_path;
2310again:
2311 key.objectid = btrfs_ino(inode);
2312 key.type = BTRFS_EXTENT_DATA_KEY;
2313 key.offset = start;
2314
2315 if (merge) {
2316 struct btrfs_file_extent_item *fi;
2317 u64 extent_len;
2318 struct btrfs_key found_key;
2319
2320 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2321 if (ret < 0)
2322 goto out_free_path;
2323
2324 path->slots[0]--;
2325 leaf = path->nodes[0];
2326 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2327
2328 fi = btrfs_item_ptr(leaf, path->slots[0],
2329 struct btrfs_file_extent_item);
2330 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2331
2332 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2333 extent_len + found_key.offset == start) {
2334 btrfs_set_file_extent_num_bytes(leaf, fi,
2335 extent_len + len);
2336 btrfs_mark_buffer_dirty(leaf);
2337 inode_add_bytes(inode, len);
2338
2339 ret = 1;
2340 goto out_free_path;
2341 } else {
2342 merge = false;
2343 btrfs_release_path(path);
2344 goto again;
2345 }
2346 }
2347
2348 ret = btrfs_insert_empty_item(trans, root, path, &key,
2349 sizeof(*extent));
2350 if (ret) {
2351 btrfs_abort_transaction(trans, root, ret);
2352 goto out_free_path;
2353 }
2354
2355 leaf = path->nodes[0];
2356 item = btrfs_item_ptr(leaf, path->slots[0],
2357 struct btrfs_file_extent_item);
2358 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2359 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2360 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2361 btrfs_set_file_extent_num_bytes(leaf, item, len);
2362 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2363 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2364 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2365 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2366 btrfs_set_file_extent_encryption(leaf, item, 0);
2367 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2368
2369 btrfs_mark_buffer_dirty(leaf);
2370 inode_add_bytes(inode, len);
2371
2372 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2373 new->disk_len, 0,
2374 backref->root_id, backref->inum,
2375 new->file_pos, 0); /* start - extent_offset */
2376 if (ret) {
2377 btrfs_abort_transaction(trans, root, ret);
2378 goto out_free_path;
2379 }
2380
2381 ret = 1;
2382out_free_path:
2383 btrfs_release_path(path);
2384 btrfs_end_transaction(trans, root);
2385out_unlock:
2386 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2387 &cached, GFP_NOFS);
2388 iput(inode);
2389 return ret;
2390}
2391
2392static void relink_file_extents(struct new_sa_defrag_extent *new)
2393{
2394 struct btrfs_path *path;
2395 struct old_sa_defrag_extent *old, *tmp;
2396 struct sa_defrag_extent_backref *backref;
2397 struct sa_defrag_extent_backref *prev = NULL;
2398 struct inode *inode;
2399 struct btrfs_root *root;
2400 struct rb_node *node;
2401 int ret;
2402
2403 inode = new->inode;
2404 root = BTRFS_I(inode)->root;
2405
2406 path = btrfs_alloc_path();
2407 if (!path)
2408 return;
2409
2410 if (!record_extent_backrefs(path, new)) {
2411 btrfs_free_path(path);
2412 goto out;
2413 }
2414 btrfs_release_path(path);
2415
2416 while (1) {
2417 node = rb_first(&new->root);
2418 if (!node)
2419 break;
2420 rb_erase(node, &new->root);
2421
2422 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2423
2424 ret = relink_extent_backref(path, prev, backref);
2425 WARN_ON(ret < 0);
2426
2427 kfree(prev);
2428
2429 if (ret == 1)
2430 prev = backref;
2431 else
2432 prev = NULL;
2433 cond_resched();
2434 }
2435 kfree(prev);
2436
2437 btrfs_free_path(path);
2438
2439 list_for_each_entry_safe(old, tmp, &new->head, list) {
2440 list_del(&old->list);
2441 kfree(old);
2442 }
2443out:
2444 atomic_dec(&root->fs_info->defrag_running);
2445 wake_up(&root->fs_info->transaction_wait);
2446
2447 kfree(new);
2448}
2449
2450static struct new_sa_defrag_extent *
2451record_old_file_extents(struct inode *inode,
2452 struct btrfs_ordered_extent *ordered)
2453{
2454 struct btrfs_root *root = BTRFS_I(inode)->root;
2455 struct btrfs_path *path;
2456 struct btrfs_key key;
2457 struct old_sa_defrag_extent *old, *tmp;
2458 struct new_sa_defrag_extent *new;
2459 int ret;
2460
2461 new = kmalloc(sizeof(*new), GFP_NOFS);
2462 if (!new)
2463 return NULL;
2464
2465 new->inode = inode;
2466 new->file_pos = ordered->file_offset;
2467 new->len = ordered->len;
2468 new->bytenr = ordered->start;
2469 new->disk_len = ordered->disk_len;
2470 new->compress_type = ordered->compress_type;
2471 new->root = RB_ROOT;
2472 INIT_LIST_HEAD(&new->head);
2473
2474 path = btrfs_alloc_path();
2475 if (!path)
2476 goto out_kfree;
2477
2478 key.objectid = btrfs_ino(inode);
2479 key.type = BTRFS_EXTENT_DATA_KEY;
2480 key.offset = new->file_pos;
2481
2482 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2483 if (ret < 0)
2484 goto out_free_path;
2485 if (ret > 0 && path->slots[0] > 0)
2486 path->slots[0]--;
2487
2488 /* find out all the old extents for the file range */
2489 while (1) {
2490 struct btrfs_file_extent_item *extent;
2491 struct extent_buffer *l;
2492 int slot;
2493 u64 num_bytes;
2494 u64 offset;
2495 u64 end;
2496 u64 disk_bytenr;
2497 u64 extent_offset;
2498
2499 l = path->nodes[0];
2500 slot = path->slots[0];
2501
2502 if (slot >= btrfs_header_nritems(l)) {
2503 ret = btrfs_next_leaf(root, path);
2504 if (ret < 0)
2505 goto out_free_list;
2506 else if (ret > 0)
2507 break;
2508 continue;
2509 }
2510
2511 btrfs_item_key_to_cpu(l, &key, slot);
2512
2513 if (key.objectid != btrfs_ino(inode))
2514 break;
2515 if (key.type != BTRFS_EXTENT_DATA_KEY)
2516 break;
2517 if (key.offset >= new->file_pos + new->len)
2518 break;
2519
2520 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2521
2522 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2523 if (key.offset + num_bytes < new->file_pos)
2524 goto next;
2525
2526 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2527 if (!disk_bytenr)
2528 goto next;
2529
2530 extent_offset = btrfs_file_extent_offset(l, extent);
2531
2532 old = kmalloc(sizeof(*old), GFP_NOFS);
2533 if (!old)
2534 goto out_free_list;
2535
2536 offset = max(new->file_pos, key.offset);
2537 end = min(new->file_pos + new->len, key.offset + num_bytes);
2538
2539 old->bytenr = disk_bytenr;
2540 old->extent_offset = extent_offset;
2541 old->offset = offset - key.offset;
2542 old->len = end - offset;
2543 old->new = new;
2544 old->count = 0;
2545 list_add_tail(&old->list, &new->head);
2546next:
2547 path->slots[0]++;
2548 cond_resched();
2549 }
2550
2551 btrfs_free_path(path);
2552 atomic_inc(&root->fs_info->defrag_running);
2553
2554 return new;
2555
2556out_free_list:
2557 list_for_each_entry_safe(old, tmp, &new->head, list) {
2558 list_del(&old->list);
2559 kfree(old);
2560 }
2561out_free_path:
2562 btrfs_free_path(path);
2563out_kfree:
2564 kfree(new);
2565 return NULL;
2566}
2567
Chris Mason5d13a982009-03-13 11:41:46 -04002568/*
2569 * helper function for btrfs_finish_ordered_io, this
2570 * just reads in some of the csum leaves to prime them into ram
2571 * before we start the transaction. It limits the amount of btree
2572 * reads required while inside the transaction.
2573 */
Chris Masond352ac62008-09-29 15:18:18 -04002574/* as ordered data IO finishes, this gets called so we can finish
2575 * an ordered extent if the range of bytes in the file it covers are
2576 * fully written.
2577 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002578static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002579{
Josef Bacik5fd02042012-05-02 14:00:54 -04002580 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002581 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002582 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002583 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002584 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002585 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002586 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002587 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002588 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002589
Liu Bo83eea1f2012-07-10 05:28:39 -06002590 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002591
Josef Bacik5fd02042012-05-02 14:00:54 -04002592 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2593 ret = -EIO;
2594 goto out;
2595 }
2596
Yan, Zhengc2167752009-11-12 09:34:21 +00002597 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002598 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002599 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2600 if (nolock)
2601 trans = btrfs_join_transaction_nolock(root);
2602 else
2603 trans = btrfs_join_transaction(root);
2604 if (IS_ERR(trans)) {
2605 ret = PTR_ERR(trans);
2606 trans = NULL;
2607 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002608 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002609 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2610 ret = btrfs_update_inode_fallback(trans, root, inode);
2611 if (ret) /* -ENOMEM or corruption */
2612 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002613 goto out;
2614 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002615
Josef Bacik2ac55d42010-02-03 19:33:23 +00002616 lock_extent_bits(io_tree, ordered_extent->file_offset,
2617 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002618 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002619
Liu Bo38c227d2013-01-29 03:18:40 +00002620 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2621 ordered_extent->file_offset + ordered_extent->len - 1,
2622 EXTENT_DEFRAG, 1, cached_state);
2623 if (ret) {
2624 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2625 if (last_snapshot >= BTRFS_I(inode)->generation)
2626 /* the inode is shared */
2627 new = record_old_file_extents(inode, ordered_extent);
2628
2629 clear_extent_bit(io_tree, ordered_extent->file_offset,
2630 ordered_extent->file_offset + ordered_extent->len - 1,
2631 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2632 }
2633
Josef Bacik0cb59c92010-07-02 12:14:14 -04002634 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002635 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002636 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002637 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002638 if (IS_ERR(trans)) {
2639 ret = PTR_ERR(trans);
2640 trans = NULL;
2641 goto out_unlock;
2642 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002643 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002644
Chris Masonc8b97812008-10-29 14:49:59 -04002645 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002646 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002647 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002648 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002649 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002650 ordered_extent->file_offset,
2651 ordered_extent->file_offset +
2652 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002653 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002654 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002655 ret = insert_reserved_file_extent(trans, inode,
2656 ordered_extent->file_offset,
2657 ordered_extent->start,
2658 ordered_extent->disk_len,
2659 ordered_extent->len,
2660 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002661 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002662 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002663 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002664 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2665 ordered_extent->file_offset, ordered_extent->len,
2666 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002667 if (ret < 0) {
2668 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002669 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002670 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002671
Chris Masone6dcd2d2008-07-17 12:53:50 -04002672 add_pending_csums(trans, inode, ordered_extent->file_offset,
2673 &ordered_extent->list);
2674
Josef Bacik6c760c02012-11-09 10:53:21 -05002675 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2676 ret = btrfs_update_inode_fallback(trans, root, inode);
2677 if (ret) { /* -ENOMEM or corruption */
2678 btrfs_abort_transaction(trans, root, ret);
2679 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002680 }
2681 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002682out_unlock:
2683 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2684 ordered_extent->file_offset +
2685 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002686out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002687 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002688 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002689 if (trans)
2690 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002691
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002692 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002693 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2694 ordered_extent->file_offset +
2695 ordered_extent->len - 1, NULL, GFP_NOFS);
2696
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002697 /*
2698 * If the ordered extent had an IOERR or something else went
2699 * wrong we need to return the space for this ordered extent
2700 * back to the allocator.
2701 */
2702 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2703 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2704 btrfs_free_reserved_extent(root, ordered_extent->start,
2705 ordered_extent->disk_len);
2706 }
2707
2708
Josef Bacik5fd02042012-05-02 14:00:54 -04002709 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002710 * This needs to be done to make sure anybody waiting knows we are done
2711 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002712 */
2713 btrfs_remove_ordered_extent(inode, ordered_extent);
2714
Liu Bo38c227d2013-01-29 03:18:40 +00002715 /* for snapshot-aware defrag */
2716 if (new)
2717 relink_file_extents(new);
2718
Chris Masone6dcd2d2008-07-17 12:53:50 -04002719 /* once for us */
2720 btrfs_put_ordered_extent(ordered_extent);
2721 /* once for the tree */
2722 btrfs_put_ordered_extent(ordered_extent);
2723
Josef Bacik5fd02042012-05-02 14:00:54 -04002724 return ret;
2725}
2726
2727static void finish_ordered_fn(struct btrfs_work *work)
2728{
2729 struct btrfs_ordered_extent *ordered_extent;
2730 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2731 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002732}
2733
Christoph Hellwigb2950862008-12-02 09:54:17 -05002734static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002735 struct extent_state *state, int uptodate)
2736{
Josef Bacik5fd02042012-05-02 14:00:54 -04002737 struct inode *inode = page->mapping->host;
2738 struct btrfs_root *root = BTRFS_I(inode)->root;
2739 struct btrfs_ordered_extent *ordered_extent = NULL;
2740 struct btrfs_workers *workers;
2741
liubo1abe9b82011-03-24 11:18:59 +00002742 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2743
Chris Mason8b62b722009-09-02 16:53:46 -04002744 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002745 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2746 end - start + 1, uptodate))
2747 return 0;
2748
2749 ordered_extent->work.func = finish_ordered_fn;
2750 ordered_extent->work.flags = 0;
2751
Liu Bo83eea1f2012-07-10 05:28:39 -06002752 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002753 workers = &root->fs_info->endio_freespace_worker;
2754 else
2755 workers = &root->fs_info->endio_write_workers;
2756 btrfs_queue_worker(workers, &ordered_extent->work);
2757
2758 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002759}
2760
Chris Masond352ac62008-09-29 15:18:18 -04002761/*
Chris Masond352ac62008-09-29 15:18:18 -04002762 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002763 * if there's a match, we allow the bio to finish. If not, the code in
2764 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002765 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002766static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002767 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002768{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002769 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002770 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002771 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002772 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002773 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002774 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002775 struct btrfs_root *root = BTRFS_I(inode)->root;
2776 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002777
Chris Masond20f7042008-12-08 16:58:54 -05002778 if (PageChecked(page)) {
2779 ClearPageChecked(page);
2780 goto good;
2781 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002782
2783 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002784 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002785
Yan Zheng17d217f2008-12-12 10:03:38 -05002786 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002787 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002788 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2789 GFP_NOFS);
2790 return 0;
2791 }
2792
Yanc2e639f2008-02-04 08:57:25 -05002793 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002794 private = state->private;
2795 ret = 0;
2796 } else {
2797 ret = get_state_private(io_tree, start, &private);
2798 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002799 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002800 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002801 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002802
Chris Masonff79f812007-10-15 16:22:25 -04002803 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2804 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002805 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002806 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002807
Cong Wang7ac687d2011-11-25 23:14:28 +08002808 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002809good:
Chris Mason07157aa2007-08-30 08:50:51 -04002810 return 0;
2811
2812zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002813 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002814 "private %llu\n",
2815 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002816 (unsigned long long)start, csum,
2817 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002818 memset(kaddr + offset, 1, end - start + 1);
2819 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002820 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002821 if (private == 0)
2822 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002823 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002824}
Chris Masonb888db22007-08-27 16:49:44 -04002825
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002826struct delayed_iput {
2827 struct list_head list;
2828 struct inode *inode;
2829};
2830
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002831/* JDM: If this is fs-wide, why can't we add a pointer to
2832 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002833void btrfs_add_delayed_iput(struct inode *inode)
2834{
2835 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2836 struct delayed_iput *delayed;
2837
2838 if (atomic_add_unless(&inode->i_count, -1, 1))
2839 return;
2840
2841 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2842 delayed->inode = inode;
2843
2844 spin_lock(&fs_info->delayed_iput_lock);
2845 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2846 spin_unlock(&fs_info->delayed_iput_lock);
2847}
2848
2849void btrfs_run_delayed_iputs(struct btrfs_root *root)
2850{
2851 LIST_HEAD(list);
2852 struct btrfs_fs_info *fs_info = root->fs_info;
2853 struct delayed_iput *delayed;
2854 int empty;
2855
2856 spin_lock(&fs_info->delayed_iput_lock);
2857 empty = list_empty(&fs_info->delayed_iputs);
2858 spin_unlock(&fs_info->delayed_iput_lock);
2859 if (empty)
2860 return;
2861
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002862 spin_lock(&fs_info->delayed_iput_lock);
2863 list_splice_init(&fs_info->delayed_iputs, &list);
2864 spin_unlock(&fs_info->delayed_iput_lock);
2865
2866 while (!list_empty(&list)) {
2867 delayed = list_entry(list.next, struct delayed_iput, list);
2868 list_del(&delayed->list);
2869 iput(delayed->inode);
2870 kfree(delayed);
2871 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002872}
2873
Yan, Zhengd68fc572010-05-16 10:49:58 -04002874/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002875 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002876 * files in the subvolume, it removes orphan item and frees block_rsv
2877 * structure.
2878 */
2879void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2880 struct btrfs_root *root)
2881{
Josef Bacik90290e12011-12-02 15:44:12 -05002882 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002883 int ret;
2884
Josef Bacik8a35d952012-05-23 14:26:42 -04002885 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002886 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2887 return;
2888
Josef Bacik90290e12011-12-02 15:44:12 -05002889 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002890 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002891 spin_unlock(&root->orphan_lock);
2892 return;
2893 }
2894
2895 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2896 spin_unlock(&root->orphan_lock);
2897 return;
2898 }
2899
2900 block_rsv = root->orphan_block_rsv;
2901 root->orphan_block_rsv = NULL;
2902 spin_unlock(&root->orphan_lock);
2903
Yan, Zhengd68fc572010-05-16 10:49:58 -04002904 if (root->orphan_item_inserted &&
2905 btrfs_root_refs(&root->root_item) > 0) {
2906 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2907 root->root_key.objectid);
2908 BUG_ON(ret);
2909 root->orphan_item_inserted = 0;
2910 }
2911
Josef Bacik90290e12011-12-02 15:44:12 -05002912 if (block_rsv) {
2913 WARN_ON(block_rsv->size > 0);
2914 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002915 }
2916}
2917
2918/*
Josef Bacik7b128762008-07-24 12:17:14 -04002919 * This creates an orphan entry for the given inode in case something goes
2920 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002921 *
2922 * NOTE: caller of this function should reserve 5 units of metadata for
2923 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002924 */
2925int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2926{
2927 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002928 struct btrfs_block_rsv *block_rsv = NULL;
2929 int reserve = 0;
2930 int insert = 0;
2931 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002932
Yan, Zhengd68fc572010-05-16 10:49:58 -04002933 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002934 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002935 if (!block_rsv)
2936 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002937 }
2938
Yan, Zhengd68fc572010-05-16 10:49:58 -04002939 spin_lock(&root->orphan_lock);
2940 if (!root->orphan_block_rsv) {
2941 root->orphan_block_rsv = block_rsv;
2942 } else if (block_rsv) {
2943 btrfs_free_block_rsv(root, block_rsv);
2944 block_rsv = NULL;
2945 }
Josef Bacik7b128762008-07-24 12:17:14 -04002946
Josef Bacik8a35d952012-05-23 14:26:42 -04002947 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2948 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002949#if 0
2950 /*
2951 * For proper ENOSPC handling, we should do orphan
2952 * cleanup when mounting. But this introduces backward
2953 * compatibility issue.
2954 */
2955 if (!xchg(&root->orphan_item_inserted, 1))
2956 insert = 2;
2957 else
2958 insert = 1;
2959#endif
2960 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002961 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002962 }
Josef Bacik7b128762008-07-24 12:17:14 -04002963
Josef Bacik72ac3c02012-05-23 14:13:11 -04002964 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2965 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002966 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002967 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002968
Yan, Zhengd68fc572010-05-16 10:49:58 -04002969 /* grab metadata reservation from transaction handle */
2970 if (reserve) {
2971 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002972 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002973 }
2974
2975 /* insert an orphan item to track this unlinked/truncated file */
2976 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002977 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002978 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002979 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2980 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002981 btrfs_abort_transaction(trans, root, ret);
2982 return ret;
2983 }
2984 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002985 }
2986
2987 /* insert an orphan item to track subvolume contains orphan files */
2988 if (insert >= 2) {
2989 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2990 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002991 if (ret && ret != -EEXIST) {
2992 btrfs_abort_transaction(trans, root, ret);
2993 return ret;
2994 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002995 }
2996 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002997}
2998
2999/*
3000 * We have done the truncate/delete so we can go ahead and remove the orphan
3001 * item for this particular inode.
3002 */
3003int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
3004{
3005 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003006 int delete_item = 0;
3007 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003008 int ret = 0;
3009
Yan, Zhengd68fc572010-05-16 10:49:58 -04003010 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003011 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3012 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003013 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003014
Josef Bacik72ac3c02012-05-23 14:13:11 -04003015 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3016 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003017 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003018 spin_unlock(&root->orphan_lock);
3019
3020 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003021 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003022 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003023 }
3024
Josef Bacik8a35d952012-05-23 14:26:42 -04003025 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003026 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003027 atomic_dec(&root->orphan_inodes);
3028 }
Josef Bacik7b128762008-07-24 12:17:14 -04003029
Yan, Zhengd68fc572010-05-16 10:49:58 -04003030 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003031}
3032
3033/*
3034 * this cleans up any orphans that may be left on the list from the last use
3035 * of this root.
3036 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003037int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003038{
3039 struct btrfs_path *path;
3040 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003041 struct btrfs_key key, found_key;
3042 struct btrfs_trans_handle *trans;
3043 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003044 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003045 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3046
Yan, Zhengd68fc572010-05-16 10:49:58 -04003047 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003048 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003049
3050 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003051 if (!path) {
3052 ret = -ENOMEM;
3053 goto out;
3054 }
Josef Bacik7b128762008-07-24 12:17:14 -04003055 path->reada = -1;
3056
3057 key.objectid = BTRFS_ORPHAN_OBJECTID;
3058 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3059 key.offset = (u64)-1;
3060
Josef Bacik7b128762008-07-24 12:17:14 -04003061 while (1) {
3062 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003063 if (ret < 0)
3064 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003065
3066 /*
3067 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003068 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003069 * find the key and see if we have stuff that matches
3070 */
3071 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003072 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003073 if (path->slots[0] == 0)
3074 break;
3075 path->slots[0]--;
3076 }
3077
3078 /* pull out the item */
3079 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003080 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3081
3082 /* make sure the item matches what we want */
3083 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3084 break;
3085 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3086 break;
3087
3088 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003089 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003090
3091 /*
3092 * this is where we are basically btrfs_lookup, without the
3093 * crossing root thing. we store the inode number in the
3094 * offset of the orphan item.
3095 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003096
3097 if (found_key.offset == last_objectid) {
3098 printk(KERN_ERR "btrfs: Error removing orphan entry, "
3099 "stopping orphan cleanup\n");
3100 ret = -EINVAL;
3101 goto out;
3102 }
3103
3104 last_objectid = found_key.offset;
3105
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003106 found_key.objectid = found_key.offset;
3107 found_key.type = BTRFS_INODE_ITEM_KEY;
3108 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003109 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003110 ret = PTR_RET(inode);
3111 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003112 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003113
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003114 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3115 struct btrfs_root *dead_root;
3116 struct btrfs_fs_info *fs_info = root->fs_info;
3117 int is_dead_root = 0;
3118
3119 /*
3120 * this is an orphan in the tree root. Currently these
3121 * could come from 2 sources:
3122 * a) a snapshot deletion in progress
3123 * b) a free space cache inode
3124 * We need to distinguish those two, as the snapshot
3125 * orphan must not get deleted.
3126 * find_dead_roots already ran before us, so if this
3127 * is a snapshot deletion, we should find the root
3128 * in the dead_roots list
3129 */
3130 spin_lock(&fs_info->trans_lock);
3131 list_for_each_entry(dead_root, &fs_info->dead_roots,
3132 root_list) {
3133 if (dead_root->root_key.objectid ==
3134 found_key.objectid) {
3135 is_dead_root = 1;
3136 break;
3137 }
3138 }
3139 spin_unlock(&fs_info->trans_lock);
3140 if (is_dead_root) {
3141 /* prevent this orphan from being found again */
3142 key.offset = found_key.objectid - 1;
3143 continue;
3144 }
3145 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003146 /*
3147 * Inode is already gone but the orphan item is still there,
3148 * kill the orphan item.
3149 */
3150 if (ret == -ESTALE) {
3151 trans = btrfs_start_transaction(root, 1);
3152 if (IS_ERR(trans)) {
3153 ret = PTR_ERR(trans);
3154 goto out;
3155 }
Josef Bacik8a35d952012-05-23 14:26:42 -04003156 printk(KERN_ERR "auto deleting %Lu\n",
3157 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003158 ret = btrfs_del_orphan_item(trans, root,
3159 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003160 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003161 btrfs_end_transaction(trans, root);
3162 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003163 }
Josef Bacik7b128762008-07-24 12:17:14 -04003164
Josef Bacik7b128762008-07-24 12:17:14 -04003165 /*
3166 * add this inode to the orphan list so btrfs_orphan_del does
3167 * the proper thing when we hit it
3168 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003169 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3170 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003171 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003172
Josef Bacik7b128762008-07-24 12:17:14 -04003173 /* if we have links, this was a truncate, lets do that */
3174 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003175 if (!S_ISREG(inode->i_mode)) {
3176 WARN_ON(1);
3177 iput(inode);
3178 continue;
3179 }
Josef Bacik7b128762008-07-24 12:17:14 -04003180 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003181
3182 /* 1 for the orphan item deletion. */
3183 trans = btrfs_start_transaction(root, 1);
3184 if (IS_ERR(trans)) {
3185 ret = PTR_ERR(trans);
3186 goto out;
3187 }
3188 ret = btrfs_orphan_add(trans, inode);
3189 btrfs_end_transaction(trans, root);
3190 if (ret)
3191 goto out;
3192
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003193 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003194 if (ret)
3195 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003196 } else {
3197 nr_unlink++;
3198 }
3199
3200 /* this will do delete_inode and everything for us */
3201 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003202 if (ret)
3203 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003204 }
Miao Xie3254c872011-11-10 20:45:05 -05003205 /* release the path since we're done with it */
3206 btrfs_release_path(path);
3207
Yan, Zhengd68fc572010-05-16 10:49:58 -04003208 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3209
3210 if (root->orphan_block_rsv)
3211 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3212 (u64)-1);
3213
3214 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003215 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003216 if (!IS_ERR(trans))
3217 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003218 }
Josef Bacik7b128762008-07-24 12:17:14 -04003219
3220 if (nr_unlink)
3221 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
3222 if (nr_truncate)
3223 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003224
3225out:
3226 if (ret)
3227 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
3228 btrfs_free_path(path);
3229 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003230}
3231
Chris Masond352ac62008-09-29 15:18:18 -04003232/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003233 * very simple check to peek ahead in the leaf looking for xattrs. If we
3234 * don't find any xattrs, we know there can't be any acls.
3235 *
3236 * slot is the slot the inode is in, objectid is the objectid of the inode
3237 */
3238static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3239 int slot, u64 objectid)
3240{
3241 u32 nritems = btrfs_header_nritems(leaf);
3242 struct btrfs_key found_key;
3243 int scanned = 0;
3244
3245 slot++;
3246 while (slot < nritems) {
3247 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3248
3249 /* we found a different objectid, there must not be acls */
3250 if (found_key.objectid != objectid)
3251 return 0;
3252
3253 /* we found an xattr, assume we've got an acl */
3254 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3255 return 1;
3256
3257 /*
3258 * we found a key greater than an xattr key, there can't
3259 * be any acls later on
3260 */
3261 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3262 return 0;
3263
3264 slot++;
3265 scanned++;
3266
3267 /*
3268 * it goes inode, inode backrefs, xattrs, extents,
3269 * so if there are a ton of hard links to an inode there can
3270 * be a lot of backrefs. Don't waste time searching too hard,
3271 * this is just an optimization
3272 */
3273 if (scanned >= 8)
3274 break;
3275 }
3276 /* we hit the end of the leaf before we found an xattr or
3277 * something larger than an xattr. We have to assume the inode
3278 * has acls
3279 */
3280 return 1;
3281}
3282
3283/*
Chris Masond352ac62008-09-29 15:18:18 -04003284 * read an inode from the btree into the in-memory inode
3285 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003286static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003287{
3288 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003289 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003290 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003291 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003292 struct btrfs_root *root = BTRFS_I(inode)->root;
3293 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003294 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003295 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003296 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003297 bool filled = false;
3298
3299 ret = btrfs_fill_inode(inode, &rdev);
3300 if (!ret)
3301 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003302
3303 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003304 if (!path)
3305 goto make_bad;
3306
Josef Bacikd90c7322011-05-17 09:50:54 -04003307 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003308 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003309
Chris Mason39279cc2007-06-12 06:35:45 -04003310 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003311 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003312 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003313
Chris Mason5f39d392007-10-15 16:14:19 -04003314 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003315
3316 if (filled)
3317 goto cache_acl;
3318
Chris Mason5f39d392007-10-15 16:14:19 -04003319 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3320 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003321 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003322 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003323 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3324 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003325 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003326
3327 tspec = btrfs_inode_atime(inode_item);
3328 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3329 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3330
3331 tspec = btrfs_inode_mtime(inode_item);
3332 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3333 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3334
3335 tspec = btrfs_inode_ctime(inode_item);
3336 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3337 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3338
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003339 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003340 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003341 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3342
3343 /*
3344 * If we were modified in the current generation and evicted from memory
3345 * and then re-read we need to do a full sync since we don't have any
3346 * idea about which extents were modified before we were evicted from
3347 * cache.
3348 */
3349 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3351 &BTRFS_I(inode)->runtime_flags);
3352
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003353 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003354 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003355 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003356 rdev = btrfs_inode_rdev(leaf, inode_item);
3357
Josef Bacikaec74772008-07-24 12:12:38 -04003358 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003359 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003360cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003361 /*
3362 * try to precache a NULL acl entry for files that don't have
3363 * any xattrs or acls
3364 */
Li Zefan33345d012011-04-20 10:31:50 +08003365 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3366 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003367 if (!maybe_acls)
3368 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003369
Chris Mason39279cc2007-06-12 06:35:45 -04003370 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003371
Chris Mason39279cc2007-06-12 06:35:45 -04003372 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003373 case S_IFREG:
3374 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003375 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003376 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003377 inode->i_fop = &btrfs_file_operations;
3378 inode->i_op = &btrfs_file_inode_operations;
3379 break;
3380 case S_IFDIR:
3381 inode->i_fop = &btrfs_dir_file_operations;
3382 if (root == root->fs_info->tree_root)
3383 inode->i_op = &btrfs_dir_ro_inode_operations;
3384 else
3385 inode->i_op = &btrfs_dir_inode_operations;
3386 break;
3387 case S_IFLNK:
3388 inode->i_op = &btrfs_symlink_inode_operations;
3389 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003390 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003391 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003392 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003393 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003394 init_special_inode(inode, inode->i_mode, rdev);
3395 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003396 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003397
3398 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003399 return;
3400
3401make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003402 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003403 make_bad_inode(inode);
3404}
3405
Chris Masond352ac62008-09-29 15:18:18 -04003406/*
3407 * given a leaf and an inode, copy the inode fields into the leaf
3408 */
Chris Masone02119d2008-09-05 16:13:11 -04003409static void fill_inode_item(struct btrfs_trans_handle *trans,
3410 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003411 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003412 struct inode *inode)
3413{
Liu Bo51fab692012-12-27 09:01:21 +00003414 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003415
Liu Bo51fab692012-12-27 09:01:21 +00003416 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003417
Liu Bo51fab692012-12-27 09:01:21 +00003418 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3419 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3420 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3421 &token);
3422 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3423 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003424
Liu Bo51fab692012-12-27 09:01:21 +00003425 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3426 inode->i_atime.tv_sec, &token);
3427 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3428 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003429
Liu Bo51fab692012-12-27 09:01:21 +00003430 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3431 inode->i_mtime.tv_sec, &token);
3432 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3433 inode->i_mtime.tv_nsec, &token);
3434
3435 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3436 inode->i_ctime.tv_sec, &token);
3437 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3438 inode->i_ctime.tv_nsec, &token);
3439
3440 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3441 &token);
3442 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3443 &token);
3444 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3445 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3446 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3447 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3448 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003449}
3450
Chris Masond352ac62008-09-29 15:18:18 -04003451/*
3452 * copy everything in the in-memory inode into the btree.
3453 */
Chris Mason21151332011-11-10 20:39:08 -05003454static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003455 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003456{
3457 struct btrfs_inode_item *inode_item;
3458 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003459 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003460 int ret;
3461
3462 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003463 if (!path)
3464 return -ENOMEM;
3465
Chris Masonb9473432009-03-13 11:00:37 -04003466 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003467 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3468 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003469 if (ret) {
3470 if (ret > 0)
3471 ret = -ENOENT;
3472 goto failed;
3473 }
3474
Chris Masonb4ce94d2009-02-04 09:25:08 -05003475 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003476 leaf = path->nodes[0];
3477 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003478 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003479
Chris Masone02119d2008-09-05 16:13:11 -04003480 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003481 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003482 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003483 ret = 0;
3484failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003485 btrfs_free_path(path);
3486 return ret;
3487}
3488
Chris Masond352ac62008-09-29 15:18:18 -04003489/*
Chris Mason21151332011-11-10 20:39:08 -05003490 * copy everything in the in-memory inode into the btree.
3491 */
3492noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3493 struct btrfs_root *root, struct inode *inode)
3494{
3495 int ret;
3496
3497 /*
3498 * If the inode is a free space inode, we can deadlock during commit
3499 * if we put it into the delayed code.
3500 *
3501 * The data relocation inode should also be directly updated
3502 * without delay
3503 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003504 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003505 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003506 btrfs_update_root_times(trans, root);
3507
Chris Mason21151332011-11-10 20:39:08 -05003508 ret = btrfs_delayed_update_inode(trans, root, inode);
3509 if (!ret)
3510 btrfs_set_inode_last_trans(trans, inode);
3511 return ret;
3512 }
3513
3514 return btrfs_update_inode_item(trans, root, inode);
3515}
3516
Josef Bacikbe6aef62012-10-22 15:43:12 -04003517noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3518 struct btrfs_root *root,
3519 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003520{
3521 int ret;
3522
3523 ret = btrfs_update_inode(trans, root, inode);
3524 if (ret == -ENOSPC)
3525 return btrfs_update_inode_item(trans, root, inode);
3526 return ret;
3527}
3528
3529/*
Chris Masond352ac62008-09-29 15:18:18 -04003530 * unlink helper that gets used here in inode.c and in the tree logging
3531 * recovery code. It remove a link in a directory with a given name, and
3532 * also drops the back refs in the inode to the directory
3533 */
Al Viro92986792011-03-04 17:14:37 +00003534static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3535 struct btrfs_root *root,
3536 struct inode *dir, struct inode *inode,
3537 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003538{
3539 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003540 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003541 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003542 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003543 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003544 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003545 u64 ino = btrfs_ino(inode);
3546 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003547
3548 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003549 if (!path) {
3550 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003551 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003552 }
3553
Chris Masonb9473432009-03-13 11:00:37 -04003554 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003555 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003556 name, name_len, -1);
3557 if (IS_ERR(di)) {
3558 ret = PTR_ERR(di);
3559 goto err;
3560 }
3561 if (!di) {
3562 ret = -ENOENT;
3563 goto err;
3564 }
Chris Mason5f39d392007-10-15 16:14:19 -04003565 leaf = path->nodes[0];
3566 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003567 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003568 if (ret)
3569 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003570 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003571
Li Zefan33345d012011-04-20 10:31:50 +08003572 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3573 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003574 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05003575 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08003576 "inode %llu parent %llu\n", name_len, name,
3577 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003578 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003579 goto err;
3580 }
3581
Miao Xie16cdcec2011-04-22 18:12:22 +08003582 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003583 if (ret) {
3584 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003585 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003586 }
Chris Mason39279cc2007-06-12 06:35:45 -04003587
Chris Masone02119d2008-09-05 16:13:11 -04003588 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003589 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003590 if (ret != 0 && ret != -ENOENT) {
3591 btrfs_abort_transaction(trans, root, ret);
3592 goto err;
3593 }
Chris Masone02119d2008-09-05 16:13:11 -04003594
3595 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3596 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003597 if (ret == -ENOENT)
3598 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003599err:
3600 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003601 if (ret)
3602 goto out;
3603
3604 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003605 inode_inc_iversion(inode);
3606 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003607 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003608 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003609out:
Chris Mason39279cc2007-06-12 06:35:45 -04003610 return ret;
3611}
3612
Al Viro92986792011-03-04 17:14:37 +00003613int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3614 struct btrfs_root *root,
3615 struct inode *dir, struct inode *inode,
3616 const char *name, int name_len)
3617{
3618 int ret;
3619 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3620 if (!ret) {
3621 btrfs_drop_nlink(inode);
3622 ret = btrfs_update_inode(trans, root, inode);
3623 }
3624 return ret;
3625}
3626
3627
Yan, Zhenga22285a2010-05-16 10:48:46 -04003628/* helper to check if there is any shared block in the path */
3629static int check_path_shared(struct btrfs_root *root,
3630 struct btrfs_path *path)
3631{
3632 struct extent_buffer *eb;
3633 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00003634 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003635
3636 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003637 int ret;
3638
Yan, Zhenga22285a2010-05-16 10:48:46 -04003639 if (!path->nodes[level])
3640 break;
3641 eb = path->nodes[level];
3642 if (!btrfs_block_can_be_shared(root, eb))
3643 continue;
3644 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
3645 &refs, NULL);
3646 if (refs > 1)
3647 return 1;
3648 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003649 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003650}
3651
3652/*
3653 * helper to start transaction for unlink and rmdir.
3654 *
3655 * unlink and rmdir are special in btrfs, they do not always free space.
3656 * so in enospc case, we should make sure they will free space before
3657 * allowing them to use the global metadata reservation.
3658 */
3659static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3660 struct dentry *dentry)
3661{
3662 struct btrfs_trans_handle *trans;
3663 struct btrfs_root *root = BTRFS_I(dir)->root;
3664 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003665 struct btrfs_dir_item *di;
3666 struct inode *inode = dentry->d_inode;
3667 u64 index;
3668 int check_link = 1;
3669 int err = -ENOSPC;
3670 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003671 u64 ino = btrfs_ino(inode);
3672 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003673
Josef Bacike70bea52011-10-11 14:18:24 -04003674 /*
3675 * 1 for the possible orphan item
3676 * 1 for the dir item
3677 * 1 for the dir index
3678 * 1 for the inode ref
3679 * 1 for the inode ref in the tree log
3680 * 2 for the dir entries in the log
3681 * 1 for the inode
3682 */
3683 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003684 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3685 return trans;
3686
Li Zefan33345d012011-04-20 10:31:50 +08003687 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003688 return ERR_PTR(-ENOSPC);
3689
3690 /* check if there is someone else holds reference */
3691 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3692 return ERR_PTR(-ENOSPC);
3693
3694 if (atomic_read(&inode->i_count) > 2)
3695 return ERR_PTR(-ENOSPC);
3696
3697 if (xchg(&root->fs_info->enospc_unlink, 1))
3698 return ERR_PTR(-ENOSPC);
3699
3700 path = btrfs_alloc_path();
3701 if (!path) {
3702 root->fs_info->enospc_unlink = 0;
3703 return ERR_PTR(-ENOMEM);
3704 }
3705
Josef Bacik3880a1b2011-10-14 14:46:51 -04003706 /* 1 for the orphan item */
3707 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003708 if (IS_ERR(trans)) {
3709 btrfs_free_path(path);
3710 root->fs_info->enospc_unlink = 0;
3711 return trans;
3712 }
3713
3714 path->skip_locking = 1;
3715 path->search_commit_root = 1;
3716
3717 ret = btrfs_lookup_inode(trans, root, path,
3718 &BTRFS_I(dir)->location, 0);
3719 if (ret < 0) {
3720 err = ret;
3721 goto out;
3722 }
3723 if (ret == 0) {
3724 if (check_path_shared(root, path))
3725 goto out;
3726 } else {
3727 check_link = 0;
3728 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003729 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003730
3731 ret = btrfs_lookup_inode(trans, root, path,
3732 &BTRFS_I(inode)->location, 0);
3733 if (ret < 0) {
3734 err = ret;
3735 goto out;
3736 }
3737 if (ret == 0) {
3738 if (check_path_shared(root, path))
3739 goto out;
3740 } else {
3741 check_link = 0;
3742 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003743 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003744
3745 if (ret == 0 && S_ISREG(inode->i_mode)) {
3746 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003747 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003748 if (ret < 0) {
3749 err = ret;
3750 goto out;
3751 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003752 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003753 if (check_path_shared(root, path))
3754 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003755 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003756 }
3757
3758 if (!check_link) {
3759 err = 0;
3760 goto out;
3761 }
3762
Li Zefan33345d012011-04-20 10:31:50 +08003763 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003764 dentry->d_name.name, dentry->d_name.len, 0);
3765 if (IS_ERR(di)) {
3766 err = PTR_ERR(di);
3767 goto out;
3768 }
3769 if (di) {
3770 if (check_path_shared(root, path))
3771 goto out;
3772 } else {
3773 err = 0;
3774 goto out;
3775 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003776 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003777
Mark Fashehf1863732012-08-08 11:32:27 -07003778 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3779 dentry->d_name.len, ino, dir_ino, 0,
3780 &index);
3781 if (ret) {
3782 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003783 goto out;
3784 }
Mark Fashehf1863732012-08-08 11:32:27 -07003785
Yan, Zhenga22285a2010-05-16 10:48:46 -04003786 if (check_path_shared(root, path))
3787 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003788
David Sterbab3b4aa72011-04-21 01:20:15 +02003789 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003790
Miao Xie16cdcec2011-04-22 18:12:22 +08003791 /*
3792 * This is a commit root search, if we can lookup inode item and other
3793 * relative items in the commit root, it means the transaction of
3794 * dir/file creation has been committed, and the dir index item that we
3795 * delay to insert has also been inserted into the commit root. So
3796 * we needn't worry about the delayed insertion of the dir index item
3797 * here.
3798 */
Li Zefan33345d012011-04-20 10:31:50 +08003799 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003800 dentry->d_name.name, dentry->d_name.len, 0);
3801 if (IS_ERR(di)) {
3802 err = PTR_ERR(di);
3803 goto out;
3804 }
3805 BUG_ON(ret == -ENOENT);
3806 if (check_path_shared(root, path))
3807 goto out;
3808
3809 err = 0;
3810out:
3811 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003812 /* Migrate the orphan reservation over */
3813 if (!err)
3814 err = btrfs_block_rsv_migrate(trans->block_rsv,
3815 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003816 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003817
Yan, Zhenga22285a2010-05-16 10:48:46 -04003818 if (err) {
3819 btrfs_end_transaction(trans, root);
3820 root->fs_info->enospc_unlink = 0;
3821 return ERR_PTR(err);
3822 }
3823
3824 trans->block_rsv = &root->fs_info->global_block_rsv;
3825 return trans;
3826}
3827
3828static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3829 struct btrfs_root *root)
3830{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003831 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003832 btrfs_block_rsv_release(root, trans->block_rsv,
3833 trans->bytes_reserved);
3834 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003835 BUG_ON(!root->fs_info->enospc_unlink);
3836 root->fs_info->enospc_unlink = 0;
3837 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003838 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003839}
3840
Chris Mason39279cc2007-06-12 06:35:45 -04003841static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3842{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003843 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003844 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003845 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003846 int ret;
3847
Yan, Zhenga22285a2010-05-16 10:48:46 -04003848 trans = __unlink_start_trans(dir, dentry);
3849 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003850 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003851
Chris Mason12fcfd22009-03-24 10:24:20 -04003852 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3853
Chris Masone02119d2008-09-05 16:13:11 -04003854 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3855 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003856 if (ret)
3857 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003858
Yan, Zhenga22285a2010-05-16 10:48:46 -04003859 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003860 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003861 if (ret)
3862 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003863 }
Josef Bacik7b128762008-07-24 12:17:14 -04003864
Tsutomu Itohb5324022011-07-19 07:27:20 +00003865out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003866 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003867 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003868 return ret;
3869}
3870
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003871int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3872 struct btrfs_root *root,
3873 struct inode *dir, u64 objectid,
3874 const char *name, int name_len)
3875{
3876 struct btrfs_path *path;
3877 struct extent_buffer *leaf;
3878 struct btrfs_dir_item *di;
3879 struct btrfs_key key;
3880 u64 index;
3881 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003882 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003883
3884 path = btrfs_alloc_path();
3885 if (!path)
3886 return -ENOMEM;
3887
Li Zefan33345d012011-04-20 10:31:50 +08003888 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003889 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003890 if (IS_ERR_OR_NULL(di)) {
3891 if (!di)
3892 ret = -ENOENT;
3893 else
3894 ret = PTR_ERR(di);
3895 goto out;
3896 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003897
3898 leaf = path->nodes[0];
3899 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3900 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3901 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003902 if (ret) {
3903 btrfs_abort_transaction(trans, root, ret);
3904 goto out;
3905 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003906 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003907
3908 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3909 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003910 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003911 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003912 if (ret != -ENOENT) {
3913 btrfs_abort_transaction(trans, root, ret);
3914 goto out;
3915 }
Li Zefan33345d012011-04-20 10:31:50 +08003916 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003917 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003918 if (IS_ERR_OR_NULL(di)) {
3919 if (!di)
3920 ret = -ENOENT;
3921 else
3922 ret = PTR_ERR(di);
3923 btrfs_abort_transaction(trans, root, ret);
3924 goto out;
3925 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003926
3927 leaf = path->nodes[0];
3928 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003929 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003930 index = key.offset;
3931 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003932 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003933
Miao Xie16cdcec2011-04-22 18:12:22 +08003934 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003935 if (ret) {
3936 btrfs_abort_transaction(trans, root, ret);
3937 goto out;
3938 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003939
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003940 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003941 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003942 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003943 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003944 if (ret)
3945 btrfs_abort_transaction(trans, root, ret);
3946out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003947 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003948 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003949}
3950
Chris Mason39279cc2007-06-12 06:35:45 -04003951static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3952{
3953 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003954 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003955 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003956 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003957
David Sterbab3ae2442012-09-13 16:04:34 -06003958 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003959 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003960 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3961 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003962
Yan, Zhenga22285a2010-05-16 10:48:46 -04003963 trans = __unlink_start_trans(dir, dentry);
3964 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003965 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003966
Li Zefan33345d012011-04-20 10:31:50 +08003967 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003968 err = btrfs_unlink_subvol(trans, root, dir,
3969 BTRFS_I(inode)->location.objectid,
3970 dentry->d_name.name,
3971 dentry->d_name.len);
3972 goto out;
3973 }
3974
Josef Bacik7b128762008-07-24 12:17:14 -04003975 err = btrfs_orphan_add(trans, inode);
3976 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003977 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003978
Chris Mason39279cc2007-06-12 06:35:45 -04003979 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003980 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3981 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003982 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003983 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003984out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003985 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003986 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003987
Chris Mason39279cc2007-06-12 06:35:45 -04003988 return err;
3989}
3990
Chris Mason323ac952008-10-01 19:05:46 -04003991/*
Chris Mason39279cc2007-06-12 06:35:45 -04003992 * this can truncate away extent items, csum items and directory items.
3993 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003994 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003995 *
3996 * csum items that cross the new i_size are truncated to the new size
3997 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003998 *
3999 * min_type is the minimum key type to truncate down to. If set to 0, this
4000 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004001 */
Yan, Zheng80825102009-11-12 09:35:36 +00004002int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4003 struct btrfs_root *root,
4004 struct inode *inode,
4005 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004006{
Chris Mason39279cc2007-06-12 06:35:45 -04004007 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004008 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004009 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004010 struct btrfs_key key;
4011 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004012 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004013 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004014 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004015 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004016 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004017 int found_extent;
4018 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004019 int pending_del_nr = 0;
4020 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004021 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004022 int ret;
4023 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004024 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004025
4026 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004027
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004028 path = btrfs_alloc_path();
4029 if (!path)
4030 return -ENOMEM;
4031 path->reada = -1;
4032
Josef Bacik5dc562c2012-08-17 13:14:17 -04004033 /*
4034 * We want to drop from the next block forward in case this new size is
4035 * not block aligned since we will be keeping the last block of the
4036 * extent just the way it is.
4037 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004038 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004039 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4040 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004041
Miao Xie16cdcec2011-04-22 18:12:22 +08004042 /*
4043 * This function is also used to drop the items in the log tree before
4044 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4045 * it is used to drop the loged items. So we shouldn't kill the delayed
4046 * items.
4047 */
4048 if (min_type == 0 && root == BTRFS_I(inode)->root)
4049 btrfs_kill_delayed_inode_items(inode);
4050
Li Zefan33345d012011-04-20 10:31:50 +08004051 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004052 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004053 key.type = (u8)-1;
4054
Chris Mason85e21ba2008-01-29 15:11:36 -05004055search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004056 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004057 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004058 if (ret < 0) {
4059 err = ret;
4060 goto out;
4061 }
Chris Masond3977122009-01-05 21:25:51 -05004062
Chris Mason85e21ba2008-01-29 15:11:36 -05004063 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004064 /* there are no items in the tree for us to truncate, we're
4065 * done
4066 */
Yan, Zheng80825102009-11-12 09:35:36 +00004067 if (path->slots[0] == 0)
4068 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004069 path->slots[0]--;
4070 }
4071
Chris Masond3977122009-01-05 21:25:51 -05004072 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004073 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004074 leaf = path->nodes[0];
4075 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4076 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004077
Li Zefan33345d012011-04-20 10:31:50 +08004078 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004079 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004080
Chris Mason85e21ba2008-01-29 15:11:36 -05004081 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004082 break;
4083
Chris Mason5f39d392007-10-15 16:14:19 -04004084 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004085 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004086 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004087 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004088 extent_type = btrfs_file_extent_type(leaf, fi);
4089 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004090 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004091 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004092 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004093 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004094 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004095 }
Yan008630c2007-11-07 13:31:09 -05004096 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004097 }
Yan, Zheng80825102009-11-12 09:35:36 +00004098 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004099 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004100 } else {
4101 if (item_end < new_size)
4102 break;
4103 if (found_key.offset >= new_size)
4104 del_item = 1;
4105 else
4106 del_item = 0;
4107 }
Chris Mason39279cc2007-06-12 06:35:45 -04004108 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004109 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004110 if (found_type != BTRFS_EXTENT_DATA_KEY)
4111 goto delete;
4112
4113 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004114 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004115 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004116 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004117 u64 orig_num_bytes =
4118 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004119 extent_num_bytes = ALIGN(new_size -
4120 found_key.offset,
4121 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004122 btrfs_set_file_extent_num_bytes(leaf, fi,
4123 extent_num_bytes);
4124 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004125 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004126 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004127 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004128 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004129 } else {
Chris Masondb945352007-10-15 16:15:53 -04004130 extent_num_bytes =
4131 btrfs_file_extent_disk_num_bytes(leaf,
4132 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004133 extent_offset = found_key.offset -
4134 btrfs_file_extent_offset(leaf, fi);
4135
Chris Mason39279cc2007-06-12 06:35:45 -04004136 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004137 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004138 if (extent_start != 0) {
4139 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004140 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004141 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004142 }
4143 }
Chris Mason90692182008-02-08 13:49:28 -05004144 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004145 /*
4146 * we can't truncate inline items that have had
4147 * special encodings
4148 */
4149 if (!del_item &&
4150 btrfs_file_extent_compression(leaf, fi) == 0 &&
4151 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4152 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004153 u32 size = new_size - found_key.offset;
4154
4155 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004156 inode_sub_bytes(inode, item_end + 1 -
4157 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004158 }
4159 size =
4160 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004161 btrfs_truncate_item(trans, root, path,
4162 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004163 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004164 inode_sub_bytes(inode, item_end + 1 -
4165 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004166 }
Chris Mason39279cc2007-06-12 06:35:45 -04004167 }
Chris Mason179e29e2007-11-01 11:28:41 -04004168delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004169 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004170 if (!pending_del_nr) {
4171 /* no pending yet, add ourselves */
4172 pending_del_slot = path->slots[0];
4173 pending_del_nr = 1;
4174 } else if (pending_del_nr &&
4175 path->slots[0] + 1 == pending_del_slot) {
4176 /* hop on the pending chunk */
4177 pending_del_nr++;
4178 pending_del_slot = path->slots[0];
4179 } else {
Chris Masond3977122009-01-05 21:25:51 -05004180 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004181 }
Chris Mason39279cc2007-06-12 06:35:45 -04004182 } else {
4183 break;
4184 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004185 if (found_extent && (root->ref_cows ||
4186 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004187 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004188 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004189 extent_num_bytes, 0,
4190 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004191 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004192 BUG_ON(ret);
4193 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004194
Yan, Zheng80825102009-11-12 09:35:36 +00004195 if (found_type == BTRFS_INODE_ITEM_KEY)
4196 break;
4197
4198 if (path->slots[0] == 0 ||
4199 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004200 if (pending_del_nr) {
4201 ret = btrfs_del_items(trans, root, path,
4202 pending_del_slot,
4203 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004204 if (ret) {
4205 btrfs_abort_transaction(trans,
4206 root, ret);
4207 goto error;
4208 }
Yan, Zheng80825102009-11-12 09:35:36 +00004209 pending_del_nr = 0;
4210 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004211 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004212 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004213 } else {
4214 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004215 }
Chris Mason39279cc2007-06-12 06:35:45 -04004216 }
Yan, Zheng80825102009-11-12 09:35:36 +00004217out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004218 if (pending_del_nr) {
4219 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4220 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004221 if (ret)
4222 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004223 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004224error:
Chris Mason39279cc2007-06-12 06:35:45 -04004225 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004226 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004227}
4228
Chris Masona52d9a82007-08-27 16:49:44 -04004229/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004230 * btrfs_truncate_page - read, zero a chunk and write a page
4231 * @inode - inode that we're zeroing
4232 * @from - the offset to start zeroing
4233 * @len - the length to zero, 0 to zero the entire range respective to the
4234 * offset
4235 * @front - zero up to the offset instead of from the offset on
4236 *
4237 * This will find the page for the "from" offset and cow the page and zero the
4238 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004239 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004240int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4241 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004242{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004243 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004244 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004245 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4246 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004247 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004248 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004249 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004250 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4251 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4252 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004253 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004254 int ret = 0;
4255 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004256 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004257
Josef Bacik2aaa6652012-08-29 14:27:18 -04004258 if ((offset & (blocksize - 1)) == 0 &&
4259 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004260 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004261 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004262 if (ret)
4263 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004264
Chris Mason211c17f2008-05-15 09:13:45 -04004265again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004266 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004267 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004268 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004269 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004270 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004271 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004272
4273 page_start = page_offset(page);
4274 page_end = page_start + PAGE_CACHE_SIZE - 1;
4275
Chris Masona52d9a82007-08-27 16:49:44 -04004276 if (!PageUptodate(page)) {
4277 ret = btrfs_readpage(NULL, page);
4278 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004279 if (page->mapping != mapping) {
4280 unlock_page(page);
4281 page_cache_release(page);
4282 goto again;
4283 }
Chris Masona52d9a82007-08-27 16:49:44 -04004284 if (!PageUptodate(page)) {
4285 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004286 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004287 }
4288 }
Chris Mason211c17f2008-05-15 09:13:45 -04004289 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004290
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004291 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004292 set_page_extent_mapped(page);
4293
4294 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4295 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004296 unlock_extent_cached(io_tree, page_start, page_end,
4297 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004298 unlock_page(page);
4299 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004300 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004301 btrfs_put_ordered_extent(ordered);
4302 goto again;
4303 }
4304
Josef Bacik2ac55d42010-02-03 19:33:23 +00004305 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004306 EXTENT_DIRTY | EXTENT_DELALLOC |
4307 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004308 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004309
Josef Bacik2ac55d42010-02-03 19:33:23 +00004310 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4311 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004312 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004313 unlock_extent_cached(io_tree, page_start, page_end,
4314 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004315 goto out_unlock;
4316 }
4317
Chris Masone6dcd2d2008-07-17 12:53:50 -04004318 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004319 if (!len)
4320 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004321 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004322 if (front)
4323 memset(kaddr, 0, offset);
4324 else
4325 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004326 flush_dcache_page(page);
4327 kunmap(page);
4328 }
Chris Mason247e7432008-07-17 12:53:51 -04004329 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004330 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004331 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4332 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004333
Chris Mason89642222008-07-24 09:41:53 -04004334out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004335 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004336 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004337 unlock_page(page);
4338 page_cache_release(page);
4339out:
4340 return ret;
4341}
4342
Josef Bacik695a0d02011-03-04 15:46:53 -05004343/*
4344 * This function puts in dummy file extents for the area we're creating a hole
4345 * for. So if we are truncating this file to a larger size we need to insert
4346 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4347 * the range between oldsize and size
4348 */
Josef Bacika41ad392011-01-31 15:30:16 -05004349int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004350{
4351 struct btrfs_trans_handle *trans;
4352 struct btrfs_root *root = BTRFS_I(inode)->root;
4353 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004354 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004355 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004356 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004357 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4358 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004359 u64 last_byte;
4360 u64 cur_offset;
4361 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004362 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004363
4364 if (size <= hole_start)
4365 return 0;
4366
Yan Zheng9036c102008-10-30 14:19:41 -04004367 while (1) {
4368 struct btrfs_ordered_extent *ordered;
4369 btrfs_wait_ordered_range(inode, hole_start,
4370 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004371 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004372 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004373 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4374 if (!ordered)
4375 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004376 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4377 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004378 btrfs_put_ordered_extent(ordered);
4379 }
4380
Yan Zheng9036c102008-10-30 14:19:41 -04004381 cur_offset = hole_start;
4382 while (1) {
4383 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4384 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004385 if (IS_ERR(em)) {
4386 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004387 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004388 break;
4389 }
Yan Zheng9036c102008-10-30 14:19:41 -04004390 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004391 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004392 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004393 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004394 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004395
Miao Xie36423202011-12-14 20:12:02 -05004396 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004397 if (IS_ERR(trans)) {
4398 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004399 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004400 }
Yan, Zheng80825102009-11-12 09:35:36 +00004401
Josef Bacik5dc562c2012-08-17 13:14:17 -04004402 err = btrfs_drop_extents(trans, root, inode,
4403 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004404 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004405 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004406 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004407 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004408 break;
Miao Xie5b397372011-09-11 10:52:24 -04004409 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004410
Yan Zheng9036c102008-10-30 14:19:41 -04004411 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004412 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004413 0, hole_size, 0, hole_size,
4414 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004415 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004416 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004417 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004418 break;
Miao Xie5b397372011-09-11 10:52:24 -04004419 }
Yan, Zheng80825102009-11-12 09:35:36 +00004420
Josef Bacik5dc562c2012-08-17 13:14:17 -04004421 btrfs_drop_extent_cache(inode, cur_offset,
4422 cur_offset + hole_size - 1, 0);
4423 hole_em = alloc_extent_map();
4424 if (!hole_em) {
4425 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4426 &BTRFS_I(inode)->runtime_flags);
4427 goto next;
4428 }
4429 hole_em->start = cur_offset;
4430 hole_em->len = hole_size;
4431 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004432
Josef Bacik5dc562c2012-08-17 13:14:17 -04004433 hole_em->block_start = EXTENT_MAP_HOLE;
4434 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004435 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004436 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4437 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4438 hole_em->generation = trans->transid;
4439
4440 while (1) {
4441 write_lock(&em_tree->lock);
4442 err = add_extent_mapping(em_tree, hole_em);
4443 if (!err)
4444 list_move(&hole_em->list,
4445 &em_tree->modified_extents);
4446 write_unlock(&em_tree->lock);
4447 if (err != -EEXIST)
4448 break;
4449 btrfs_drop_extent_cache(inode, cur_offset,
4450 cur_offset +
4451 hole_size - 1, 0);
4452 }
4453 free_extent_map(hole_em);
4454next:
Miao Xie36423202011-12-14 20:12:02 -05004455 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004456 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004457 }
4458 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004459 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004460 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004461 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004462 break;
4463 }
4464
Yan, Zhenga22285a2010-05-16 10:48:46 -04004465 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004466 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4467 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004468 return err;
4469}
4470
Eric Sandeen3972f262013-01-12 02:57:22 +00004471static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004472{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004473 struct btrfs_root *root = BTRFS_I(inode)->root;
4474 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004475 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004476 loff_t newsize = attr->ia_size;
4477 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004478 int ret;
4479
Josef Bacika41ad392011-01-31 15:30:16 -05004480 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004481 return 0;
4482
Eric Sandeen3972f262013-01-12 02:57:22 +00004483 /*
4484 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4485 * special case where we need to update the times despite not having
4486 * these flags set. For all other operations the VFS set these flags
4487 * explicitly if it wants a timestamp update.
4488 */
4489 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4490 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4491
Josef Bacika41ad392011-01-31 15:30:16 -05004492 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004493 truncate_pagecache(inode, oldsize, newsize);
4494 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004495 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004496 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004497
Miao Xief4a2f4c2011-12-14 20:12:01 -05004498 trans = btrfs_start_transaction(root, 1);
4499 if (IS_ERR(trans))
4500 return PTR_ERR(trans);
4501
4502 i_size_write(inode, newsize);
4503 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4504 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004505 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004506 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004507
Josef Bacika41ad392011-01-31 15:30:16 -05004508 /*
4509 * We're truncating a file that used to have good data down to
4510 * zero. Make sure it gets into the ordered flush list so that
4511 * any new writes get down to disk quickly.
4512 */
4513 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004514 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4515 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004516
Josef Bacikf3fe8202013-01-07 17:03:21 -05004517 /*
4518 * 1 for the orphan item we're going to add
4519 * 1 for the orphan item deletion.
4520 */
4521 trans = btrfs_start_transaction(root, 2);
4522 if (IS_ERR(trans))
4523 return PTR_ERR(trans);
4524
4525 /*
4526 * We need to do this in case we fail at _any_ point during the
4527 * actual truncate. Once we do the truncate_setsize we could
4528 * invalidate pages which forces any outstanding ordered io to
4529 * be instantly completed which will give us extents that need
4530 * to be truncated. If we fail to get an orphan inode down we
4531 * could have left over extents that were never meant to live,
4532 * so we need to garuntee from this point on that everything
4533 * will be consistent.
4534 */
4535 ret = btrfs_orphan_add(trans, inode);
4536 btrfs_end_transaction(trans, root);
4537 if (ret)
4538 return ret;
4539
Josef Bacika41ad392011-01-31 15:30:16 -05004540 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4541 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004542
4543 /* Disable nonlocked read DIO to avoid the end less truncate */
4544 btrfs_inode_block_unlocked_dio(inode);
4545 inode_dio_wait(inode);
4546 btrfs_inode_resume_unlocked_dio(inode);
4547
Josef Bacika41ad392011-01-31 15:30:16 -05004548 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004549 if (ret && inode->i_nlink)
4550 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004551 }
4552
Josef Bacika41ad392011-01-31 15:30:16 -05004553 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004554}
4555
Chris Mason39279cc2007-06-12 06:35:45 -04004556static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4557{
4558 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004559 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004560 int err;
4561
Li Zefanb83cc962010-12-20 16:04:08 +08004562 if (btrfs_root_readonly(root))
4563 return -EROFS;
4564
Chris Mason39279cc2007-06-12 06:35:45 -04004565 err = inode_change_ok(inode, attr);
4566 if (err)
4567 return err;
4568
Chris Mason5a3f23d2009-03-31 13:27:11 -04004569 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004570 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004571 if (err)
4572 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004573 }
Yan Zheng9036c102008-10-30 14:19:41 -04004574
Christoph Hellwig10257742010-06-04 11:30:02 +02004575 if (attr->ia_valid) {
4576 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004577 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004578 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004579
Josef Bacik22c44fe2011-11-30 10:45:38 -05004580 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004581 err = btrfs_acl_chmod(inode);
4582 }
4583
Chris Mason39279cc2007-06-12 06:35:45 -04004584 return err;
4585}
Chris Mason61295eb2008-01-14 16:24:38 -05004586
Al Virobd555972010-06-07 11:35:40 -04004587void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004588{
4589 struct btrfs_trans_handle *trans;
4590 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004591 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004592 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004593 int ret;
4594
liubo1abe9b82011-03-24 11:18:59 +00004595 trace_btrfs_inode_evict(inode);
4596
Chris Mason39279cc2007-06-12 06:35:45 -04004597 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004598 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004599 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004600 goto no_delete;
4601
Chris Mason39279cc2007-06-12 06:35:45 -04004602 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004603 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004604 goto no_delete;
4605 }
Al Virobd555972010-06-07 11:35:40 -04004606 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004607 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004608
Yan, Zhengc71bf092009-11-12 09:34:40 +00004609 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004610 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004611 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004612 goto no_delete;
4613 }
4614
Yan, Zheng76dda932009-09-21 16:00:26 -04004615 if (inode->i_nlink > 0) {
4616 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4617 goto no_delete;
4618 }
4619
Miao Xie0e8c36a2012-12-19 06:59:51 +00004620 ret = btrfs_commit_inode_delayed_inode(inode);
4621 if (ret) {
4622 btrfs_orphan_del(NULL, inode);
4623 goto no_delete;
4624 }
4625
Miao Xie66d8f3d2012-09-06 04:02:28 -06004626 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004627 if (!rsv) {
4628 btrfs_orphan_del(NULL, inode);
4629 goto no_delete;
4630 }
Josef Bacik4a338542011-08-29 11:01:31 -04004631 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004632 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004633 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004634
Chris Masondbe674a2008-07-17 12:54:05 -04004635 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004636
Josef Bacik4289a662011-08-05 13:22:24 -04004637 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004638 * This is a bit simpler than btrfs_truncate since we've already
4639 * reserved our space for our orphan item in the unlink, so we just
4640 * need to reserve some slack space in case we add bytes and update
4641 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004642 */
Yan, Zheng80825102009-11-12 09:35:36 +00004643 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004644 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4645 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004646
Josef Bacik726c35f2011-09-26 15:46:06 -04004647 /*
4648 * Try and steal from the global reserve since we will
4649 * likely not use this space anyway, we want to try as
4650 * hard as possible to get this to work.
4651 */
4652 if (ret)
4653 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4654
Yan, Zhengd68fc572010-05-16 10:49:58 -04004655 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04004656 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04004657 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004658 btrfs_orphan_del(NULL, inode);
4659 btrfs_free_block_rsv(root, rsv);
4660 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004661 }
4662
Miao Xie0e8c36a2012-12-19 06:59:51 +00004663 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004664 if (IS_ERR(trans)) {
4665 btrfs_orphan_del(NULL, inode);
4666 btrfs_free_block_rsv(root, rsv);
4667 goto no_delete;
4668 }
4669
4670 trans->block_rsv = rsv;
4671
Yan, Zhengd68fc572010-05-16 10:49:58 -04004672 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004673 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004674 break;
4675
Miao Xie8407aa42012-09-07 01:43:32 -06004676 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004677 btrfs_end_transaction(trans, root);
4678 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004679 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004680 }
4681
Josef Bacik4289a662011-08-05 13:22:24 -04004682 btrfs_free_block_rsv(root, rsv);
4683
Yan, Zheng80825102009-11-12 09:35:36 +00004684 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004685 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004686 ret = btrfs_orphan_del(trans, inode);
4687 BUG_ON(ret);
4688 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004689
Josef Bacik4289a662011-08-05 13:22:24 -04004690 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004691 if (!(root == root->fs_info->tree_root ||
4692 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004693 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004694
Chris Mason54aa1f42007-06-22 14:16:25 -04004695 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004696 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004697no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004698 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004699 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004700}
4701
4702/*
4703 * this returns the key found in the dir entry in the location pointer.
4704 * If no dir entries were found, location->objectid is 0.
4705 */
4706static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4707 struct btrfs_key *location)
4708{
4709 const char *name = dentry->d_name.name;
4710 int namelen = dentry->d_name.len;
4711 struct btrfs_dir_item *di;
4712 struct btrfs_path *path;
4713 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004714 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004715
4716 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004717 if (!path)
4718 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004719
Li Zefan33345d012011-04-20 10:31:50 +08004720 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004721 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004722 if (IS_ERR(di))
4723 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004724
David Sterbac7040052011-04-19 18:00:01 +02004725 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004726 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004727
Chris Mason5f39d392007-10-15 16:14:19 -04004728 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004729out:
Chris Mason39279cc2007-06-12 06:35:45 -04004730 btrfs_free_path(path);
4731 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004732out_err:
4733 location->objectid = 0;
4734 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004735}
4736
4737/*
4738 * when we hit a tree root in a directory, the btrfs part of the inode
4739 * needs to be changed to reflect the root directory of the tree root. This
4740 * is kind of like crossing a mount point.
4741 */
4742static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004743 struct inode *dir,
4744 struct dentry *dentry,
4745 struct btrfs_key *location,
4746 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004747{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004748 struct btrfs_path *path;
4749 struct btrfs_root *new_root;
4750 struct btrfs_root_ref *ref;
4751 struct extent_buffer *leaf;
4752 int ret;
4753 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004754
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004755 path = btrfs_alloc_path();
4756 if (!path) {
4757 err = -ENOMEM;
4758 goto out;
4759 }
Chris Mason39279cc2007-06-12 06:35:45 -04004760
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004761 err = -ENOENT;
4762 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4763 BTRFS_I(dir)->root->root_key.objectid,
4764 location->objectid);
4765 if (ret) {
4766 if (ret < 0)
4767 err = ret;
4768 goto out;
4769 }
Chris Mason39279cc2007-06-12 06:35:45 -04004770
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004771 leaf = path->nodes[0];
4772 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004773 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004774 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4775 goto out;
4776
4777 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4778 (unsigned long)(ref + 1),
4779 dentry->d_name.len);
4780 if (ret)
4781 goto out;
4782
David Sterbab3b4aa72011-04-21 01:20:15 +02004783 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004784
4785 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4786 if (IS_ERR(new_root)) {
4787 err = PTR_ERR(new_root);
4788 goto out;
4789 }
4790
4791 if (btrfs_root_refs(&new_root->root_item) == 0) {
4792 err = -ENOENT;
4793 goto out;
4794 }
4795
4796 *sub_root = new_root;
4797 location->objectid = btrfs_root_dirid(&new_root->root_item);
4798 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004799 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004800 err = 0;
4801out:
4802 btrfs_free_path(path);
4803 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004804}
4805
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004806static void inode_tree_add(struct inode *inode)
4807{
4808 struct btrfs_root *root = BTRFS_I(inode)->root;
4809 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004810 struct rb_node **p;
4811 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004812 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004813again:
4814 p = &root->inode_tree.rb_node;
4815 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004816
Al Viro1d3382cb2010-10-23 15:19:20 -04004817 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004818 return;
4819
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004820 spin_lock(&root->inode_lock);
4821 while (*p) {
4822 parent = *p;
4823 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4824
Li Zefan33345d012011-04-20 10:31:50 +08004825 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004826 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004827 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004828 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004829 else {
4830 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004831 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004832 rb_erase(parent, &root->inode_tree);
4833 RB_CLEAR_NODE(parent);
4834 spin_unlock(&root->inode_lock);
4835 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004836 }
4837 }
4838 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4839 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4840 spin_unlock(&root->inode_lock);
4841}
4842
4843static void inode_tree_del(struct inode *inode)
4844{
4845 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004846 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004847
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004848 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004849 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004850 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004851 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004852 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004853 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004854 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004855
Josef Bacik0af3d002010-06-21 14:48:16 -04004856 /*
4857 * Free space cache has inodes in the tree root, but the tree root has a
4858 * root_refs of 0, so this could end up dropping the tree root as a
4859 * snapshot, so we need the extra !root->fs_info->tree_root check to
4860 * make sure we don't drop it.
4861 */
4862 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4863 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004864 synchronize_srcu(&root->fs_info->subvol_srcu);
4865 spin_lock(&root->inode_lock);
4866 empty = RB_EMPTY_ROOT(&root->inode_tree);
4867 spin_unlock(&root->inode_lock);
4868 if (empty)
4869 btrfs_add_dead_root(root);
4870 }
4871}
4872
Jeff Mahoney143bede2012-03-01 14:56:26 +01004873void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004874{
4875 struct rb_node *node;
4876 struct rb_node *prev;
4877 struct btrfs_inode *entry;
4878 struct inode *inode;
4879 u64 objectid = 0;
4880
4881 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4882
4883 spin_lock(&root->inode_lock);
4884again:
4885 node = root->inode_tree.rb_node;
4886 prev = NULL;
4887 while (node) {
4888 prev = node;
4889 entry = rb_entry(node, struct btrfs_inode, rb_node);
4890
Li Zefan33345d012011-04-20 10:31:50 +08004891 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004892 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004893 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004894 node = node->rb_right;
4895 else
4896 break;
4897 }
4898 if (!node) {
4899 while (prev) {
4900 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004901 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004902 node = prev;
4903 break;
4904 }
4905 prev = rb_next(prev);
4906 }
4907 }
4908 while (node) {
4909 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004910 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004911 inode = igrab(&entry->vfs_inode);
4912 if (inode) {
4913 spin_unlock(&root->inode_lock);
4914 if (atomic_read(&inode->i_count) > 1)
4915 d_prune_aliases(inode);
4916 /*
Al Viro45321ac2010-06-07 13:43:19 -04004917 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004918 * the inode cache when its usage count
4919 * hits zero.
4920 */
4921 iput(inode);
4922 cond_resched();
4923 spin_lock(&root->inode_lock);
4924 goto again;
4925 }
4926
4927 if (cond_resched_lock(&root->inode_lock))
4928 goto again;
4929
4930 node = rb_next(node);
4931 }
4932 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004933}
4934
Chris Masone02119d2008-09-05 16:13:11 -04004935static int btrfs_init_locked_inode(struct inode *inode, void *p)
4936{
4937 struct btrfs_iget_args *args = p;
4938 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004939 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004940 return 0;
4941}
4942
4943static int btrfs_find_actor(struct inode *inode, void *opaque)
4944{
4945 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004946 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004947 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004948}
4949
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004950static struct inode *btrfs_iget_locked(struct super_block *s,
4951 u64 objectid,
4952 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004953{
4954 struct inode *inode;
4955 struct btrfs_iget_args args;
4956 args.ino = objectid;
4957 args.root = root;
4958
4959 inode = iget5_locked(s, objectid, btrfs_find_actor,
4960 btrfs_init_locked_inode,
4961 (void *)&args);
4962 return inode;
4963}
4964
Balaji Rao1a54ef82008-07-21 02:01:04 +05304965/* Get an inode object given its location and corresponding root.
4966 * Returns in *is_new if the inode was read from disk
4967 */
4968struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004969 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304970{
4971 struct inode *inode;
4972
4973 inode = btrfs_iget_locked(s, location->objectid, root);
4974 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004975 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304976
4977 if (inode->i_state & I_NEW) {
4978 BTRFS_I(inode)->root = root;
4979 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4980 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004981 if (!is_bad_inode(inode)) {
4982 inode_tree_add(inode);
4983 unlock_new_inode(inode);
4984 if (new)
4985 *new = 1;
4986 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004987 unlock_new_inode(inode);
4988 iput(inode);
4989 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004990 }
4991 }
4992
Balaji Rao1a54ef82008-07-21 02:01:04 +05304993 return inode;
4994}
4995
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004996static struct inode *new_simple_dir(struct super_block *s,
4997 struct btrfs_key *key,
4998 struct btrfs_root *root)
4999{
5000 struct inode *inode = new_inode(s);
5001
5002 if (!inode)
5003 return ERR_PTR(-ENOMEM);
5004
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005005 BTRFS_I(inode)->root = root;
5006 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005007 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005008
5009 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005010 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005011 inode->i_fop = &simple_dir_operations;
5012 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5013 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5014
5015 return inode;
5016}
5017
Chris Mason3de45862008-11-17 21:02:50 -05005018struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005019{
Chris Masond3977122009-01-05 21:25:51 -05005020 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005021 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005022 struct btrfs_root *sub_root = root;
5023 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005024 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005025 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005026
5027 if (dentry->d_name.len > BTRFS_NAME_LEN)
5028 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005029
Jeff Layton39e3c952012-11-28 11:30:53 -05005030 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005031 if (ret < 0)
5032 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005033
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005034 if (location.objectid == 0)
5035 return NULL;
5036
5037 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005038 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005039 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005040 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005041
5042 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5043
Yan, Zheng76dda932009-09-21 16:00:26 -04005044 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005045 ret = fixup_tree_root_location(root, dir, dentry,
5046 &location, &sub_root);
5047 if (ret < 0) {
5048 if (ret != -ENOENT)
5049 inode = ERR_PTR(ret);
5050 else
5051 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5052 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005053 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005054 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005055 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5056
Julia Lawall34d19ba2011-01-24 19:55:19 +00005057 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005058 down_read(&root->fs_info->cleanup_work_sem);
5059 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005060 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005061 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005062 if (ret)
5063 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005064 }
5065
Chris Mason3de45862008-11-17 21:02:50 -05005066 return inode;
5067}
5068
Nick Pigginfe15ce42011-01-07 17:49:23 +11005069static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005070{
5071 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005072 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005073
Li Zefan848cce02012-02-21 17:04:28 +08005074 if (!inode && !IS_ROOT(dentry))
5075 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005076
Li Zefan848cce02012-02-21 17:04:28 +08005077 if (inode) {
5078 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005079 if (btrfs_root_refs(&root->root_item) == 0)
5080 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005081
5082 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5083 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005084 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005085 return 0;
5086}
5087
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005088static void btrfs_dentry_release(struct dentry *dentry)
5089{
5090 if (dentry->d_fsdata)
5091 kfree(dentry->d_fsdata);
5092}
5093
Chris Mason3de45862008-11-17 21:02:50 -05005094static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005095 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005096{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005097 struct dentry *ret;
5098
5099 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005100 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005101}
5102
Miao Xie16cdcec2011-04-22 18:12:22 +08005103unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005104 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5105};
5106
David Woodhousecbdf5a22008-08-06 19:42:33 +01005107static int btrfs_real_readdir(struct file *filp, void *dirent,
5108 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005109{
Al Viro496ad9a2013-01-23 17:07:38 -05005110 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005111 struct btrfs_root *root = BTRFS_I(inode)->root;
5112 struct btrfs_item *item;
5113 struct btrfs_dir_item *di;
5114 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005115 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005116 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005117 struct list_head ins_list;
5118 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005119 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005120 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005121 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005122 unsigned char d_type;
5123 int over = 0;
5124 u32 di_cur;
5125 u32 di_total;
5126 u32 di_len;
5127 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005128 char tmp_name[32];
5129 char *name_ptr;
5130 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005131 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005132
5133 /* FIXME, use a real flag for deciding about the key type */
5134 if (root->fs_info->tree_root == root)
5135 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005136
Chris Mason39544012007-12-12 14:38:19 -05005137 /* special case for "." */
5138 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005139 over = filldir(dirent, ".", 1,
5140 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005141 if (over)
5142 return 0;
5143 filp->f_pos = 1;
5144 }
Chris Mason39544012007-12-12 14:38:19 -05005145 /* special case for .., just use the back ref */
5146 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005147 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005148 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005149 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005150 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005151 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005152 filp->f_pos = 2;
5153 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005154 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005155 if (!path)
5156 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005157
Josef Bacik026fd312011-05-13 10:32:11 -04005158 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005159
Miao Xie16cdcec2011-04-22 18:12:22 +08005160 if (key_type == BTRFS_DIR_INDEX_KEY) {
5161 INIT_LIST_HEAD(&ins_list);
5162 INIT_LIST_HEAD(&del_list);
5163 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5164 }
5165
Chris Mason39279cc2007-06-12 06:35:45 -04005166 btrfs_set_key_type(&key, key_type);
5167 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005168 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005169
Chris Mason39279cc2007-06-12 06:35:45 -04005170 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5171 if (ret < 0)
5172 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005173
5174 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005175 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005176 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005177 if (slot >= btrfs_header_nritems(leaf)) {
5178 ret = btrfs_next_leaf(root, path);
5179 if (ret < 0)
5180 goto err;
5181 else if (ret > 0)
5182 break;
5183 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005184 }
Chris Mason3de45862008-11-17 21:02:50 -05005185
Chris Mason5f39d392007-10-15 16:14:19 -04005186 item = btrfs_item_nr(leaf, slot);
5187 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5188
5189 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005190 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005191 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005192 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005193 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005194 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005195 if (key_type == BTRFS_DIR_INDEX_KEY &&
5196 btrfs_should_delete_dir_index(&del_list,
5197 found_key.offset))
5198 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005199
5200 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005201 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005202
Chris Mason39279cc2007-06-12 06:35:45 -04005203 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5204 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005205 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005206
5207 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005208 struct btrfs_key location;
5209
Josef Bacik22a94d42011-03-16 16:47:17 -04005210 if (verify_dir_item(root, leaf, di))
5211 break;
5212
Chris Mason5f39d392007-10-15 16:14:19 -04005213 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005214 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005215 name_ptr = tmp_name;
5216 } else {
5217 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005218 if (!name_ptr) {
5219 ret = -ENOMEM;
5220 goto err;
5221 }
Chris Mason5f39d392007-10-15 16:14:19 -04005222 }
5223 read_extent_buffer(leaf, name_ptr,
5224 (unsigned long)(di + 1), name_len);
5225
5226 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5227 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005228
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005229
Chris Mason3de45862008-11-17 21:02:50 -05005230 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005231 * skip it.
5232 *
5233 * In contrast to old kernels, we insert the snapshot's
5234 * dir item and dir index after it has been created, so
5235 * we won't find a reference to our own snapshot. We
5236 * still keep the following code for backward
5237 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005238 */
5239 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5240 location.objectid == root->root_key.objectid) {
5241 over = 0;
5242 goto skip;
5243 }
Chris Mason5f39d392007-10-15 16:14:19 -04005244 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005245 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005246 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005247
Chris Mason3de45862008-11-17 21:02:50 -05005248skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005249 if (name_ptr != tmp_name)
5250 kfree(name_ptr);
5251
Chris Mason39279cc2007-06-12 06:35:45 -04005252 if (over)
5253 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005254 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005255 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005256 di_cur += di_len;
5257 di = (struct btrfs_dir_item *)((char *)di + di_len);
5258 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005259next:
5260 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005261 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005262
Miao Xie16cdcec2011-04-22 18:12:22 +08005263 if (key_type == BTRFS_DIR_INDEX_KEY) {
5264 if (is_curr)
5265 filp->f_pos++;
5266 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5267 &ins_list);
5268 if (ret)
5269 goto nopos;
5270 }
5271
David Woodhouse49593bf2008-08-17 17:08:36 +01005272 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005273 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005274 /*
5275 * 32-bit glibc will use getdents64, but then strtol -
5276 * so the last number we can serve is this.
5277 */
5278 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005279 else
5280 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005281nopos:
5282 ret = 0;
5283err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005284 if (key_type == BTRFS_DIR_INDEX_KEY)
5285 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005286 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005287 return ret;
5288}
5289
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005290int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005291{
5292 struct btrfs_root *root = BTRFS_I(inode)->root;
5293 struct btrfs_trans_handle *trans;
5294 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005295 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005296
Josef Bacik72ac3c02012-05-23 14:13:11 -04005297 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005298 return 0;
5299
Liu Bo83eea1f2012-07-10 05:28:39 -06005300 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005301 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005302
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005303 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005304 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005305 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005306 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005307 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005308 if (IS_ERR(trans))
5309 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005310 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005311 }
5312 return ret;
5313}
5314
5315/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005316 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005317 * inode changes. But, it is most likely to find the inode in cache.
5318 * FIXME, needs more benchmarking...there are no reasons other than performance
5319 * to keep or drop this code.
5320 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05005321int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005322{
5323 struct btrfs_root *root = BTRFS_I(inode)->root;
5324 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005325 int ret;
5326
Josef Bacik72ac3c02012-05-23 14:13:11 -04005327 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005328 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005329
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005330 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005331 if (IS_ERR(trans))
5332 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005333
5334 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005335 if (ret && ret == -ENOSPC) {
5336 /* whoops, lets try again with the full transaction */
5337 btrfs_end_transaction(trans, root);
5338 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005339 if (IS_ERR(trans))
5340 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005341
Chris Mason94b60442010-05-26 11:02:00 -04005342 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005343 }
Chris Mason39279cc2007-06-12 06:35:45 -04005344 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005345 if (BTRFS_I(inode)->delayed_node)
5346 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005347
5348 return ret;
5349}
5350
5351/*
5352 * This is a copy of file_update_time. We need this so we can return error on
5353 * ENOSPC for updating the inode in the case of file write and mmap writes.
5354 */
Josef Bacike41f9412012-03-26 09:46:47 -04005355static int btrfs_update_time(struct inode *inode, struct timespec *now,
5356 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005357{
Alexander Block2bc5565282012-06-15 09:49:33 +02005358 struct btrfs_root *root = BTRFS_I(inode)->root;
5359
5360 if (btrfs_root_readonly(root))
5361 return -EROFS;
5362
Josef Bacike41f9412012-03-26 09:46:47 -04005363 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005364 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005365 if (flags & S_CTIME)
5366 inode->i_ctime = *now;
5367 if (flags & S_MTIME)
5368 inode->i_mtime = *now;
5369 if (flags & S_ATIME)
5370 inode->i_atime = *now;
5371 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005372}
5373
Chris Masond352ac62008-09-29 15:18:18 -04005374/*
5375 * find the highest existing sequence number in a directory
5376 * and then set the in-memory index_cnt variable to reflect
5377 * free sequence numbers
5378 */
Josef Bacikaec74772008-07-24 12:12:38 -04005379static int btrfs_set_inode_index_count(struct inode *inode)
5380{
5381 struct btrfs_root *root = BTRFS_I(inode)->root;
5382 struct btrfs_key key, found_key;
5383 struct btrfs_path *path;
5384 struct extent_buffer *leaf;
5385 int ret;
5386
Li Zefan33345d012011-04-20 10:31:50 +08005387 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005388 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5389 key.offset = (u64)-1;
5390
5391 path = btrfs_alloc_path();
5392 if (!path)
5393 return -ENOMEM;
5394
5395 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5396 if (ret < 0)
5397 goto out;
5398 /* FIXME: we should be able to handle this */
5399 if (ret == 0)
5400 goto out;
5401 ret = 0;
5402
5403 /*
5404 * MAGIC NUMBER EXPLANATION:
5405 * since we search a directory based on f_pos we have to start at 2
5406 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5407 * else has to start at 2
5408 */
5409 if (path->slots[0] == 0) {
5410 BTRFS_I(inode)->index_cnt = 2;
5411 goto out;
5412 }
5413
5414 path->slots[0]--;
5415
5416 leaf = path->nodes[0];
5417 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5418
Li Zefan33345d012011-04-20 10:31:50 +08005419 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005420 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5421 BTRFS_I(inode)->index_cnt = 2;
5422 goto out;
5423 }
5424
5425 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5426out:
5427 btrfs_free_path(path);
5428 return ret;
5429}
5430
Chris Masond352ac62008-09-29 15:18:18 -04005431/*
5432 * helper to find a free sequence number in a given directory. This current
5433 * code is very simple, later versions will do smarter things in the btree
5434 */
Chris Mason3de45862008-11-17 21:02:50 -05005435int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005436{
5437 int ret = 0;
5438
5439 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005440 ret = btrfs_inode_delayed_dir_index_count(dir);
5441 if (ret) {
5442 ret = btrfs_set_inode_index_count(dir);
5443 if (ret)
5444 return ret;
5445 }
Josef Bacikaec74772008-07-24 12:12:38 -04005446 }
5447
Chris Mason00e4e6b2008-08-05 11:18:09 -04005448 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005449 BTRFS_I(dir)->index_cnt++;
5450
5451 return ret;
5452}
5453
Chris Mason39279cc2007-06-12 06:35:45 -04005454static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5455 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005456 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005457 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005458 u64 ref_objectid, u64 objectid,
5459 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005460{
5461 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005462 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005463 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005464 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005465 struct btrfs_inode_ref *ref;
5466 struct btrfs_key key[2];
5467 u32 sizes[2];
5468 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005469 int ret;
5470 int owner;
5471
Chris Mason5f39d392007-10-15 16:14:19 -04005472 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005473 if (!path)
5474 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005475
Chris Mason39279cc2007-06-12 06:35:45 -04005476 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005477 if (!inode) {
5478 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005479 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005480 }
Chris Mason39279cc2007-06-12 06:35:45 -04005481
Li Zefan581bb052011-04-20 10:06:11 +08005482 /*
5483 * we have to initialize this early, so we can reclaim the inode
5484 * number if we fail afterwards in this function.
5485 */
5486 inode->i_ino = objectid;
5487
Josef Bacikaec74772008-07-24 12:12:38 -04005488 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005489 trace_btrfs_inode_request(dir);
5490
Chris Mason3de45862008-11-17 21:02:50 -05005491 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005492 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005493 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005494 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005495 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005496 }
Josef Bacikaec74772008-07-24 12:12:38 -04005497 }
5498 /*
5499 * index_cnt is ignored for everything but a dir,
5500 * btrfs_get_inode_index_count has an explanation for the magic
5501 * number
5502 */
5503 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005504 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005505 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005506 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005507
Josef Bacik5dc562c2012-08-17 13:14:17 -04005508 /*
5509 * We could have gotten an inode number from somebody who was fsynced
5510 * and then removed in this same transaction, so let's just set full
5511 * sync since it will be a full sync anyway and this will blow away the
5512 * old info in the log.
5513 */
5514 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5515
Al Viro569254b2011-07-24 17:08:40 -04005516 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005517 owner = 0;
5518 else
5519 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005520
5521 key[0].objectid = objectid;
5522 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5523 key[0].offset = 0;
5524
Mark Fashehf1863732012-08-08 11:32:27 -07005525 /*
5526 * Start new inodes with an inode_ref. This is slightly more
5527 * efficient for small numbers of hard links since they will
5528 * be packed into one item. Extended refs will kick in if we
5529 * add more hard links than can fit in the ref item.
5530 */
Chris Mason9c583092008-01-29 15:15:18 -05005531 key[1].objectid = objectid;
5532 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5533 key[1].offset = ref_objectid;
5534
5535 sizes[0] = sizeof(struct btrfs_inode_item);
5536 sizes[1] = name_len + sizeof(*ref);
5537
Chris Masonb9473432009-03-13 11:00:37 -04005538 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005539 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5540 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005541 goto fail;
5542
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005543 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005544 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005545 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005546 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5547 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005548 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5549 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005550 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005551
5552 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5553 struct btrfs_inode_ref);
5554 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005555 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005556 ptr = (unsigned long)(ref + 1);
5557 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5558
Chris Mason5f39d392007-10-15 16:14:19 -04005559 btrfs_mark_buffer_dirty(path->nodes[0]);
5560 btrfs_free_path(path);
5561
Chris Mason39279cc2007-06-12 06:35:45 -04005562 location = &BTRFS_I(inode)->location;
5563 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005564 location->offset = 0;
5565 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5566
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005567 btrfs_inherit_iflags(inode, dir);
5568
Al Viro569254b2011-07-24 17:08:40 -04005569 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005570 if (btrfs_test_opt(root, NODATASUM))
5571 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005572 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005573 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5574 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005575 }
5576
Chris Mason39279cc2007-06-12 06:35:45 -04005577 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005578 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005579
5580 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005581 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005582
Alexander Block8ea05e32012-07-25 17:35:53 +02005583 btrfs_update_root_times(trans, root);
5584
Chris Mason39279cc2007-06-12 06:35:45 -04005585 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005586fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005587 if (dir)
5588 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005589 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005590 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005591 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005592}
5593
5594static inline u8 btrfs_inode_type(struct inode *inode)
5595{
5596 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5597}
5598
Chris Masond352ac62008-09-29 15:18:18 -04005599/*
5600 * utility function to add 'inode' into 'parent_inode' with
5601 * a give name and a given sequence number.
5602 * if 'add_backref' is true, also insert a backref from the
5603 * inode to the parent directory.
5604 */
Chris Masone02119d2008-09-05 16:13:11 -04005605int btrfs_add_link(struct btrfs_trans_handle *trans,
5606 struct inode *parent_inode, struct inode *inode,
5607 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005608{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005609 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005610 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005611 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005612 u64 ino = btrfs_ino(inode);
5613 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005614
Li Zefan33345d012011-04-20 10:31:50 +08005615 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005616 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5617 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005618 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005619 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5620 key.offset = 0;
5621 }
Chris Mason39279cc2007-06-12 06:35:45 -04005622
Li Zefan33345d012011-04-20 10:31:50 +08005623 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005624 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5625 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005626 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005627 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005628 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5629 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005630 }
5631
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005632 /* Nothing to clean up yet */
5633 if (ret)
5634 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005635
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005636 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5637 parent_inode, &key,
5638 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005639 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005640 goto fail_dir_item;
5641 else if (ret) {
5642 btrfs_abort_transaction(trans, root, ret);
5643 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005644 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005645
5646 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5647 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005648 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005649 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5650 ret = btrfs_update_inode(trans, root, parent_inode);
5651 if (ret)
5652 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005653 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005654
5655fail_dir_item:
5656 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5657 u64 local_index;
5658 int err;
5659 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5660 key.objectid, root->root_key.objectid,
5661 parent_ino, &local_index, name, name_len);
5662
5663 } else if (add_backref) {
5664 u64 local_index;
5665 int err;
5666
5667 err = btrfs_del_inode_ref(trans, root, name, name_len,
5668 ino, parent_ino, &local_index);
5669 }
5670 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005671}
5672
5673static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005674 struct inode *dir, struct dentry *dentry,
5675 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005676{
Josef Bacika1b075d2010-11-19 20:36:11 +00005677 int err = btrfs_add_link(trans, dir, inode,
5678 dentry->d_name.name, dentry->d_name.len,
5679 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005680 if (err > 0)
5681 err = -EEXIST;
5682 return err;
5683}
5684
Josef Bacik618e21d2007-07-11 10:18:17 -04005685static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005686 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005687{
5688 struct btrfs_trans_handle *trans;
5689 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005690 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005691 int err;
5692 int drop_inode = 0;
5693 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005694 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005695
5696 if (!new_valid_dev(rdev))
5697 return -EINVAL;
5698
Josef Bacik9ed74f22009-09-11 16:12:44 -04005699 /*
5700 * 2 for inode item and ref
5701 * 2 for dir items
5702 * 1 for xattr if selinux is on
5703 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005704 trans = btrfs_start_transaction(root, 5);
5705 if (IS_ERR(trans))
5706 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005707
Li Zefan581bb052011-04-20 10:06:11 +08005708 err = btrfs_find_free_ino(root, &objectid);
5709 if (err)
5710 goto out_unlock;
5711
Josef Bacikaec74772008-07-24 12:12:38 -04005712 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005713 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005714 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005715 if (IS_ERR(inode)) {
5716 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005717 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005718 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005719
Eric Paris2a7dba32011-02-01 11:05:39 -05005720 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005721 if (err) {
5722 drop_inode = 1;
5723 goto out_unlock;
5724 }
5725
Casey Schauflerad19db72011-12-15 10:09:07 -05005726 /*
5727 * If the active LSM wants to access the inode during
5728 * d_instantiate it needs these. Smack checks to see
5729 * if the filesystem supports xattrs by looking at the
5730 * ops vector.
5731 */
5732
5733 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005734 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005735 if (err)
5736 drop_inode = 1;
5737 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005738 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005739 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005740 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005741 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005742out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005743 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005744 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005745 if (drop_inode) {
5746 inode_dec_link_count(inode);
5747 iput(inode);
5748 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005749 return err;
5750}
5751
Chris Mason39279cc2007-06-12 06:35:45 -04005752static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005753 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005754{
5755 struct btrfs_trans_handle *trans;
5756 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005757 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005758 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005759 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005760 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005761 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005762
Josef Bacik9ed74f22009-09-11 16:12:44 -04005763 /*
5764 * 2 for inode item and ref
5765 * 2 for dir items
5766 * 1 for xattr if selinux is on
5767 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005768 trans = btrfs_start_transaction(root, 5);
5769 if (IS_ERR(trans))
5770 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005771
Li Zefan581bb052011-04-20 10:06:11 +08005772 err = btrfs_find_free_ino(root, &objectid);
5773 if (err)
5774 goto out_unlock;
5775
Josef Bacikaec74772008-07-24 12:12:38 -04005776 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005777 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005778 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005779 if (IS_ERR(inode)) {
5780 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005781 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005782 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005783 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005784
Eric Paris2a7dba32011-02-01 11:05:39 -05005785 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005786 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005787 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005788
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005789 err = btrfs_update_inode(trans, root, inode);
5790 if (err)
5791 goto out_unlock;
5792
Casey Schauflerad19db72011-12-15 10:09:07 -05005793 /*
5794 * If the active LSM wants to access the inode during
5795 * d_instantiate it needs these. Smack checks to see
5796 * if the filesystem supports xattrs by looking at the
5797 * ops vector.
5798 */
5799 inode->i_fop = &btrfs_file_operations;
5800 inode->i_op = &btrfs_file_inode_operations;
5801
Josef Bacika1b075d2010-11-19 20:36:11 +00005802 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005803 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005804 goto out_unlock;
5805
5806 inode->i_mapping->a_ops = &btrfs_aops;
5807 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5808 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5809 d_instantiate(dentry, inode);
5810
Chris Mason39279cc2007-06-12 06:35:45 -04005811out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005812 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005813 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005814 inode_dec_link_count(inode);
5815 iput(inode);
5816 }
Liu Bob53d3f52012-11-14 14:34:34 +00005817 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005818 return err;
5819}
5820
5821static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5822 struct dentry *dentry)
5823{
5824 struct btrfs_trans_handle *trans;
5825 struct btrfs_root *root = BTRFS_I(dir)->root;
5826 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005827 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005828 int err;
5829 int drop_inode = 0;
5830
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005831 /* do not allow sys_link's with other subvols of the same device */
5832 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005833 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005834
Mark Fashehf1863732012-08-08 11:32:27 -07005835 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005836 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005837
Chris Mason3de45862008-11-17 21:02:50 -05005838 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005839 if (err)
5840 goto fail;
5841
Yan, Zhenga22285a2010-05-16 10:48:46 -04005842 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005843 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005844 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005845 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005846 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005847 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005848 if (IS_ERR(trans)) {
5849 err = PTR_ERR(trans);
5850 goto fail;
5851 }
Chris Mason5f39d392007-10-15 16:14:19 -04005852
Miao Xie31534952011-04-13 13:19:21 +08005853 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005854 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005855 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005856 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005857 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005858
Josef Bacika1b075d2010-11-19 20:36:11 +00005859 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005860
Yan, Zhenga5719522009-09-24 09:17:31 -04005861 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005862 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005863 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005864 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005865 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005866 if (err)
5867 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005868 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005869 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005870 }
Chris Mason39279cc2007-06-12 06:35:45 -04005871
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005872 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005873fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005874 if (drop_inode) {
5875 inode_dec_link_count(inode);
5876 iput(inode);
5877 }
Liu Bob53d3f52012-11-14 14:34:34 +00005878 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005879 return err;
5880}
5881
Al Viro18bb1db2011-07-26 01:41:39 -04005882static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005883{
Chris Masonb9d86662008-05-02 16:13:49 -04005884 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005885 struct btrfs_trans_handle *trans;
5886 struct btrfs_root *root = BTRFS_I(dir)->root;
5887 int err = 0;
5888 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005889 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005890 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005891
Josef Bacik9ed74f22009-09-11 16:12:44 -04005892 /*
5893 * 2 items for inode and ref
5894 * 2 items for dir items
5895 * 1 for xattr if selinux is on
5896 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005897 trans = btrfs_start_transaction(root, 5);
5898 if (IS_ERR(trans))
5899 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005900
Li Zefan581bb052011-04-20 10:06:11 +08005901 err = btrfs_find_free_ino(root, &objectid);
5902 if (err)
5903 goto out_fail;
5904
Josef Bacikaec74772008-07-24 12:12:38 -04005905 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005906 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005907 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005908 if (IS_ERR(inode)) {
5909 err = PTR_ERR(inode);
5910 goto out_fail;
5911 }
Chris Mason5f39d392007-10-15 16:14:19 -04005912
Chris Mason39279cc2007-06-12 06:35:45 -04005913 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005914
Eric Paris2a7dba32011-02-01 11:05:39 -05005915 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005916 if (err)
5917 goto out_fail;
5918
Chris Mason39279cc2007-06-12 06:35:45 -04005919 inode->i_op = &btrfs_dir_inode_operations;
5920 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005921
Chris Masondbe674a2008-07-17 12:54:05 -04005922 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005923 err = btrfs_update_inode(trans, root, inode);
5924 if (err)
5925 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005926
Josef Bacika1b075d2010-11-19 20:36:11 +00005927 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5928 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005929 if (err)
5930 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005931
Chris Mason39279cc2007-06-12 06:35:45 -04005932 d_instantiate(dentry, inode);
5933 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005934
5935out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005936 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005937 if (drop_on_err)
5938 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005939 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005940 return err;
5941}
5942
Chris Masond352ac62008-09-29 15:18:18 -04005943/* helper for btfs_get_extent. Given an existing extent in the tree,
5944 * and an extent that you want to insert, deal with overlap and insert
5945 * the new extent into the tree.
5946 */
Chris Mason3b951512008-04-17 11:29:12 -04005947static int merge_extent_mapping(struct extent_map_tree *em_tree,
5948 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005949 struct extent_map *em,
5950 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005951{
5952 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005953
Chris Masone6dcd2d2008-07-17 12:53:50 -04005954 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5955 start_diff = map_start - em->start;
5956 em->start = map_start;
5957 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005958 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5959 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005960 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005961 em->block_len -= start_diff;
5962 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005963 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005964}
5965
Chris Masonc8b97812008-10-29 14:49:59 -04005966static noinline int uncompress_inline(struct btrfs_path *path,
5967 struct inode *inode, struct page *page,
5968 size_t pg_offset, u64 extent_offset,
5969 struct btrfs_file_extent_item *item)
5970{
5971 int ret;
5972 struct extent_buffer *leaf = path->nodes[0];
5973 char *tmp;
5974 size_t max_size;
5975 unsigned long inline_size;
5976 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005977 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005978
5979 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005980 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005981 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5982 inline_size = btrfs_file_extent_inline_item_len(leaf,
5983 btrfs_item_nr(leaf, path->slots[0]));
5984 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005985 if (!tmp)
5986 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005987 ptr = btrfs_file_extent_inline_start(item);
5988
5989 read_extent_buffer(leaf, tmp, ptr, inline_size);
5990
Chris Mason5b050f02008-11-11 09:34:41 -05005991 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005992 ret = btrfs_decompress(compress_type, tmp, page,
5993 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005994 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005995 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005996 unsigned long copy_size = min_t(u64,
5997 PAGE_CACHE_SIZE - pg_offset,
5998 max_size - extent_offset);
5999 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006000 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006001 }
6002 kfree(tmp);
6003 return 0;
6004}
6005
Chris Masond352ac62008-09-29 15:18:18 -04006006/*
6007 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006008 * the ugly parts come from merging extents from the disk with the in-ram
6009 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006010 * where the in-ram extents might be locked pending data=ordered completion.
6011 *
6012 * This also copies inline extents directly into the page.
6013 */
Chris Masond3977122009-01-05 21:25:51 -05006014
Chris Masona52d9a82007-08-27 16:49:44 -04006015struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006016 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006017 int create)
6018{
6019 int ret;
6020 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006021 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006022 u64 extent_start = 0;
6023 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006024 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006025 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006026 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006027 struct btrfs_root *root = BTRFS_I(inode)->root;
6028 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006029 struct extent_buffer *leaf;
6030 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006031 struct extent_map *em = NULL;
6032 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006033 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006034 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006035 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006036
Chris Masona52d9a82007-08-27 16:49:44 -04006037again:
Chris Mason890871b2009-09-02 16:24:52 -04006038 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006039 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006040 if (em)
6041 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006042 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006043
Chris Masona52d9a82007-08-27 16:49:44 -04006044 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006045 if (em->start > start || em->start + em->len <= start)
6046 free_extent_map(em);
6047 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006048 free_extent_map(em);
6049 else
6050 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006051 }
David Sterba172ddd62011-04-21 00:48:27 +02006052 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006053 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006054 err = -ENOMEM;
6055 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006056 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006057 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006058 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006059 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006060 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006061 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006062
6063 if (!path) {
6064 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006065 if (!path) {
6066 err = -ENOMEM;
6067 goto out;
6068 }
6069 /*
6070 * Chances are we'll be called again, so go ahead and do
6071 * readahead
6072 */
6073 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006074 }
6075
Chris Mason179e29e2007-11-01 11:28:41 -04006076 ret = btrfs_lookup_file_extent(trans, root, path,
6077 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006078 if (ret < 0) {
6079 err = ret;
6080 goto out;
6081 }
6082
6083 if (ret != 0) {
6084 if (path->slots[0] == 0)
6085 goto not_found;
6086 path->slots[0]--;
6087 }
6088
Chris Mason5f39d392007-10-15 16:14:19 -04006089 leaf = path->nodes[0];
6090 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006091 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006092 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006093 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6094 found_type = btrfs_key_type(&found_key);
6095 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006096 found_type != BTRFS_EXTENT_DATA_KEY) {
6097 goto not_found;
6098 }
6099
Chris Mason5f39d392007-10-15 16:14:19 -04006100 found_type = btrfs_file_extent_type(leaf, item);
6101 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006102 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006103 if (found_type == BTRFS_FILE_EXTENT_REG ||
6104 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006105 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006106 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006107 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6108 size_t size;
6109 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006110 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006111 }
6112
6113 if (start >= extent_end) {
6114 path->slots[0]++;
6115 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6116 ret = btrfs_next_leaf(root, path);
6117 if (ret < 0) {
6118 err = ret;
6119 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006120 }
Yan Zheng9036c102008-10-30 14:19:41 -04006121 if (ret > 0)
6122 goto not_found;
6123 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006124 }
Yan Zheng9036c102008-10-30 14:19:41 -04006125 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6126 if (found_key.objectid != objectid ||
6127 found_key.type != BTRFS_EXTENT_DATA_KEY)
6128 goto not_found;
6129 if (start + len <= found_key.offset)
6130 goto not_found;
6131 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006132 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006133 em->len = found_key.offset - start;
6134 goto not_found_em;
6135 }
6136
Yan Zhengd899e052008-10-30 14:25:28 -04006137 if (found_type == BTRFS_FILE_EXTENT_REG ||
6138 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006139 em->start = extent_start;
6140 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006141 em->orig_start = extent_start -
6142 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006143 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6144 item);
Chris Masondb945352007-10-15 16:15:53 -04006145 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6146 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006147 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006148 goto insert;
6149 }
Li Zefan261507a02010-12-17 14:21:50 +08006150 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006151 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006152 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006153 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006154 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006155 } else {
6156 bytenr += btrfs_file_extent_offset(leaf, item);
6157 em->block_start = bytenr;
6158 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006159 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6160 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006161 }
Chris Masona52d9a82007-08-27 16:49:44 -04006162 goto insert;
6163 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006164 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006165 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006166 size_t size;
6167 size_t extent_offset;
6168 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006169
Chris Masona52d9a82007-08-27 16:49:44 -04006170 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006171 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006172 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006173 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006174 goto out;
6175 }
6176
Yan Zheng9036c102008-10-30 14:19:41 -04006177 size = btrfs_file_extent_inline_len(leaf, item);
6178 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006179 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006180 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006181 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006182 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006183 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006184 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006185 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006186 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006187 em->compress_type = compress_type;
6188 }
Yan689f9342007-10-29 11:41:07 -04006189 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006190 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006191 if (btrfs_file_extent_compression(leaf, item) !=
6192 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006193 ret = uncompress_inline(path, inode, page,
6194 pg_offset,
6195 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006196 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006197 } else {
6198 map = kmap(page);
6199 read_extent_buffer(leaf, map + pg_offset, ptr,
6200 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006201 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6202 memset(map + pg_offset + copy_size, 0,
6203 PAGE_CACHE_SIZE - pg_offset -
6204 copy_size);
6205 }
Chris Masonc8b97812008-10-29 14:49:59 -04006206 kunmap(page);
6207 }
Chris Mason179e29e2007-11-01 11:28:41 -04006208 flush_dcache_page(page);
6209 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006210 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006211 if (!trans) {
6212 kunmap(page);
6213 free_extent_map(em);
6214 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006215
David Sterbab3b4aa72011-04-21 01:20:15 +02006216 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006217 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006218
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006219 if (IS_ERR(trans))
6220 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006221 goto again;
6222 }
Chris Masonc8b97812008-10-29 14:49:59 -04006223 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006224 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006225 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006226 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006227 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006228 }
Chris Masond1310b22008-01-24 16:13:08 -05006229 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006230 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006231 goto insert;
6232 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006233 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006234 }
6235not_found:
6236 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006237 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006238 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006239not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006240 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006241 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006242insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006243 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006244 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05006245 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
6246 "[%llu %llu]\n", (unsigned long long)em->start,
6247 (unsigned long long)em->len,
6248 (unsigned long long)start,
6249 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006250 err = -EIO;
6251 goto out;
6252 }
Chris Masond1310b22008-01-24 16:13:08 -05006253
6254 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006255 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006256 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04006257 /* it is possible that someone inserted the extent into the tree
6258 * while we had the lock dropped. It is also possible that
6259 * an overlapping map exists in the tree
6260 */
Chris Masona52d9a82007-08-27 16:49:44 -04006261 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006262 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006263
6264 ret = 0;
6265
Chris Mason3b951512008-04-17 11:29:12 -04006266 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006267 if (existing && (existing->start > start ||
6268 existing->start + existing->len <= start)) {
6269 free_extent_map(existing);
6270 existing = NULL;
6271 }
Chris Mason3b951512008-04-17 11:29:12 -04006272 if (!existing) {
6273 existing = lookup_extent_mapping(em_tree, em->start,
6274 em->len);
6275 if (existing) {
6276 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006277 em, start,
6278 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006279 free_extent_map(existing);
6280 if (err) {
6281 free_extent_map(em);
6282 em = NULL;
6283 }
6284 } else {
6285 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006286 free_extent_map(em);
6287 em = NULL;
6288 }
6289 } else {
6290 free_extent_map(em);
6291 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006292 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006293 }
Chris Masona52d9a82007-08-27 16:49:44 -04006294 }
Chris Mason890871b2009-09-02 16:24:52 -04006295 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006296out:
liubo1abe9b82011-03-24 11:18:59 +00006297
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006298 if (em)
6299 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006300
Chris Masonf4219502008-07-22 11:18:09 -04006301 if (path)
6302 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006303 if (trans) {
6304 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006305 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006306 err = ret;
6307 }
Chris Masona52d9a82007-08-27 16:49:44 -04006308 if (err) {
6309 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006310 return ERR_PTR(err);
6311 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006312 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006313 return em;
6314}
6315
Chris Masonec29ed52011-02-23 16:23:20 -05006316struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6317 size_t pg_offset, u64 start, u64 len,
6318 int create)
6319{
6320 struct extent_map *em;
6321 struct extent_map *hole_em = NULL;
6322 u64 range_start = start;
6323 u64 end;
6324 u64 found;
6325 u64 found_end;
6326 int err = 0;
6327
6328 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6329 if (IS_ERR(em))
6330 return em;
6331 if (em) {
6332 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006333 * if our em maps to
6334 * - a hole or
6335 * - a pre-alloc extent,
6336 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006337 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006338 if (em->block_start != EXTENT_MAP_HOLE &&
6339 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006340 return em;
6341 else
6342 hole_em = em;
6343 }
6344
6345 /* check to see if we've wrapped (len == -1 or similar) */
6346 end = start + len;
6347 if (end < start)
6348 end = (u64)-1;
6349 else
6350 end -= 1;
6351
6352 em = NULL;
6353
6354 /* ok, we didn't find anything, lets look for delalloc */
6355 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6356 end, len, EXTENT_DELALLOC, 1);
6357 found_end = range_start + found;
6358 if (found_end < range_start)
6359 found_end = (u64)-1;
6360
6361 /*
6362 * we didn't find anything useful, return
6363 * the original results from get_extent()
6364 */
6365 if (range_start > end || found_end <= start) {
6366 em = hole_em;
6367 hole_em = NULL;
6368 goto out;
6369 }
6370
6371 /* adjust the range_start to make sure it doesn't
6372 * go backwards from the start they passed in
6373 */
6374 range_start = max(start,range_start);
6375 found = found_end - range_start;
6376
6377 if (found > 0) {
6378 u64 hole_start = start;
6379 u64 hole_len = len;
6380
David Sterba172ddd62011-04-21 00:48:27 +02006381 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006382 if (!em) {
6383 err = -ENOMEM;
6384 goto out;
6385 }
6386 /*
6387 * when btrfs_get_extent can't find anything it
6388 * returns one huge hole
6389 *
6390 * make sure what it found really fits our range, and
6391 * adjust to make sure it is based on the start from
6392 * the caller
6393 */
6394 if (hole_em) {
6395 u64 calc_end = extent_map_end(hole_em);
6396
6397 if (calc_end <= start || (hole_em->start > end)) {
6398 free_extent_map(hole_em);
6399 hole_em = NULL;
6400 } else {
6401 hole_start = max(hole_em->start, start);
6402 hole_len = calc_end - hole_start;
6403 }
6404 }
6405 em->bdev = NULL;
6406 if (hole_em && range_start > hole_start) {
6407 /* our hole starts before our delalloc, so we
6408 * have to return just the parts of the hole
6409 * that go until the delalloc starts
6410 */
6411 em->len = min(hole_len,
6412 range_start - hole_start);
6413 em->start = hole_start;
6414 em->orig_start = hole_start;
6415 /*
6416 * don't adjust block start at all,
6417 * it is fixed at EXTENT_MAP_HOLE
6418 */
6419 em->block_start = hole_em->block_start;
6420 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006421 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6422 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006423 } else {
6424 em->start = range_start;
6425 em->len = found;
6426 em->orig_start = range_start;
6427 em->block_start = EXTENT_MAP_DELALLOC;
6428 em->block_len = found;
6429 }
6430 } else if (hole_em) {
6431 return hole_em;
6432 }
6433out:
6434
6435 free_extent_map(hole_em);
6436 if (err) {
6437 free_extent_map(em);
6438 return ERR_PTR(err);
6439 }
6440 return em;
6441}
6442
Josef Bacik4b46fce2010-05-23 11:00:55 -04006443static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6444 u64 start, u64 len)
6445{
6446 struct btrfs_root *root = BTRFS_I(inode)->root;
6447 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006448 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006449 struct btrfs_key ins;
6450 u64 alloc_hint;
6451 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006452
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006453 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006454 if (IS_ERR(trans))
6455 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006456
6457 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6458
6459 alloc_hint = get_extent_allocation_hint(inode, start, len);
6460 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006461 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006462 if (ret) {
6463 em = ERR_PTR(ret);
6464 goto out;
6465 }
6466
Josef Bacik70c8a912012-10-11 16:54:30 -04006467 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
6468 ins.offset, ins.offset, 0);
6469 if (IS_ERR(em))
6470 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006471
6472 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6473 ins.offset, ins.offset, 0);
6474 if (ret) {
6475 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6476 em = ERR_PTR(ret);
6477 }
6478out:
6479 btrfs_end_transaction(trans, root);
6480 return em;
6481}
6482
Chris Mason46bfbb52010-05-26 11:04:10 -04006483/*
6484 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6485 * block must be cow'd
6486 */
6487static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
6488 struct inode *inode, u64 offset, u64 len)
6489{
6490 struct btrfs_path *path;
6491 int ret;
6492 struct extent_buffer *leaf;
6493 struct btrfs_root *root = BTRFS_I(inode)->root;
6494 struct btrfs_file_extent_item *fi;
6495 struct btrfs_key key;
6496 u64 disk_bytenr;
6497 u64 backref_offset;
6498 u64 extent_end;
6499 u64 num_bytes;
6500 int slot;
6501 int found_type;
6502
6503 path = btrfs_alloc_path();
6504 if (!path)
6505 return -ENOMEM;
6506
Li Zefan33345d012011-04-20 10:31:50 +08006507 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006508 offset, 0);
6509 if (ret < 0)
6510 goto out;
6511
6512 slot = path->slots[0];
6513 if (ret == 1) {
6514 if (slot == 0) {
6515 /* can't find the item, must cow */
6516 ret = 0;
6517 goto out;
6518 }
6519 slot--;
6520 }
6521 ret = 0;
6522 leaf = path->nodes[0];
6523 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006524 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006525 key.type != BTRFS_EXTENT_DATA_KEY) {
6526 /* not our file or wrong item type, must cow */
6527 goto out;
6528 }
6529
6530 if (key.offset > offset) {
6531 /* Wrong offset, must cow */
6532 goto out;
6533 }
6534
6535 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6536 found_type = btrfs_file_extent_type(leaf, fi);
6537 if (found_type != BTRFS_FILE_EXTENT_REG &&
6538 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6539 /* not a regular extent, must cow */
6540 goto out;
6541 }
6542 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6543 backref_offset = btrfs_file_extent_offset(leaf, fi);
6544
6545 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6546 if (extent_end < offset + len) {
6547 /* extent doesn't include our full range, must cow */
6548 goto out;
6549 }
6550
6551 if (btrfs_extent_readonly(root, disk_bytenr))
6552 goto out;
6553
6554 /*
6555 * look for other files referencing this extent, if we
6556 * find any we must cow
6557 */
Li Zefan33345d012011-04-20 10:31:50 +08006558 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006559 key.offset - backref_offset, disk_bytenr))
6560 goto out;
6561
6562 /*
6563 * adjust disk_bytenr and num_bytes to cover just the bytes
6564 * in this extent we are about to write. If there
6565 * are any csums in that range we have to cow in order
6566 * to keep the csums correct
6567 */
6568 disk_bytenr += backref_offset;
6569 disk_bytenr += offset - key.offset;
6570 num_bytes = min(offset + len, extent_end) - offset;
6571 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6572 goto out;
6573 /*
6574 * all of the above have passed, it is safe to overwrite this extent
6575 * without cow
6576 */
6577 ret = 1;
6578out:
6579 btrfs_free_path(path);
6580 return ret;
6581}
6582
Josef Bacikeb838e72012-07-31 16:28:48 -04006583static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6584 struct extent_state **cached_state, int writing)
6585{
6586 struct btrfs_ordered_extent *ordered;
6587 int ret = 0;
6588
6589 while (1) {
6590 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6591 0, cached_state);
6592 /*
6593 * We're concerned with the entire range that we're going to be
6594 * doing DIO to, so we need to make sure theres no ordered
6595 * extents in this range.
6596 */
6597 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6598 lockend - lockstart + 1);
6599
6600 /*
6601 * We need to make sure there are no buffered pages in this
6602 * range either, we could have raced between the invalidate in
6603 * generic_file_direct_write and locking the extent. The
6604 * invalidate needs to happen so that reads after a write do not
6605 * get stale data.
6606 */
6607 if (!ordered && (!writing ||
6608 !test_range_bit(&BTRFS_I(inode)->io_tree,
6609 lockstart, lockend, EXTENT_UPTODATE, 0,
6610 *cached_state)))
6611 break;
6612
6613 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6614 cached_state, GFP_NOFS);
6615
6616 if (ordered) {
6617 btrfs_start_ordered_extent(inode, ordered, 1);
6618 btrfs_put_ordered_extent(ordered);
6619 } else {
6620 /* Screw you mmap */
6621 ret = filemap_write_and_wait_range(inode->i_mapping,
6622 lockstart,
6623 lockend);
6624 if (ret)
6625 break;
6626
6627 /*
6628 * If we found a page that couldn't be invalidated just
6629 * fall back to buffered.
6630 */
6631 ret = invalidate_inode_pages2_range(inode->i_mapping,
6632 lockstart >> PAGE_CACHE_SHIFT,
6633 lockend >> PAGE_CACHE_SHIFT);
6634 if (ret)
6635 break;
6636 }
6637
6638 cond_resched();
6639 }
6640
6641 return ret;
6642}
6643
Josef Bacik69ffb542012-09-11 15:40:07 -04006644static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6645 u64 len, u64 orig_start,
6646 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05006647 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006648{
6649 struct extent_map_tree *em_tree;
6650 struct extent_map *em;
6651 struct btrfs_root *root = BTRFS_I(inode)->root;
6652 int ret;
6653
6654 em_tree = &BTRFS_I(inode)->extent_tree;
6655 em = alloc_extent_map();
6656 if (!em)
6657 return ERR_PTR(-ENOMEM);
6658
6659 em->start = start;
6660 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006661 em->mod_start = start;
6662 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006663 em->len = len;
6664 em->block_len = block_len;
6665 em->block_start = block_start;
6666 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006667 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006668 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006669 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6670 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006671 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006672
6673 do {
6674 btrfs_drop_extent_cache(inode, em->start,
6675 em->start + em->len - 1, 0);
6676 write_lock(&em_tree->lock);
6677 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04006678 if (!ret)
6679 list_move(&em->list,
6680 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006681 write_unlock(&em_tree->lock);
6682 } while (ret == -EEXIST);
6683
6684 if (ret) {
6685 free_extent_map(em);
6686 return ERR_PTR(ret);
6687 }
6688
6689 return em;
6690}
6691
6692
Josef Bacik4b46fce2010-05-23 11:00:55 -04006693static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6694 struct buffer_head *bh_result, int create)
6695{
6696 struct extent_map *em;
6697 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006698 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006699 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006700 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006701 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006702 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006703 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006704 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006705
Miao Xie172a5042013-02-21 02:48:22 -07006706 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006707 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006708 else
Josef Bacikc3298612012-08-03 16:49:19 -04006709 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006710
Josef Bacikc3298612012-08-03 16:49:19 -04006711 lockstart = start;
6712 lockend = start + len - 1;
6713
Josef Bacikeb838e72012-07-31 16:28:48 -04006714 /*
6715 * If this errors out it's because we couldn't invalidate pagecache for
6716 * this range and we need to fallback to buffered.
6717 */
6718 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6719 return -ENOTBLK;
6720
Josef Bacik4b46fce2010-05-23 11:00:55 -04006721 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006722 if (IS_ERR(em)) {
6723 ret = PTR_ERR(em);
6724 goto unlock_err;
6725 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006726
6727 /*
6728 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6729 * io. INLINE is special, and we could probably kludge it in here, but
6730 * it's still buffered so for safety lets just fall back to the generic
6731 * buffered path.
6732 *
6733 * For COMPRESSED we _have_ to read the entire extent in so we can
6734 * decompress it, so there will be buffering required no matter what we
6735 * do, so go ahead and fallback to buffered.
6736 *
6737 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6738 * to buffered IO. Don't blame me, this is the price we pay for using
6739 * the generic code.
6740 */
6741 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6742 em->block_start == EXTENT_MAP_INLINE) {
6743 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006744 ret = -ENOTBLK;
6745 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006746 }
6747
6748 /* Just a good old fashioned hole, return */
6749 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6750 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6751 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006752 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006753 }
6754
6755 /*
6756 * We don't allocate a new extent in the following cases
6757 *
6758 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6759 * existing extent.
6760 * 2) The extent is marked as PREALLOC. We're good to go here and can
6761 * just use the extent.
6762 *
6763 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006764 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006765 len = min(len, em->len - (start - em->start));
6766 lockstart = start + len;
6767 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006768 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006769
6770 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6771 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6772 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006773 int type;
6774 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006775 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006776
6777 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6778 type = BTRFS_ORDERED_PREALLOC;
6779 else
6780 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006781 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006782 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006783
6784 /*
6785 * we're not going to log anything, but we do need
6786 * to make sure the current transaction stays open
6787 * while we look for nocow cross refs
6788 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006789 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006790 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006791 goto must_cow;
6792
6793 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006794 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006795 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006796
6797 if (type == BTRFS_ORDERED_PREALLOC) {
6798 free_extent_map(em);
6799 em = create_pinned_em(inode, start, len,
6800 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006801 block_start, len,
6802 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006803 if (IS_ERR(em)) {
6804 btrfs_end_transaction(trans, root);
6805 goto unlock_err;
6806 }
6807 }
6808
Chris Mason46bfbb52010-05-26 11:04:10 -04006809 ret = btrfs_add_ordered_extent_dio(inode, start,
6810 block_start, len, len, type);
6811 btrfs_end_transaction(trans, root);
6812 if (ret) {
6813 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006814 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006815 }
6816 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006817 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006818 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006819 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006820must_cow:
6821 /*
6822 * this will cow the extent, reset the len in case we changed
6823 * it above
6824 */
6825 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006826 free_extent_map(em);
6827 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006828 if (IS_ERR(em)) {
6829 ret = PTR_ERR(em);
6830 goto unlock_err;
6831 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006832 len = min(len, em->len - (start - em->start));
6833unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006834 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6835 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006836 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006837 bh_result->b_bdev = em->bdev;
6838 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006839 if (create) {
6840 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6841 set_buffer_new(bh_result);
6842
6843 /*
6844 * Need to update the i_size under the extent lock so buffered
6845 * readers will get the updated i_size when we unlock.
6846 */
6847 if (start + len > i_size_read(inode))
6848 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006849
Miao Xie172a5042013-02-21 02:48:22 -07006850 spin_lock(&BTRFS_I(inode)->lock);
6851 BTRFS_I(inode)->outstanding_extents++;
6852 spin_unlock(&BTRFS_I(inode)->lock);
6853
Miao Xie09348562013-02-07 10:12:07 +00006854 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6855 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6856 &cached_state, GFP_NOFS);
6857 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006858 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006859
Josef Bacikeb838e72012-07-31 16:28:48 -04006860 /*
6861 * In the case of write we need to clear and unlock the entire range,
6862 * in the case of read we need to unlock only the end area that we
6863 * aren't using if there is any left over space.
6864 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006865 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006866 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6867 lockend, unlock_bits, 1, 0,
6868 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006869 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006870 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006871 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006872
Josef Bacik4b46fce2010-05-23 11:00:55 -04006873 free_extent_map(em);
6874
6875 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006876
6877unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006878 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6879 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6880 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006881}
6882
6883struct btrfs_dio_private {
6884 struct inode *inode;
6885 u64 logical_offset;
6886 u64 disk_bytenr;
6887 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006888 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006889
6890 /* number of bios pending for this dio */
6891 atomic_t pending_bios;
6892
6893 /* IO errors */
6894 int errors;
6895
6896 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006897};
6898
6899static void btrfs_endio_direct_read(struct bio *bio, int err)
6900{
Miao Xiee65e1532010-11-22 03:04:43 +00006901 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006902 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6903 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006904 struct inode *inode = dip->inode;
6905 struct btrfs_root *root = BTRFS_I(inode)->root;
6906 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006907
6908 start = dip->logical_offset;
6909 do {
6910 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6911 struct page *page = bvec->bv_page;
6912 char *kaddr;
6913 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006914 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006915 unsigned long flags;
6916
Josef Bacikc3298612012-08-03 16:49:19 -04006917 if (get_state_private(&BTRFS_I(inode)->io_tree,
6918 start, &private))
6919 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006920 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006921 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006922 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6923 csum, bvec->bv_len);
6924 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006925 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006926 local_irq_restore(flags);
6927
6928 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006929 if (csum != private) {
6930failed:
Li Zefan33345d012011-04-20 10:31:50 +08006931 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006932 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006933 (unsigned long long)btrfs_ino(inode),
6934 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006935 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006936 err = -EIO;
6937 }
6938 }
6939
6940 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006941 bvec++;
6942 } while (bvec <= bvec_end);
6943
6944 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006945 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006946 bio->bi_private = dip->private;
6947
Josef Bacik4b46fce2010-05-23 11:00:55 -04006948 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006949
6950 /* If we had a csum failure make sure to clear the uptodate flag */
6951 if (err)
6952 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006953 dio_end_io(bio, err);
6954}
6955
6956static void btrfs_endio_direct_write(struct bio *bio, int err)
6957{
6958 struct btrfs_dio_private *dip = bio->bi_private;
6959 struct inode *inode = dip->inode;
6960 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006961 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006962 u64 ordered_offset = dip->logical_offset;
6963 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006964 int ret;
6965
6966 if (err)
6967 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006968again:
6969 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6970 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006971 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006972 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006973 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006974
Josef Bacik5fd02042012-05-02 14:00:54 -04006975 ordered->work.func = finish_ordered_fn;
6976 ordered->work.flags = 0;
6977 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6978 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006979out_test:
6980 /*
6981 * our bio might span multiple ordered extents. If we haven't
6982 * completed the accounting for the whole dio, go back and try again
6983 */
6984 if (ordered_offset < dip->logical_offset + dip->bytes) {
6985 ordered_bytes = dip->logical_offset + dip->bytes -
6986 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006987 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006988 goto again;
6989 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006990out_done:
6991 bio->bi_private = dip->private;
6992
Josef Bacik4b46fce2010-05-23 11:00:55 -04006993 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006994
6995 /* If we had an error make sure to clear the uptodate flag */
6996 if (err)
6997 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006998 dio_end_io(bio, err);
6999}
7000
Chris Masoneaf25d92010-05-25 09:48:28 -04007001static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7002 struct bio *bio, int mirror_num,
7003 unsigned long bio_flags, u64 offset)
7004{
7005 int ret;
7006 struct btrfs_root *root = BTRFS_I(inode)->root;
7007 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007008 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007009 return 0;
7010}
7011
Miao Xiee65e1532010-11-22 03:04:43 +00007012static void btrfs_end_dio_bio(struct bio *bio, int err)
7013{
7014 struct btrfs_dio_private *dip = bio->bi_private;
7015
7016 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007017 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007018 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007019 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007020 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007021 dip->errors = 1;
7022
7023 /*
7024 * before atomic variable goto zero, we must make sure
7025 * dip->errors is perceived to be set.
7026 */
7027 smp_mb__before_atomic_dec();
7028 }
7029
7030 /* if there are more bios still pending for this dio, just exit */
7031 if (!atomic_dec_and_test(&dip->pending_bios))
7032 goto out;
7033
7034 if (dip->errors)
7035 bio_io_error(dip->orig_bio);
7036 else {
7037 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
7038 bio_endio(dip->orig_bio, 0);
7039 }
7040out:
7041 bio_put(bio);
7042}
7043
7044static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7045 u64 first_sector, gfp_t gfp_flags)
7046{
7047 int nr_vecs = bio_get_nr_vecs(bdev);
7048 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7049}
7050
7051static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7052 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007053 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007054{
7055 int write = rw & REQ_WRITE;
7056 struct btrfs_root *root = BTRFS_I(inode)->root;
7057 int ret;
7058
Josef Bacikb812ce22012-11-16 13:56:32 -05007059 if (async_submit)
7060 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7061
Miao Xiee65e1532010-11-22 03:04:43 +00007062 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007063
7064 if (!write) {
7065 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7066 if (ret)
7067 goto err;
7068 }
Miao Xiee65e1532010-11-22 03:04:43 +00007069
Josef Bacik1ae39932011-04-06 14:41:34 -04007070 if (skip_sum)
7071 goto map;
7072
7073 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007074 ret = btrfs_wq_submit_bio(root->fs_info,
7075 inode, rw, bio, 0, 0,
7076 file_offset,
7077 __btrfs_submit_bio_start_direct_io,
7078 __btrfs_submit_bio_done);
7079 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007080 } else if (write) {
7081 /*
7082 * If we aren't doing async submit, calculate the csum of the
7083 * bio now.
7084 */
7085 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7086 if (ret)
7087 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007088 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007089 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007090 if (ret)
7091 goto err;
7092 }
Miao Xiee65e1532010-11-22 03:04:43 +00007093
Josef Bacik1ae39932011-04-06 14:41:34 -04007094map:
7095 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007096err:
7097 bio_put(bio);
7098 return ret;
7099}
7100
7101static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7102 int skip_sum)
7103{
7104 struct inode *inode = dip->inode;
7105 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007106 struct bio *bio;
7107 struct bio *orig_bio = dip->orig_bio;
7108 struct bio_vec *bvec = orig_bio->bi_io_vec;
7109 u64 start_sector = orig_bio->bi_sector;
7110 u64 file_offset = dip->logical_offset;
7111 u64 submit_len = 0;
7112 u64 map_length;
7113 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007114 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007115 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007116
Miao Xiee65e1532010-11-22 03:04:43 +00007117 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007118 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007119 &map_length, NULL, 0);
7120 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007121 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007122 return -EIO;
7123 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007124 if (map_length >= orig_bio->bi_size) {
7125 bio = orig_bio;
7126 goto submit;
7127 }
7128
David Woodhouse53b381b2013-01-29 18:40:14 -05007129 /* async crcs make it difficult to collect full stripe writes. */
7130 if (btrfs_get_alloc_profile(root, 1) &
7131 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7132 async_submit = 0;
7133 else
7134 async_submit = 1;
7135
Josef Bacik02f57c72011-04-06 14:25:44 -04007136 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7137 if (!bio)
7138 return -ENOMEM;
7139 bio->bi_private = dip;
7140 bio->bi_end_io = btrfs_end_dio_bio;
7141 atomic_inc(&dip->pending_bios);
7142
Miao Xiee65e1532010-11-22 03:04:43 +00007143 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7144 if (unlikely(map_length < submit_len + bvec->bv_len ||
7145 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7146 bvec->bv_offset) < bvec->bv_len)) {
7147 /*
7148 * inc the count before we submit the bio so
7149 * we know the end IO handler won't happen before
7150 * we inc the count. Otherwise, the dip might get freed
7151 * before we're done setting it up
7152 */
7153 atomic_inc(&dip->pending_bios);
7154 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7155 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007156 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007157 if (ret) {
7158 bio_put(bio);
7159 atomic_dec(&dip->pending_bios);
7160 goto out_err;
7161 }
7162
Miao Xiee65e1532010-11-22 03:04:43 +00007163 start_sector += submit_len >> 9;
7164 file_offset += submit_len;
7165
7166 submit_len = 0;
7167 nr_pages = 0;
7168
7169 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7170 start_sector, GFP_NOFS);
7171 if (!bio)
7172 goto out_err;
7173 bio->bi_private = dip;
7174 bio->bi_end_io = btrfs_end_dio_bio;
7175
7176 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007177 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007178 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007179 &map_length, NULL, 0);
7180 if (ret) {
7181 bio_put(bio);
7182 goto out_err;
7183 }
7184 } else {
7185 submit_len += bvec->bv_len;
7186 nr_pages ++;
7187 bvec++;
7188 }
7189 }
7190
Josef Bacik02f57c72011-04-06 14:25:44 -04007191submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007192 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007193 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007194 if (!ret)
7195 return 0;
7196
7197 bio_put(bio);
7198out_err:
7199 dip->errors = 1;
7200 /*
7201 * before atomic variable goto zero, we must
7202 * make sure dip->errors is perceived to be set.
7203 */
7204 smp_mb__before_atomic_dec();
7205 if (atomic_dec_and_test(&dip->pending_bios))
7206 bio_io_error(dip->orig_bio);
7207
7208 /* bio_end_io() will handle error, so we needn't return it */
7209 return 0;
7210}
7211
Josef Bacik4b46fce2010-05-23 11:00:55 -04007212static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
7213 loff_t file_offset)
7214{
7215 struct btrfs_root *root = BTRFS_I(inode)->root;
7216 struct btrfs_dio_private *dip;
7217 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007218 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007219 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007220 int ret = 0;
7221
7222 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7223
7224 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7225 if (!dip) {
7226 ret = -ENOMEM;
7227 goto free_ordered;
7228 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007229
7230 dip->private = bio->bi_private;
7231 dip->inode = inode;
7232 dip->logical_offset = file_offset;
7233
Josef Bacik4b46fce2010-05-23 11:00:55 -04007234 dip->bytes = 0;
7235 do {
7236 dip->bytes += bvec->bv_len;
7237 bvec++;
7238 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
7239
Chris Mason46bfbb52010-05-26 11:04:10 -04007240 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007241 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007242 dip->errors = 0;
7243 dip->orig_bio = bio;
7244 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007245
7246 if (write)
7247 bio->bi_end_io = btrfs_endio_direct_write;
7248 else
7249 bio->bi_end_io = btrfs_endio_direct_read;
7250
Miao Xiee65e1532010-11-22 03:04:43 +00007251 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7252 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007253 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007254free_ordered:
7255 /*
7256 * If this is a write, we need to clean up the reserved space and kill
7257 * the ordered extent.
7258 */
7259 if (write) {
7260 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007261 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007262 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7263 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7264 btrfs_free_reserved_extent(root, ordered->start,
7265 ordered->disk_len);
7266 btrfs_put_ordered_extent(ordered);
7267 btrfs_put_ordered_extent(ordered);
7268 }
7269 bio_endio(bio, ret);
7270}
7271
Chris Mason5a5f79b2010-05-26 21:33:37 -04007272static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7273 const struct iovec *iov, loff_t offset,
7274 unsigned long nr_segs)
7275{
7276 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007277 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007278 size_t size;
7279 unsigned long addr;
7280 unsigned blocksize_mask = root->sectorsize - 1;
7281 ssize_t retval = -EINVAL;
7282 loff_t end = offset;
7283
7284 if (offset & blocksize_mask)
7285 goto out;
7286
7287 /* Check the memory alignment. Blocks cannot straddle pages */
7288 for (seg = 0; seg < nr_segs; seg++) {
7289 addr = (unsigned long)iov[seg].iov_base;
7290 size = iov[seg].iov_len;
7291 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007292 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007293 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007294
7295 /* If this is a write we don't need to check anymore */
7296 if (rw & WRITE)
7297 continue;
7298
7299 /*
7300 * Check to make sure we don't have duplicate iov_base's in this
7301 * iovec, if so return EINVAL, otherwise we'll get csum errors
7302 * when reading back.
7303 */
7304 for (i = seg + 1; i < nr_segs; i++) {
7305 if (iov[seg].iov_base == iov[i].iov_base)
7306 goto out;
7307 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007308 }
7309 retval = 0;
7310out:
7311 return retval;
7312}
Josef Bacikeb838e72012-07-31 16:28:48 -04007313
Chris Mason16432982008-04-10 10:23:21 -04007314static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7315 const struct iovec *iov, loff_t offset,
7316 unsigned long nr_segs)
7317{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007318 struct file *file = iocb->ki_filp;
7319 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007320 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007321 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007322 bool wakeup = true;
7323 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007324 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007325
Chris Mason5a5f79b2010-05-26 21:33:37 -04007326 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007327 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007328 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007329
Miao Xie38851cc2013-02-08 07:04:11 +00007330 atomic_inc(&inode->i_dio_count);
7331 smp_mb__after_atomic_inc();
7332
Miao Xie09348562013-02-07 10:12:07 +00007333 if (rw & WRITE) {
7334 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007335 /*
7336 * If the write DIO is beyond the EOF, we need update
7337 * the isize, but it is protected by i_mutex. So we can
7338 * not unlock the i_mutex at this case.
7339 */
7340 if (offset + count <= inode->i_size) {
7341 mutex_unlock(&inode->i_mutex);
7342 relock = true;
7343 }
Miao Xie09348562013-02-07 10:12:07 +00007344 ret = btrfs_delalloc_reserve_space(inode, count);
7345 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007346 goto out;
7347 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7348 &BTRFS_I(inode)->runtime_flags))) {
7349 inode_dio_done(inode);
7350 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7351 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007352 }
7353
7354 ret = __blockdev_direct_IO(rw, iocb, inode,
7355 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7356 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007357 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007358 if (rw & WRITE) {
7359 if (ret < 0 && ret != -EIOCBQUEUED)
7360 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007361 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007362 btrfs_delalloc_release_space(inode,
7363 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007364 else
7365 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007366 }
Miao Xie38851cc2013-02-08 07:04:11 +00007367out:
Miao Xie2e60a512013-02-08 07:01:08 +00007368 if (wakeup)
7369 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007370 if (relock)
7371 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007372
7373 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007374}
7375
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007376#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7377
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007378static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7379 __u64 start, __u64 len)
7380{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007381 int ret;
7382
7383 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7384 if (ret)
7385 return ret;
7386
Chris Masonec29ed52011-02-23 16:23:20 -05007387 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007388}
7389
Chris Mason9ebefb182007-06-15 13:50:00 -04007390int btrfs_readpage(struct file *file, struct page *page)
7391{
Chris Masond1310b22008-01-24 16:13:08 -05007392 struct extent_io_tree *tree;
7393 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007394 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007395}
Chris Mason1832a6d2007-12-21 16:27:21 -05007396
Chris Mason39279cc2007-06-12 06:35:45 -04007397static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7398{
Chris Masond1310b22008-01-24 16:13:08 -05007399 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007400
7401
7402 if (current->flags & PF_MEMALLOC) {
7403 redirty_page_for_writepage(wbc, page);
7404 unlock_page(page);
7405 return 0;
7406 }
Chris Masond1310b22008-01-24 16:13:08 -05007407 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007408 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7409}
Chris Mason39279cc2007-06-12 06:35:45 -04007410
Chris Masonf4219502008-07-22 11:18:09 -04007411int btrfs_writepages(struct address_space *mapping,
7412 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007413{
Chris Masond1310b22008-01-24 16:13:08 -05007414 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007415
Chris Masond1310b22008-01-24 16:13:08 -05007416 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007417 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7418}
7419
Chris Mason3ab2fb52007-11-08 10:59:22 -05007420static int
7421btrfs_readpages(struct file *file, struct address_space *mapping,
7422 struct list_head *pages, unsigned nr_pages)
7423{
Chris Masond1310b22008-01-24 16:13:08 -05007424 struct extent_io_tree *tree;
7425 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007426 return extent_readpages(tree, mapping, pages, nr_pages,
7427 btrfs_get_extent);
7428}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007429static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007430{
Chris Masond1310b22008-01-24 16:13:08 -05007431 struct extent_io_tree *tree;
7432 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007433 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007434
Chris Masond1310b22008-01-24 16:13:08 -05007435 tree = &BTRFS_I(page->mapping->host)->io_tree;
7436 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007437 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007438 if (ret == 1) {
7439 ClearPagePrivate(page);
7440 set_page_private(page, 0);
7441 page_cache_release(page);
7442 }
7443 return ret;
7444}
Chris Mason39279cc2007-06-12 06:35:45 -04007445
Chris Masone6dcd2d2008-07-17 12:53:50 -04007446static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7447{
Chris Mason98509cf2008-09-11 15:51:43 -04007448 if (PageWriteback(page) || PageDirty(page))
7449 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007450 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007451}
7452
Chris Masona52d9a82007-08-27 16:49:44 -04007453static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7454{
Josef Bacik5fd02042012-05-02 14:00:54 -04007455 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007456 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007457 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007458 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007459 u64 page_start = page_offset(page);
7460 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007461
Chris Mason8b62b722009-09-02 16:53:46 -04007462 /*
7463 * we have the page locked, so new writeback can't start,
7464 * and the dirty bit won't be cleared while we are here.
7465 *
7466 * Wait for IO on this page so that we can safely clear
7467 * the PagePrivate2 bit and do ordered accounting
7468 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007469 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007470
Josef Bacik5fd02042012-05-02 14:00:54 -04007471 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007472 if (offset) {
7473 btrfs_releasepage(page, GFP_NOFS);
7474 return;
7475 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007476 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007477 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007478 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007479 /*
7480 * IO on this page will never be started, so we need
7481 * to account for any ordered extents now
7482 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007483 clear_extent_bit(tree, page_start, page_end,
7484 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007485 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7486 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007487 /*
7488 * whoever cleared the private bit is responsible
7489 * for the finish_ordered_io
7490 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007491 if (TestClearPagePrivate2(page) &&
7492 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7493 PAGE_CACHE_SIZE, 1)) {
7494 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007495 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007496 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007497 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007498 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007499 }
7500 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007501 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007502 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7503 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007504 __btrfs_releasepage(page, GFP_NOFS);
7505
Chris Mason4a096752008-07-21 10:29:44 -04007506 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007507 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007508 ClearPagePrivate(page);
7509 set_page_private(page, 0);
7510 page_cache_release(page);
7511 }
Chris Mason39279cc2007-06-12 06:35:45 -04007512}
7513
Chris Mason9ebefb182007-06-15 13:50:00 -04007514/*
7515 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7516 * called from a page fault handler when a page is first dirtied. Hence we must
7517 * be careful to check for EOF conditions here. We set the page up correctly
7518 * for a written page which means we get ENOSPC checking when writing into
7519 * holes and correct delalloc and unwritten extent mapping on filesystems that
7520 * support these features.
7521 *
7522 * We are not allowed to take the i_mutex here so we have to play games to
7523 * protect against truncate races as the page could now be beyond EOF. Because
7524 * vmtruncate() writes the inode size before removing pages, once we have the
7525 * page lock we can determine safely if the page is beyond EOF. If it is not
7526 * beyond EOF, then the page is guaranteed safe against truncation until we
7527 * unlock the page.
7528 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007529int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007530{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007531 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007532 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007533 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007534 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7535 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007536 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007537 char *kaddr;
7538 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007539 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007540 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007541 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007542 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007543 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007544
Jan Karab2b5ef52012-06-12 16:20:45 +02007545 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007546 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007547 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007548 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007549 reserved = 1;
7550 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007551 if (ret) {
7552 if (ret == -ENOMEM)
7553 ret = VM_FAULT_OOM;
7554 else /* -ENOSPC, -EIO, etc */
7555 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007556 if (reserved)
7557 goto out;
7558 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007559 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007560
Nick Piggin56a76f82009-03-31 15:23:23 -07007561 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007562again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007563 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007564 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007565 page_start = page_offset(page);
7566 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007567
Chris Mason9ebefb182007-06-15 13:50:00 -04007568 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007569 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007570 /* page got truncated out from underneath us */
7571 goto out_unlock;
7572 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007573 wait_on_page_writeback(page);
7574
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007575 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007576 set_page_extent_mapped(page);
7577
Chris Masoneb84ae02008-07-17 13:53:27 -04007578 /*
7579 * we can't set the delalloc bits if there are pending ordered
7580 * extents. Drop our locks and wait for them to finish
7581 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007582 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7583 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007584 unlock_extent_cached(io_tree, page_start, page_end,
7585 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007586 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007587 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007588 btrfs_put_ordered_extent(ordered);
7589 goto again;
7590 }
7591
Josef Bacikfbf19082009-10-01 17:10:23 -04007592 /*
7593 * XXX - page_mkwrite gets called every time the page is dirtied, even
7594 * if it was already dirty, so for space accounting reasons we need to
7595 * clear any delalloc bits for the range we are fixing to save. There
7596 * is probably a better way to do this, but for now keep consistent with
7597 * prepare_pages in the normal write path.
7598 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007599 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007600 EXTENT_DIRTY | EXTENT_DELALLOC |
7601 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007602 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007603
Josef Bacik2ac55d42010-02-03 19:33:23 +00007604 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7605 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007606 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007607 unlock_extent_cached(io_tree, page_start, page_end,
7608 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007609 ret = VM_FAULT_SIGBUS;
7610 goto out_unlock;
7611 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007612 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007613
7614 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007615 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007616 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007617 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007618 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007619
Chris Masone6dcd2d2008-07-17 12:53:50 -04007620 if (zero_start != PAGE_CACHE_SIZE) {
7621 kaddr = kmap(page);
7622 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7623 flush_dcache_page(page);
7624 kunmap(page);
7625 }
Chris Mason247e7432008-07-17 12:53:51 -04007626 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007627 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007628 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007629
Chris Mason257c62e2009-10-13 13:21:08 -04007630 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7631 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007632 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007633
Josef Bacik2ac55d42010-02-03 19:33:23 +00007634 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007635
7636out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007637 if (!ret) {
7638 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007639 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007640 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007641 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007642out:
Josef Bacikec39e182012-01-12 19:10:12 -05007643 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007644out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007645 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007646 return ret;
7647}
7648
Josef Bacika41ad392011-01-31 15:30:16 -05007649static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007650{
7651 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007652 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007653 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007654 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007655 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007656 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007657 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007658
Josef Bacik2aaa6652012-08-29 14:27:18 -04007659 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007660 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007661 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007662
Chris Mason4a096752008-07-21 10:29:44 -04007663 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007664 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007665
Josef Bacikfcb80c22011-05-03 10:40:22 -04007666 /*
7667 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7668 * 3 things going on here
7669 *
7670 * 1) We need to reserve space for our orphan item and the space to
7671 * delete our orphan item. Lord knows we don't want to have a dangling
7672 * orphan item because we didn't reserve space to remove it.
7673 *
7674 * 2) We need to reserve space to update our inode.
7675 *
7676 * 3) We need to have something to cache all the space that is going to
7677 * be free'd up by the truncate operation, but also have some slack
7678 * space reserved in case it uses space during the truncate (thank you
7679 * very much snapshotting).
7680 *
7681 * And we need these to all be seperate. The fact is we can use alot of
7682 * space doing the truncate, and we have no earthly idea how much space
7683 * we will use, so we need the truncate reservation to be seperate so it
7684 * doesn't end up using space reserved for updating the inode or
7685 * removing the orphan item. We also need to be able to stop the
7686 * transaction and start a new one, which means we need to be able to
7687 * update the inode several times, and we have no idea of knowing how
7688 * many times that will be, so we can't just reserve 1 item for the
7689 * entirety of the opration, so that has to be done seperately as well.
7690 * Then there is the orphan item, which does indeed need to be held on
7691 * to for the whole operation, and we need nobody to touch this reserved
7692 * space except the orphan code.
7693 *
7694 * So that leaves us with
7695 *
7696 * 1) root->orphan_block_rsv - for the orphan deletion.
7697 * 2) rsv - for the truncate reservation, which we will steal from the
7698 * transaction reservation.
7699 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7700 * updating the inode.
7701 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007702 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007703 if (!rsv)
7704 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007705 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007706 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007707
Josef Bacik907cbce2011-08-08 13:46:15 -04007708 /*
Josef Bacik07127182011-08-19 10:29:59 -04007709 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007710 * 1 for updating the inode.
7711 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007712 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007713 if (IS_ERR(trans)) {
7714 err = PTR_ERR(trans);
7715 goto out;
7716 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007717
Josef Bacik907cbce2011-08-08 13:46:15 -04007718 /* Migrate the slack space for the truncate to our reserve */
7719 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7720 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007721 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007722
Chris Mason5a3f23d2009-03-31 13:27:11 -04007723 /*
7724 * setattr is responsible for setting the ordered_data_close flag,
7725 * but that is only tested during the last file release. That
7726 * could happen well after the next commit, leaving a great big
7727 * window where new writes may get lost if someone chooses to write
7728 * to this file after truncating to zero
7729 *
7730 * The inode doesn't have any dirty data here, and so if we commit
7731 * this is a noop. If someone immediately starts writing to the inode
7732 * it is very likely we'll catch some of their writes in this
7733 * transaction, and the commit will find this file on the ordered
7734 * data list with good things to send down.
7735 *
7736 * This is a best effort solution, there is still a window where
7737 * using truncate to replace the contents of the file will
7738 * end up with a zero length file after a crash.
7739 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007740 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7741 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007742 btrfs_add_ordered_operation(trans, root, inode);
7743
Josef Bacik5dc562c2012-08-17 13:14:17 -04007744 /*
7745 * So if we truncate and then write and fsync we normally would just
7746 * write the extents that changed, which is a problem if we need to
7747 * first truncate that entire inode. So set this flag so we write out
7748 * all of the extents in the inode to the sync log so we're completely
7749 * safe.
7750 */
7751 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007752 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007753
Yan, Zheng80825102009-11-12 09:35:36 +00007754 while (1) {
7755 ret = btrfs_truncate_inode_items(trans, root, inode,
7756 inode->i_size,
7757 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007758 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007759 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007760 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007761 }
Chris Mason39279cc2007-06-12 06:35:45 -04007762
Josef Bacikfcb80c22011-05-03 10:40:22 -04007763 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007764 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007765 if (ret) {
7766 err = ret;
7767 break;
7768 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007769
Yan, Zheng80825102009-11-12 09:35:36 +00007770 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007771 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007772
7773 trans = btrfs_start_transaction(root, 2);
7774 if (IS_ERR(trans)) {
7775 ret = err = PTR_ERR(trans);
7776 trans = NULL;
7777 break;
7778 }
7779
7780 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7781 rsv, min_size);
7782 BUG_ON(ret); /* shouldn't happen */
7783 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007784 }
7785
7786 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007787 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007788 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007789 if (ret)
7790 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007791 }
7792
Chris Mason917c16b2011-11-08 14:49:59 -05007793 if (trans) {
7794 trans->block_rsv = &root->fs_info->trans_block_rsv;
7795 ret = btrfs_update_inode(trans, root, inode);
7796 if (ret && !err)
7797 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007798
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007799 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007800 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007801 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007802
7803out:
7804 btrfs_free_block_rsv(root, rsv);
7805
Josef Bacik3893e332011-01-31 16:03:11 -05007806 if (ret && !err)
7807 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007808
Josef Bacik3893e332011-01-31 16:03:11 -05007809 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007810}
7811
Sven Wegener3b963622008-06-09 21:57:42 -04007812/*
Chris Masond352ac62008-09-29 15:18:18 -04007813 * create a new subvolume directory/inode (helper for the ioctl).
7814 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007815int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007816 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007817{
Chris Mason39279cc2007-06-12 06:35:45 -04007818 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007819 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007820 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007821
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007822 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7823 new_dirid, new_dirid,
7824 S_IFDIR | (~current_umask() & S_IRWXUGO),
7825 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007826 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007827 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007828 inode->i_op = &btrfs_dir_inode_operations;
7829 inode->i_fop = &btrfs_dir_file_operations;
7830
Miklos Szeredibfe86842011-10-28 14:13:29 +02007831 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007832 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007833
Yan, Zheng76dda932009-09-21 16:00:26 -04007834 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007835
Yan, Zheng76dda932009-09-21 16:00:26 -04007836 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007837 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007838}
7839
Chris Mason39279cc2007-06-12 06:35:45 -04007840struct inode *btrfs_alloc_inode(struct super_block *sb)
7841{
7842 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007843 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007844
7845 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7846 if (!ei)
7847 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007848
7849 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007850 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007851 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007852 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007853 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007854 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007855 ei->disk_i_size = 0;
7856 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007857 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007858 ei->index_cnt = (u64)-1;
7859 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007860 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007861
Josef Bacik9e0baf62011-07-15 15:16:44 +00007862 spin_lock_init(&ei->lock);
7863 ei->outstanding_extents = 0;
7864 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007865
Josef Bacik72ac3c02012-05-23 14:13:11 -04007866 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007867 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007868
Miao Xie16cdcec2011-04-22 18:12:22 +08007869 ei->delayed_node = NULL;
7870
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007871 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007872 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007873 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7874 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007875 ei->io_tree.track_uptodate = 1;
7876 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007877 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007878 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007879 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007880 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007881 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007882 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007883 RB_CLEAR_NODE(&ei->rb_node);
7884
7885 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007886}
7887
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007888static void btrfs_i_callback(struct rcu_head *head)
7889{
7890 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007891 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7892}
7893
Chris Mason39279cc2007-06-12 06:35:45 -04007894void btrfs_destroy_inode(struct inode *inode)
7895{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007896 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007897 struct btrfs_root *root = BTRFS_I(inode)->root;
7898
Al Virob3d9b7a2012-06-09 13:51:19 -04007899 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007900 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007901 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7902 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007903 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7904 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007905
Chris Mason5a3f23d2009-03-31 13:27:11 -04007906 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007907 * This can happen where we create an inode, but somebody else also
7908 * created the same inode and we need to destroy the one we already
7909 * created.
7910 */
7911 if (!root)
7912 goto free;
7913
7914 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007915 * Make sure we're properly removed from the ordered operation
7916 * lists.
7917 */
7918 smp_mb();
7919 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7920 spin_lock(&root->fs_info->ordered_extent_lock);
7921 list_del_init(&BTRFS_I(inode)->ordered_operations);
7922 spin_unlock(&root->fs_info->ordered_extent_lock);
7923 }
7924
Josef Bacik8a35d952012-05-23 14:26:42 -04007925 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7926 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007927 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7928 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007929 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007930 }
Josef Bacik7b128762008-07-24 12:17:14 -04007931
Chris Masond3977122009-01-05 21:25:51 -05007932 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007933 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7934 if (!ordered)
7935 break;
7936 else {
Chris Masond3977122009-01-05 21:25:51 -05007937 printk(KERN_ERR "btrfs found ordered "
7938 "extent %llu %llu on inode cleanup\n",
7939 (unsigned long long)ordered->file_offset,
7940 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007941 btrfs_remove_ordered_extent(inode, ordered);
7942 btrfs_put_ordered_extent(ordered);
7943 btrfs_put_ordered_extent(ordered);
7944 }
7945 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007946 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007947 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007948free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007949 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007950 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007951}
7952
Al Viro45321ac2010-06-07 13:43:19 -04007953int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007954{
7955 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007956
Liu Bofa6ac872013-02-20 14:10:23 +00007957 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007958 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007959 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007960 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007961 else
Al Viro45321ac2010-06-07 13:43:19 -04007962 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007963}
7964
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007965static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007966{
7967 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7968
7969 inode_init_once(&ei->vfs_inode);
7970}
7971
7972void btrfs_destroy_cachep(void)
7973{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007974 /*
7975 * Make sure all delayed rcu free inodes are flushed before we
7976 * destroy cache.
7977 */
7978 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007979 if (btrfs_inode_cachep)
7980 kmem_cache_destroy(btrfs_inode_cachep);
7981 if (btrfs_trans_handle_cachep)
7982 kmem_cache_destroy(btrfs_trans_handle_cachep);
7983 if (btrfs_transaction_cachep)
7984 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007985 if (btrfs_path_cachep)
7986 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007987 if (btrfs_free_space_cachep)
7988 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007989 if (btrfs_delalloc_work_cachep)
7990 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007991}
7992
7993int btrfs_init_cachep(void)
7994{
David Sterba837e1972012-09-07 03:00:48 -06007995 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007996 sizeof(struct btrfs_inode), 0,
7997 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007998 if (!btrfs_inode_cachep)
7999 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008000
David Sterba837e1972012-09-07 03:00:48 -06008001 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008002 sizeof(struct btrfs_trans_handle), 0,
8003 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008004 if (!btrfs_trans_handle_cachep)
8005 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008006
David Sterba837e1972012-09-07 03:00:48 -06008007 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008008 sizeof(struct btrfs_transaction), 0,
8009 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008010 if (!btrfs_transaction_cachep)
8011 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008012
David Sterba837e1972012-09-07 03:00:48 -06008013 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008014 sizeof(struct btrfs_path), 0,
8015 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008016 if (!btrfs_path_cachep)
8017 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008018
David Sterba837e1972012-09-07 03:00:48 -06008019 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008020 sizeof(struct btrfs_free_space), 0,
8021 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8022 if (!btrfs_free_space_cachep)
8023 goto fail;
8024
Miao Xie8ccf6f192012-10-25 09:28:04 +00008025 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8026 sizeof(struct btrfs_delalloc_work), 0,
8027 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8028 NULL);
8029 if (!btrfs_delalloc_work_cachep)
8030 goto fail;
8031
Chris Mason39279cc2007-06-12 06:35:45 -04008032 return 0;
8033fail:
8034 btrfs_destroy_cachep();
8035 return -ENOMEM;
8036}
8037
8038static int btrfs_getattr(struct vfsmount *mnt,
8039 struct dentry *dentry, struct kstat *stat)
8040{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008041 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008042 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008043 u32 blocksize = inode->i_sb->s_blocksize;
8044
Chris Mason39279cc2007-06-12 06:35:45 -04008045 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008046 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008047 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008048
8049 spin_lock(&BTRFS_I(inode)->lock);
8050 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8051 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008052 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008053 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008054 return 0;
8055}
8056
Chris Masond3977122009-01-05 21:25:51 -05008057static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8058 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008059{
8060 struct btrfs_trans_handle *trans;
8061 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008062 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008063 struct inode *new_inode = new_dentry->d_inode;
8064 struct inode *old_inode = old_dentry->d_inode;
8065 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008066 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008067 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008068 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008069 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008070
Li Zefan33345d012011-04-20 10:31:50 +08008071 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008072 return -EPERM;
8073
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008074 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008075 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008076 return -EXDEV;
8077
Li Zefan33345d012011-04-20 10:31:50 +08008078 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8079 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008080 return -ENOTEMPTY;
8081
Chris Mason39279cc2007-06-12 06:35:45 -04008082 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008083 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008084 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008085
8086
8087 /* check for collisions, even if the name isn't there */
8088 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8089 new_dentry->d_name.name,
8090 new_dentry->d_name.len);
8091
8092 if (ret) {
8093 if (ret == -EEXIST) {
8094 /* we shouldn't get
8095 * eexist without a new_inode */
8096 if (!new_inode) {
8097 WARN_ON(1);
8098 return ret;
8099 }
8100 } else {
8101 /* maybe -EOVERFLOW */
8102 return ret;
8103 }
8104 }
8105 ret = 0;
8106
Chris Mason5a3f23d2009-03-31 13:27:11 -04008107 /*
8108 * we're using rename to replace one file with another.
8109 * and the replacement file is large. Start IO on it now so
8110 * we don't add too much work to the end of the transaction
8111 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008112 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008113 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8114 filemap_flush(old_inode->i_mapping);
8115
Yan, Zheng76dda932009-09-21 16:00:26 -04008116 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008117 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008118 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008119 /*
8120 * We want to reserve the absolute worst case amount of items. So if
8121 * both inodes are subvols and we need to unlink them then that would
8122 * require 4 item modifications, but if they are both normal inodes it
8123 * would require 5 item modifications, so we'll assume their normal
8124 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8125 * should cover the worst case number of items we'll modify.
8126 */
8127 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008128 if (IS_ERR(trans)) {
8129 ret = PTR_ERR(trans);
8130 goto out_notrans;
8131 }
Chris Mason5f39d392007-10-15 16:14:19 -04008132
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008133 if (dest != root)
8134 btrfs_record_root_in_trans(trans, dest);
8135
Yan, Zhenga5719522009-09-24 09:17:31 -04008136 ret = btrfs_set_inode_index(new_dir, &index);
8137 if (ret)
8138 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008139
Li Zefan33345d012011-04-20 10:31:50 +08008140 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008141 /* force full log commit if subvolume involved. */
8142 root->fs_info->last_trans_log_full_commit = trans->transid;
8143 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008144 ret = btrfs_insert_inode_ref(trans, dest,
8145 new_dentry->d_name.name,
8146 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008147 old_ino,
8148 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008149 if (ret)
8150 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008151 /*
8152 * this is an ugly little race, but the rename is required
8153 * to make sure that if we crash, the inode is either at the
8154 * old name or the new one. pinning the log transaction lets
8155 * us make sure we don't allow a log commit to come in after
8156 * we unlink the name but before we add the new name back in.
8157 */
8158 btrfs_pin_log_trans(root);
8159 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008160 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008161 * make sure the inode gets flushed if it is replacing
8162 * something.
8163 */
Li Zefan33345d012011-04-20 10:31:50 +08008164 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008165 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008166
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008167 inode_inc_iversion(old_dir);
8168 inode_inc_iversion(new_dir);
8169 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008170 old_dir->i_ctime = old_dir->i_mtime = ctime;
8171 new_dir->i_ctime = new_dir->i_mtime = ctime;
8172 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008173
Chris Mason12fcfd22009-03-24 10:24:20 -04008174 if (old_dentry->d_parent != new_dentry->d_parent)
8175 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8176
Li Zefan33345d012011-04-20 10:31:50 +08008177 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008178 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8179 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8180 old_dentry->d_name.name,
8181 old_dentry->d_name.len);
8182 } else {
Al Viro92986792011-03-04 17:14:37 +00008183 ret = __btrfs_unlink_inode(trans, root, old_dir,
8184 old_dentry->d_inode,
8185 old_dentry->d_name.name,
8186 old_dentry->d_name.len);
8187 if (!ret)
8188 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008189 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008190 if (ret) {
8191 btrfs_abort_transaction(trans, root, ret);
8192 goto out_fail;
8193 }
Chris Mason39279cc2007-06-12 06:35:45 -04008194
8195 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008196 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008197 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008198 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008199 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8200 root_objectid = BTRFS_I(new_inode)->location.objectid;
8201 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8202 root_objectid,
8203 new_dentry->d_name.name,
8204 new_dentry->d_name.len);
8205 BUG_ON(new_inode->i_nlink == 0);
8206 } else {
8207 ret = btrfs_unlink_inode(trans, dest, new_dir,
8208 new_dentry->d_inode,
8209 new_dentry->d_name.name,
8210 new_dentry->d_name.len);
8211 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008212 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008213 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008214 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008215 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008216 if (ret) {
8217 btrfs_abort_transaction(trans, root, ret);
8218 goto out_fail;
8219 }
Chris Mason39279cc2007-06-12 06:35:45 -04008220 }
Josef Bacikaec74772008-07-24 12:12:38 -04008221
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008222 ret = btrfs_add_link(trans, new_dir, old_inode,
8223 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008224 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008225 if (ret) {
8226 btrfs_abort_transaction(trans, root, ret);
8227 goto out_fail;
8228 }
Chris Mason39279cc2007-06-12 06:35:45 -04008229
Li Zefan33345d012011-04-20 10:31:50 +08008230 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008231 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008232 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008233 btrfs_end_log_trans(root);
8234 }
Chris Mason39279cc2007-06-12 06:35:45 -04008235out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008236 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008237out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008238 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008239 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008240
Chris Mason39279cc2007-06-12 06:35:45 -04008241 return ret;
8242}
8243
Miao Xie8ccf6f192012-10-25 09:28:04 +00008244static void btrfs_run_delalloc_work(struct btrfs_work *work)
8245{
8246 struct btrfs_delalloc_work *delalloc_work;
8247
8248 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8249 work);
8250 if (delalloc_work->wait)
8251 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8252 else
8253 filemap_flush(delalloc_work->inode->i_mapping);
8254
8255 if (delalloc_work->delay_iput)
8256 btrfs_add_delayed_iput(delalloc_work->inode);
8257 else
8258 iput(delalloc_work->inode);
8259 complete(&delalloc_work->completion);
8260}
8261
8262struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8263 int wait, int delay_iput)
8264{
8265 struct btrfs_delalloc_work *work;
8266
8267 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8268 if (!work)
8269 return NULL;
8270
8271 init_completion(&work->completion);
8272 INIT_LIST_HEAD(&work->list);
8273 work->inode = inode;
8274 work->wait = wait;
8275 work->delay_iput = delay_iput;
8276 work->work.func = btrfs_run_delalloc_work;
8277
8278 return work;
8279}
8280
8281void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8282{
8283 wait_for_completion(&work->completion);
8284 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8285}
8286
Chris Masond352ac62008-09-29 15:18:18 -04008287/*
8288 * some fairly slow code that needs optimization. This walks the list
8289 * of all the inodes with pending delalloc and forces them to disk.
8290 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008291int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008292{
Chris Masonea8c2812008-08-04 23:17:27 -04008293 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008294 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008295 struct btrfs_delalloc_work *work, *next;
8296 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008297 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008298 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008299
Yan Zhengc146afa2008-11-12 14:34:12 -05008300 if (root->fs_info->sb->s_flags & MS_RDONLY)
8301 return -EROFS;
8302
Miao Xie8ccf6f192012-10-25 09:28:04 +00008303 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008304 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008305
Chris Mason75eff682008-12-15 15:54:40 -05008306 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008307 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8308 while (!list_empty(&splice)) {
8309 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008310 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008311
8312 list_del_init(&binode->delalloc_inodes);
8313
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008314 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008315 if (!inode) {
8316 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8317 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008318 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008319 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008320
8321 list_add_tail(&binode->delalloc_inodes,
8322 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008323 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008324
8325 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8326 if (unlikely(!work)) {
8327 ret = -ENOMEM;
8328 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008329 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008330 list_add_tail(&work->list, &works);
8331 btrfs_queue_worker(&root->fs_info->flush_workers,
8332 &work->work);
8333
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008334 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008335 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008336 }
Chris Mason75eff682008-12-15 15:54:40 -05008337 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008338
Miao Xie1eafa6c2013-01-22 10:49:00 +00008339 list_for_each_entry_safe(work, next, &works, list) {
8340 list_del_init(&work->list);
8341 btrfs_wait_and_free_delalloc_work(work);
8342 }
8343
Chris Mason8c8bee12008-09-29 11:19:10 -04008344 /* the filemap_flush will queue IO into the worker threads, but
8345 * we have to make sure the IO is actually started and that
8346 * ordered extents get created before we return
8347 */
8348 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008349 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008350 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008351 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008352 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8353 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008354 }
8355 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008356 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008357out:
8358 list_for_each_entry_safe(work, next, &works, list) {
8359 list_del_init(&work->list);
8360 btrfs_wait_and_free_delalloc_work(work);
8361 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008362
8363 if (!list_empty_careful(&splice)) {
8364 spin_lock(&root->fs_info->delalloc_lock);
8365 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8366 spin_unlock(&root->fs_info->delalloc_lock);
8367 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008368 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008369}
8370
Chris Mason39279cc2007-06-12 06:35:45 -04008371static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8372 const char *symname)
8373{
8374 struct btrfs_trans_handle *trans;
8375 struct btrfs_root *root = BTRFS_I(dir)->root;
8376 struct btrfs_path *path;
8377 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008378 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008379 int err;
8380 int drop_inode = 0;
8381 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008382 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008383 int name_len;
8384 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008385 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008386 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008387 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008388
8389 name_len = strlen(symname) + 1;
8390 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8391 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008392
Josef Bacik9ed74f22009-09-11 16:12:44 -04008393 /*
8394 * 2 items for inode item and ref
8395 * 2 items for dir items
8396 * 1 item for xattr if selinux is on
8397 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008398 trans = btrfs_start_transaction(root, 5);
8399 if (IS_ERR(trans))
8400 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008401
Li Zefan581bb052011-04-20 10:06:11 +08008402 err = btrfs_find_free_ino(root, &objectid);
8403 if (err)
8404 goto out_unlock;
8405
Josef Bacikaec74772008-07-24 12:12:38 -04008406 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008407 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008408 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008409 if (IS_ERR(inode)) {
8410 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008411 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008412 }
Chris Mason39279cc2007-06-12 06:35:45 -04008413
Eric Paris2a7dba32011-02-01 11:05:39 -05008414 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008415 if (err) {
8416 drop_inode = 1;
8417 goto out_unlock;
8418 }
8419
Casey Schauflerad19db72011-12-15 10:09:07 -05008420 /*
8421 * If the active LSM wants to access the inode during
8422 * d_instantiate it needs these. Smack checks to see
8423 * if the filesystem supports xattrs by looking at the
8424 * ops vector.
8425 */
8426 inode->i_fop = &btrfs_file_operations;
8427 inode->i_op = &btrfs_file_inode_operations;
8428
Josef Bacika1b075d2010-11-19 20:36:11 +00008429 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008430 if (err)
8431 drop_inode = 1;
8432 else {
8433 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008434 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008435 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008436 }
Chris Mason39279cc2007-06-12 06:35:45 -04008437 if (drop_inode)
8438 goto out_unlock;
8439
8440 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008441 if (!path) {
8442 err = -ENOMEM;
8443 drop_inode = 1;
8444 goto out_unlock;
8445 }
Li Zefan33345d012011-04-20 10:31:50 +08008446 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008447 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008448 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8449 datasize = btrfs_file_extent_calc_inline_size(name_len);
8450 err = btrfs_insert_empty_item(trans, root, path, &key,
8451 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008452 if (err) {
8453 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008454 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008455 goto out_unlock;
8456 }
Chris Mason5f39d392007-10-15 16:14:19 -04008457 leaf = path->nodes[0];
8458 ei = btrfs_item_ptr(leaf, path->slots[0],
8459 struct btrfs_file_extent_item);
8460 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8461 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008462 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008463 btrfs_set_file_extent_encryption(leaf, ei, 0);
8464 btrfs_set_file_extent_compression(leaf, ei, 0);
8465 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8466 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8467
Chris Mason39279cc2007-06-12 06:35:45 -04008468 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008469 write_extent_buffer(leaf, symname, ptr, name_len);
8470 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008471 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008472
Chris Mason39279cc2007-06-12 06:35:45 -04008473 inode->i_op = &btrfs_symlink_inode_operations;
8474 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008475 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008476 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008477 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008478 err = btrfs_update_inode(trans, root, inode);
8479 if (err)
8480 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008481
8482out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008483 if (!err)
8484 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008485 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008486 if (drop_inode) {
8487 inode_dec_link_count(inode);
8488 iput(inode);
8489 }
Liu Bob53d3f52012-11-14 14:34:34 +00008490 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008491 return err;
8492}
Chris Mason16432982008-04-10 10:23:21 -04008493
Josef Bacik0af3d002010-06-21 14:48:16 -04008494static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8495 u64 start, u64 num_bytes, u64 min_size,
8496 loff_t actual_len, u64 *alloc_hint,
8497 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008498{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008499 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8500 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008501 struct btrfs_root *root = BTRFS_I(inode)->root;
8502 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008503 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008504 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04008505 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008506 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008507
Josef Bacik0af3d002010-06-21 14:48:16 -04008508 if (trans)
8509 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008510 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008511 if (own_trans) {
8512 trans = btrfs_start_transaction(root, 3);
8513 if (IS_ERR(trans)) {
8514 ret = PTR_ERR(trans);
8515 break;
8516 }
Yan Zhengd899e052008-10-30 14:25:28 -04008517 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008518
Zach Brown24542bf2012-11-16 00:04:43 +00008519 ret = btrfs_reserve_extent(trans, root,
8520 min(num_bytes, 256ULL * 1024 * 1024),
8521 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008522 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008523 if (own_trans)
8524 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008525 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008526 }
8527
Yan Zhengd899e052008-10-30 14:25:28 -04008528 ret = insert_reserved_file_extent(trans, inode,
8529 cur_offset, ins.objectid,
8530 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008531 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008532 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008533 if (ret) {
8534 btrfs_abort_transaction(trans, root, ret);
8535 if (own_trans)
8536 btrfs_end_transaction(trans, root);
8537 break;
8538 }
Chris Masona1ed8352009-09-11 12:27:37 -04008539 btrfs_drop_extent_cache(inode, cur_offset,
8540 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008541
Josef Bacik5dc562c2012-08-17 13:14:17 -04008542 em = alloc_extent_map();
8543 if (!em) {
8544 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8545 &BTRFS_I(inode)->runtime_flags);
8546 goto next;
8547 }
8548
8549 em->start = cur_offset;
8550 em->orig_start = cur_offset;
8551 em->len = ins.offset;
8552 em->block_start = ins.objectid;
8553 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008554 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008555 em->bdev = root->fs_info->fs_devices->latest_bdev;
8556 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8557 em->generation = trans->transid;
8558
8559 while (1) {
8560 write_lock(&em_tree->lock);
8561 ret = add_extent_mapping(em_tree, em);
8562 if (!ret)
8563 list_move(&em->list,
8564 &em_tree->modified_extents);
8565 write_unlock(&em_tree->lock);
8566 if (ret != -EEXIST)
8567 break;
8568 btrfs_drop_extent_cache(inode, cur_offset,
8569 cur_offset + ins.offset - 1,
8570 0);
8571 }
8572 free_extent_map(em);
8573next:
Yan Zhengd899e052008-10-30 14:25:28 -04008574 num_bytes -= ins.offset;
8575 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008576 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008577
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008578 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008579 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008580 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008581 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008582 (actual_len > inode->i_size) &&
8583 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008584 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008585 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008586 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008587 i_size = cur_offset;
8588 i_size_write(inode, i_size);
8589 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008590 }
8591
Yan Zhengd899e052008-10-30 14:25:28 -04008592 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008593
8594 if (ret) {
8595 btrfs_abort_transaction(trans, root, ret);
8596 if (own_trans)
8597 btrfs_end_transaction(trans, root);
8598 break;
8599 }
Yan Zhengd899e052008-10-30 14:25:28 -04008600
Josef Bacik0af3d002010-06-21 14:48:16 -04008601 if (own_trans)
8602 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008603 }
Yan Zhengd899e052008-10-30 14:25:28 -04008604 return ret;
8605}
8606
Josef Bacik0af3d002010-06-21 14:48:16 -04008607int btrfs_prealloc_file_range(struct inode *inode, int mode,
8608 u64 start, u64 num_bytes, u64 min_size,
8609 loff_t actual_len, u64 *alloc_hint)
8610{
8611 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8612 min_size, actual_len, alloc_hint,
8613 NULL);
8614}
8615
8616int btrfs_prealloc_file_range_trans(struct inode *inode,
8617 struct btrfs_trans_handle *trans, int mode,
8618 u64 start, u64 num_bytes, u64 min_size,
8619 loff_t actual_len, u64 *alloc_hint)
8620{
8621 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8622 min_size, actual_len, alloc_hint, trans);
8623}
8624
Chris Masone6dcd2d2008-07-17 12:53:50 -04008625static int btrfs_set_page_dirty(struct page *page)
8626{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008627 return __set_page_dirty_nobuffers(page);
8628}
8629
Al Viro10556cb2011-06-20 19:28:19 -04008630static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008631{
Li Zefanb83cc962010-12-20 16:04:08 +08008632 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008633 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008634
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008635 if (mask & MAY_WRITE &&
8636 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8637 if (btrfs_root_readonly(root))
8638 return -EROFS;
8639 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8640 return -EACCES;
8641 }
Al Viro2830ba72011-06-20 19:16:29 -04008642 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008643}
Chris Mason39279cc2007-06-12 06:35:45 -04008644
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008645static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008646 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008647 .lookup = btrfs_lookup,
8648 .create = btrfs_create,
8649 .unlink = btrfs_unlink,
8650 .link = btrfs_link,
8651 .mkdir = btrfs_mkdir,
8652 .rmdir = btrfs_rmdir,
8653 .rename = btrfs_rename,
8654 .symlink = btrfs_symlink,
8655 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008656 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008657 .setxattr = btrfs_setxattr,
8658 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008659 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008660 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008661 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008662 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008663};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008664static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008665 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008666 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008667 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008668};
Yan, Zheng76dda932009-09-21 16:00:26 -04008669
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008670static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008671 .llseek = generic_file_llseek,
8672 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008673 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008674 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008675#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008676 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008677#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008678 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008679 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008680};
8681
Chris Masond1310b22008-01-24 16:13:08 -05008682static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008683 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008684 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008685 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008686 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008687 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008688 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008689 .set_bit_hook = btrfs_set_bit_hook,
8690 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008691 .merge_extent_hook = btrfs_merge_extent_hook,
8692 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008693};
8694
Chris Mason35054392009-01-21 13:11:13 -05008695/*
8696 * btrfs doesn't support the bmap operation because swapfiles
8697 * use bmap to make a mapping of extents in the file. They assume
8698 * these extents won't change over the life of the file and they
8699 * use the bmap result to do IO directly to the drive.
8700 *
8701 * the btrfs bmap call would return logical addresses that aren't
8702 * suitable for IO and they also will change frequently as COW
8703 * operations happen. So, swapfile + btrfs == corruption.
8704 *
8705 * For now we're avoiding this by dropping bmap.
8706 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008707static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008708 .readpage = btrfs_readpage,
8709 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008710 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008711 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008712 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008713 .invalidatepage = btrfs_invalidatepage,
8714 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008715 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008716 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008717};
8718
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008719static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008720 .readpage = btrfs_readpage,
8721 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008722 .invalidatepage = btrfs_invalidatepage,
8723 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008724};
8725
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008726static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008727 .getattr = btrfs_getattr,
8728 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008729 .setxattr = btrfs_setxattr,
8730 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008731 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008732 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008733 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008734 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008735 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008736 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008737};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008738static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008739 .getattr = btrfs_getattr,
8740 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008741 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008742 .setxattr = btrfs_setxattr,
8743 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008744 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008745 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008746 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008747 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008748};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008749static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008750 .readlink = generic_readlink,
8751 .follow_link = page_follow_link_light,
8752 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008753 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008754 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008755 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008756 .setxattr = btrfs_setxattr,
8757 .getxattr = btrfs_getxattr,
8758 .listxattr = btrfs_listxattr,
8759 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008760 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008761 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008762};
Yan, Zheng76dda932009-09-21 16:00:26 -04008763
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008764const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008765 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008766 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008767};