blob: 4d0aec0cf5d80ed3a2994ec452f8270dacd7ad27 [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;
236 u64 aligned_end = (end + root->sectorsize - 1) &
237 ~((u64)root->sectorsize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400238 u64 data_len = inline_len;
239 int ret;
240
241 if (compressed_size)
242 data_len = compressed_size;
243
244 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400245 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400246 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
247 (!compressed_size &&
248 (actual_end & (root->sectorsize - 1)) == 0) ||
249 end + 1 < isize ||
250 data_len > root->fs_info->max_inline) {
251 return 1;
252 }
253
Josef Bacik26714852012-08-29 12:24:27 -0400254 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100255 if (ret)
256 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400257
258 if (isize > actual_end)
259 inline_len = min_t(u64, isize, actual_end);
260 ret = insert_inline_extent(trans, root, inode, start,
261 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000262 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400263 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100264 btrfs_abort_transaction(trans, root, ret);
265 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400266 } else if (ret == -ENOSPC) {
267 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100268 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400269
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400270 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400271 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400272 return 0;
273}
274
Chris Mason771ed682008-11-06 22:02:51 -0500275struct async_extent {
276 u64 start;
277 u64 ram_size;
278 u64 compressed_size;
279 struct page **pages;
280 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800281 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500282 struct list_head list;
283};
284
285struct async_cow {
286 struct inode *inode;
287 struct btrfs_root *root;
288 struct page *locked_page;
289 u64 start;
290 u64 end;
291 struct list_head extents;
292 struct btrfs_work work;
293};
294
295static noinline int add_async_extent(struct async_cow *cow,
296 u64 start, u64 ram_size,
297 u64 compressed_size,
298 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800299 unsigned long nr_pages,
300 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500301{
302 struct async_extent *async_extent;
303
304 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100305 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500306 async_extent->start = start;
307 async_extent->ram_size = ram_size;
308 async_extent->compressed_size = compressed_size;
309 async_extent->pages = pages;
310 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800311 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500312 list_add_tail(&async_extent->list, &cow->extents);
313 return 0;
314}
315
Chris Masonc8b97812008-10-29 14:49:59 -0400316/*
Chris Mason771ed682008-11-06 22:02:51 -0500317 * we create compressed extents in two phases. The first
318 * phase compresses a range of pages that have already been
319 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400320 *
Chris Mason771ed682008-11-06 22:02:51 -0500321 * This is done inside an ordered work queue, and the compression
322 * is spread across many cpus. The actual IO submission is step
323 * two, and the ordered work queue takes care of making sure that
324 * happens in the same order things were put onto the queue by
325 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400326 *
Chris Mason771ed682008-11-06 22:02:51 -0500327 * If this code finds it can't get good compression, it puts an
328 * entry onto the work queue to write the uncompressed bytes. This
329 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300330 * are written in the same order that the flusher thread sent them
331 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400332 */
Chris Mason771ed682008-11-06 22:02:51 -0500333static noinline int compress_file_range(struct inode *inode,
334 struct page *locked_page,
335 u64 start, u64 end,
336 struct async_cow *async_cow,
337 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400338{
339 struct btrfs_root *root = BTRFS_I(inode)->root;
340 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400341 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400342 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400343 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500344 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400345 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400346 struct page **pages = NULL;
347 unsigned long nr_pages;
348 unsigned long nr_pages_ret = 0;
349 unsigned long total_compressed = 0;
350 unsigned long total_in = 0;
351 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500352 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400353 int i;
354 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800355 int compress_type = root->fs_info->compress_type;
Chris Masonb888db2b2007-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 Masonf03d9301f2009-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);
Chris Masondb945352007-10-15 16:15:53 -0400394 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
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 */
493 total_compressed = (total_compressed + blocksize - 1) &
494 ~(blocksize - 1);
495
496 /*
497 * one last check to make sure the compression is really a
498 * win, compare the page count read with the blocks on disk
499 */
500 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
501 ~(PAGE_CACHE_SIZE - 1);
502 if (total_compressed >= total_in) {
503 will_compress = 0;
504 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400505 num_bytes = total_in;
506 }
507 }
508 if (!will_compress && pages) {
509 /*
510 * the compression code ran but failed to make things smaller,
511 * free any pages it allocated and our page pointer array
512 */
513 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400514 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400515 page_cache_release(pages[i]);
516 }
517 kfree(pages);
518 pages = NULL;
519 total_compressed = 0;
520 nr_pages_ret = 0;
521
522 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500523 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
524 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500525 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500526 }
Chris Masonc8b97812008-10-29 14:49:59 -0400527 }
Chris Mason771ed682008-11-06 22:02:51 -0500528 if (will_compress) {
529 *num_added += 1;
530
531 /* the async work queues will take care of doing actual
532 * allocation on disk for these compressed pages,
533 * and will submit them to the elevator.
534 */
535 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800536 total_compressed, pages, nr_pages_ret,
537 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500538
Yan, Zheng24ae6362010-12-06 07:02:36 +0000539 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500540 start += num_bytes;
541 pages = NULL;
542 cond_resched();
543 goto again;
544 }
545 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500546cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500547 /*
548 * No compression, but we still need to write the pages in
549 * the file we've been given so far. redirty the locked
550 * page if it corresponds to our extent and set things up
551 * for the async work queue to run cow_file_range to do
552 * the normal delalloc dance
553 */
554 if (page_offset(locked_page) >= start &&
555 page_offset(locked_page) <= end) {
556 __set_page_dirty_nobuffers(locked_page);
557 /* unlocked later on in the async handlers */
558 }
Li Zefan261507a02010-12-17 14:21:50 +0800559 add_async_extent(async_cow, start, end - start + 1,
560 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500561 *num_added += 1;
562 }
563
564out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100565 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500566
567free_pages_out:
568 for (i = 0; i < nr_pages_ret; i++) {
569 WARN_ON(pages[i]->mapping);
570 page_cache_release(pages[i]);
571 }
Chris Masond3977122009-01-05 21:25:51 -0500572 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500573
574 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100575
576cleanup_and_out:
577 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
578 start, end, NULL,
579 EXTENT_CLEAR_UNLOCK_PAGE |
580 EXTENT_CLEAR_DIRTY |
581 EXTENT_CLEAR_DELALLOC |
582 EXTENT_SET_WRITEBACK |
583 EXTENT_END_WRITEBACK);
584 if (!trans || IS_ERR(trans))
585 btrfs_error(root->fs_info, ret, "Failed to join transaction");
586 else
587 btrfs_abort_transaction(trans, root, ret);
588 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500589}
590
591/*
592 * phase two of compressed writeback. This is the ordered portion
593 * of the code, which only gets called in the order the work was
594 * queued. We walk all the async extents created by compress_file_range
595 * and send them down to the disk.
596 */
597static noinline int submit_compressed_extents(struct inode *inode,
598 struct async_cow *async_cow)
599{
600 struct async_extent *async_extent;
601 u64 alloc_hint = 0;
602 struct btrfs_trans_handle *trans;
603 struct btrfs_key ins;
604 struct extent_map *em;
605 struct btrfs_root *root = BTRFS_I(inode)->root;
606 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
607 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500608 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500609
610 if (list_empty(&async_cow->extents))
611 return 0;
612
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500613again:
Chris Masond3977122009-01-05 21:25:51 -0500614 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500615 async_extent = list_entry(async_cow->extents.next,
616 struct async_extent, list);
617 list_del(&async_extent->list);
618
619 io_tree = &BTRFS_I(inode)->io_tree;
620
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500621retry:
Chris Mason771ed682008-11-06 22:02:51 -0500622 /* did the compression code fall back to uncompressed IO? */
623 if (!async_extent->pages) {
624 int page_started = 0;
625 unsigned long nr_written = 0;
626
627 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000628 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100629 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500630
631 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500632 ret = cow_file_range(inode, async_cow->locked_page,
633 async_extent->start,
634 async_extent->start +
635 async_extent->ram_size - 1,
636 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500637
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100638 /* JDM XXX */
639
Chris Mason771ed682008-11-06 22:02:51 -0500640 /*
641 * if page_started, cow_file_range inserted an
642 * inline extent and took care of all the unlocking
643 * and IO for us. Otherwise, we need to submit
644 * all those pages down to the drive.
645 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500646 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500647 extent_write_locked_range(io_tree,
648 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500649 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500650 async_extent->ram_size - 1,
651 btrfs_get_extent,
652 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500653 else if (ret)
654 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500655 kfree(async_extent);
656 cond_resched();
657 continue;
658 }
659
660 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100661 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500662
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400663 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100664 if (IS_ERR(trans)) {
665 ret = PTR_ERR(trans);
666 } else {
667 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
668 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500669 async_extent->compressed_size,
670 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500671 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600672 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100673 btrfs_abort_transaction(trans, root, ret);
674 btrfs_end_transaction(trans, root);
675 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000676
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500677 if (ret) {
678 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500679
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500680 for (i = 0; i < async_extent->nr_pages; i++) {
681 WARN_ON(async_extent->pages[i]->mapping);
682 page_cache_release(async_extent->pages[i]);
683 }
684 kfree(async_extent->pages);
685 async_extent->nr_pages = 0;
686 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500687
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100688 if (ret == -ENOSPC)
689 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500690 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500691 }
692
Yan, Zhengc2167752009-11-12 09:34:21 +0000693 /*
694 * here we're doing allocation and writeback of the
695 * compressed pages
696 */
697 btrfs_drop_extent_cache(inode, async_extent->start,
698 async_extent->start +
699 async_extent->ram_size - 1, 0);
700
David Sterba172ddd62011-04-21 00:48:27 +0200701 em = alloc_extent_map();
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500702 if (!em)
703 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500704 em->start = async_extent->start;
705 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500706 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400707 em->mod_start = em->start;
708 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500709
710 em->block_start = ins.objectid;
711 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500712 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500713 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800714 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500715 set_bit(EXTENT_FLAG_PINNED, &em->flags);
716 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400717 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500718
Chris Masond3977122009-01-05 21:25:51 -0500719 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400720 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500721 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400722 if (!ret)
723 list_move(&em->list,
724 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400725 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500726 if (ret != -EEXIST) {
727 free_extent_map(em);
728 break;
729 }
730 btrfs_drop_extent_cache(inode, async_extent->start,
731 async_extent->start +
732 async_extent->ram_size - 1, 0);
733 }
734
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500735 if (ret)
736 goto out_free_reserve;
737
Li Zefan261507a02010-12-17 14:21:50 +0800738 ret = btrfs_add_ordered_extent_compress(inode,
739 async_extent->start,
740 ins.objectid,
741 async_extent->ram_size,
742 ins.offset,
743 BTRFS_ORDERED_COMPRESSED,
744 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500745 if (ret)
746 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500747
Chris Mason771ed682008-11-06 22:02:51 -0500748 /*
749 * clear dirty, set writeback and unlock the pages.
750 */
751 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400752 &BTRFS_I(inode)->io_tree,
753 async_extent->start,
754 async_extent->start +
755 async_extent->ram_size - 1,
756 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
757 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400758 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400759 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500760
761 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500762 async_extent->start,
763 async_extent->ram_size,
764 ins.objectid,
765 ins.offset, async_extent->pages,
766 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500767 alloc_hint = ins.objectid + ins.offset;
768 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500769 if (ret)
770 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500771 cond_resched();
772 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100773 ret = 0;
774out:
775 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500776out_free_reserve:
777 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100778out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500779 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
780 async_extent->start,
781 async_extent->start +
782 async_extent->ram_size - 1,
783 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
784 EXTENT_CLEAR_UNLOCK |
785 EXTENT_CLEAR_DELALLOC |
786 EXTENT_CLEAR_DIRTY |
787 EXTENT_SET_WRITEBACK |
788 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100789 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500790 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500791}
792
Josef Bacik4b46fce2010-05-23 11:00:55 -0400793static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
794 u64 num_bytes)
795{
796 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
797 struct extent_map *em;
798 u64 alloc_hint = 0;
799
800 read_lock(&em_tree->lock);
801 em = search_extent_mapping(em_tree, start, num_bytes);
802 if (em) {
803 /*
804 * if block start isn't an actual block number then find the
805 * first block in this inode and use that as a hint. If that
806 * block is also bogus then just don't worry about it.
807 */
808 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
809 free_extent_map(em);
810 em = search_extent_mapping(em_tree, 0, 0);
811 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
812 alloc_hint = em->block_start;
813 if (em)
814 free_extent_map(em);
815 } else {
816 alloc_hint = em->block_start;
817 free_extent_map(em);
818 }
819 }
820 read_unlock(&em_tree->lock);
821
822 return alloc_hint;
823}
824
Chris Mason771ed682008-11-06 22:02:51 -0500825/*
826 * when extent_io.c finds a delayed allocation range in the file,
827 * the call backs end up in this code. The basic idea is to
828 * allocate extents on disk for the range, and create ordered data structs
829 * in ram to track those extents.
830 *
831 * locked_page is the page that writepage had locked already. We use
832 * it to make sure we don't do extra locks or unlocks.
833 *
834 * *page_started is set to one if we unlock locked_page and do everything
835 * required to start IO on it. It may be clean and already done with
836 * IO when we return.
837 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000838static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
839 struct inode *inode,
840 struct btrfs_root *root,
841 struct page *locked_page,
842 u64 start, u64 end, int *page_started,
843 unsigned long *nr_written,
844 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500845{
Chris Mason771ed682008-11-06 22:02:51 -0500846 u64 alloc_hint = 0;
847 u64 num_bytes;
848 unsigned long ram_size;
849 u64 disk_num_bytes;
850 u64 cur_alloc_size;
851 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500852 struct btrfs_key ins;
853 struct extent_map *em;
854 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
855 int ret = 0;
856
Liu Bo83eea1f2012-07-10 05:28:39 -0600857 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500858
Chris Mason771ed682008-11-06 22:02:51 -0500859 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
860 num_bytes = max(blocksize, num_bytes);
861 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500862
Chris Mason4cb53002011-05-24 15:35:30 -0400863 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400864 if (num_bytes < 64 * 1024 &&
865 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400866 btrfs_add_inode_defrag(trans, inode);
867
Chris Mason771ed682008-11-06 22:02:51 -0500868 if (start == 0) {
869 /* lets try to make an inline extent */
870 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000871 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500872 if (ret == 0) {
873 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400874 &BTRFS_I(inode)->io_tree,
875 start, end, NULL,
876 EXTENT_CLEAR_UNLOCK_PAGE |
877 EXTENT_CLEAR_UNLOCK |
878 EXTENT_CLEAR_DELALLOC |
879 EXTENT_CLEAR_DIRTY |
880 EXTENT_SET_WRITEBACK |
881 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000882
Chris Mason771ed682008-11-06 22:02:51 -0500883 *nr_written = *nr_written +
884 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
885 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500886 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100887 } else if (ret < 0) {
888 btrfs_abort_transaction(trans, root, ret);
889 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500890 }
891 }
Chris Masonc8b97812008-10-29 14:49:59 -0400892
893 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200894 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400895
Josef Bacik4b46fce2010-05-23 11:00:55 -0400896 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400897 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400898
Chris Masond3977122009-01-05 21:25:51 -0500899 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400900 unsigned long op;
901
Josef Bacik287a0ab2010-03-19 18:07:23 +0000902 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400903 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500904 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500905 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100906 if (ret < 0) {
907 btrfs_abort_transaction(trans, root, ret);
908 goto out_unlock;
909 }
Chris Masond3977122009-01-05 21:25:51 -0500910
David Sterba172ddd62011-04-21 00:48:27 +0200911 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100912 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400913 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500914 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500915 ram_size = ins.offset;
916 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400917 em->mod_start = em->start;
918 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400919
Chris Masone6dcd2d2008-07-17 12:53:50 -0400920 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400921 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500922 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400923 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400924 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400925 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400926
Chris Masond3977122009-01-05 21:25:51 -0500927 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400928 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400929 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400930 if (!ret)
931 list_move(&em->list,
932 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400933 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400934 if (ret != -EEXIST) {
935 free_extent_map(em);
936 break;
937 }
938 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400939 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400940 }
941
Chris Mason98d20f62008-04-14 09:46:10 -0400942 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400943 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500944 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100945 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400946
Yan Zheng17d217f2008-12-12 10:03:38 -0500947 if (root->root_key.objectid ==
948 BTRFS_DATA_RELOC_TREE_OBJECTID) {
949 ret = btrfs_reloc_clone_csums(inode, start,
950 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100951 if (ret) {
952 btrfs_abort_transaction(trans, root, ret);
953 goto out_unlock;
954 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500955 }
956
Chris Masond3977122009-01-05 21:25:51 -0500957 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400958 break;
Chris Masond3977122009-01-05 21:25:51 -0500959
Chris Masonc8b97812008-10-29 14:49:59 -0400960 /* we're not doing compressed IO, don't unlock the first
961 * page (which the caller expects to stay locked), don't
962 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400963 *
964 * Do set the Private2 bit so we know this page was properly
965 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400966 */
Chris Masona791e352009-10-08 11:27:10 -0400967 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
968 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
969 EXTENT_SET_PRIVATE2;
970
Chris Masonc8b97812008-10-29 14:49:59 -0400971 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
972 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400973 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400974 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500975 num_bytes -= cur_alloc_size;
976 alloc_hint = ins.objectid + ins.offset;
977 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400978 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100979out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500980 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000981
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100982out_unlock:
983 extent_clear_unlock_delalloc(inode,
984 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400985 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100986 EXTENT_CLEAR_UNLOCK_PAGE |
987 EXTENT_CLEAR_UNLOCK |
988 EXTENT_CLEAR_DELALLOC |
989 EXTENT_CLEAR_DIRTY |
990 EXTENT_SET_WRITEBACK |
991 EXTENT_END_WRITEBACK);
992
993 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500994}
Chris Masonc8b97812008-10-29 14:49:59 -0400995
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000996static noinline int cow_file_range(struct inode *inode,
997 struct page *locked_page,
998 u64 start, u64 end, int *page_started,
999 unsigned long *nr_written,
1000 int unlock)
1001{
1002 struct btrfs_trans_handle *trans;
1003 struct btrfs_root *root = BTRFS_I(inode)->root;
1004 int ret;
1005
1006 trans = btrfs_join_transaction(root);
1007 if (IS_ERR(trans)) {
1008 extent_clear_unlock_delalloc(inode,
1009 &BTRFS_I(inode)->io_tree,
1010 start, end, locked_page,
1011 EXTENT_CLEAR_UNLOCK_PAGE |
1012 EXTENT_CLEAR_UNLOCK |
1013 EXTENT_CLEAR_DELALLOC |
1014 EXTENT_CLEAR_DIRTY |
1015 EXTENT_SET_WRITEBACK |
1016 EXTENT_END_WRITEBACK);
1017 return PTR_ERR(trans);
1018 }
1019 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1020
1021 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1022 page_started, nr_written, unlock);
1023
1024 btrfs_end_transaction(trans, root);
1025
1026 return ret;
1027}
1028
Chris Mason771ed682008-11-06 22:02:51 -05001029/*
1030 * work queue call back to started compression on a file and pages
1031 */
1032static noinline void async_cow_start(struct btrfs_work *work)
1033{
1034 struct async_cow *async_cow;
1035 int num_added = 0;
1036 async_cow = container_of(work, struct async_cow, work);
1037
1038 compress_file_range(async_cow->inode, async_cow->locked_page,
1039 async_cow->start, async_cow->end, async_cow,
1040 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001041 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001042 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001043 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001044 }
Chris Mason771ed682008-11-06 22:02:51 -05001045}
1046
1047/*
1048 * work queue call back to submit previously compressed pages
1049 */
1050static noinline void async_cow_submit(struct btrfs_work *work)
1051{
1052 struct async_cow *async_cow;
1053 struct btrfs_root *root;
1054 unsigned long nr_pages;
1055
1056 async_cow = container_of(work, struct async_cow, work);
1057
1058 root = async_cow->root;
1059 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1060 PAGE_CACHE_SHIFT;
1061
Josef Bacik66657b32012-08-01 15:36:24 -04001062 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001063 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001064 waitqueue_active(&root->fs_info->async_submit_wait))
1065 wake_up(&root->fs_info->async_submit_wait);
1066
Chris Masond3977122009-01-05 21:25:51 -05001067 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001068 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001069}
Chris Masonc8b97812008-10-29 14:49:59 -04001070
Chris Mason771ed682008-11-06 22:02:51 -05001071static noinline void async_cow_free(struct btrfs_work *work)
1072{
1073 struct async_cow *async_cow;
1074 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001075 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001076 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001077 kfree(async_cow);
1078}
1079
1080static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1081 u64 start, u64 end, int *page_started,
1082 unsigned long *nr_written)
1083{
1084 struct async_cow *async_cow;
1085 struct btrfs_root *root = BTRFS_I(inode)->root;
1086 unsigned long nr_pages;
1087 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001088 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001089
Chris Masona3429ab2009-10-08 12:30:20 -04001090 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1091 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001092 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001093 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001094 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001095 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001096 async_cow->root = root;
1097 async_cow->locked_page = locked_page;
1098 async_cow->start = start;
1099
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001100 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001101 cur_end = end;
1102 else
1103 cur_end = min(end, start + 512 * 1024 - 1);
1104
1105 async_cow->end = cur_end;
1106 INIT_LIST_HEAD(&async_cow->extents);
1107
1108 async_cow->work.func = async_cow_start;
1109 async_cow->work.ordered_func = async_cow_submit;
1110 async_cow->work.ordered_free = async_cow_free;
1111 async_cow->work.flags = 0;
1112
Chris Mason771ed682008-11-06 22:02:51 -05001113 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1114 PAGE_CACHE_SHIFT;
1115 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1116
1117 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1118 &async_cow->work);
1119
1120 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1121 wait_event(root->fs_info->async_submit_wait,
1122 (atomic_read(&root->fs_info->async_delalloc_pages) <
1123 limit));
1124 }
1125
Chris Masond3977122009-01-05 21:25:51 -05001126 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001127 atomic_read(&root->fs_info->async_delalloc_pages)) {
1128 wait_event(root->fs_info->async_submit_wait,
1129 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1130 0));
1131 }
1132
1133 *nr_written += nr_pages;
1134 start = cur_end + 1;
1135 }
1136 *page_started = 1;
1137 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001138}
1139
Chris Masond3977122009-01-05 21:25:51 -05001140static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001141 u64 bytenr, u64 num_bytes)
1142{
1143 int ret;
1144 struct btrfs_ordered_sum *sums;
1145 LIST_HEAD(list);
1146
Yan Zheng07d400a2009-01-06 11:42:00 -05001147 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001148 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001149 if (ret == 0 && list_empty(&list))
1150 return 0;
1151
1152 while (!list_empty(&list)) {
1153 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1154 list_del(&sums->list);
1155 kfree(sums);
1156 }
1157 return 1;
1158}
1159
Chris Masond352ac62008-09-29 15:18:18 -04001160/*
1161 * when nowcow writeback call back. This checks for snapshots or COW copies
1162 * of the extents that exist in the file, and COWs the file as required.
1163 *
1164 * If no cow copies or snapshots exist, we write directly to the existing
1165 * blocks on disk
1166 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001167static noinline int run_delalloc_nocow(struct inode *inode,
1168 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001169 u64 start, u64 end, int *page_started, int force,
1170 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001171{
Chris Masonbe20aa92007-12-17 20:14:01 -05001172 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001173 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001174 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001175 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001176 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001177 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001178 u64 cow_start;
1179 u64 cur_offset;
1180 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001181 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001182 u64 disk_bytenr;
1183 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001184 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001185 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001186 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001187 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001188 int nocow;
1189 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001190 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001191 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001192
1193 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001194 if (!path) {
1195 extent_clear_unlock_delalloc(inode,
1196 &BTRFS_I(inode)->io_tree,
1197 start, end, locked_page,
1198 EXTENT_CLEAR_UNLOCK_PAGE |
1199 EXTENT_CLEAR_UNLOCK |
1200 EXTENT_CLEAR_DELALLOC |
1201 EXTENT_CLEAR_DIRTY |
1202 EXTENT_SET_WRITEBACK |
1203 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001204 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001205 }
Li Zefan82d59022011-04-20 10:33:24 +08001206
Liu Bo83eea1f2012-07-10 05:28:39 -06001207 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001208
1209 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001210 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001211 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001212 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001213
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001214 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001215 extent_clear_unlock_delalloc(inode,
1216 &BTRFS_I(inode)->io_tree,
1217 start, end, locked_page,
1218 EXTENT_CLEAR_UNLOCK_PAGE |
1219 EXTENT_CLEAR_UNLOCK |
1220 EXTENT_CLEAR_DELALLOC |
1221 EXTENT_CLEAR_DIRTY |
1222 EXTENT_SET_WRITEBACK |
1223 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001224 btrfs_free_path(path);
1225 return PTR_ERR(trans);
1226 }
1227
Josef Bacik74b21072011-04-13 12:02:53 -04001228 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001229
Yan Zheng80ff3852008-10-30 14:20:02 -04001230 cow_start = (u64)-1;
1231 cur_offset = start;
1232 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001233 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001234 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001235 if (ret < 0) {
1236 btrfs_abort_transaction(trans, root, ret);
1237 goto error;
1238 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001239 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1240 leaf = path->nodes[0];
1241 btrfs_item_key_to_cpu(leaf, &found_key,
1242 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001243 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 found_key.type == BTRFS_EXTENT_DATA_KEY)
1245 path->slots[0]--;
1246 }
1247 check_prev = 0;
1248next_slot:
1249 leaf = path->nodes[0];
1250 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1251 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001252 if (ret < 0) {
1253 btrfs_abort_transaction(trans, root, ret);
1254 goto error;
1255 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001256 if (ret > 0)
1257 break;
1258 leaf = path->nodes[0];
1259 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001260
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 nocow = 0;
1262 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001263 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001264 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001265
Li Zefan33345d012011-04-20 10:31:50 +08001266 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1268 found_key.offset > end)
1269 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001270
Yan Zheng80ff3852008-10-30 14:20:02 -04001271 if (found_key.offset > cur_offset) {
1272 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001273 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 goto out_check;
1275 }
Chris Masonc31f8832008-01-08 15:46:31 -05001276
Yan Zheng80ff3852008-10-30 14:20:02 -04001277 fi = btrfs_item_ptr(leaf, path->slots[0],
1278 struct btrfs_file_extent_item);
1279 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001280
Yan Zhengd899e052008-10-30 14:25:28 -04001281 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1282 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001284 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 extent_end = found_key.offset +
1286 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001287 disk_num_bytes =
1288 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 if (extent_end <= start) {
1290 path->slots[0]++;
1291 goto next_slot;
1292 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001293 if (disk_bytenr == 0)
1294 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 if (btrfs_file_extent_compression(leaf, fi) ||
1296 btrfs_file_extent_encryption(leaf, fi) ||
1297 btrfs_file_extent_other_encoding(leaf, fi))
1298 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001299 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1300 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001301 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001303 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001304 found_key.offset -
1305 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001306 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001307 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001308 disk_bytenr += cur_offset - found_key.offset;
1309 num_bytes = min(end + 1, extent_end) - cur_offset;
1310 /*
1311 * force cow if csum exists in the range.
1312 * this ensure that csum for a given extent are
1313 * either valid or do not exist.
1314 */
1315 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1316 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 nocow = 1;
1318 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1319 extent_end = found_key.offset +
1320 btrfs_file_extent_inline_len(leaf, fi);
1321 extent_end = ALIGN(extent_end, root->sectorsize);
1322 } else {
1323 BUG_ON(1);
1324 }
1325out_check:
1326 if (extent_end <= start) {
1327 path->slots[0]++;
1328 goto next_slot;
1329 }
1330 if (!nocow) {
1331 if (cow_start == (u64)-1)
1332 cow_start = cur_offset;
1333 cur_offset = extent_end;
1334 if (cur_offset > end)
1335 break;
1336 path->slots[0]++;
1337 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001338 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001339
David Sterbab3b4aa72011-04-21 01:20:15 +02001340 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001342 ret = __cow_file_range(trans, inode, root, locked_page,
1343 cow_start, found_key.offset - 1,
1344 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001345 if (ret) {
1346 btrfs_abort_transaction(trans, root, ret);
1347 goto error;
1348 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001349 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001350 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001351
Yan Zhengd899e052008-10-30 14:25:28 -04001352 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1353 struct extent_map *em;
1354 struct extent_map_tree *em_tree;
1355 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001356 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001357 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001358 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001359 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001360 em->len = num_bytes;
1361 em->block_len = num_bytes;
1362 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001363 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001364 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001365 em->mod_start = em->start;
1366 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001367 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001368 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001369 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001370 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001371 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001372 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001373 if (!ret)
1374 list_move(&em->list,
1375 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001376 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001377 if (ret != -EEXIST) {
1378 free_extent_map(em);
1379 break;
1380 }
1381 btrfs_drop_extent_cache(inode, em->start,
1382 em->start + em->len - 1, 0);
1383 }
1384 type = BTRFS_ORDERED_PREALLOC;
1385 } else {
1386 type = BTRFS_ORDERED_NOCOW;
1387 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001388
1389 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001390 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001391 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001392
Yan, Zhengefa56462010-05-16 10:49:59 -04001393 if (root->root_key.objectid ==
1394 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1395 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1396 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001397 if (ret) {
1398 btrfs_abort_transaction(trans, root, ret);
1399 goto error;
1400 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001401 }
1402
Yan Zhengd899e052008-10-30 14:25:28 -04001403 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001404 cur_offset, cur_offset + num_bytes - 1,
1405 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1406 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1407 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 cur_offset = extent_end;
1409 if (cur_offset > end)
1410 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001411 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001412 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001413
Josef Bacik17ca04a2012-05-31 15:58:55 -04001414 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001415 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001416 cur_offset = end;
1417 }
1418
Yan Zheng80ff3852008-10-30 14:20:02 -04001419 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001420 ret = __cow_file_range(trans, inode, root, locked_page,
1421 cow_start, end,
1422 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001423 if (ret) {
1424 btrfs_abort_transaction(trans, root, ret);
1425 goto error;
1426 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001427 }
1428
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001429error:
Miao Xiea698d0752012-09-20 01:51:59 -06001430 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001431 if (!ret)
1432 ret = err;
1433
Josef Bacik17ca04a2012-05-31 15:58:55 -04001434 if (ret && cur_offset < end)
1435 extent_clear_unlock_delalloc(inode,
1436 &BTRFS_I(inode)->io_tree,
1437 cur_offset, end, locked_page,
1438 EXTENT_CLEAR_UNLOCK_PAGE |
1439 EXTENT_CLEAR_UNLOCK |
1440 EXTENT_CLEAR_DELALLOC |
1441 EXTENT_CLEAR_DIRTY |
1442 EXTENT_SET_WRITEBACK |
1443 EXTENT_END_WRITEBACK);
1444
Yan Zheng7ea394f2008-08-05 13:05:02 -04001445 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001446 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001447}
1448
Chris Masond352ac62008-09-29 15:18:18 -04001449/*
1450 * extent_io.c call back to do delayed allocation processing
1451 */
Chris Masonc8b97812008-10-29 14:49:59 -04001452static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001453 u64 start, u64 end, int *page_started,
1454 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001455{
Chris Masonbe20aa92007-12-17 20:14:01 -05001456 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001457 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001458
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001459 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001460 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001461 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001462 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001463 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001464 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001465 } else if (!btrfs_test_opt(root, COMPRESS) &&
1466 !(BTRFS_I(inode)->force_compress) &&
1467 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001468 ret = cow_file_range(inode, locked_page, start, end,
1469 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001470 } else {
1471 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1472 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001473 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001474 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001475 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001476 return ret;
1477}
1478
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001479static void btrfs_split_extent_hook(struct inode *inode,
1480 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001481{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001482 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001483 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001484 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001485
Josef Bacik9e0baf62011-07-15 15:16:44 +00001486 spin_lock(&BTRFS_I(inode)->lock);
1487 BTRFS_I(inode)->outstanding_extents++;
1488 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001489}
1490
1491/*
1492 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1493 * extents so we can keep track of new extents that are just merged onto old
1494 * extents, such as when we are doing sequential writes, so we can properly
1495 * account for the metadata space we'll need.
1496 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001497static void btrfs_merge_extent_hook(struct inode *inode,
1498 struct extent_state *new,
1499 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001500{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001501 /* not delalloc, ignore it */
1502 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001503 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001504
Josef Bacik9e0baf62011-07-15 15:16:44 +00001505 spin_lock(&BTRFS_I(inode)->lock);
1506 BTRFS_I(inode)->outstanding_extents--;
1507 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001508}
1509
Chris Masond352ac62008-09-29 15:18:18 -04001510/*
1511 * extent_io.c set_bit_hook, used to track delayed allocation
1512 * bytes in this file, and to maintain the list of inodes that
1513 * have pending delalloc work to be done.
1514 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001515static void btrfs_set_bit_hook(struct inode *inode,
1516 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001517{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001518
Chris Mason75eff682008-12-15 15:54:40 -05001519 /*
1520 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001521 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001522 * bit, which is only set or cleared with irqs on
1523 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001524 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001525 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001526 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001527 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001528
Josef Bacik9e0baf62011-07-15 15:16:44 +00001529 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001530 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001531 } else {
1532 spin_lock(&BTRFS_I(inode)->lock);
1533 BTRFS_I(inode)->outstanding_extents++;
1534 spin_unlock(&BTRFS_I(inode)->lock);
1535 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001536
Miao Xie963d6782013-01-29 10:10:51 +00001537 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1538 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001539 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001540 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001541 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1542 &BTRFS_I(inode)->runtime_flags)) {
1543 spin_lock(&root->fs_info->delalloc_lock);
1544 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1545 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1546 &root->fs_info->delalloc_inodes);
1547 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1548 &BTRFS_I(inode)->runtime_flags);
1549 }
1550 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001551 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001552 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001553 }
Chris Mason291d6732008-01-29 15:55:23 -05001554}
1555
Chris Masond352ac62008-09-29 15:18:18 -04001556/*
1557 * extent_io.c clear_bit_hook, see set_bit_hook for why
1558 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001559static void btrfs_clear_bit_hook(struct inode *inode,
1560 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001561{
Chris Mason75eff682008-12-15 15:54:40 -05001562 /*
1563 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001564 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001565 * bit, which is only set or cleared with irqs on
1566 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001567 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001568 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001569 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001570 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001571
Josef Bacik9e0baf62011-07-15 15:16:44 +00001572 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001573 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001574 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1575 spin_lock(&BTRFS_I(inode)->lock);
1576 BTRFS_I(inode)->outstanding_extents--;
1577 spin_unlock(&BTRFS_I(inode)->lock);
1578 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001579
1580 if (*bits & EXTENT_DO_ACCOUNTING)
1581 btrfs_delalloc_release_metadata(inode, len);
1582
Josef Bacik0cb59c92010-07-02 12:14:14 -04001583 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1584 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001585 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001586
Miao Xie963d6782013-01-29 10:10:51 +00001587 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1588 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001589 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001590 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001591 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001592 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1593 &BTRFS_I(inode)->runtime_flags)) {
1594 spin_lock(&root->fs_info->delalloc_lock);
1595 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1596 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1597 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1598 &BTRFS_I(inode)->runtime_flags);
1599 }
1600 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001601 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001602 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001603 }
Chris Mason291d6732008-01-29 15:55:23 -05001604}
1605
Chris Masond352ac62008-09-29 15:18:18 -04001606/*
1607 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1608 * we don't create bios that span stripes or chunks
1609 */
David Woodhouse64a16702009-07-15 23:29:37 +01001610int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001611 size_t size, struct bio *bio,
1612 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001613{
1614 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001615 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001616 u64 length = 0;
1617 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001618 int ret;
1619
Chris Mason771ed682008-11-06 22:02:51 -05001620 if (bio_flags & EXTENT_BIO_COMPRESSED)
1621 return 0;
1622
Chris Masonf2d8d742008-04-21 10:03:05 -04001623 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001624 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001625 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001626 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001627 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001628 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001629 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001630 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001631 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001632}
1633
Chris Masond352ac62008-09-29 15:18:18 -04001634/*
1635 * in order to insert checksums into the metadata in large chunks,
1636 * we wait until bio submission time. All the pages in the bio are
1637 * checksummed and sums are attached onto the ordered extent record.
1638 *
1639 * At IO completion time the cums attached on the ordered extent record
1640 * are inserted into the btree
1641 */
Chris Masond3977122009-01-05 21:25:51 -05001642static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1643 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001644 unsigned long bio_flags,
1645 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001646{
Chris Mason065631f2008-02-20 12:07:25 -05001647 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001648 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001649
Chris Masond20f7042008-12-08 16:58:54 -05001650 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001651 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001652 return 0;
1653}
Chris Masone0156402008-04-16 11:15:20 -04001654
Chris Mason4a69a412008-11-06 22:03:00 -05001655/*
1656 * in order to insert checksums into the metadata in large chunks,
1657 * we wait until bio submission time. All the pages in the bio are
1658 * checksummed and sums are attached onto the ordered extent record.
1659 *
1660 * At IO completion time the cums attached on the ordered extent record
1661 * are inserted into the btree
1662 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001663static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001664 int mirror_num, unsigned long bio_flags,
1665 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001666{
1667 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001668 int ret;
1669
1670 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1671 if (ret)
1672 bio_endio(bio, ret);
1673 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001674}
1675
Chris Masond352ac62008-09-29 15:18:18 -04001676/*
Chris Masoncad321a2008-12-17 14:51:42 -05001677 * extent_io.c submission hook. This does the right thing for csum calculation
1678 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001679 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001680static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001681 int mirror_num, unsigned long bio_flags,
1682 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001683{
1684 struct btrfs_root *root = BTRFS_I(inode)->root;
1685 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001686 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001687 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001688 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001689
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001690 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001691
Liu Bo83eea1f2012-07-10 05:28:39 -06001692 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001693 metadata = 2;
1694
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001695 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001696 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1697 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001698 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001699
Chris Masond20f7042008-12-08 16:58:54 -05001700 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001701 ret = btrfs_submit_compressed_read(inode, bio,
1702 mirror_num,
1703 bio_flags);
1704 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001705 } else if (!skip_sum) {
1706 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1707 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001708 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001709 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001710 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001711 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001712 /* csum items have already been cloned */
1713 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1714 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001715 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001716 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001717 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001718 bio_flags, bio_offset,
1719 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001720 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001721 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001722 } else if (!skip_sum) {
1723 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1724 if (ret)
1725 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001726 }
1727
Chris Mason0b86a832008-03-24 15:01:56 -04001728mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001729 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1730
1731out:
1732 if (ret < 0)
1733 bio_endio(bio, ret);
1734 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001735}
Chris Mason6885f302008-02-20 16:11:05 -05001736
Chris Masond352ac62008-09-29 15:18:18 -04001737/*
1738 * given a list of ordered sums record them in the inode. This happens
1739 * at IO completion time based on sums calculated at bio submission time.
1740 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001741static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001742 struct inode *inode, u64 file_offset,
1743 struct list_head *list)
1744{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001745 struct btrfs_ordered_sum *sum;
1746
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001747 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001748 btrfs_csum_file_blocks(trans,
1749 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001750 }
1751 return 0;
1752}
1753
Josef Bacik2ac55d42010-02-03 19:33:23 +00001754int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1755 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001756{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001757 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001758 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001759 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001760}
1761
Chris Masond352ac62008-09-29 15:18:18 -04001762/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001763struct btrfs_writepage_fixup {
1764 struct page *page;
1765 struct btrfs_work work;
1766};
1767
Christoph Hellwigb2950862008-12-02 09:54:17 -05001768static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001769{
1770 struct btrfs_writepage_fixup *fixup;
1771 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001772 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001773 struct page *page;
1774 struct inode *inode;
1775 u64 page_start;
1776 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001777 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001778
1779 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1780 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001781again:
Chris Mason247e7432008-07-17 12:53:51 -04001782 lock_page(page);
1783 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1784 ClearPageChecked(page);
1785 goto out_page;
1786 }
1787
1788 inode = page->mapping->host;
1789 page_start = page_offset(page);
1790 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1791
Josef Bacik2ac55d42010-02-03 19:33:23 +00001792 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001793 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001794
1795 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001796 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001797 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001798
1799 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1800 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001801 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1802 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001803 unlock_page(page);
1804 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001805 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001806 goto again;
1807 }
Chris Mason247e7432008-07-17 12:53:51 -04001808
Jeff Mahoney87826df2012-02-15 16:23:57 +01001809 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1810 if (ret) {
1811 mapping_set_error(page->mapping, ret);
1812 end_extent_writepage(page, ret, page_start, page_end);
1813 ClearPageChecked(page);
1814 goto out;
1815 }
1816
Josef Bacik2ac55d42010-02-03 19:33:23 +00001817 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001818 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001819 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001820out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001821 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1822 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001823out_page:
1824 unlock_page(page);
1825 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001826 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001827}
1828
1829/*
1830 * There are a few paths in the higher layers of the kernel that directly
1831 * set the page dirty bit without asking the filesystem if it is a
1832 * good idea. This causes problems because we want to make sure COW
1833 * properly happens and the data=ordered rules are followed.
1834 *
Chris Masonc8b97812008-10-29 14:49:59 -04001835 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001836 * hasn't been properly setup for IO. We kick off an async process
1837 * to fix it up. The async helper will wait for ordered extents, set
1838 * the delalloc bit and make it safe to write the page.
1839 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001840static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001841{
1842 struct inode *inode = page->mapping->host;
1843 struct btrfs_writepage_fixup *fixup;
1844 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001845
Chris Mason8b62b722009-09-02 16:53:46 -04001846 /* this page is properly in the ordered list */
1847 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001848 return 0;
1849
1850 if (PageChecked(page))
1851 return -EAGAIN;
1852
1853 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1854 if (!fixup)
1855 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001856
Chris Mason247e7432008-07-17 12:53:51 -04001857 SetPageChecked(page);
1858 page_cache_get(page);
1859 fixup->work.func = btrfs_writepage_fixup_worker;
1860 fixup->page = page;
1861 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001862 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001863}
1864
Yan Zhengd899e052008-10-30 14:25:28 -04001865static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1866 struct inode *inode, u64 file_pos,
1867 u64 disk_bytenr, u64 disk_num_bytes,
1868 u64 num_bytes, u64 ram_bytes,
1869 u8 compression, u8 encryption,
1870 u16 other_encoding, int extent_type)
1871{
1872 struct btrfs_root *root = BTRFS_I(inode)->root;
1873 struct btrfs_file_extent_item *fi;
1874 struct btrfs_path *path;
1875 struct extent_buffer *leaf;
1876 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001877 int ret;
1878
1879 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001880 if (!path)
1881 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001882
Chris Masonb9473432009-03-13 11:00:37 -04001883 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001884
1885 /*
1886 * we may be replacing one extent in the tree with another.
1887 * The new extent is pinned in the extent map, and we don't want
1888 * to drop it from the cache until it is completely in the btree.
1889 *
1890 * So, tell btrfs_drop_extents to leave this extent in the cache.
1891 * the caller is expected to unpin it and allow it to be merged
1892 * with the others.
1893 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001894 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001895 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001896 if (ret)
1897 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001898
Li Zefan33345d012011-04-20 10:31:50 +08001899 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001900 ins.offset = file_pos;
1901 ins.type = BTRFS_EXTENT_DATA_KEY;
1902 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001903 if (ret)
1904 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001905 leaf = path->nodes[0];
1906 fi = btrfs_item_ptr(leaf, path->slots[0],
1907 struct btrfs_file_extent_item);
1908 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1909 btrfs_set_file_extent_type(leaf, fi, extent_type);
1910 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1911 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1912 btrfs_set_file_extent_offset(leaf, fi, 0);
1913 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1914 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1915 btrfs_set_file_extent_compression(leaf, fi, compression);
1916 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1917 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001918
Yan Zhengd899e052008-10-30 14:25:28 -04001919 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001920 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001921
1922 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001923
1924 ins.objectid = disk_bytenr;
1925 ins.offset = disk_num_bytes;
1926 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001927 ret = btrfs_alloc_reserved_file_extent(trans, root,
1928 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001929 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001930out:
Yan Zhengd899e052008-10-30 14:25:28 -04001931 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001932
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001933 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001934}
1935
Liu Bo38c227d2013-01-29 03:18:40 +00001936/* snapshot-aware defrag */
1937struct sa_defrag_extent_backref {
1938 struct rb_node node;
1939 struct old_sa_defrag_extent *old;
1940 u64 root_id;
1941 u64 inum;
1942 u64 file_pos;
1943 u64 extent_offset;
1944 u64 num_bytes;
1945 u64 generation;
1946};
1947
1948struct old_sa_defrag_extent {
1949 struct list_head list;
1950 struct new_sa_defrag_extent *new;
1951
1952 u64 extent_offset;
1953 u64 bytenr;
1954 u64 offset;
1955 u64 len;
1956 int count;
1957};
1958
1959struct new_sa_defrag_extent {
1960 struct rb_root root;
1961 struct list_head head;
1962 struct btrfs_path *path;
1963 struct inode *inode;
1964 u64 file_pos;
1965 u64 len;
1966 u64 bytenr;
1967 u64 disk_len;
1968 u8 compress_type;
1969};
1970
1971static int backref_comp(struct sa_defrag_extent_backref *b1,
1972 struct sa_defrag_extent_backref *b2)
1973{
1974 if (b1->root_id < b2->root_id)
1975 return -1;
1976 else if (b1->root_id > b2->root_id)
1977 return 1;
1978
1979 if (b1->inum < b2->inum)
1980 return -1;
1981 else if (b1->inum > b2->inum)
1982 return 1;
1983
1984 if (b1->file_pos < b2->file_pos)
1985 return -1;
1986 else if (b1->file_pos > b2->file_pos)
1987 return 1;
1988
1989 /*
1990 * [------------------------------] ===> (a range of space)
1991 * |<--->| |<---->| =============> (fs/file tree A)
1992 * |<---------------------------->| ===> (fs/file tree B)
1993 *
1994 * A range of space can refer to two file extents in one tree while
1995 * refer to only one file extent in another tree.
1996 *
1997 * So we may process a disk offset more than one time(two extents in A)
1998 * and locate at the same extent(one extent in B), then insert two same
1999 * backrefs(both refer to the extent in B).
2000 */
2001 return 0;
2002}
2003
2004static void backref_insert(struct rb_root *root,
2005 struct sa_defrag_extent_backref *backref)
2006{
2007 struct rb_node **p = &root->rb_node;
2008 struct rb_node *parent = NULL;
2009 struct sa_defrag_extent_backref *entry;
2010 int ret;
2011
2012 while (*p) {
2013 parent = *p;
2014 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2015
2016 ret = backref_comp(backref, entry);
2017 if (ret < 0)
2018 p = &(*p)->rb_left;
2019 else
2020 p = &(*p)->rb_right;
2021 }
2022
2023 rb_link_node(&backref->node, parent, p);
2024 rb_insert_color(&backref->node, root);
2025}
2026
2027/*
2028 * Note the backref might has changed, and in this case we just return 0.
2029 */
2030static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2031 void *ctx)
2032{
2033 struct btrfs_file_extent_item *extent;
2034 struct btrfs_fs_info *fs_info;
2035 struct old_sa_defrag_extent *old = ctx;
2036 struct new_sa_defrag_extent *new = old->new;
2037 struct btrfs_path *path = new->path;
2038 struct btrfs_key key;
2039 struct btrfs_root *root;
2040 struct sa_defrag_extent_backref *backref;
2041 struct extent_buffer *leaf;
2042 struct inode *inode = new->inode;
2043 int slot;
2044 int ret;
2045 u64 extent_offset;
2046 u64 num_bytes;
2047
2048 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2049 inum == btrfs_ino(inode))
2050 return 0;
2051
2052 key.objectid = root_id;
2053 key.type = BTRFS_ROOT_ITEM_KEY;
2054 key.offset = (u64)-1;
2055
2056 fs_info = BTRFS_I(inode)->root->fs_info;
2057 root = btrfs_read_fs_root_no_name(fs_info, &key);
2058 if (IS_ERR(root)) {
2059 if (PTR_ERR(root) == -ENOENT)
2060 return 0;
2061 WARN_ON(1);
2062 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2063 inum, offset, root_id);
2064 return PTR_ERR(root);
2065 }
2066
2067 key.objectid = inum;
2068 key.type = BTRFS_EXTENT_DATA_KEY;
2069 if (offset > (u64)-1 << 32)
2070 key.offset = 0;
2071 else
2072 key.offset = offset;
2073
2074 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2075 if (ret < 0) {
2076 WARN_ON(1);
2077 return ret;
2078 }
2079
2080 while (1) {
2081 cond_resched();
2082
2083 leaf = path->nodes[0];
2084 slot = path->slots[0];
2085
2086 if (slot >= btrfs_header_nritems(leaf)) {
2087 ret = btrfs_next_leaf(root, path);
2088 if (ret < 0) {
2089 goto out;
2090 } else if (ret > 0) {
2091 ret = 0;
2092 goto out;
2093 }
2094 continue;
2095 }
2096
2097 path->slots[0]++;
2098
2099 btrfs_item_key_to_cpu(leaf, &key, slot);
2100
2101 if (key.objectid > inum)
2102 goto out;
2103
2104 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2105 continue;
2106
2107 extent = btrfs_item_ptr(leaf, slot,
2108 struct btrfs_file_extent_item);
2109
2110 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2111 continue;
2112
2113 extent_offset = btrfs_file_extent_offset(leaf, extent);
2114 if (key.offset - extent_offset != offset)
2115 continue;
2116
2117 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2118 if (extent_offset >= old->extent_offset + old->offset +
2119 old->len || extent_offset + num_bytes <=
2120 old->extent_offset + old->offset)
2121 continue;
2122
2123 break;
2124 }
2125
2126 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2127 if (!backref) {
2128 ret = -ENOENT;
2129 goto out;
2130 }
2131
2132 backref->root_id = root_id;
2133 backref->inum = inum;
2134 backref->file_pos = offset + extent_offset;
2135 backref->num_bytes = num_bytes;
2136 backref->extent_offset = extent_offset;
2137 backref->generation = btrfs_file_extent_generation(leaf, extent);
2138 backref->old = old;
2139 backref_insert(&new->root, backref);
2140 old->count++;
2141out:
2142 btrfs_release_path(path);
2143 WARN_ON(ret);
2144 return ret;
2145}
2146
2147static noinline bool record_extent_backrefs(struct btrfs_path *path,
2148 struct new_sa_defrag_extent *new)
2149{
2150 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2151 struct old_sa_defrag_extent *old, *tmp;
2152 int ret;
2153
2154 new->path = path;
2155
2156 list_for_each_entry_safe(old, tmp, &new->head, list) {
2157 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2158 path, record_one_backref,
2159 old);
2160 BUG_ON(ret < 0 && ret != -ENOENT);
2161
2162 /* no backref to be processed for this extent */
2163 if (!old->count) {
2164 list_del(&old->list);
2165 kfree(old);
2166 }
2167 }
2168
2169 if (list_empty(&new->head))
2170 return false;
2171
2172 return true;
2173}
2174
2175static int relink_is_mergable(struct extent_buffer *leaf,
2176 struct btrfs_file_extent_item *fi,
2177 u64 disk_bytenr)
2178{
2179 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2180 return 0;
2181
2182 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2183 return 0;
2184
2185 if (btrfs_file_extent_compression(leaf, fi) ||
2186 btrfs_file_extent_encryption(leaf, fi) ||
2187 btrfs_file_extent_other_encoding(leaf, fi))
2188 return 0;
2189
2190 return 1;
2191}
2192
2193/*
2194 * Note the backref might has changed, and in this case we just return 0.
2195 */
2196static noinline int relink_extent_backref(struct btrfs_path *path,
2197 struct sa_defrag_extent_backref *prev,
2198 struct sa_defrag_extent_backref *backref)
2199{
2200 struct btrfs_file_extent_item *extent;
2201 struct btrfs_file_extent_item *item;
2202 struct btrfs_ordered_extent *ordered;
2203 struct btrfs_trans_handle *trans;
2204 struct btrfs_fs_info *fs_info;
2205 struct btrfs_root *root;
2206 struct btrfs_key key;
2207 struct extent_buffer *leaf;
2208 struct old_sa_defrag_extent *old = backref->old;
2209 struct new_sa_defrag_extent *new = old->new;
2210 struct inode *src_inode = new->inode;
2211 struct inode *inode;
2212 struct extent_state *cached = NULL;
2213 int ret = 0;
2214 u64 start;
2215 u64 len;
2216 u64 lock_start;
2217 u64 lock_end;
2218 bool merge = false;
2219 int index;
2220
2221 if (prev && prev->root_id == backref->root_id &&
2222 prev->inum == backref->inum &&
2223 prev->file_pos + prev->num_bytes == backref->file_pos)
2224 merge = true;
2225
2226 /* step 1: get root */
2227 key.objectid = backref->root_id;
2228 key.type = BTRFS_ROOT_ITEM_KEY;
2229 key.offset = (u64)-1;
2230
2231 fs_info = BTRFS_I(src_inode)->root->fs_info;
2232 index = srcu_read_lock(&fs_info->subvol_srcu);
2233
2234 root = btrfs_read_fs_root_no_name(fs_info, &key);
2235 if (IS_ERR(root)) {
2236 srcu_read_unlock(&fs_info->subvol_srcu, index);
2237 if (PTR_ERR(root) == -ENOENT)
2238 return 0;
2239 return PTR_ERR(root);
2240 }
2241 if (btrfs_root_refs(&root->root_item) == 0) {
2242 srcu_read_unlock(&fs_info->subvol_srcu, index);
2243 /* parse ENOENT to 0 */
2244 return 0;
2245 }
2246
2247 /* step 2: get inode */
2248 key.objectid = backref->inum;
2249 key.type = BTRFS_INODE_ITEM_KEY;
2250 key.offset = 0;
2251
2252 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2253 if (IS_ERR(inode)) {
2254 srcu_read_unlock(&fs_info->subvol_srcu, index);
2255 return 0;
2256 }
2257
2258 srcu_read_unlock(&fs_info->subvol_srcu, index);
2259
2260 /* step 3: relink backref */
2261 lock_start = backref->file_pos;
2262 lock_end = backref->file_pos + backref->num_bytes - 1;
2263 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2264 0, &cached);
2265
2266 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2267 if (ordered) {
2268 btrfs_put_ordered_extent(ordered);
2269 goto out_unlock;
2270 }
2271
2272 trans = btrfs_join_transaction(root);
2273 if (IS_ERR(trans)) {
2274 ret = PTR_ERR(trans);
2275 goto out_unlock;
2276 }
2277
2278 key.objectid = backref->inum;
2279 key.type = BTRFS_EXTENT_DATA_KEY;
2280 key.offset = backref->file_pos;
2281
2282 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2283 if (ret < 0) {
2284 goto out_free_path;
2285 } else if (ret > 0) {
2286 ret = 0;
2287 goto out_free_path;
2288 }
2289
2290 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2291 struct btrfs_file_extent_item);
2292
2293 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2294 backref->generation)
2295 goto out_free_path;
2296
2297 btrfs_release_path(path);
2298
2299 start = backref->file_pos;
2300 if (backref->extent_offset < old->extent_offset + old->offset)
2301 start += old->extent_offset + old->offset -
2302 backref->extent_offset;
2303
2304 len = min(backref->extent_offset + backref->num_bytes,
2305 old->extent_offset + old->offset + old->len);
2306 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2307
2308 ret = btrfs_drop_extents(trans, root, inode, start,
2309 start + len, 1);
2310 if (ret)
2311 goto out_free_path;
2312again:
2313 key.objectid = btrfs_ino(inode);
2314 key.type = BTRFS_EXTENT_DATA_KEY;
2315 key.offset = start;
2316
2317 if (merge) {
2318 struct btrfs_file_extent_item *fi;
2319 u64 extent_len;
2320 struct btrfs_key found_key;
2321
2322 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2323 if (ret < 0)
2324 goto out_free_path;
2325
2326 path->slots[0]--;
2327 leaf = path->nodes[0];
2328 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2329
2330 fi = btrfs_item_ptr(leaf, path->slots[0],
2331 struct btrfs_file_extent_item);
2332 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2333
2334 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2335 extent_len + found_key.offset == start) {
2336 btrfs_set_file_extent_num_bytes(leaf, fi,
2337 extent_len + len);
2338 btrfs_mark_buffer_dirty(leaf);
2339 inode_add_bytes(inode, len);
2340
2341 ret = 1;
2342 goto out_free_path;
2343 } else {
2344 merge = false;
2345 btrfs_release_path(path);
2346 goto again;
2347 }
2348 }
2349
2350 ret = btrfs_insert_empty_item(trans, root, path, &key,
2351 sizeof(*extent));
2352 if (ret) {
2353 btrfs_abort_transaction(trans, root, ret);
2354 goto out_free_path;
2355 }
2356
2357 leaf = path->nodes[0];
2358 item = btrfs_item_ptr(leaf, path->slots[0],
2359 struct btrfs_file_extent_item);
2360 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2361 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2362 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2363 btrfs_set_file_extent_num_bytes(leaf, item, len);
2364 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2365 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2366 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2367 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2368 btrfs_set_file_extent_encryption(leaf, item, 0);
2369 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2370
2371 btrfs_mark_buffer_dirty(leaf);
2372 inode_add_bytes(inode, len);
2373
2374 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2375 new->disk_len, 0,
2376 backref->root_id, backref->inum,
2377 new->file_pos, 0); /* start - extent_offset */
2378 if (ret) {
2379 btrfs_abort_transaction(trans, root, ret);
2380 goto out_free_path;
2381 }
2382
2383 ret = 1;
2384out_free_path:
2385 btrfs_release_path(path);
2386 btrfs_end_transaction(trans, root);
2387out_unlock:
2388 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2389 &cached, GFP_NOFS);
2390 iput(inode);
2391 return ret;
2392}
2393
2394static void relink_file_extents(struct new_sa_defrag_extent *new)
2395{
2396 struct btrfs_path *path;
2397 struct old_sa_defrag_extent *old, *tmp;
2398 struct sa_defrag_extent_backref *backref;
2399 struct sa_defrag_extent_backref *prev = NULL;
2400 struct inode *inode;
2401 struct btrfs_root *root;
2402 struct rb_node *node;
2403 int ret;
2404
2405 inode = new->inode;
2406 root = BTRFS_I(inode)->root;
2407
2408 path = btrfs_alloc_path();
2409 if (!path)
2410 return;
2411
2412 if (!record_extent_backrefs(path, new)) {
2413 btrfs_free_path(path);
2414 goto out;
2415 }
2416 btrfs_release_path(path);
2417
2418 while (1) {
2419 node = rb_first(&new->root);
2420 if (!node)
2421 break;
2422 rb_erase(node, &new->root);
2423
2424 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2425
2426 ret = relink_extent_backref(path, prev, backref);
2427 WARN_ON(ret < 0);
2428
2429 kfree(prev);
2430
2431 if (ret == 1)
2432 prev = backref;
2433 else
2434 prev = NULL;
2435 cond_resched();
2436 }
2437 kfree(prev);
2438
2439 btrfs_free_path(path);
2440
2441 list_for_each_entry_safe(old, tmp, &new->head, list) {
2442 list_del(&old->list);
2443 kfree(old);
2444 }
2445out:
2446 atomic_dec(&root->fs_info->defrag_running);
2447 wake_up(&root->fs_info->transaction_wait);
2448
2449 kfree(new);
2450}
2451
2452static struct new_sa_defrag_extent *
2453record_old_file_extents(struct inode *inode,
2454 struct btrfs_ordered_extent *ordered)
2455{
2456 struct btrfs_root *root = BTRFS_I(inode)->root;
2457 struct btrfs_path *path;
2458 struct btrfs_key key;
2459 struct old_sa_defrag_extent *old, *tmp;
2460 struct new_sa_defrag_extent *new;
2461 int ret;
2462
2463 new = kmalloc(sizeof(*new), GFP_NOFS);
2464 if (!new)
2465 return NULL;
2466
2467 new->inode = inode;
2468 new->file_pos = ordered->file_offset;
2469 new->len = ordered->len;
2470 new->bytenr = ordered->start;
2471 new->disk_len = ordered->disk_len;
2472 new->compress_type = ordered->compress_type;
2473 new->root = RB_ROOT;
2474 INIT_LIST_HEAD(&new->head);
2475
2476 path = btrfs_alloc_path();
2477 if (!path)
2478 goto out_kfree;
2479
2480 key.objectid = btrfs_ino(inode);
2481 key.type = BTRFS_EXTENT_DATA_KEY;
2482 key.offset = new->file_pos;
2483
2484 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2485 if (ret < 0)
2486 goto out_free_path;
2487 if (ret > 0 && path->slots[0] > 0)
2488 path->slots[0]--;
2489
2490 /* find out all the old extents for the file range */
2491 while (1) {
2492 struct btrfs_file_extent_item *extent;
2493 struct extent_buffer *l;
2494 int slot;
2495 u64 num_bytes;
2496 u64 offset;
2497 u64 end;
2498 u64 disk_bytenr;
2499 u64 extent_offset;
2500
2501 l = path->nodes[0];
2502 slot = path->slots[0];
2503
2504 if (slot >= btrfs_header_nritems(l)) {
2505 ret = btrfs_next_leaf(root, path);
2506 if (ret < 0)
2507 goto out_free_list;
2508 else if (ret > 0)
2509 break;
2510 continue;
2511 }
2512
2513 btrfs_item_key_to_cpu(l, &key, slot);
2514
2515 if (key.objectid != btrfs_ino(inode))
2516 break;
2517 if (key.type != BTRFS_EXTENT_DATA_KEY)
2518 break;
2519 if (key.offset >= new->file_pos + new->len)
2520 break;
2521
2522 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2523
2524 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2525 if (key.offset + num_bytes < new->file_pos)
2526 goto next;
2527
2528 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2529 if (!disk_bytenr)
2530 goto next;
2531
2532 extent_offset = btrfs_file_extent_offset(l, extent);
2533
2534 old = kmalloc(sizeof(*old), GFP_NOFS);
2535 if (!old)
2536 goto out_free_list;
2537
2538 offset = max(new->file_pos, key.offset);
2539 end = min(new->file_pos + new->len, key.offset + num_bytes);
2540
2541 old->bytenr = disk_bytenr;
2542 old->extent_offset = extent_offset;
2543 old->offset = offset - key.offset;
2544 old->len = end - offset;
2545 old->new = new;
2546 old->count = 0;
2547 list_add_tail(&old->list, &new->head);
2548next:
2549 path->slots[0]++;
2550 cond_resched();
2551 }
2552
2553 btrfs_free_path(path);
2554 atomic_inc(&root->fs_info->defrag_running);
2555
2556 return new;
2557
2558out_free_list:
2559 list_for_each_entry_safe(old, tmp, &new->head, list) {
2560 list_del(&old->list);
2561 kfree(old);
2562 }
2563out_free_path:
2564 btrfs_free_path(path);
2565out_kfree:
2566 kfree(new);
2567 return NULL;
2568}
2569
Chris Mason5d13a982009-03-13 11:41:46 -04002570/*
2571 * helper function for btrfs_finish_ordered_io, this
2572 * just reads in some of the csum leaves to prime them into ram
2573 * before we start the transaction. It limits the amount of btree
2574 * reads required while inside the transaction.
2575 */
Chris Masond352ac62008-09-29 15:18:18 -04002576/* as ordered data IO finishes, this gets called so we can finish
2577 * an ordered extent if the range of bytes in the file it covers are
2578 * fully written.
2579 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002580static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002581{
Josef Bacik5fd02042012-05-02 14:00:54 -04002582 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002583 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002584 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002585 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002586 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002587 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002588 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002589 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002590 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002591
Liu Bo83eea1f2012-07-10 05:28:39 -06002592 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002593
Josef Bacik5fd02042012-05-02 14:00:54 -04002594 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2595 ret = -EIO;
2596 goto out;
2597 }
2598
Yan, Zhengc2167752009-11-12 09:34:21 +00002599 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002600 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002601 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2602 if (nolock)
2603 trans = btrfs_join_transaction_nolock(root);
2604 else
2605 trans = btrfs_join_transaction(root);
2606 if (IS_ERR(trans)) {
2607 ret = PTR_ERR(trans);
2608 trans = NULL;
2609 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002610 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002611 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2612 ret = btrfs_update_inode_fallback(trans, root, inode);
2613 if (ret) /* -ENOMEM or corruption */
2614 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002615 goto out;
2616 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002617
Josef Bacik2ac55d42010-02-03 19:33:23 +00002618 lock_extent_bits(io_tree, ordered_extent->file_offset,
2619 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002620 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002621
Liu Bo38c227d2013-01-29 03:18:40 +00002622 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2623 ordered_extent->file_offset + ordered_extent->len - 1,
2624 EXTENT_DEFRAG, 1, cached_state);
2625 if (ret) {
2626 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2627 if (last_snapshot >= BTRFS_I(inode)->generation)
2628 /* the inode is shared */
2629 new = record_old_file_extents(inode, ordered_extent);
2630
2631 clear_extent_bit(io_tree, ordered_extent->file_offset,
2632 ordered_extent->file_offset + ordered_extent->len - 1,
2633 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2634 }
2635
Josef Bacik0cb59c92010-07-02 12:14:14 -04002636 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002637 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002638 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002639 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002640 if (IS_ERR(trans)) {
2641 ret = PTR_ERR(trans);
2642 trans = NULL;
2643 goto out_unlock;
2644 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002645 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002646
Chris Masonc8b97812008-10-29 14:49:59 -04002647 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002648 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002649 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002650 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002651 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002652 ordered_extent->file_offset,
2653 ordered_extent->file_offset +
2654 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002655 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002656 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002657 ret = insert_reserved_file_extent(trans, inode,
2658 ordered_extent->file_offset,
2659 ordered_extent->start,
2660 ordered_extent->disk_len,
2661 ordered_extent->len,
2662 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002663 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002664 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002665 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002666 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2667 ordered_extent->file_offset, ordered_extent->len,
2668 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002669 if (ret < 0) {
2670 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002671 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002672 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002673
Chris Masone6dcd2d2008-07-17 12:53:50 -04002674 add_pending_csums(trans, inode, ordered_extent->file_offset,
2675 &ordered_extent->list);
2676
Josef Bacik6c760c02012-11-09 10:53:21 -05002677 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2678 ret = btrfs_update_inode_fallback(trans, root, inode);
2679 if (ret) { /* -ENOMEM or corruption */
2680 btrfs_abort_transaction(trans, root, ret);
2681 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002682 }
2683 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002684out_unlock:
2685 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2686 ordered_extent->file_offset +
2687 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002688out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002689 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002690 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002691 if (trans)
2692 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002693
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002694 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002695 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2696 ordered_extent->file_offset +
2697 ordered_extent->len - 1, NULL, GFP_NOFS);
2698
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002699 /*
2700 * If the ordered extent had an IOERR or something else went
2701 * wrong we need to return the space for this ordered extent
2702 * back to the allocator.
2703 */
2704 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2705 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2706 btrfs_free_reserved_extent(root, ordered_extent->start,
2707 ordered_extent->disk_len);
2708 }
2709
2710
Josef Bacik5fd02042012-05-02 14:00:54 -04002711 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002712 * This needs to be done to make sure anybody waiting knows we are done
2713 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002714 */
2715 btrfs_remove_ordered_extent(inode, ordered_extent);
2716
Liu Bo38c227d2013-01-29 03:18:40 +00002717 /* for snapshot-aware defrag */
2718 if (new)
2719 relink_file_extents(new);
2720
Chris Masone6dcd2d2008-07-17 12:53:50 -04002721 /* once for us */
2722 btrfs_put_ordered_extent(ordered_extent);
2723 /* once for the tree */
2724 btrfs_put_ordered_extent(ordered_extent);
2725
Josef Bacik5fd02042012-05-02 14:00:54 -04002726 return ret;
2727}
2728
2729static void finish_ordered_fn(struct btrfs_work *work)
2730{
2731 struct btrfs_ordered_extent *ordered_extent;
2732 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2733 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002734}
2735
Christoph Hellwigb2950862008-12-02 09:54:17 -05002736static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002737 struct extent_state *state, int uptodate)
2738{
Josef Bacik5fd02042012-05-02 14:00:54 -04002739 struct inode *inode = page->mapping->host;
2740 struct btrfs_root *root = BTRFS_I(inode)->root;
2741 struct btrfs_ordered_extent *ordered_extent = NULL;
2742 struct btrfs_workers *workers;
2743
liubo1abe9b82011-03-24 11:18:59 +00002744 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2745
Chris Mason8b62b722009-09-02 16:53:46 -04002746 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002747 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2748 end - start + 1, uptodate))
2749 return 0;
2750
2751 ordered_extent->work.func = finish_ordered_fn;
2752 ordered_extent->work.flags = 0;
2753
Liu Bo83eea1f2012-07-10 05:28:39 -06002754 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002755 workers = &root->fs_info->endio_freespace_worker;
2756 else
2757 workers = &root->fs_info->endio_write_workers;
2758 btrfs_queue_worker(workers, &ordered_extent->work);
2759
2760 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002761}
2762
Chris Masond352ac62008-09-29 15:18:18 -04002763/*
Chris Masond352ac62008-09-29 15:18:18 -04002764 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002765 * if there's a match, we allow the bio to finish. If not, the code in
2766 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002767 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002768static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002769 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002770{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002771 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002772 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002773 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002774 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002775 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002776 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002777 struct btrfs_root *root = BTRFS_I(inode)->root;
2778 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002779
Chris Masond20f7042008-12-08 16:58:54 -05002780 if (PageChecked(page)) {
2781 ClearPageChecked(page);
2782 goto good;
2783 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002784
2785 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002786 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002787
Yan Zheng17d217f2008-12-12 10:03:38 -05002788 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002789 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002790 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2791 GFP_NOFS);
2792 return 0;
2793 }
2794
Yanc2e639f2008-02-04 08:57:25 -05002795 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002796 private = state->private;
2797 ret = 0;
2798 } else {
2799 ret = get_state_private(io_tree, start, &private);
2800 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002801 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002802 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002803 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002804
Chris Masonff79f812007-10-15 16:22:25 -04002805 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2806 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002807 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002808 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002809
Cong Wang7ac687d2011-11-25 23:14:28 +08002810 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002811good:
Chris Mason07157aa2007-08-30 08:50:51 -04002812 return 0;
2813
2814zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002815 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002816 "private %llu\n",
2817 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002818 (unsigned long long)start, csum,
2819 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002820 memset(kaddr + offset, 1, end - start + 1);
2821 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002822 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002823 if (private == 0)
2824 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002825 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002826}
Chris Masonb888db2b2007-08-27 16:49:44 -04002827
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002828struct delayed_iput {
2829 struct list_head list;
2830 struct inode *inode;
2831};
2832
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002833/* JDM: If this is fs-wide, why can't we add a pointer to
2834 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002835void btrfs_add_delayed_iput(struct inode *inode)
2836{
2837 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2838 struct delayed_iput *delayed;
2839
2840 if (atomic_add_unless(&inode->i_count, -1, 1))
2841 return;
2842
2843 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2844 delayed->inode = inode;
2845
2846 spin_lock(&fs_info->delayed_iput_lock);
2847 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2848 spin_unlock(&fs_info->delayed_iput_lock);
2849}
2850
2851void btrfs_run_delayed_iputs(struct btrfs_root *root)
2852{
2853 LIST_HEAD(list);
2854 struct btrfs_fs_info *fs_info = root->fs_info;
2855 struct delayed_iput *delayed;
2856 int empty;
2857
2858 spin_lock(&fs_info->delayed_iput_lock);
2859 empty = list_empty(&fs_info->delayed_iputs);
2860 spin_unlock(&fs_info->delayed_iput_lock);
2861 if (empty)
2862 return;
2863
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002864 spin_lock(&fs_info->delayed_iput_lock);
2865 list_splice_init(&fs_info->delayed_iputs, &list);
2866 spin_unlock(&fs_info->delayed_iput_lock);
2867
2868 while (!list_empty(&list)) {
2869 delayed = list_entry(list.next, struct delayed_iput, list);
2870 list_del(&delayed->list);
2871 iput(delayed->inode);
2872 kfree(delayed);
2873 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002874}
2875
Yan, Zhengd68fc572010-05-16 10:49:58 -04002876/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002877 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002878 * files in the subvolume, it removes orphan item and frees block_rsv
2879 * structure.
2880 */
2881void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2882 struct btrfs_root *root)
2883{
Josef Bacik90290e12011-12-02 15:44:12 -05002884 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002885 int ret;
2886
Josef Bacik8a35d952012-05-23 14:26:42 -04002887 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002888 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2889 return;
2890
Josef Bacik90290e12011-12-02 15:44:12 -05002891 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002892 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002893 spin_unlock(&root->orphan_lock);
2894 return;
2895 }
2896
2897 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2898 spin_unlock(&root->orphan_lock);
2899 return;
2900 }
2901
2902 block_rsv = root->orphan_block_rsv;
2903 root->orphan_block_rsv = NULL;
2904 spin_unlock(&root->orphan_lock);
2905
Yan, Zhengd68fc572010-05-16 10:49:58 -04002906 if (root->orphan_item_inserted &&
2907 btrfs_root_refs(&root->root_item) > 0) {
2908 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2909 root->root_key.objectid);
2910 BUG_ON(ret);
2911 root->orphan_item_inserted = 0;
2912 }
2913
Josef Bacik90290e12011-12-02 15:44:12 -05002914 if (block_rsv) {
2915 WARN_ON(block_rsv->size > 0);
2916 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002917 }
2918}
2919
2920/*
Josef Bacik7b128762008-07-24 12:17:14 -04002921 * This creates an orphan entry for the given inode in case something goes
2922 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002923 *
2924 * NOTE: caller of this function should reserve 5 units of metadata for
2925 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002926 */
2927int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2928{
2929 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002930 struct btrfs_block_rsv *block_rsv = NULL;
2931 int reserve = 0;
2932 int insert = 0;
2933 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002934
Yan, Zhengd68fc572010-05-16 10:49:58 -04002935 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002936 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002937 if (!block_rsv)
2938 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002939 }
2940
Yan, Zhengd68fc572010-05-16 10:49:58 -04002941 spin_lock(&root->orphan_lock);
2942 if (!root->orphan_block_rsv) {
2943 root->orphan_block_rsv = block_rsv;
2944 } else if (block_rsv) {
2945 btrfs_free_block_rsv(root, block_rsv);
2946 block_rsv = NULL;
2947 }
Josef Bacik7b128762008-07-24 12:17:14 -04002948
Josef Bacik8a35d952012-05-23 14:26:42 -04002949 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2950 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002951#if 0
2952 /*
2953 * For proper ENOSPC handling, we should do orphan
2954 * cleanup when mounting. But this introduces backward
2955 * compatibility issue.
2956 */
2957 if (!xchg(&root->orphan_item_inserted, 1))
2958 insert = 2;
2959 else
2960 insert = 1;
2961#endif
2962 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002963 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002964 }
Josef Bacik7b128762008-07-24 12:17:14 -04002965
Josef Bacik72ac3c02012-05-23 14:13:11 -04002966 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2967 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002968 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002969 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002970
Yan, Zhengd68fc572010-05-16 10:49:58 -04002971 /* grab metadata reservation from transaction handle */
2972 if (reserve) {
2973 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002974 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002975 }
2976
2977 /* insert an orphan item to track this unlinked/truncated file */
2978 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002979 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002980 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002981 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2982 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002983 btrfs_abort_transaction(trans, root, ret);
2984 return ret;
2985 }
2986 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002987 }
2988
2989 /* insert an orphan item to track subvolume contains orphan files */
2990 if (insert >= 2) {
2991 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2992 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002993 if (ret && ret != -EEXIST) {
2994 btrfs_abort_transaction(trans, root, ret);
2995 return ret;
2996 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002997 }
2998 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002999}
3000
3001/*
3002 * We have done the truncate/delete so we can go ahead and remove the orphan
3003 * item for this particular inode.
3004 */
3005int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
3006{
3007 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003008 int delete_item = 0;
3009 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003010 int ret = 0;
3011
Yan, Zhengd68fc572010-05-16 10:49:58 -04003012 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003013 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3014 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003015 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003016
Josef Bacik72ac3c02012-05-23 14:13:11 -04003017 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3018 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003019 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003020 spin_unlock(&root->orphan_lock);
3021
3022 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003023 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003024 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003025 }
3026
Josef Bacik8a35d952012-05-23 14:26:42 -04003027 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003028 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003029 atomic_dec(&root->orphan_inodes);
3030 }
Josef Bacik7b128762008-07-24 12:17:14 -04003031
Yan, Zhengd68fc572010-05-16 10:49:58 -04003032 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003033}
3034
3035/*
3036 * this cleans up any orphans that may be left on the list from the last use
3037 * of this root.
3038 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003039int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003040{
3041 struct btrfs_path *path;
3042 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003043 struct btrfs_key key, found_key;
3044 struct btrfs_trans_handle *trans;
3045 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003046 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003047 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3048
Yan, Zhengd68fc572010-05-16 10:49:58 -04003049 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003050 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003051
3052 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003053 if (!path) {
3054 ret = -ENOMEM;
3055 goto out;
3056 }
Josef Bacik7b128762008-07-24 12:17:14 -04003057 path->reada = -1;
3058
3059 key.objectid = BTRFS_ORPHAN_OBJECTID;
3060 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3061 key.offset = (u64)-1;
3062
Josef Bacik7b128762008-07-24 12:17:14 -04003063 while (1) {
3064 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003065 if (ret < 0)
3066 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003067
3068 /*
3069 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003070 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003071 * find the key and see if we have stuff that matches
3072 */
3073 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003074 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003075 if (path->slots[0] == 0)
3076 break;
3077 path->slots[0]--;
3078 }
3079
3080 /* pull out the item */
3081 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003082 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3083
3084 /* make sure the item matches what we want */
3085 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3086 break;
3087 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3088 break;
3089
3090 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003091 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003092
3093 /*
3094 * this is where we are basically btrfs_lookup, without the
3095 * crossing root thing. we store the inode number in the
3096 * offset of the orphan item.
3097 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003098
3099 if (found_key.offset == last_objectid) {
3100 printk(KERN_ERR "btrfs: Error removing orphan entry, "
3101 "stopping orphan cleanup\n");
3102 ret = -EINVAL;
3103 goto out;
3104 }
3105
3106 last_objectid = found_key.offset;
3107
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003108 found_key.objectid = found_key.offset;
3109 found_key.type = BTRFS_INODE_ITEM_KEY;
3110 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003111 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003112 ret = PTR_RET(inode);
3113 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003114 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003115
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003116 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3117 struct btrfs_root *dead_root;
3118 struct btrfs_fs_info *fs_info = root->fs_info;
3119 int is_dead_root = 0;
3120
3121 /*
3122 * this is an orphan in the tree root. Currently these
3123 * could come from 2 sources:
3124 * a) a snapshot deletion in progress
3125 * b) a free space cache inode
3126 * We need to distinguish those two, as the snapshot
3127 * orphan must not get deleted.
3128 * find_dead_roots already ran before us, so if this
3129 * is a snapshot deletion, we should find the root
3130 * in the dead_roots list
3131 */
3132 spin_lock(&fs_info->trans_lock);
3133 list_for_each_entry(dead_root, &fs_info->dead_roots,
3134 root_list) {
3135 if (dead_root->root_key.objectid ==
3136 found_key.objectid) {
3137 is_dead_root = 1;
3138 break;
3139 }
3140 }
3141 spin_unlock(&fs_info->trans_lock);
3142 if (is_dead_root) {
3143 /* prevent this orphan from being found again */
3144 key.offset = found_key.objectid - 1;
3145 continue;
3146 }
3147 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003148 /*
3149 * Inode is already gone but the orphan item is still there,
3150 * kill the orphan item.
3151 */
3152 if (ret == -ESTALE) {
3153 trans = btrfs_start_transaction(root, 1);
3154 if (IS_ERR(trans)) {
3155 ret = PTR_ERR(trans);
3156 goto out;
3157 }
Josef Bacik8a35d952012-05-23 14:26:42 -04003158 printk(KERN_ERR "auto deleting %Lu\n",
3159 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003160 ret = btrfs_del_orphan_item(trans, root,
3161 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003162 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003163 btrfs_end_transaction(trans, root);
3164 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003165 }
Josef Bacik7b128762008-07-24 12:17:14 -04003166
Josef Bacik7b128762008-07-24 12:17:14 -04003167 /*
3168 * add this inode to the orphan list so btrfs_orphan_del does
3169 * the proper thing when we hit it
3170 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003171 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3172 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003173 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003174
Josef Bacik7b128762008-07-24 12:17:14 -04003175 /* if we have links, this was a truncate, lets do that */
3176 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003177 if (!S_ISREG(inode->i_mode)) {
3178 WARN_ON(1);
3179 iput(inode);
3180 continue;
3181 }
Josef Bacik7b128762008-07-24 12:17:14 -04003182 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003183
3184 /* 1 for the orphan item deletion. */
3185 trans = btrfs_start_transaction(root, 1);
3186 if (IS_ERR(trans)) {
3187 ret = PTR_ERR(trans);
3188 goto out;
3189 }
3190 ret = btrfs_orphan_add(trans, inode);
3191 btrfs_end_transaction(trans, root);
3192 if (ret)
3193 goto out;
3194
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003195 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003196 if (ret)
3197 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003198 } else {
3199 nr_unlink++;
3200 }
3201
3202 /* this will do delete_inode and everything for us */
3203 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003204 if (ret)
3205 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003206 }
Miao Xie3254c872011-11-10 20:45:05 -05003207 /* release the path since we're done with it */
3208 btrfs_release_path(path);
3209
Yan, Zhengd68fc572010-05-16 10:49:58 -04003210 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3211
3212 if (root->orphan_block_rsv)
3213 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3214 (u64)-1);
3215
3216 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003217 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003218 if (!IS_ERR(trans))
3219 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003220 }
Josef Bacik7b128762008-07-24 12:17:14 -04003221
3222 if (nr_unlink)
3223 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
3224 if (nr_truncate)
3225 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003226
3227out:
3228 if (ret)
3229 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
3230 btrfs_free_path(path);
3231 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003232}
3233
Chris Masond352ac62008-09-29 15:18:18 -04003234/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003235 * very simple check to peek ahead in the leaf looking for xattrs. If we
3236 * don't find any xattrs, we know there can't be any acls.
3237 *
3238 * slot is the slot the inode is in, objectid is the objectid of the inode
3239 */
3240static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3241 int slot, u64 objectid)
3242{
3243 u32 nritems = btrfs_header_nritems(leaf);
3244 struct btrfs_key found_key;
3245 int scanned = 0;
3246
3247 slot++;
3248 while (slot < nritems) {
3249 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3250
3251 /* we found a different objectid, there must not be acls */
3252 if (found_key.objectid != objectid)
3253 return 0;
3254
3255 /* we found an xattr, assume we've got an acl */
3256 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3257 return 1;
3258
3259 /*
3260 * we found a key greater than an xattr key, there can't
3261 * be any acls later on
3262 */
3263 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3264 return 0;
3265
3266 slot++;
3267 scanned++;
3268
3269 /*
3270 * it goes inode, inode backrefs, xattrs, extents,
3271 * so if there are a ton of hard links to an inode there can
3272 * be a lot of backrefs. Don't waste time searching too hard,
3273 * this is just an optimization
3274 */
3275 if (scanned >= 8)
3276 break;
3277 }
3278 /* we hit the end of the leaf before we found an xattr or
3279 * something larger than an xattr. We have to assume the inode
3280 * has acls
3281 */
3282 return 1;
3283}
3284
3285/*
Chris Masond352ac62008-09-29 15:18:18 -04003286 * read an inode from the btree into the in-memory inode
3287 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003288static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003289{
3290 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003291 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003292 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003293 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003294 struct btrfs_root *root = BTRFS_I(inode)->root;
3295 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003296 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003297 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003298 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003299 bool filled = false;
3300
3301 ret = btrfs_fill_inode(inode, &rdev);
3302 if (!ret)
3303 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003304
3305 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003306 if (!path)
3307 goto make_bad;
3308
Josef Bacikd90c7322011-05-17 09:50:54 -04003309 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003310 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003311
Chris Mason39279cc2007-06-12 06:35:45 -04003312 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003313 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003314 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003315
Chris Mason5f39d392007-10-15 16:14:19 -04003316 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003317
3318 if (filled)
3319 goto cache_acl;
3320
Chris Mason5f39d392007-10-15 16:14:19 -04003321 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3322 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003323 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003324 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003325 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3326 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003327 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003328
3329 tspec = btrfs_inode_atime(inode_item);
3330 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3331 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3332
3333 tspec = btrfs_inode_mtime(inode_item);
3334 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3335 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3336
3337 tspec = btrfs_inode_ctime(inode_item);
3338 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3339 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3340
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003341 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003342 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003343 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3344
3345 /*
3346 * If we were modified in the current generation and evicted from memory
3347 * and then re-read we need to do a full sync since we don't have any
3348 * idea about which extents were modified before we were evicted from
3349 * cache.
3350 */
3351 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3352 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3353 &BTRFS_I(inode)->runtime_flags);
3354
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003355 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003356 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003357 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003358 rdev = btrfs_inode_rdev(leaf, inode_item);
3359
Josef Bacikaec74772008-07-24 12:12:38 -04003360 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003361 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003362cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003363 /*
3364 * try to precache a NULL acl entry for files that don't have
3365 * any xattrs or acls
3366 */
Li Zefan33345d012011-04-20 10:31:50 +08003367 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3368 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003369 if (!maybe_acls)
3370 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003371
Chris Mason39279cc2007-06-12 06:35:45 -04003372 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003373
Chris Mason39279cc2007-06-12 06:35:45 -04003374 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003375 case S_IFREG:
3376 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003377 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003378 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003379 inode->i_fop = &btrfs_file_operations;
3380 inode->i_op = &btrfs_file_inode_operations;
3381 break;
3382 case S_IFDIR:
3383 inode->i_fop = &btrfs_dir_file_operations;
3384 if (root == root->fs_info->tree_root)
3385 inode->i_op = &btrfs_dir_ro_inode_operations;
3386 else
3387 inode->i_op = &btrfs_dir_inode_operations;
3388 break;
3389 case S_IFLNK:
3390 inode->i_op = &btrfs_symlink_inode_operations;
3391 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003392 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003393 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003394 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003395 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003396 init_special_inode(inode, inode->i_mode, rdev);
3397 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003398 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003399
3400 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003401 return;
3402
3403make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003404 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003405 make_bad_inode(inode);
3406}
3407
Chris Masond352ac62008-09-29 15:18:18 -04003408/*
3409 * given a leaf and an inode, copy the inode fields into the leaf
3410 */
Chris Masone02119d2008-09-05 16:13:11 -04003411static void fill_inode_item(struct btrfs_trans_handle *trans,
3412 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003413 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003414 struct inode *inode)
3415{
Liu Bo51fab692012-12-27 09:01:21 +00003416 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003417
Liu Bo51fab692012-12-27 09:01:21 +00003418 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003419
Liu Bo51fab692012-12-27 09:01:21 +00003420 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3421 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3422 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3423 &token);
3424 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3425 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003426
Liu Bo51fab692012-12-27 09:01:21 +00003427 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3428 inode->i_atime.tv_sec, &token);
3429 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3430 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003431
Liu Bo51fab692012-12-27 09:01:21 +00003432 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3433 inode->i_mtime.tv_sec, &token);
3434 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3435 inode->i_mtime.tv_nsec, &token);
3436
3437 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3438 inode->i_ctime.tv_sec, &token);
3439 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3440 inode->i_ctime.tv_nsec, &token);
3441
3442 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3443 &token);
3444 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3445 &token);
3446 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3447 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3448 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3449 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3450 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003451}
3452
Chris Masond352ac62008-09-29 15:18:18 -04003453/*
3454 * copy everything in the in-memory inode into the btree.
3455 */
Chris Mason21151332011-11-10 20:39:08 -05003456static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003457 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003458{
3459 struct btrfs_inode_item *inode_item;
3460 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003461 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003462 int ret;
3463
3464 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003465 if (!path)
3466 return -ENOMEM;
3467
Chris Masonb9473432009-03-13 11:00:37 -04003468 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003469 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3470 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003471 if (ret) {
3472 if (ret > 0)
3473 ret = -ENOENT;
3474 goto failed;
3475 }
3476
Chris Masonb4ce94d2009-02-04 09:25:08 -05003477 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003478 leaf = path->nodes[0];
3479 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003480 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003481
Chris Masone02119d2008-09-05 16:13:11 -04003482 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003483 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003484 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003485 ret = 0;
3486failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003487 btrfs_free_path(path);
3488 return ret;
3489}
3490
Chris Masond352ac62008-09-29 15:18:18 -04003491/*
Chris Mason21151332011-11-10 20:39:08 -05003492 * copy everything in the in-memory inode into the btree.
3493 */
3494noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3495 struct btrfs_root *root, struct inode *inode)
3496{
3497 int ret;
3498
3499 /*
3500 * If the inode is a free space inode, we can deadlock during commit
3501 * if we put it into the delayed code.
3502 *
3503 * The data relocation inode should also be directly updated
3504 * without delay
3505 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003506 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003507 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003508 btrfs_update_root_times(trans, root);
3509
Chris Mason21151332011-11-10 20:39:08 -05003510 ret = btrfs_delayed_update_inode(trans, root, inode);
3511 if (!ret)
3512 btrfs_set_inode_last_trans(trans, inode);
3513 return ret;
3514 }
3515
3516 return btrfs_update_inode_item(trans, root, inode);
3517}
3518
Josef Bacikbe6aef62012-10-22 15:43:12 -04003519noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3520 struct btrfs_root *root,
3521 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003522{
3523 int ret;
3524
3525 ret = btrfs_update_inode(trans, root, inode);
3526 if (ret == -ENOSPC)
3527 return btrfs_update_inode_item(trans, root, inode);
3528 return ret;
3529}
3530
3531/*
Chris Masond352ac62008-09-29 15:18:18 -04003532 * unlink helper that gets used here in inode.c and in the tree logging
3533 * recovery code. It remove a link in a directory with a given name, and
3534 * also drops the back refs in the inode to the directory
3535 */
Al Viro92986792011-03-04 17:14:37 +00003536static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3537 struct btrfs_root *root,
3538 struct inode *dir, struct inode *inode,
3539 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003540{
3541 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003542 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003543 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003544 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003545 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003546 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003547 u64 ino = btrfs_ino(inode);
3548 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003549
3550 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003551 if (!path) {
3552 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003553 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003554 }
3555
Chris Masonb9473432009-03-13 11:00:37 -04003556 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003557 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003558 name, name_len, -1);
3559 if (IS_ERR(di)) {
3560 ret = PTR_ERR(di);
3561 goto err;
3562 }
3563 if (!di) {
3564 ret = -ENOENT;
3565 goto err;
3566 }
Chris Mason5f39d392007-10-15 16:14:19 -04003567 leaf = path->nodes[0];
3568 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003569 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003570 if (ret)
3571 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003572 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003573
Li Zefan33345d012011-04-20 10:31:50 +08003574 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3575 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003576 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05003577 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08003578 "inode %llu parent %llu\n", name_len, name,
3579 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003580 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003581 goto err;
3582 }
3583
Miao Xie16cdcec2011-04-22 18:12:22 +08003584 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003585 if (ret) {
3586 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003587 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003588 }
Chris Mason39279cc2007-06-12 06:35:45 -04003589
Chris Masone02119d2008-09-05 16:13:11 -04003590 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003591 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003592 if (ret != 0 && ret != -ENOENT) {
3593 btrfs_abort_transaction(trans, root, ret);
3594 goto err;
3595 }
Chris Masone02119d2008-09-05 16:13:11 -04003596
3597 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3598 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003599 if (ret == -ENOENT)
3600 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003601err:
3602 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003603 if (ret)
3604 goto out;
3605
3606 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003607 inode_inc_iversion(inode);
3608 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003609 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003610 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003611out:
Chris Mason39279cc2007-06-12 06:35:45 -04003612 return ret;
3613}
3614
Al Viro92986792011-03-04 17:14:37 +00003615int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3616 struct btrfs_root *root,
3617 struct inode *dir, struct inode *inode,
3618 const char *name, int name_len)
3619{
3620 int ret;
3621 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3622 if (!ret) {
3623 btrfs_drop_nlink(inode);
3624 ret = btrfs_update_inode(trans, root, inode);
3625 }
3626 return ret;
3627}
3628
3629
Yan, Zhenga22285a2010-05-16 10:48:46 -04003630/* helper to check if there is any shared block in the path */
3631static int check_path_shared(struct btrfs_root *root,
3632 struct btrfs_path *path)
3633{
3634 struct extent_buffer *eb;
3635 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00003636 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003637
3638 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003639 int ret;
3640
Yan, Zhenga22285a2010-05-16 10:48:46 -04003641 if (!path->nodes[level])
3642 break;
3643 eb = path->nodes[level];
3644 if (!btrfs_block_can_be_shared(root, eb))
3645 continue;
3646 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
3647 &refs, NULL);
3648 if (refs > 1)
3649 return 1;
3650 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003651 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003652}
3653
3654/*
3655 * helper to start transaction for unlink and rmdir.
3656 *
3657 * unlink and rmdir are special in btrfs, they do not always free space.
3658 * so in enospc case, we should make sure they will free space before
3659 * allowing them to use the global metadata reservation.
3660 */
3661static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3662 struct dentry *dentry)
3663{
3664 struct btrfs_trans_handle *trans;
3665 struct btrfs_root *root = BTRFS_I(dir)->root;
3666 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003667 struct btrfs_dir_item *di;
3668 struct inode *inode = dentry->d_inode;
3669 u64 index;
3670 int check_link = 1;
3671 int err = -ENOSPC;
3672 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003673 u64 ino = btrfs_ino(inode);
3674 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003675
Josef Bacike70bea52011-10-11 14:18:24 -04003676 /*
3677 * 1 for the possible orphan item
3678 * 1 for the dir item
3679 * 1 for the dir index
3680 * 1 for the inode ref
3681 * 1 for the inode ref in the tree log
3682 * 2 for the dir entries in the log
3683 * 1 for the inode
3684 */
3685 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003686 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3687 return trans;
3688
Li Zefan33345d012011-04-20 10:31:50 +08003689 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003690 return ERR_PTR(-ENOSPC);
3691
3692 /* check if there is someone else holds reference */
3693 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3694 return ERR_PTR(-ENOSPC);
3695
3696 if (atomic_read(&inode->i_count) > 2)
3697 return ERR_PTR(-ENOSPC);
3698
3699 if (xchg(&root->fs_info->enospc_unlink, 1))
3700 return ERR_PTR(-ENOSPC);
3701
3702 path = btrfs_alloc_path();
3703 if (!path) {
3704 root->fs_info->enospc_unlink = 0;
3705 return ERR_PTR(-ENOMEM);
3706 }
3707
Josef Bacik3880a1b2011-10-14 14:46:51 -04003708 /* 1 for the orphan item */
3709 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003710 if (IS_ERR(trans)) {
3711 btrfs_free_path(path);
3712 root->fs_info->enospc_unlink = 0;
3713 return trans;
3714 }
3715
3716 path->skip_locking = 1;
3717 path->search_commit_root = 1;
3718
3719 ret = btrfs_lookup_inode(trans, root, path,
3720 &BTRFS_I(dir)->location, 0);
3721 if (ret < 0) {
3722 err = ret;
3723 goto out;
3724 }
3725 if (ret == 0) {
3726 if (check_path_shared(root, path))
3727 goto out;
3728 } else {
3729 check_link = 0;
3730 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003731 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003732
3733 ret = btrfs_lookup_inode(trans, root, path,
3734 &BTRFS_I(inode)->location, 0);
3735 if (ret < 0) {
3736 err = ret;
3737 goto out;
3738 }
3739 if (ret == 0) {
3740 if (check_path_shared(root, path))
3741 goto out;
3742 } else {
3743 check_link = 0;
3744 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003745 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003746
3747 if (ret == 0 && S_ISREG(inode->i_mode)) {
3748 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003749 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003750 if (ret < 0) {
3751 err = ret;
3752 goto out;
3753 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003754 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003755 if (check_path_shared(root, path))
3756 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003757 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003758 }
3759
3760 if (!check_link) {
3761 err = 0;
3762 goto out;
3763 }
3764
Li Zefan33345d012011-04-20 10:31:50 +08003765 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003766 dentry->d_name.name, dentry->d_name.len, 0);
3767 if (IS_ERR(di)) {
3768 err = PTR_ERR(di);
3769 goto out;
3770 }
3771 if (di) {
3772 if (check_path_shared(root, path))
3773 goto out;
3774 } else {
3775 err = 0;
3776 goto out;
3777 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003778 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003779
Mark Fashehf1863732012-08-08 11:32:27 -07003780 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3781 dentry->d_name.len, ino, dir_ino, 0,
3782 &index);
3783 if (ret) {
3784 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003785 goto out;
3786 }
Mark Fashehf1863732012-08-08 11:32:27 -07003787
Yan, Zhenga22285a2010-05-16 10:48:46 -04003788 if (check_path_shared(root, path))
3789 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003790
David Sterbab3b4aa72011-04-21 01:20:15 +02003791 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003792
Miao Xie16cdcec2011-04-22 18:12:22 +08003793 /*
3794 * This is a commit root search, if we can lookup inode item and other
3795 * relative items in the commit root, it means the transaction of
3796 * dir/file creation has been committed, and the dir index item that we
3797 * delay to insert has also been inserted into the commit root. So
3798 * we needn't worry about the delayed insertion of the dir index item
3799 * here.
3800 */
Li Zefan33345d012011-04-20 10:31:50 +08003801 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003802 dentry->d_name.name, dentry->d_name.len, 0);
3803 if (IS_ERR(di)) {
3804 err = PTR_ERR(di);
3805 goto out;
3806 }
3807 BUG_ON(ret == -ENOENT);
3808 if (check_path_shared(root, path))
3809 goto out;
3810
3811 err = 0;
3812out:
3813 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003814 /* Migrate the orphan reservation over */
3815 if (!err)
3816 err = btrfs_block_rsv_migrate(trans->block_rsv,
3817 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003818 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003819
Yan, Zhenga22285a2010-05-16 10:48:46 -04003820 if (err) {
3821 btrfs_end_transaction(trans, root);
3822 root->fs_info->enospc_unlink = 0;
3823 return ERR_PTR(err);
3824 }
3825
3826 trans->block_rsv = &root->fs_info->global_block_rsv;
3827 return trans;
3828}
3829
3830static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3831 struct btrfs_root *root)
3832{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003833 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003834 btrfs_block_rsv_release(root, trans->block_rsv,
3835 trans->bytes_reserved);
3836 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003837 BUG_ON(!root->fs_info->enospc_unlink);
3838 root->fs_info->enospc_unlink = 0;
3839 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003840 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003841}
3842
Chris Mason39279cc2007-06-12 06:35:45 -04003843static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3844{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003845 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003846 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003847 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003848 int ret;
3849
Yan, Zhenga22285a2010-05-16 10:48:46 -04003850 trans = __unlink_start_trans(dir, dentry);
3851 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003852 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003853
Chris Mason12fcfd22009-03-24 10:24:20 -04003854 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3855
Chris Masone02119d2008-09-05 16:13:11 -04003856 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3857 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003858 if (ret)
3859 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003860
Yan, Zhenga22285a2010-05-16 10:48:46 -04003861 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003862 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003863 if (ret)
3864 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003865 }
Josef Bacik7b128762008-07-24 12:17:14 -04003866
Tsutomu Itohb5324022011-07-19 07:27:20 +00003867out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003868 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003869 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003870 return ret;
3871}
3872
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003873int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3874 struct btrfs_root *root,
3875 struct inode *dir, u64 objectid,
3876 const char *name, int name_len)
3877{
3878 struct btrfs_path *path;
3879 struct extent_buffer *leaf;
3880 struct btrfs_dir_item *di;
3881 struct btrfs_key key;
3882 u64 index;
3883 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003884 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003885
3886 path = btrfs_alloc_path();
3887 if (!path)
3888 return -ENOMEM;
3889
Li Zefan33345d012011-04-20 10:31:50 +08003890 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003891 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003892 if (IS_ERR_OR_NULL(di)) {
3893 if (!di)
3894 ret = -ENOENT;
3895 else
3896 ret = PTR_ERR(di);
3897 goto out;
3898 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003899
3900 leaf = path->nodes[0];
3901 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3902 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3903 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003904 if (ret) {
3905 btrfs_abort_transaction(trans, root, ret);
3906 goto out;
3907 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003908 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003909
3910 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3911 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003912 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003913 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003914 if (ret != -ENOENT) {
3915 btrfs_abort_transaction(trans, root, ret);
3916 goto out;
3917 }
Li Zefan33345d012011-04-20 10:31:50 +08003918 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003919 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003920 if (IS_ERR_OR_NULL(di)) {
3921 if (!di)
3922 ret = -ENOENT;
3923 else
3924 ret = PTR_ERR(di);
3925 btrfs_abort_transaction(trans, root, ret);
3926 goto out;
3927 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003928
3929 leaf = path->nodes[0];
3930 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003931 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003932 index = key.offset;
3933 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003934 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003935
Miao Xie16cdcec2011-04-22 18:12:22 +08003936 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003937 if (ret) {
3938 btrfs_abort_transaction(trans, root, ret);
3939 goto out;
3940 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003941
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003942 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003943 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003944 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003945 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003946 if (ret)
3947 btrfs_abort_transaction(trans, root, ret);
3948out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003949 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003950 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003951}
3952
Chris Mason39279cc2007-06-12 06:35:45 -04003953static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3954{
3955 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003956 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003957 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003958 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003959
David Sterbab3ae2442012-09-13 16:04:34 -06003960 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003961 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003962 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3963 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003964
Yan, Zhenga22285a2010-05-16 10:48:46 -04003965 trans = __unlink_start_trans(dir, dentry);
3966 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003967 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003968
Li Zefan33345d012011-04-20 10:31:50 +08003969 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003970 err = btrfs_unlink_subvol(trans, root, dir,
3971 BTRFS_I(inode)->location.objectid,
3972 dentry->d_name.name,
3973 dentry->d_name.len);
3974 goto out;
3975 }
3976
Josef Bacik7b128762008-07-24 12:17:14 -04003977 err = btrfs_orphan_add(trans, inode);
3978 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003979 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003980
Chris Mason39279cc2007-06-12 06:35:45 -04003981 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003982 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3983 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003984 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003985 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003986out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003987 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003988 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003989
Chris Mason39279cc2007-06-12 06:35:45 -04003990 return err;
3991}
3992
Chris Mason323ac952008-10-01 19:05:46 -04003993/*
Chris Mason39279cc2007-06-12 06:35:45 -04003994 * this can truncate away extent items, csum items and directory items.
3995 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003996 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003997 *
3998 * csum items that cross the new i_size are truncated to the new size
3999 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004000 *
4001 * min_type is the minimum key type to truncate down to. If set to 0, this
4002 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004003 */
Yan, Zheng80825102009-11-12 09:35:36 +00004004int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4005 struct btrfs_root *root,
4006 struct inode *inode,
4007 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004008{
Chris Mason39279cc2007-06-12 06:35:45 -04004009 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004010 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004011 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004012 struct btrfs_key key;
4013 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004014 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004015 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004016 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004017 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004018 u64 mask = root->sectorsize - 1;
4019 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004020 int found_extent;
4021 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004022 int pending_del_nr = 0;
4023 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004024 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004025 int ret;
4026 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004027 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004028
4029 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004030
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004031 path = btrfs_alloc_path();
4032 if (!path)
4033 return -ENOMEM;
4034 path->reada = -1;
4035
Josef Bacik5dc562c2012-08-17 13:14:17 -04004036 /*
4037 * We want to drop from the next block forward in case this new size is
4038 * not block aligned since we will be keeping the last block of the
4039 * extent just the way it is.
4040 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004041 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004042 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004043
Miao Xie16cdcec2011-04-22 18:12:22 +08004044 /*
4045 * This function is also used to drop the items in the log tree before
4046 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4047 * it is used to drop the loged items. So we shouldn't kill the delayed
4048 * items.
4049 */
4050 if (min_type == 0 && root == BTRFS_I(inode)->root)
4051 btrfs_kill_delayed_inode_items(inode);
4052
Li Zefan33345d012011-04-20 10:31:50 +08004053 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004054 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004055 key.type = (u8)-1;
4056
Chris Mason85e21ba2008-01-29 15:11:36 -05004057search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004058 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004059 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004060 if (ret < 0) {
4061 err = ret;
4062 goto out;
4063 }
Chris Masond3977122009-01-05 21:25:51 -05004064
Chris Mason85e21ba2008-01-29 15:11:36 -05004065 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004066 /* there are no items in the tree for us to truncate, we're
4067 * done
4068 */
Yan, Zheng80825102009-11-12 09:35:36 +00004069 if (path->slots[0] == 0)
4070 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004071 path->slots[0]--;
4072 }
4073
Chris Masond3977122009-01-05 21:25:51 -05004074 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004075 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004076 leaf = path->nodes[0];
4077 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4078 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004079
Li Zefan33345d012011-04-20 10:31:50 +08004080 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004081 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004082
Chris Mason85e21ba2008-01-29 15:11:36 -05004083 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004084 break;
4085
Chris Mason5f39d392007-10-15 16:14:19 -04004086 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004087 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004088 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004089 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004090 extent_type = btrfs_file_extent_type(leaf, fi);
4091 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004092 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004093 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004094 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004095 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004096 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004097 }
Yan008630c2007-11-07 13:31:09 -05004098 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004099 }
Yan, Zheng80825102009-11-12 09:35:36 +00004100 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004101 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004102 } else {
4103 if (item_end < new_size)
4104 break;
4105 if (found_key.offset >= new_size)
4106 del_item = 1;
4107 else
4108 del_item = 0;
4109 }
Chris Mason39279cc2007-06-12 06:35:45 -04004110 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004111 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004112 if (found_type != BTRFS_EXTENT_DATA_KEY)
4113 goto delete;
4114
4115 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004116 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004117 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004118 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004119 u64 orig_num_bytes =
4120 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04004121 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04004122 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05004123 extent_num_bytes = extent_num_bytes &
4124 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04004125 btrfs_set_file_extent_num_bytes(leaf, fi,
4126 extent_num_bytes);
4127 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004128 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004129 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004130 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004131 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004132 } else {
Chris Masondb945352007-10-15 16:15:53 -04004133 extent_num_bytes =
4134 btrfs_file_extent_disk_num_bytes(leaf,
4135 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004136 extent_offset = found_key.offset -
4137 btrfs_file_extent_offset(leaf, fi);
4138
Chris Mason39279cc2007-06-12 06:35:45 -04004139 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004140 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004141 if (extent_start != 0) {
4142 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004143 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004144 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004145 }
4146 }
Chris Mason90692182008-02-08 13:49:28 -05004147 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004148 /*
4149 * we can't truncate inline items that have had
4150 * special encodings
4151 */
4152 if (!del_item &&
4153 btrfs_file_extent_compression(leaf, fi) == 0 &&
4154 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4155 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004156 u32 size = new_size - found_key.offset;
4157
4158 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004159 inode_sub_bytes(inode, item_end + 1 -
4160 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004161 }
4162 size =
4163 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004164 btrfs_truncate_item(trans, root, path,
4165 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004166 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004167 inode_sub_bytes(inode, item_end + 1 -
4168 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004169 }
Chris Mason39279cc2007-06-12 06:35:45 -04004170 }
Chris Mason179e29e2007-11-01 11:28:41 -04004171delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004172 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004173 if (!pending_del_nr) {
4174 /* no pending yet, add ourselves */
4175 pending_del_slot = path->slots[0];
4176 pending_del_nr = 1;
4177 } else if (pending_del_nr &&
4178 path->slots[0] + 1 == pending_del_slot) {
4179 /* hop on the pending chunk */
4180 pending_del_nr++;
4181 pending_del_slot = path->slots[0];
4182 } else {
Chris Masond3977122009-01-05 21:25:51 -05004183 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004184 }
Chris Mason39279cc2007-06-12 06:35:45 -04004185 } else {
4186 break;
4187 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004188 if (found_extent && (root->ref_cows ||
4189 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004190 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004191 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004192 extent_num_bytes, 0,
4193 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004194 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004195 BUG_ON(ret);
4196 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004197
Yan, Zheng80825102009-11-12 09:35:36 +00004198 if (found_type == BTRFS_INODE_ITEM_KEY)
4199 break;
4200
4201 if (path->slots[0] == 0 ||
4202 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004203 if (pending_del_nr) {
4204 ret = btrfs_del_items(trans, root, path,
4205 pending_del_slot,
4206 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004207 if (ret) {
4208 btrfs_abort_transaction(trans,
4209 root, ret);
4210 goto error;
4211 }
Yan, Zheng80825102009-11-12 09:35:36 +00004212 pending_del_nr = 0;
4213 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004214 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004215 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004216 } else {
4217 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004218 }
Chris Mason39279cc2007-06-12 06:35:45 -04004219 }
Yan, Zheng80825102009-11-12 09:35:36 +00004220out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004221 if (pending_del_nr) {
4222 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4223 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004224 if (ret)
4225 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004226 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004227error:
Chris Mason39279cc2007-06-12 06:35:45 -04004228 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004229 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004230}
4231
Chris Masona52d9a82007-08-27 16:49:44 -04004232/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004233 * btrfs_truncate_page - read, zero a chunk and write a page
4234 * @inode - inode that we're zeroing
4235 * @from - the offset to start zeroing
4236 * @len - the length to zero, 0 to zero the entire range respective to the
4237 * offset
4238 * @front - zero up to the offset instead of from the offset on
4239 *
4240 * This will find the page for the "from" offset and cow the page and zero the
4241 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004242 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004243int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4244 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004245{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004246 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004247 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004248 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4249 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004250 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004251 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004252 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004253 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4254 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4255 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004256 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004257 int ret = 0;
4258 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004259 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004260
Josef Bacik2aaa6652012-08-29 14:27:18 -04004261 if ((offset & (blocksize - 1)) == 0 &&
4262 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004263 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004264 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004265 if (ret)
4266 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004267
Chris Mason211c17f2008-05-15 09:13:45 -04004268again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004269 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004270 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004271 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004272 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004273 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004274 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004275
4276 page_start = page_offset(page);
4277 page_end = page_start + PAGE_CACHE_SIZE - 1;
4278
Chris Masona52d9a82007-08-27 16:49:44 -04004279 if (!PageUptodate(page)) {
4280 ret = btrfs_readpage(NULL, page);
4281 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004282 if (page->mapping != mapping) {
4283 unlock_page(page);
4284 page_cache_release(page);
4285 goto again;
4286 }
Chris Masona52d9a82007-08-27 16:49:44 -04004287 if (!PageUptodate(page)) {
4288 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004289 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004290 }
4291 }
Chris Mason211c17f2008-05-15 09:13:45 -04004292 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004293
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004294 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004295 set_page_extent_mapped(page);
4296
4297 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4298 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004299 unlock_extent_cached(io_tree, page_start, page_end,
4300 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004301 unlock_page(page);
4302 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004303 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004304 btrfs_put_ordered_extent(ordered);
4305 goto again;
4306 }
4307
Josef Bacik2ac55d42010-02-03 19:33:23 +00004308 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004309 EXTENT_DIRTY | EXTENT_DELALLOC |
4310 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004311 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004312
Josef Bacik2ac55d42010-02-03 19:33:23 +00004313 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4314 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004315 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004316 unlock_extent_cached(io_tree, page_start, page_end,
4317 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004318 goto out_unlock;
4319 }
4320
Chris Masone6dcd2d2008-07-17 12:53:50 -04004321 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004322 if (!len)
4323 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004324 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004325 if (front)
4326 memset(kaddr, 0, offset);
4327 else
4328 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004329 flush_dcache_page(page);
4330 kunmap(page);
4331 }
Chris Mason247e7432008-07-17 12:53:51 -04004332 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004333 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004334 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4335 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004336
Chris Mason89642222008-07-24 09:41:53 -04004337out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004338 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004339 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004340 unlock_page(page);
4341 page_cache_release(page);
4342out:
4343 return ret;
4344}
4345
Josef Bacik695a0d02011-03-04 15:46:53 -05004346/*
4347 * This function puts in dummy file extents for the area we're creating a hole
4348 * for. So if we are truncating this file to a larger size we need to insert
4349 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4350 * the range between oldsize and size
4351 */
Josef Bacika41ad392011-01-31 15:30:16 -05004352int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004353{
4354 struct btrfs_trans_handle *trans;
4355 struct btrfs_root *root = BTRFS_I(inode)->root;
4356 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004357 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004358 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004359 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04004360 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05004361 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04004362 u64 block_end = (size + mask) & ~mask;
4363 u64 last_byte;
4364 u64 cur_offset;
4365 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004366 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004367
4368 if (size <= hole_start)
4369 return 0;
4370
Yan Zheng9036c102008-10-30 14:19:41 -04004371 while (1) {
4372 struct btrfs_ordered_extent *ordered;
4373 btrfs_wait_ordered_range(inode, hole_start,
4374 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004375 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004376 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004377 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4378 if (!ordered)
4379 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004380 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4381 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004382 btrfs_put_ordered_extent(ordered);
4383 }
4384
Yan Zheng9036c102008-10-30 14:19:41 -04004385 cur_offset = hole_start;
4386 while (1) {
4387 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4388 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004389 if (IS_ERR(em)) {
4390 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004391 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004392 break;
4393 }
Yan Zheng9036c102008-10-30 14:19:41 -04004394 last_byte = min(extent_map_end(em), block_end);
4395 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00004396 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004397 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004398 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004399
Miao Xie36423202011-12-14 20:12:02 -05004400 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004401 if (IS_ERR(trans)) {
4402 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004403 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004404 }
Yan, Zheng80825102009-11-12 09:35:36 +00004405
Josef Bacik5dc562c2012-08-17 13:14:17 -04004406 err = btrfs_drop_extents(trans, root, inode,
4407 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004408 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004409 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004410 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004411 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004412 break;
Miao Xie5b397372011-09-11 10:52:24 -04004413 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004414
Yan Zheng9036c102008-10-30 14:19:41 -04004415 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004416 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004417 0, hole_size, 0, hole_size,
4418 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004419 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004420 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004421 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004422 break;
Miao Xie5b397372011-09-11 10:52:24 -04004423 }
Yan, Zheng80825102009-11-12 09:35:36 +00004424
Josef Bacik5dc562c2012-08-17 13:14:17 -04004425 btrfs_drop_extent_cache(inode, cur_offset,
4426 cur_offset + hole_size - 1, 0);
4427 hole_em = alloc_extent_map();
4428 if (!hole_em) {
4429 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4430 &BTRFS_I(inode)->runtime_flags);
4431 goto next;
4432 }
4433 hole_em->start = cur_offset;
4434 hole_em->len = hole_size;
4435 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004436
Josef Bacik5dc562c2012-08-17 13:14:17 -04004437 hole_em->block_start = EXTENT_MAP_HOLE;
4438 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004439 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004440 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4441 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4442 hole_em->generation = trans->transid;
4443
4444 while (1) {
4445 write_lock(&em_tree->lock);
4446 err = add_extent_mapping(em_tree, hole_em);
4447 if (!err)
4448 list_move(&hole_em->list,
4449 &em_tree->modified_extents);
4450 write_unlock(&em_tree->lock);
4451 if (err != -EEXIST)
4452 break;
4453 btrfs_drop_extent_cache(inode, cur_offset,
4454 cur_offset +
4455 hole_size - 1, 0);
4456 }
4457 free_extent_map(hole_em);
4458next:
Miao Xie36423202011-12-14 20:12:02 -05004459 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004460 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004461 }
4462 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004463 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004464 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004465 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004466 break;
4467 }
4468
Yan, Zhenga22285a2010-05-16 10:48:46 -04004469 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004470 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4471 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004472 return err;
4473}
4474
Eric Sandeen3972f262013-01-12 02:57:22 +00004475static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004476{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004477 struct btrfs_root *root = BTRFS_I(inode)->root;
4478 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004479 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004480 loff_t newsize = attr->ia_size;
4481 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004482 int ret;
4483
Josef Bacika41ad392011-01-31 15:30:16 -05004484 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004485 return 0;
4486
Eric Sandeen3972f262013-01-12 02:57:22 +00004487 /*
4488 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4489 * special case where we need to update the times despite not having
4490 * these flags set. For all other operations the VFS set these flags
4491 * explicitly if it wants a timestamp update.
4492 */
4493 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4494 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4495
Josef Bacika41ad392011-01-31 15:30:16 -05004496 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004497 truncate_pagecache(inode, oldsize, newsize);
4498 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004499 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004500 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004501
Miao Xief4a2f4c2011-12-14 20:12:01 -05004502 trans = btrfs_start_transaction(root, 1);
4503 if (IS_ERR(trans))
4504 return PTR_ERR(trans);
4505
4506 i_size_write(inode, newsize);
4507 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4508 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004509 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004510 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004511
Josef Bacika41ad392011-01-31 15:30:16 -05004512 /*
4513 * We're truncating a file that used to have good data down to
4514 * zero. Make sure it gets into the ordered flush list so that
4515 * any new writes get down to disk quickly.
4516 */
4517 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004518 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4519 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004520
Josef Bacikf3fe8202013-01-07 17:03:21 -05004521 /*
4522 * 1 for the orphan item we're going to add
4523 * 1 for the orphan item deletion.
4524 */
4525 trans = btrfs_start_transaction(root, 2);
4526 if (IS_ERR(trans))
4527 return PTR_ERR(trans);
4528
4529 /*
4530 * We need to do this in case we fail at _any_ point during the
4531 * actual truncate. Once we do the truncate_setsize we could
4532 * invalidate pages which forces any outstanding ordered io to
4533 * be instantly completed which will give us extents that need
4534 * to be truncated. If we fail to get an orphan inode down we
4535 * could have left over extents that were never meant to live,
4536 * so we need to garuntee from this point on that everything
4537 * will be consistent.
4538 */
4539 ret = btrfs_orphan_add(trans, inode);
4540 btrfs_end_transaction(trans, root);
4541 if (ret)
4542 return ret;
4543
Josef Bacika41ad392011-01-31 15:30:16 -05004544 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4545 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004546
4547 /* Disable nonlocked read DIO to avoid the end less truncate */
4548 btrfs_inode_block_unlocked_dio(inode);
4549 inode_dio_wait(inode);
4550 btrfs_inode_resume_unlocked_dio(inode);
4551
Josef Bacika41ad392011-01-31 15:30:16 -05004552 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004553 if (ret && inode->i_nlink)
4554 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004555 }
4556
Josef Bacika41ad392011-01-31 15:30:16 -05004557 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004558}
4559
Chris Mason39279cc2007-06-12 06:35:45 -04004560static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4561{
4562 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004563 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004564 int err;
4565
Li Zefanb83cc962010-12-20 16:04:08 +08004566 if (btrfs_root_readonly(root))
4567 return -EROFS;
4568
Chris Mason39279cc2007-06-12 06:35:45 -04004569 err = inode_change_ok(inode, attr);
4570 if (err)
4571 return err;
4572
Chris Mason5a3f23d2009-03-31 13:27:11 -04004573 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004574 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004575 if (err)
4576 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004577 }
Yan Zheng9036c102008-10-30 14:19:41 -04004578
Christoph Hellwig10257742010-06-04 11:30:02 +02004579 if (attr->ia_valid) {
4580 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004581 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004582 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004583
Josef Bacik22c44fe2011-11-30 10:45:38 -05004584 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004585 err = btrfs_acl_chmod(inode);
4586 }
4587
Chris Mason39279cc2007-06-12 06:35:45 -04004588 return err;
4589}
Chris Mason61295eb2008-01-14 16:24:38 -05004590
Al Virobd555972010-06-07 11:35:40 -04004591void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004592{
4593 struct btrfs_trans_handle *trans;
4594 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004595 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004596 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004597 int ret;
4598
liubo1abe9b82011-03-24 11:18:59 +00004599 trace_btrfs_inode_evict(inode);
4600
Chris Mason39279cc2007-06-12 06:35:45 -04004601 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004602 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004603 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004604 goto no_delete;
4605
Chris Mason39279cc2007-06-12 06:35:45 -04004606 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004607 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004608 goto no_delete;
4609 }
Al Virobd555972010-06-07 11:35:40 -04004610 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004611 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004612
Yan, Zhengc71bf092009-11-12 09:34:40 +00004613 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004614 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004615 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004616 goto no_delete;
4617 }
4618
Yan, Zheng76dda932009-09-21 16:00:26 -04004619 if (inode->i_nlink > 0) {
4620 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4621 goto no_delete;
4622 }
4623
Miao Xie0e8c36a2012-12-19 06:59:51 +00004624 ret = btrfs_commit_inode_delayed_inode(inode);
4625 if (ret) {
4626 btrfs_orphan_del(NULL, inode);
4627 goto no_delete;
4628 }
4629
Miao Xie66d8f3d2012-09-06 04:02:28 -06004630 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004631 if (!rsv) {
4632 btrfs_orphan_del(NULL, inode);
4633 goto no_delete;
4634 }
Josef Bacik4a338542011-08-29 11:01:31 -04004635 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004636 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004637 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004638
Chris Masondbe674a2008-07-17 12:54:05 -04004639 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004640
Josef Bacik4289a662011-08-05 13:22:24 -04004641 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004642 * This is a bit simpler than btrfs_truncate since we've already
4643 * reserved our space for our orphan item in the unlink, so we just
4644 * need to reserve some slack space in case we add bytes and update
4645 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004646 */
Yan, Zheng80825102009-11-12 09:35:36 +00004647 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004648 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4649 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004650
Josef Bacik726c35f2011-09-26 15:46:06 -04004651 /*
4652 * Try and steal from the global reserve since we will
4653 * likely not use this space anyway, we want to try as
4654 * hard as possible to get this to work.
4655 */
4656 if (ret)
4657 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4658
Yan, Zhengd68fc572010-05-16 10:49:58 -04004659 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04004660 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04004661 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004662 btrfs_orphan_del(NULL, inode);
4663 btrfs_free_block_rsv(root, rsv);
4664 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004665 }
4666
Miao Xie0e8c36a2012-12-19 06:59:51 +00004667 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004668 if (IS_ERR(trans)) {
4669 btrfs_orphan_del(NULL, inode);
4670 btrfs_free_block_rsv(root, rsv);
4671 goto no_delete;
4672 }
4673
4674 trans->block_rsv = rsv;
4675
Yan, Zhengd68fc572010-05-16 10:49:58 -04004676 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004677 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004678 break;
4679
Miao Xie8407aa42012-09-07 01:43:32 -06004680 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004681 btrfs_end_transaction(trans, root);
4682 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004683 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004684 }
4685
Josef Bacik4289a662011-08-05 13:22:24 -04004686 btrfs_free_block_rsv(root, rsv);
4687
Yan, Zheng80825102009-11-12 09:35:36 +00004688 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004689 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004690 ret = btrfs_orphan_del(trans, inode);
4691 BUG_ON(ret);
4692 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004693
Josef Bacik4289a662011-08-05 13:22:24 -04004694 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004695 if (!(root == root->fs_info->tree_root ||
4696 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004697 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004698
Chris Mason54aa1f42007-06-22 14:16:25 -04004699 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004700 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004701no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004702 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004703 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004704}
4705
4706/*
4707 * this returns the key found in the dir entry in the location pointer.
4708 * If no dir entries were found, location->objectid is 0.
4709 */
4710static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4711 struct btrfs_key *location)
4712{
4713 const char *name = dentry->d_name.name;
4714 int namelen = dentry->d_name.len;
4715 struct btrfs_dir_item *di;
4716 struct btrfs_path *path;
4717 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004718 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004719
4720 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004721 if (!path)
4722 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004723
Li Zefan33345d012011-04-20 10:31:50 +08004724 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004725 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004726 if (IS_ERR(di))
4727 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004728
David Sterbac7040052011-04-19 18:00:01 +02004729 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004730 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004731
Chris Mason5f39d392007-10-15 16:14:19 -04004732 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004733out:
Chris Mason39279cc2007-06-12 06:35:45 -04004734 btrfs_free_path(path);
4735 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004736out_err:
4737 location->objectid = 0;
4738 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004739}
4740
4741/*
4742 * when we hit a tree root in a directory, the btrfs part of the inode
4743 * needs to be changed to reflect the root directory of the tree root. This
4744 * is kind of like crossing a mount point.
4745 */
4746static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004747 struct inode *dir,
4748 struct dentry *dentry,
4749 struct btrfs_key *location,
4750 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004751{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004752 struct btrfs_path *path;
4753 struct btrfs_root *new_root;
4754 struct btrfs_root_ref *ref;
4755 struct extent_buffer *leaf;
4756 int ret;
4757 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004758
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004759 path = btrfs_alloc_path();
4760 if (!path) {
4761 err = -ENOMEM;
4762 goto out;
4763 }
Chris Mason39279cc2007-06-12 06:35:45 -04004764
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004765 err = -ENOENT;
4766 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4767 BTRFS_I(dir)->root->root_key.objectid,
4768 location->objectid);
4769 if (ret) {
4770 if (ret < 0)
4771 err = ret;
4772 goto out;
4773 }
Chris Mason39279cc2007-06-12 06:35:45 -04004774
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004775 leaf = path->nodes[0];
4776 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004777 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004778 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4779 goto out;
4780
4781 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4782 (unsigned long)(ref + 1),
4783 dentry->d_name.len);
4784 if (ret)
4785 goto out;
4786
David Sterbab3b4aa72011-04-21 01:20:15 +02004787 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004788
4789 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4790 if (IS_ERR(new_root)) {
4791 err = PTR_ERR(new_root);
4792 goto out;
4793 }
4794
4795 if (btrfs_root_refs(&new_root->root_item) == 0) {
4796 err = -ENOENT;
4797 goto out;
4798 }
4799
4800 *sub_root = new_root;
4801 location->objectid = btrfs_root_dirid(&new_root->root_item);
4802 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004803 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004804 err = 0;
4805out:
4806 btrfs_free_path(path);
4807 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004808}
4809
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004810static void inode_tree_add(struct inode *inode)
4811{
4812 struct btrfs_root *root = BTRFS_I(inode)->root;
4813 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004814 struct rb_node **p;
4815 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004816 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004817again:
4818 p = &root->inode_tree.rb_node;
4819 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004820
Al Viro1d3382cb2010-10-23 15:19:20 -04004821 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004822 return;
4823
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004824 spin_lock(&root->inode_lock);
4825 while (*p) {
4826 parent = *p;
4827 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4828
Li Zefan33345d012011-04-20 10:31:50 +08004829 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004830 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004831 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004832 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004833 else {
4834 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004835 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004836 rb_erase(parent, &root->inode_tree);
4837 RB_CLEAR_NODE(parent);
4838 spin_unlock(&root->inode_lock);
4839 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004840 }
4841 }
4842 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4843 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4844 spin_unlock(&root->inode_lock);
4845}
4846
4847static void inode_tree_del(struct inode *inode)
4848{
4849 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004850 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004851
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004852 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004853 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004854 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004855 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004856 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004857 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004858 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004859
Josef Bacik0af3d002010-06-21 14:48:16 -04004860 /*
4861 * Free space cache has inodes in the tree root, but the tree root has a
4862 * root_refs of 0, so this could end up dropping the tree root as a
4863 * snapshot, so we need the extra !root->fs_info->tree_root check to
4864 * make sure we don't drop it.
4865 */
4866 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4867 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004868 synchronize_srcu(&root->fs_info->subvol_srcu);
4869 spin_lock(&root->inode_lock);
4870 empty = RB_EMPTY_ROOT(&root->inode_tree);
4871 spin_unlock(&root->inode_lock);
4872 if (empty)
4873 btrfs_add_dead_root(root);
4874 }
4875}
4876
Jeff Mahoney143bede2012-03-01 14:56:26 +01004877void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004878{
4879 struct rb_node *node;
4880 struct rb_node *prev;
4881 struct btrfs_inode *entry;
4882 struct inode *inode;
4883 u64 objectid = 0;
4884
4885 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4886
4887 spin_lock(&root->inode_lock);
4888again:
4889 node = root->inode_tree.rb_node;
4890 prev = NULL;
4891 while (node) {
4892 prev = node;
4893 entry = rb_entry(node, struct btrfs_inode, rb_node);
4894
Li Zefan33345d012011-04-20 10:31:50 +08004895 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004896 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004897 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004898 node = node->rb_right;
4899 else
4900 break;
4901 }
4902 if (!node) {
4903 while (prev) {
4904 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004905 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004906 node = prev;
4907 break;
4908 }
4909 prev = rb_next(prev);
4910 }
4911 }
4912 while (node) {
4913 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004914 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004915 inode = igrab(&entry->vfs_inode);
4916 if (inode) {
4917 spin_unlock(&root->inode_lock);
4918 if (atomic_read(&inode->i_count) > 1)
4919 d_prune_aliases(inode);
4920 /*
Al Viro45321ac2010-06-07 13:43:19 -04004921 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004922 * the inode cache when its usage count
4923 * hits zero.
4924 */
4925 iput(inode);
4926 cond_resched();
4927 spin_lock(&root->inode_lock);
4928 goto again;
4929 }
4930
4931 if (cond_resched_lock(&root->inode_lock))
4932 goto again;
4933
4934 node = rb_next(node);
4935 }
4936 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004937}
4938
Chris Masone02119d2008-09-05 16:13:11 -04004939static int btrfs_init_locked_inode(struct inode *inode, void *p)
4940{
4941 struct btrfs_iget_args *args = p;
4942 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004943 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004944 return 0;
4945}
4946
4947static int btrfs_find_actor(struct inode *inode, void *opaque)
4948{
4949 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004950 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004951 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004952}
4953
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004954static struct inode *btrfs_iget_locked(struct super_block *s,
4955 u64 objectid,
4956 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004957{
4958 struct inode *inode;
4959 struct btrfs_iget_args args;
4960 args.ino = objectid;
4961 args.root = root;
4962
4963 inode = iget5_locked(s, objectid, btrfs_find_actor,
4964 btrfs_init_locked_inode,
4965 (void *)&args);
4966 return inode;
4967}
4968
Balaji Rao1a54ef82008-07-21 02:01:04 +05304969/* Get an inode object given its location and corresponding root.
4970 * Returns in *is_new if the inode was read from disk
4971 */
4972struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004973 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304974{
4975 struct inode *inode;
4976
4977 inode = btrfs_iget_locked(s, location->objectid, root);
4978 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004979 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304980
4981 if (inode->i_state & I_NEW) {
4982 BTRFS_I(inode)->root = root;
4983 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4984 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004985 if (!is_bad_inode(inode)) {
4986 inode_tree_add(inode);
4987 unlock_new_inode(inode);
4988 if (new)
4989 *new = 1;
4990 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004991 unlock_new_inode(inode);
4992 iput(inode);
4993 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004994 }
4995 }
4996
Balaji Rao1a54ef82008-07-21 02:01:04 +05304997 return inode;
4998}
4999
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005000static struct inode *new_simple_dir(struct super_block *s,
5001 struct btrfs_key *key,
5002 struct btrfs_root *root)
5003{
5004 struct inode *inode = new_inode(s);
5005
5006 if (!inode)
5007 return ERR_PTR(-ENOMEM);
5008
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005009 BTRFS_I(inode)->root = root;
5010 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005011 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005012
5013 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005014 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005015 inode->i_fop = &simple_dir_operations;
5016 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5017 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5018
5019 return inode;
5020}
5021
Chris Mason3de45862008-11-17 21:02:50 -05005022struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005023{
Chris Masond3977122009-01-05 21:25:51 -05005024 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005025 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005026 struct btrfs_root *sub_root = root;
5027 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005028 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005029 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005030
5031 if (dentry->d_name.len > BTRFS_NAME_LEN)
5032 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005033
Jeff Layton39e3c952012-11-28 11:30:53 -05005034 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005035 if (ret < 0)
5036 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005037
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005038 if (location.objectid == 0)
5039 return NULL;
5040
5041 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005042 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005043 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005044 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005045
5046 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5047
Yan, Zheng76dda932009-09-21 16:00:26 -04005048 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005049 ret = fixup_tree_root_location(root, dir, dentry,
5050 &location, &sub_root);
5051 if (ret < 0) {
5052 if (ret != -ENOENT)
5053 inode = ERR_PTR(ret);
5054 else
5055 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5056 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005057 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005058 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005059 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5060
Julia Lawall34d19ba2011-01-24 19:55:19 +00005061 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005062 down_read(&root->fs_info->cleanup_work_sem);
5063 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005064 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005065 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005066 if (ret)
5067 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005068 }
5069
Chris Mason3de45862008-11-17 21:02:50 -05005070 return inode;
5071}
5072
Nick Pigginfe15ce42011-01-07 17:49:23 +11005073static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005074{
5075 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005076 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005077
Li Zefan848cce02012-02-21 17:04:28 +08005078 if (!inode && !IS_ROOT(dentry))
5079 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005080
Li Zefan848cce02012-02-21 17:04:28 +08005081 if (inode) {
5082 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005083 if (btrfs_root_refs(&root->root_item) == 0)
5084 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005085
5086 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5087 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005088 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005089 return 0;
5090}
5091
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005092static void btrfs_dentry_release(struct dentry *dentry)
5093{
5094 if (dentry->d_fsdata)
5095 kfree(dentry->d_fsdata);
5096}
5097
Chris Mason3de45862008-11-17 21:02:50 -05005098static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005099 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005100{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005101 struct dentry *ret;
5102
5103 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005104 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005105}
5106
Miao Xie16cdcec2011-04-22 18:12:22 +08005107unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005108 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5109};
5110
David Woodhousecbdf5a22008-08-06 19:42:33 +01005111static int btrfs_real_readdir(struct file *filp, void *dirent,
5112 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005113{
Chris Mason6da6aba2007-12-18 16:15:09 -05005114 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005115 struct btrfs_root *root = BTRFS_I(inode)->root;
5116 struct btrfs_item *item;
5117 struct btrfs_dir_item *di;
5118 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005119 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005120 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005121 struct list_head ins_list;
5122 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005123 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005124 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005125 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005126 unsigned char d_type;
5127 int over = 0;
5128 u32 di_cur;
5129 u32 di_total;
5130 u32 di_len;
5131 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005132 char tmp_name[32];
5133 char *name_ptr;
5134 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005135 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005136
5137 /* FIXME, use a real flag for deciding about the key type */
5138 if (root->fs_info->tree_root == root)
5139 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005140
Chris Mason39544012007-12-12 14:38:19 -05005141 /* special case for "." */
5142 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005143 over = filldir(dirent, ".", 1,
5144 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005145 if (over)
5146 return 0;
5147 filp->f_pos = 1;
5148 }
Chris Mason39544012007-12-12 14:38:19 -05005149 /* special case for .., just use the back ref */
5150 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005151 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005152 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005153 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005154 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005155 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005156 filp->f_pos = 2;
5157 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005158 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005159 if (!path)
5160 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005161
Josef Bacik026fd312011-05-13 10:32:11 -04005162 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005163
Miao Xie16cdcec2011-04-22 18:12:22 +08005164 if (key_type == BTRFS_DIR_INDEX_KEY) {
5165 INIT_LIST_HEAD(&ins_list);
5166 INIT_LIST_HEAD(&del_list);
5167 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5168 }
5169
Chris Mason39279cc2007-06-12 06:35:45 -04005170 btrfs_set_key_type(&key, key_type);
5171 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005172 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005173
Chris Mason39279cc2007-06-12 06:35:45 -04005174 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5175 if (ret < 0)
5176 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005177
5178 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005179 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005180 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005181 if (slot >= btrfs_header_nritems(leaf)) {
5182 ret = btrfs_next_leaf(root, path);
5183 if (ret < 0)
5184 goto err;
5185 else if (ret > 0)
5186 break;
5187 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005188 }
Chris Mason3de45862008-11-17 21:02:50 -05005189
Chris Mason5f39d392007-10-15 16:14:19 -04005190 item = btrfs_item_nr(leaf, slot);
5191 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5192
5193 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005194 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005195 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005196 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005197 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005198 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005199 if (key_type == BTRFS_DIR_INDEX_KEY &&
5200 btrfs_should_delete_dir_index(&del_list,
5201 found_key.offset))
5202 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005203
5204 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005205 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005206
Chris Mason39279cc2007-06-12 06:35:45 -04005207 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5208 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005209 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005210
5211 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005212 struct btrfs_key location;
5213
Josef Bacik22a94d42011-03-16 16:47:17 -04005214 if (verify_dir_item(root, leaf, di))
5215 break;
5216
Chris Mason5f39d392007-10-15 16:14:19 -04005217 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005218 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005219 name_ptr = tmp_name;
5220 } else {
5221 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005222 if (!name_ptr) {
5223 ret = -ENOMEM;
5224 goto err;
5225 }
Chris Mason5f39d392007-10-15 16:14:19 -04005226 }
5227 read_extent_buffer(leaf, name_ptr,
5228 (unsigned long)(di + 1), name_len);
5229
5230 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5231 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005232
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005233
Chris Mason3de45862008-11-17 21:02:50 -05005234 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005235 * skip it.
5236 *
5237 * In contrast to old kernels, we insert the snapshot's
5238 * dir item and dir index after it has been created, so
5239 * we won't find a reference to our own snapshot. We
5240 * still keep the following code for backward
5241 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005242 */
5243 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5244 location.objectid == root->root_key.objectid) {
5245 over = 0;
5246 goto skip;
5247 }
Chris Mason5f39d392007-10-15 16:14:19 -04005248 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005249 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005250 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005251
Chris Mason3de45862008-11-17 21:02:50 -05005252skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005253 if (name_ptr != tmp_name)
5254 kfree(name_ptr);
5255
Chris Mason39279cc2007-06-12 06:35:45 -04005256 if (over)
5257 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005258 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005259 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005260 di_cur += di_len;
5261 di = (struct btrfs_dir_item *)((char *)di + di_len);
5262 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005263next:
5264 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005265 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005266
Miao Xie16cdcec2011-04-22 18:12:22 +08005267 if (key_type == BTRFS_DIR_INDEX_KEY) {
5268 if (is_curr)
5269 filp->f_pos++;
5270 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5271 &ins_list);
5272 if (ret)
5273 goto nopos;
5274 }
5275
David Woodhouse49593bf2008-08-17 17:08:36 +01005276 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005277 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005278 /*
5279 * 32-bit glibc will use getdents64, but then strtol -
5280 * so the last number we can serve is this.
5281 */
5282 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005283 else
5284 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005285nopos:
5286 ret = 0;
5287err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005288 if (key_type == BTRFS_DIR_INDEX_KEY)
5289 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005290 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005291 return ret;
5292}
5293
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005294int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005295{
5296 struct btrfs_root *root = BTRFS_I(inode)->root;
5297 struct btrfs_trans_handle *trans;
5298 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005299 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005300
Josef Bacik72ac3c02012-05-23 14:13:11 -04005301 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005302 return 0;
5303
Liu Bo83eea1f2012-07-10 05:28:39 -06005304 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005305 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005306
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005307 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005308 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005309 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005310 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005311 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005312 if (IS_ERR(trans))
5313 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005314 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005315 }
5316 return ret;
5317}
5318
5319/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005320 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005321 * inode changes. But, it is most likely to find the inode in cache.
5322 * FIXME, needs more benchmarking...there are no reasons other than performance
5323 * to keep or drop this code.
5324 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05005325int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005326{
5327 struct btrfs_root *root = BTRFS_I(inode)->root;
5328 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005329 int ret;
5330
Josef Bacik72ac3c02012-05-23 14:13:11 -04005331 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005332 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005333
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005334 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005335 if (IS_ERR(trans))
5336 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005337
5338 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005339 if (ret && ret == -ENOSPC) {
5340 /* whoops, lets try again with the full transaction */
5341 btrfs_end_transaction(trans, root);
5342 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005343 if (IS_ERR(trans))
5344 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005345
Chris Mason94b60442010-05-26 11:02:00 -04005346 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005347 }
Chris Mason39279cc2007-06-12 06:35:45 -04005348 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005349 if (BTRFS_I(inode)->delayed_node)
5350 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005351
5352 return ret;
5353}
5354
5355/*
5356 * This is a copy of file_update_time. We need this so we can return error on
5357 * ENOSPC for updating the inode in the case of file write and mmap writes.
5358 */
Josef Bacike41f9412012-03-26 09:46:47 -04005359static int btrfs_update_time(struct inode *inode, struct timespec *now,
5360 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005361{
Alexander Block2bc5565282012-06-15 09:49:33 +02005362 struct btrfs_root *root = BTRFS_I(inode)->root;
5363
5364 if (btrfs_root_readonly(root))
5365 return -EROFS;
5366
Josef Bacike41f9412012-03-26 09:46:47 -04005367 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005368 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005369 if (flags & S_CTIME)
5370 inode->i_ctime = *now;
5371 if (flags & S_MTIME)
5372 inode->i_mtime = *now;
5373 if (flags & S_ATIME)
5374 inode->i_atime = *now;
5375 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005376}
5377
Chris Masond352ac62008-09-29 15:18:18 -04005378/*
5379 * find the highest existing sequence number in a directory
5380 * and then set the in-memory index_cnt variable to reflect
5381 * free sequence numbers
5382 */
Josef Bacikaec74772008-07-24 12:12:38 -04005383static int btrfs_set_inode_index_count(struct inode *inode)
5384{
5385 struct btrfs_root *root = BTRFS_I(inode)->root;
5386 struct btrfs_key key, found_key;
5387 struct btrfs_path *path;
5388 struct extent_buffer *leaf;
5389 int ret;
5390
Li Zefan33345d012011-04-20 10:31:50 +08005391 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005392 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5393 key.offset = (u64)-1;
5394
5395 path = btrfs_alloc_path();
5396 if (!path)
5397 return -ENOMEM;
5398
5399 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5400 if (ret < 0)
5401 goto out;
5402 /* FIXME: we should be able to handle this */
5403 if (ret == 0)
5404 goto out;
5405 ret = 0;
5406
5407 /*
5408 * MAGIC NUMBER EXPLANATION:
5409 * since we search a directory based on f_pos we have to start at 2
5410 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5411 * else has to start at 2
5412 */
5413 if (path->slots[0] == 0) {
5414 BTRFS_I(inode)->index_cnt = 2;
5415 goto out;
5416 }
5417
5418 path->slots[0]--;
5419
5420 leaf = path->nodes[0];
5421 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5422
Li Zefan33345d012011-04-20 10:31:50 +08005423 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005424 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5425 BTRFS_I(inode)->index_cnt = 2;
5426 goto out;
5427 }
5428
5429 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5430out:
5431 btrfs_free_path(path);
5432 return ret;
5433}
5434
Chris Masond352ac62008-09-29 15:18:18 -04005435/*
5436 * helper to find a free sequence number in a given directory. This current
5437 * code is very simple, later versions will do smarter things in the btree
5438 */
Chris Mason3de45862008-11-17 21:02:50 -05005439int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005440{
5441 int ret = 0;
5442
5443 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005444 ret = btrfs_inode_delayed_dir_index_count(dir);
5445 if (ret) {
5446 ret = btrfs_set_inode_index_count(dir);
5447 if (ret)
5448 return ret;
5449 }
Josef Bacikaec74772008-07-24 12:12:38 -04005450 }
5451
Chris Mason00e4e6b2008-08-05 11:18:09 -04005452 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005453 BTRFS_I(dir)->index_cnt++;
5454
5455 return ret;
5456}
5457
Chris Mason39279cc2007-06-12 06:35:45 -04005458static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5459 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005460 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005461 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005462 u64 ref_objectid, u64 objectid,
5463 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005464{
5465 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005466 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005467 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005468 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005469 struct btrfs_inode_ref *ref;
5470 struct btrfs_key key[2];
5471 u32 sizes[2];
5472 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005473 int ret;
5474 int owner;
5475
Chris Mason5f39d392007-10-15 16:14:19 -04005476 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005477 if (!path)
5478 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005479
Chris Mason39279cc2007-06-12 06:35:45 -04005480 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005481 if (!inode) {
5482 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005483 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005484 }
Chris Mason39279cc2007-06-12 06:35:45 -04005485
Li Zefan581bb052011-04-20 10:06:11 +08005486 /*
5487 * we have to initialize this early, so we can reclaim the inode
5488 * number if we fail afterwards in this function.
5489 */
5490 inode->i_ino = objectid;
5491
Josef Bacikaec74772008-07-24 12:12:38 -04005492 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005493 trace_btrfs_inode_request(dir);
5494
Chris Mason3de45862008-11-17 21:02:50 -05005495 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005496 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005497 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005498 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005499 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005500 }
Josef Bacikaec74772008-07-24 12:12:38 -04005501 }
5502 /*
5503 * index_cnt is ignored for everything but a dir,
5504 * btrfs_get_inode_index_count has an explanation for the magic
5505 * number
5506 */
5507 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005508 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005509 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005510 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005511
Josef Bacik5dc562c2012-08-17 13:14:17 -04005512 /*
5513 * We could have gotten an inode number from somebody who was fsynced
5514 * and then removed in this same transaction, so let's just set full
5515 * sync since it will be a full sync anyway and this will blow away the
5516 * old info in the log.
5517 */
5518 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5519
Al Viro569254b2011-07-24 17:08:40 -04005520 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005521 owner = 0;
5522 else
5523 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005524
5525 key[0].objectid = objectid;
5526 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5527 key[0].offset = 0;
5528
Mark Fashehf1863732012-08-08 11:32:27 -07005529 /*
5530 * Start new inodes with an inode_ref. This is slightly more
5531 * efficient for small numbers of hard links since they will
5532 * be packed into one item. Extended refs will kick in if we
5533 * add more hard links than can fit in the ref item.
5534 */
Chris Mason9c583092008-01-29 15:15:18 -05005535 key[1].objectid = objectid;
5536 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5537 key[1].offset = ref_objectid;
5538
5539 sizes[0] = sizeof(struct btrfs_inode_item);
5540 sizes[1] = name_len + sizeof(*ref);
5541
Chris Masonb9473432009-03-13 11:00:37 -04005542 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005543 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5544 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005545 goto fail;
5546
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005547 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005548 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005549 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005550 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5551 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005552 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5553 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005554 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005555
5556 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5557 struct btrfs_inode_ref);
5558 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005559 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005560 ptr = (unsigned long)(ref + 1);
5561 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5562
Chris Mason5f39d392007-10-15 16:14:19 -04005563 btrfs_mark_buffer_dirty(path->nodes[0]);
5564 btrfs_free_path(path);
5565
Chris Mason39279cc2007-06-12 06:35:45 -04005566 location = &BTRFS_I(inode)->location;
5567 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005568 location->offset = 0;
5569 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5570
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005571 btrfs_inherit_iflags(inode, dir);
5572
Al Viro569254b2011-07-24 17:08:40 -04005573 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005574 if (btrfs_test_opt(root, NODATASUM))
5575 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005576 if (btrfs_test_opt(root, NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04005577 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5578 }
5579
Chris Mason39279cc2007-06-12 06:35:45 -04005580 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005581 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005582
5583 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005584 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005585
Alexander Block8ea05e32012-07-25 17:35:53 +02005586 btrfs_update_root_times(trans, root);
5587
Chris Mason39279cc2007-06-12 06:35:45 -04005588 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005589fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005590 if (dir)
5591 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005592 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005593 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005594 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005595}
5596
5597static inline u8 btrfs_inode_type(struct inode *inode)
5598{
5599 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5600}
5601
Chris Masond352ac62008-09-29 15:18:18 -04005602/*
5603 * utility function to add 'inode' into 'parent_inode' with
5604 * a give name and a given sequence number.
5605 * if 'add_backref' is true, also insert a backref from the
5606 * inode to the parent directory.
5607 */
Chris Masone02119d2008-09-05 16:13:11 -04005608int btrfs_add_link(struct btrfs_trans_handle *trans,
5609 struct inode *parent_inode, struct inode *inode,
5610 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005611{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005612 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005613 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005614 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005615 u64 ino = btrfs_ino(inode);
5616 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005617
Li Zefan33345d012011-04-20 10:31:50 +08005618 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005619 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5620 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005621 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005622 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5623 key.offset = 0;
5624 }
Chris Mason39279cc2007-06-12 06:35:45 -04005625
Li Zefan33345d012011-04-20 10:31:50 +08005626 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005627 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5628 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005629 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005630 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005631 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5632 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005633 }
5634
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005635 /* Nothing to clean up yet */
5636 if (ret)
5637 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005639 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5640 parent_inode, &key,
5641 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005642 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005643 goto fail_dir_item;
5644 else if (ret) {
5645 btrfs_abort_transaction(trans, root, ret);
5646 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005647 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005648
5649 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5650 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005651 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005652 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5653 ret = btrfs_update_inode(trans, root, parent_inode);
5654 if (ret)
5655 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005656 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005657
5658fail_dir_item:
5659 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5660 u64 local_index;
5661 int err;
5662 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5663 key.objectid, root->root_key.objectid,
5664 parent_ino, &local_index, name, name_len);
5665
5666 } else if (add_backref) {
5667 u64 local_index;
5668 int err;
5669
5670 err = btrfs_del_inode_ref(trans, root, name, name_len,
5671 ino, parent_ino, &local_index);
5672 }
5673 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005674}
5675
5676static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005677 struct inode *dir, struct dentry *dentry,
5678 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005679{
Josef Bacika1b075d2010-11-19 20:36:11 +00005680 int err = btrfs_add_link(trans, dir, inode,
5681 dentry->d_name.name, dentry->d_name.len,
5682 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005683 if (err > 0)
5684 err = -EEXIST;
5685 return err;
5686}
5687
Josef Bacik618e21d2007-07-11 10:18:17 -04005688static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005689 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005690{
5691 struct btrfs_trans_handle *trans;
5692 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005693 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005694 int err;
5695 int drop_inode = 0;
5696 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005697 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005698
5699 if (!new_valid_dev(rdev))
5700 return -EINVAL;
5701
Josef Bacik9ed74f22009-09-11 16:12:44 -04005702 /*
5703 * 2 for inode item and ref
5704 * 2 for dir items
5705 * 1 for xattr if selinux is on
5706 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005707 trans = btrfs_start_transaction(root, 5);
5708 if (IS_ERR(trans))
5709 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005710
Li Zefan581bb052011-04-20 10:06:11 +08005711 err = btrfs_find_free_ino(root, &objectid);
5712 if (err)
5713 goto out_unlock;
5714
Josef Bacikaec74772008-07-24 12:12:38 -04005715 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005716 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005717 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005718 if (IS_ERR(inode)) {
5719 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005720 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005721 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005722
Eric Paris2a7dba32011-02-01 11:05:39 -05005723 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005724 if (err) {
5725 drop_inode = 1;
5726 goto out_unlock;
5727 }
5728
Casey Schauflerad19db72011-12-15 10:09:07 -05005729 /*
5730 * If the active LSM wants to access the inode during
5731 * d_instantiate it needs these. Smack checks to see
5732 * if the filesystem supports xattrs by looking at the
5733 * ops vector.
5734 */
5735
5736 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005737 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005738 if (err)
5739 drop_inode = 1;
5740 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005741 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005742 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005743 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005744 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005745out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005746 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005747 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005748 if (drop_inode) {
5749 inode_dec_link_count(inode);
5750 iput(inode);
5751 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005752 return err;
5753}
5754
Chris Mason39279cc2007-06-12 06:35:45 -04005755static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005756 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005757{
5758 struct btrfs_trans_handle *trans;
5759 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005760 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005761 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005762 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005763 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005764 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005765
Josef Bacik9ed74f22009-09-11 16:12:44 -04005766 /*
5767 * 2 for inode item and ref
5768 * 2 for dir items
5769 * 1 for xattr if selinux is on
5770 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005771 trans = btrfs_start_transaction(root, 5);
5772 if (IS_ERR(trans))
5773 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005774
Li Zefan581bb052011-04-20 10:06:11 +08005775 err = btrfs_find_free_ino(root, &objectid);
5776 if (err)
5777 goto out_unlock;
5778
Josef Bacikaec74772008-07-24 12:12:38 -04005779 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005780 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005781 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005782 if (IS_ERR(inode)) {
5783 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005784 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005785 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005786 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005787
Eric Paris2a7dba32011-02-01 11:05:39 -05005788 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005789 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005790 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005791
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005792 err = btrfs_update_inode(trans, root, inode);
5793 if (err)
5794 goto out_unlock;
5795
Casey Schauflerad19db72011-12-15 10:09:07 -05005796 /*
5797 * If the active LSM wants to access the inode during
5798 * d_instantiate it needs these. Smack checks to see
5799 * if the filesystem supports xattrs by looking at the
5800 * ops vector.
5801 */
5802 inode->i_fop = &btrfs_file_operations;
5803 inode->i_op = &btrfs_file_inode_operations;
5804
Josef Bacika1b075d2010-11-19 20:36:11 +00005805 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005806 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005807 goto out_unlock;
5808
5809 inode->i_mapping->a_ops = &btrfs_aops;
5810 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5811 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5812 d_instantiate(dentry, inode);
5813
Chris Mason39279cc2007-06-12 06:35:45 -04005814out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005815 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005816 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005817 inode_dec_link_count(inode);
5818 iput(inode);
5819 }
Liu Bob53d3f52012-11-14 14:34:34 +00005820 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005821 return err;
5822}
5823
5824static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5825 struct dentry *dentry)
5826{
5827 struct btrfs_trans_handle *trans;
5828 struct btrfs_root *root = BTRFS_I(dir)->root;
5829 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005830 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005831 int err;
5832 int drop_inode = 0;
5833
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005834 /* do not allow sys_link's with other subvols of the same device */
5835 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005836 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005837
Mark Fashehf1863732012-08-08 11:32:27 -07005838 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005839 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005840
Chris Mason3de45862008-11-17 21:02:50 -05005841 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005842 if (err)
5843 goto fail;
5844
Yan, Zhenga22285a2010-05-16 10:48:46 -04005845 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005846 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005847 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005848 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005849 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005850 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005851 if (IS_ERR(trans)) {
5852 err = PTR_ERR(trans);
5853 goto fail;
5854 }
Chris Mason5f39d392007-10-15 16:14:19 -04005855
Miao Xie31534952011-04-13 13:19:21 +08005856 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005857 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005858 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005859 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005860 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005861
Josef Bacika1b075d2010-11-19 20:36:11 +00005862 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005863
Yan, Zhenga5719522009-09-24 09:17:31 -04005864 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005865 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005866 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005867 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005868 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005869 if (err)
5870 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005871 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005872 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005873 }
Chris Mason39279cc2007-06-12 06:35:45 -04005874
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005875 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005876fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005877 if (drop_inode) {
5878 inode_dec_link_count(inode);
5879 iput(inode);
5880 }
Liu Bob53d3f52012-11-14 14:34:34 +00005881 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005882 return err;
5883}
5884
Al Viro18bb1db2011-07-26 01:41:39 -04005885static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005886{
Chris Masonb9d86662008-05-02 16:13:49 -04005887 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005888 struct btrfs_trans_handle *trans;
5889 struct btrfs_root *root = BTRFS_I(dir)->root;
5890 int err = 0;
5891 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005892 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005893 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005894
Josef Bacik9ed74f22009-09-11 16:12:44 -04005895 /*
5896 * 2 items for inode and ref
5897 * 2 items for dir items
5898 * 1 for xattr if selinux is on
5899 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005900 trans = btrfs_start_transaction(root, 5);
5901 if (IS_ERR(trans))
5902 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005903
Li Zefan581bb052011-04-20 10:06:11 +08005904 err = btrfs_find_free_ino(root, &objectid);
5905 if (err)
5906 goto out_fail;
5907
Josef Bacikaec74772008-07-24 12:12:38 -04005908 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005909 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005910 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005911 if (IS_ERR(inode)) {
5912 err = PTR_ERR(inode);
5913 goto out_fail;
5914 }
Chris Mason5f39d392007-10-15 16:14:19 -04005915
Chris Mason39279cc2007-06-12 06:35:45 -04005916 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005917
Eric Paris2a7dba32011-02-01 11:05:39 -05005918 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005919 if (err)
5920 goto out_fail;
5921
Chris Mason39279cc2007-06-12 06:35:45 -04005922 inode->i_op = &btrfs_dir_inode_operations;
5923 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005924
Chris Masondbe674a2008-07-17 12:54:05 -04005925 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005926 err = btrfs_update_inode(trans, root, inode);
5927 if (err)
5928 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005929
Josef Bacika1b075d2010-11-19 20:36:11 +00005930 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5931 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005932 if (err)
5933 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005934
Chris Mason39279cc2007-06-12 06:35:45 -04005935 d_instantiate(dentry, inode);
5936 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005937
5938out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005939 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005940 if (drop_on_err)
5941 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005942 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005943 return err;
5944}
5945
Chris Masond352ac62008-09-29 15:18:18 -04005946/* helper for btfs_get_extent. Given an existing extent in the tree,
5947 * and an extent that you want to insert, deal with overlap and insert
5948 * the new extent into the tree.
5949 */
Chris Mason3b951512008-04-17 11:29:12 -04005950static int merge_extent_mapping(struct extent_map_tree *em_tree,
5951 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005952 struct extent_map *em,
5953 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005954{
5955 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005956
Chris Masone6dcd2d2008-07-17 12:53:50 -04005957 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5958 start_diff = map_start - em->start;
5959 em->start = map_start;
5960 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005961 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5962 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005963 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005964 em->block_len -= start_diff;
5965 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005966 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005967}
5968
Chris Masonc8b97812008-10-29 14:49:59 -04005969static noinline int uncompress_inline(struct btrfs_path *path,
5970 struct inode *inode, struct page *page,
5971 size_t pg_offset, u64 extent_offset,
5972 struct btrfs_file_extent_item *item)
5973{
5974 int ret;
5975 struct extent_buffer *leaf = path->nodes[0];
5976 char *tmp;
5977 size_t max_size;
5978 unsigned long inline_size;
5979 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005980 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005981
5982 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005983 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005984 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5985 inline_size = btrfs_file_extent_inline_item_len(leaf,
5986 btrfs_item_nr(leaf, path->slots[0]));
5987 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005988 if (!tmp)
5989 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005990 ptr = btrfs_file_extent_inline_start(item);
5991
5992 read_extent_buffer(leaf, tmp, ptr, inline_size);
5993
Chris Mason5b050f02008-11-11 09:34:41 -05005994 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005995 ret = btrfs_decompress(compress_type, tmp, page,
5996 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005997 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005998 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005999 unsigned long copy_size = min_t(u64,
6000 PAGE_CACHE_SIZE - pg_offset,
6001 max_size - extent_offset);
6002 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006003 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006004 }
6005 kfree(tmp);
6006 return 0;
6007}
6008
Chris Masond352ac62008-09-29 15:18:18 -04006009/*
6010 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006011 * the ugly parts come from merging extents from the disk with the in-ram
6012 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006013 * where the in-ram extents might be locked pending data=ordered completion.
6014 *
6015 * This also copies inline extents directly into the page.
6016 */
Chris Masond3977122009-01-05 21:25:51 -05006017
Chris Masona52d9a82007-08-27 16:49:44 -04006018struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006019 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006020 int create)
6021{
6022 int ret;
6023 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006024 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006025 u64 extent_start = 0;
6026 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006027 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006028 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006029 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006030 struct btrfs_root *root = BTRFS_I(inode)->root;
6031 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006032 struct extent_buffer *leaf;
6033 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006034 struct extent_map *em = NULL;
6035 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006036 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006037 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006038 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006039
Chris Masona52d9a82007-08-27 16:49:44 -04006040again:
Chris Mason890871b2009-09-02 16:24:52 -04006041 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006042 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006043 if (em)
6044 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006045 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006046
Chris Masona52d9a82007-08-27 16:49:44 -04006047 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006048 if (em->start > start || em->start + em->len <= start)
6049 free_extent_map(em);
6050 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006051 free_extent_map(em);
6052 else
6053 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006054 }
David Sterba172ddd62011-04-21 00:48:27 +02006055 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006056 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006057 err = -ENOMEM;
6058 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006059 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006060 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006061 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006062 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006063 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006064 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006065
6066 if (!path) {
6067 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006068 if (!path) {
6069 err = -ENOMEM;
6070 goto out;
6071 }
6072 /*
6073 * Chances are we'll be called again, so go ahead and do
6074 * readahead
6075 */
6076 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006077 }
6078
Chris Mason179e29e2007-11-01 11:28:41 -04006079 ret = btrfs_lookup_file_extent(trans, root, path,
6080 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006081 if (ret < 0) {
6082 err = ret;
6083 goto out;
6084 }
6085
6086 if (ret != 0) {
6087 if (path->slots[0] == 0)
6088 goto not_found;
6089 path->slots[0]--;
6090 }
6091
Chris Mason5f39d392007-10-15 16:14:19 -04006092 leaf = path->nodes[0];
6093 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006094 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006095 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006096 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6097 found_type = btrfs_key_type(&found_key);
6098 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006099 found_type != BTRFS_EXTENT_DATA_KEY) {
6100 goto not_found;
6101 }
6102
Chris Mason5f39d392007-10-15 16:14:19 -04006103 found_type = btrfs_file_extent_type(leaf, item);
6104 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006105 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006106 if (found_type == BTRFS_FILE_EXTENT_REG ||
6107 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006108 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006109 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006110 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6111 size_t size;
6112 size = btrfs_file_extent_inline_len(leaf, item);
6113 extent_end = (extent_start + size + root->sectorsize - 1) &
6114 ~((u64)root->sectorsize - 1);
6115 }
6116
6117 if (start >= extent_end) {
6118 path->slots[0]++;
6119 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6120 ret = btrfs_next_leaf(root, path);
6121 if (ret < 0) {
6122 err = ret;
6123 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006124 }
Yan Zheng9036c102008-10-30 14:19:41 -04006125 if (ret > 0)
6126 goto not_found;
6127 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006128 }
Yan Zheng9036c102008-10-30 14:19:41 -04006129 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6130 if (found_key.objectid != objectid ||
6131 found_key.type != BTRFS_EXTENT_DATA_KEY)
6132 goto not_found;
6133 if (start + len <= found_key.offset)
6134 goto not_found;
6135 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006136 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006137 em->len = found_key.offset - start;
6138 goto not_found_em;
6139 }
6140
Yan Zhengd899e052008-10-30 14:25:28 -04006141 if (found_type == BTRFS_FILE_EXTENT_REG ||
6142 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006143 em->start = extent_start;
6144 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006145 em->orig_start = extent_start -
6146 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006147 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6148 item);
Chris Masondb945352007-10-15 16:15:53 -04006149 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6150 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006151 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006152 goto insert;
6153 }
Li Zefan261507a02010-12-17 14:21:50 +08006154 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006155 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006156 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006157 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006158 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006159 } else {
6160 bytenr += btrfs_file_extent_offset(leaf, item);
6161 em->block_start = bytenr;
6162 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006163 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6164 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006165 }
Chris Masona52d9a82007-08-27 16:49:44 -04006166 goto insert;
6167 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006168 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006169 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006170 size_t size;
6171 size_t extent_offset;
6172 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006173
Chris Masona52d9a82007-08-27 16:49:44 -04006174 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006175 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006176 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006177 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006178 goto out;
6179 }
6180
Yan Zheng9036c102008-10-30 14:19:41 -04006181 size = btrfs_file_extent_inline_len(leaf, item);
6182 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006183 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006184 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006185 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05006186 em->len = (copy_size + root->sectorsize - 1) &
6187 ~((u64)root->sectorsize - 1);
Josef Bacikb4939682012-12-03 10:31:19 -05006188 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006189 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006190 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006191 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006192 em->compress_type = compress_type;
6193 }
Yan689f9342007-10-29 11:41:07 -04006194 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006195 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006196 if (btrfs_file_extent_compression(leaf, item) !=
6197 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006198 ret = uncompress_inline(path, inode, page,
6199 pg_offset,
6200 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006201 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006202 } else {
6203 map = kmap(page);
6204 read_extent_buffer(leaf, map + pg_offset, ptr,
6205 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006206 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6207 memset(map + pg_offset + copy_size, 0,
6208 PAGE_CACHE_SIZE - pg_offset -
6209 copy_size);
6210 }
Chris Masonc8b97812008-10-29 14:49:59 -04006211 kunmap(page);
6212 }
Chris Mason179e29e2007-11-01 11:28:41 -04006213 flush_dcache_page(page);
6214 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006215 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006216 if (!trans) {
6217 kunmap(page);
6218 free_extent_map(em);
6219 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006220
David Sterbab3b4aa72011-04-21 01:20:15 +02006221 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006222 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006223
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006224 if (IS_ERR(trans))
6225 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006226 goto again;
6227 }
Chris Masonc8b97812008-10-29 14:49:59 -04006228 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006229 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006230 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006231 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006232 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006233 }
Chris Masond1310b22008-01-24 16:13:08 -05006234 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006235 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006236 goto insert;
6237 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006238 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006239 }
6240not_found:
6241 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006242 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006243 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006244not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006245 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006246 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006247insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006248 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006249 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05006250 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
6251 "[%llu %llu]\n", (unsigned long long)em->start,
6252 (unsigned long long)em->len,
6253 (unsigned long long)start,
6254 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006255 err = -EIO;
6256 goto out;
6257 }
Chris Masond1310b22008-01-24 16:13:08 -05006258
6259 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006260 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006261 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04006262 /* it is possible that someone inserted the extent into the tree
6263 * while we had the lock dropped. It is also possible that
6264 * an overlapping map exists in the tree
6265 */
Chris Masona52d9a82007-08-27 16:49:44 -04006266 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006267 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006268
6269 ret = 0;
6270
Chris Mason3b951512008-04-17 11:29:12 -04006271 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006272 if (existing && (existing->start > start ||
6273 existing->start + existing->len <= start)) {
6274 free_extent_map(existing);
6275 existing = NULL;
6276 }
Chris Mason3b951512008-04-17 11:29:12 -04006277 if (!existing) {
6278 existing = lookup_extent_mapping(em_tree, em->start,
6279 em->len);
6280 if (existing) {
6281 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006282 em, start,
6283 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006284 free_extent_map(existing);
6285 if (err) {
6286 free_extent_map(em);
6287 em = NULL;
6288 }
6289 } else {
6290 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006291 free_extent_map(em);
6292 em = NULL;
6293 }
6294 } else {
6295 free_extent_map(em);
6296 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006297 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006298 }
Chris Masona52d9a82007-08-27 16:49:44 -04006299 }
Chris Mason890871b2009-09-02 16:24:52 -04006300 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006301out:
liubo1abe9b82011-03-24 11:18:59 +00006302
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006303 if (em)
6304 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006305
Chris Masonf4219502008-07-22 11:18:09 -04006306 if (path)
6307 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006308 if (trans) {
6309 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006310 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006311 err = ret;
6312 }
Chris Masona52d9a82007-08-27 16:49:44 -04006313 if (err) {
6314 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006315 return ERR_PTR(err);
6316 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006317 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006318 return em;
6319}
6320
Chris Masonec29ed52011-02-23 16:23:20 -05006321struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6322 size_t pg_offset, u64 start, u64 len,
6323 int create)
6324{
6325 struct extent_map *em;
6326 struct extent_map *hole_em = NULL;
6327 u64 range_start = start;
6328 u64 end;
6329 u64 found;
6330 u64 found_end;
6331 int err = 0;
6332
6333 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6334 if (IS_ERR(em))
6335 return em;
6336 if (em) {
6337 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006338 * if our em maps to
6339 * - a hole or
6340 * - a pre-alloc extent,
6341 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006342 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006343 if (em->block_start != EXTENT_MAP_HOLE &&
6344 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006345 return em;
6346 else
6347 hole_em = em;
6348 }
6349
6350 /* check to see if we've wrapped (len == -1 or similar) */
6351 end = start + len;
6352 if (end < start)
6353 end = (u64)-1;
6354 else
6355 end -= 1;
6356
6357 em = NULL;
6358
6359 /* ok, we didn't find anything, lets look for delalloc */
6360 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6361 end, len, EXTENT_DELALLOC, 1);
6362 found_end = range_start + found;
6363 if (found_end < range_start)
6364 found_end = (u64)-1;
6365
6366 /*
6367 * we didn't find anything useful, return
6368 * the original results from get_extent()
6369 */
6370 if (range_start > end || found_end <= start) {
6371 em = hole_em;
6372 hole_em = NULL;
6373 goto out;
6374 }
6375
6376 /* adjust the range_start to make sure it doesn't
6377 * go backwards from the start they passed in
6378 */
6379 range_start = max(start,range_start);
6380 found = found_end - range_start;
6381
6382 if (found > 0) {
6383 u64 hole_start = start;
6384 u64 hole_len = len;
6385
David Sterba172ddd62011-04-21 00:48:27 +02006386 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006387 if (!em) {
6388 err = -ENOMEM;
6389 goto out;
6390 }
6391 /*
6392 * when btrfs_get_extent can't find anything it
6393 * returns one huge hole
6394 *
6395 * make sure what it found really fits our range, and
6396 * adjust to make sure it is based on the start from
6397 * the caller
6398 */
6399 if (hole_em) {
6400 u64 calc_end = extent_map_end(hole_em);
6401
6402 if (calc_end <= start || (hole_em->start > end)) {
6403 free_extent_map(hole_em);
6404 hole_em = NULL;
6405 } else {
6406 hole_start = max(hole_em->start, start);
6407 hole_len = calc_end - hole_start;
6408 }
6409 }
6410 em->bdev = NULL;
6411 if (hole_em && range_start > hole_start) {
6412 /* our hole starts before our delalloc, so we
6413 * have to return just the parts of the hole
6414 * that go until the delalloc starts
6415 */
6416 em->len = min(hole_len,
6417 range_start - hole_start);
6418 em->start = hole_start;
6419 em->orig_start = hole_start;
6420 /*
6421 * don't adjust block start at all,
6422 * it is fixed at EXTENT_MAP_HOLE
6423 */
6424 em->block_start = hole_em->block_start;
6425 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006426 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6427 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006428 } else {
6429 em->start = range_start;
6430 em->len = found;
6431 em->orig_start = range_start;
6432 em->block_start = EXTENT_MAP_DELALLOC;
6433 em->block_len = found;
6434 }
6435 } else if (hole_em) {
6436 return hole_em;
6437 }
6438out:
6439
6440 free_extent_map(hole_em);
6441 if (err) {
6442 free_extent_map(em);
6443 return ERR_PTR(err);
6444 }
6445 return em;
6446}
6447
Josef Bacik4b46fce2010-05-23 11:00:55 -04006448static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6449 u64 start, u64 len)
6450{
6451 struct btrfs_root *root = BTRFS_I(inode)->root;
6452 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006453 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006454 struct btrfs_key ins;
6455 u64 alloc_hint;
6456 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006457
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006458 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006459 if (IS_ERR(trans))
6460 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006461
6462 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6463
6464 alloc_hint = get_extent_allocation_hint(inode, start, len);
6465 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006466 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006467 if (ret) {
6468 em = ERR_PTR(ret);
6469 goto out;
6470 }
6471
Josef Bacik70c8a912012-10-11 16:54:30 -04006472 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
6473 ins.offset, ins.offset, 0);
6474 if (IS_ERR(em))
6475 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006476
6477 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6478 ins.offset, ins.offset, 0);
6479 if (ret) {
6480 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6481 em = ERR_PTR(ret);
6482 }
6483out:
6484 btrfs_end_transaction(trans, root);
6485 return em;
6486}
6487
Chris Mason46bfbb52010-05-26 11:04:10 -04006488/*
6489 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6490 * block must be cow'd
6491 */
6492static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
6493 struct inode *inode, u64 offset, u64 len)
6494{
6495 struct btrfs_path *path;
6496 int ret;
6497 struct extent_buffer *leaf;
6498 struct btrfs_root *root = BTRFS_I(inode)->root;
6499 struct btrfs_file_extent_item *fi;
6500 struct btrfs_key key;
6501 u64 disk_bytenr;
6502 u64 backref_offset;
6503 u64 extent_end;
6504 u64 num_bytes;
6505 int slot;
6506 int found_type;
6507
6508 path = btrfs_alloc_path();
6509 if (!path)
6510 return -ENOMEM;
6511
Li Zefan33345d012011-04-20 10:31:50 +08006512 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006513 offset, 0);
6514 if (ret < 0)
6515 goto out;
6516
6517 slot = path->slots[0];
6518 if (ret == 1) {
6519 if (slot == 0) {
6520 /* can't find the item, must cow */
6521 ret = 0;
6522 goto out;
6523 }
6524 slot--;
6525 }
6526 ret = 0;
6527 leaf = path->nodes[0];
6528 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006529 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006530 key.type != BTRFS_EXTENT_DATA_KEY) {
6531 /* not our file or wrong item type, must cow */
6532 goto out;
6533 }
6534
6535 if (key.offset > offset) {
6536 /* Wrong offset, must cow */
6537 goto out;
6538 }
6539
6540 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6541 found_type = btrfs_file_extent_type(leaf, fi);
6542 if (found_type != BTRFS_FILE_EXTENT_REG &&
6543 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6544 /* not a regular extent, must cow */
6545 goto out;
6546 }
6547 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6548 backref_offset = btrfs_file_extent_offset(leaf, fi);
6549
6550 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6551 if (extent_end < offset + len) {
6552 /* extent doesn't include our full range, must cow */
6553 goto out;
6554 }
6555
6556 if (btrfs_extent_readonly(root, disk_bytenr))
6557 goto out;
6558
6559 /*
6560 * look for other files referencing this extent, if we
6561 * find any we must cow
6562 */
Li Zefan33345d012011-04-20 10:31:50 +08006563 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006564 key.offset - backref_offset, disk_bytenr))
6565 goto out;
6566
6567 /*
6568 * adjust disk_bytenr and num_bytes to cover just the bytes
6569 * in this extent we are about to write. If there
6570 * are any csums in that range we have to cow in order
6571 * to keep the csums correct
6572 */
6573 disk_bytenr += backref_offset;
6574 disk_bytenr += offset - key.offset;
6575 num_bytes = min(offset + len, extent_end) - offset;
6576 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6577 goto out;
6578 /*
6579 * all of the above have passed, it is safe to overwrite this extent
6580 * without cow
6581 */
6582 ret = 1;
6583out:
6584 btrfs_free_path(path);
6585 return ret;
6586}
6587
Josef Bacikeb838e72012-07-31 16:28:48 -04006588static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6589 struct extent_state **cached_state, int writing)
6590{
6591 struct btrfs_ordered_extent *ordered;
6592 int ret = 0;
6593
6594 while (1) {
6595 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6596 0, cached_state);
6597 /*
6598 * We're concerned with the entire range that we're going to be
6599 * doing DIO to, so we need to make sure theres no ordered
6600 * extents in this range.
6601 */
6602 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6603 lockend - lockstart + 1);
6604
6605 /*
6606 * We need to make sure there are no buffered pages in this
6607 * range either, we could have raced between the invalidate in
6608 * generic_file_direct_write and locking the extent. The
6609 * invalidate needs to happen so that reads after a write do not
6610 * get stale data.
6611 */
6612 if (!ordered && (!writing ||
6613 !test_range_bit(&BTRFS_I(inode)->io_tree,
6614 lockstart, lockend, EXTENT_UPTODATE, 0,
6615 *cached_state)))
6616 break;
6617
6618 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6619 cached_state, GFP_NOFS);
6620
6621 if (ordered) {
6622 btrfs_start_ordered_extent(inode, ordered, 1);
6623 btrfs_put_ordered_extent(ordered);
6624 } else {
6625 /* Screw you mmap */
6626 ret = filemap_write_and_wait_range(inode->i_mapping,
6627 lockstart,
6628 lockend);
6629 if (ret)
6630 break;
6631
6632 /*
6633 * If we found a page that couldn't be invalidated just
6634 * fall back to buffered.
6635 */
6636 ret = invalidate_inode_pages2_range(inode->i_mapping,
6637 lockstart >> PAGE_CACHE_SHIFT,
6638 lockend >> PAGE_CACHE_SHIFT);
6639 if (ret)
6640 break;
6641 }
6642
6643 cond_resched();
6644 }
6645
6646 return ret;
6647}
6648
Josef Bacik69ffb542012-09-11 15:40:07 -04006649static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6650 u64 len, u64 orig_start,
6651 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05006652 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006653{
6654 struct extent_map_tree *em_tree;
6655 struct extent_map *em;
6656 struct btrfs_root *root = BTRFS_I(inode)->root;
6657 int ret;
6658
6659 em_tree = &BTRFS_I(inode)->extent_tree;
6660 em = alloc_extent_map();
6661 if (!em)
6662 return ERR_PTR(-ENOMEM);
6663
6664 em->start = start;
6665 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006666 em->mod_start = start;
6667 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006668 em->len = len;
6669 em->block_len = block_len;
6670 em->block_start = block_start;
6671 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006672 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006673 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006674 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6675 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006676 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006677
6678 do {
6679 btrfs_drop_extent_cache(inode, em->start,
6680 em->start + em->len - 1, 0);
6681 write_lock(&em_tree->lock);
6682 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04006683 if (!ret)
6684 list_move(&em->list,
6685 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006686 write_unlock(&em_tree->lock);
6687 } while (ret == -EEXIST);
6688
6689 if (ret) {
6690 free_extent_map(em);
6691 return ERR_PTR(ret);
6692 }
6693
6694 return em;
6695}
6696
6697
Josef Bacik4b46fce2010-05-23 11:00:55 -04006698static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6699 struct buffer_head *bh_result, int create)
6700{
6701 struct extent_map *em;
6702 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006703 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006704 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006705 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006706 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006707 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006708 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006709 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006710
Josef Bacikeb838e72012-07-31 16:28:48 -04006711 if (create) {
Miao Xie09348562013-02-07 10:12:07 +00006712 spin_lock(&BTRFS_I(inode)->lock);
6713 BTRFS_I(inode)->outstanding_extents++;
6714 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikeb838e72012-07-31 16:28:48 -04006715 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie09348562013-02-07 10:12:07 +00006716 } else
Josef Bacikc3298612012-08-03 16:49:19 -04006717 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006718
Josef Bacikc3298612012-08-03 16:49:19 -04006719 lockstart = start;
6720 lockend = start + len - 1;
6721
Josef Bacikeb838e72012-07-31 16:28:48 -04006722 /*
6723 * If this errors out it's because we couldn't invalidate pagecache for
6724 * this range and we need to fallback to buffered.
6725 */
6726 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6727 return -ENOTBLK;
6728
Josef Bacik4b46fce2010-05-23 11:00:55 -04006729 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006730 if (IS_ERR(em)) {
6731 ret = PTR_ERR(em);
6732 goto unlock_err;
6733 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006734
6735 /*
6736 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6737 * io. INLINE is special, and we could probably kludge it in here, but
6738 * it's still buffered so for safety lets just fall back to the generic
6739 * buffered path.
6740 *
6741 * For COMPRESSED we _have_ to read the entire extent in so we can
6742 * decompress it, so there will be buffering required no matter what we
6743 * do, so go ahead and fallback to buffered.
6744 *
6745 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6746 * to buffered IO. Don't blame me, this is the price we pay for using
6747 * the generic code.
6748 */
6749 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6750 em->block_start == EXTENT_MAP_INLINE) {
6751 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006752 ret = -ENOTBLK;
6753 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006754 }
6755
6756 /* Just a good old fashioned hole, return */
6757 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6758 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6759 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006760 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006761 }
6762
6763 /*
6764 * We don't allocate a new extent in the following cases
6765 *
6766 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6767 * existing extent.
6768 * 2) The extent is marked as PREALLOC. We're good to go here and can
6769 * just use the extent.
6770 *
6771 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006772 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006773 len = min(len, em->len - (start - em->start));
6774 lockstart = start + len;
6775 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006776 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006777
6778 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6779 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6780 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006781 int type;
6782 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006783 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006784
6785 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6786 type = BTRFS_ORDERED_PREALLOC;
6787 else
6788 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006789 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006790 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006791
6792 /*
6793 * we're not going to log anything, but we do need
6794 * to make sure the current transaction stays open
6795 * while we look for nocow cross refs
6796 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006797 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006798 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006799 goto must_cow;
6800
6801 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006802 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006803 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006804
6805 if (type == BTRFS_ORDERED_PREALLOC) {
6806 free_extent_map(em);
6807 em = create_pinned_em(inode, start, len,
6808 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006809 block_start, len,
6810 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006811 if (IS_ERR(em)) {
6812 btrfs_end_transaction(trans, root);
6813 goto unlock_err;
6814 }
6815 }
6816
Chris Mason46bfbb52010-05-26 11:04:10 -04006817 ret = btrfs_add_ordered_extent_dio(inode, start,
6818 block_start, len, len, type);
6819 btrfs_end_transaction(trans, root);
6820 if (ret) {
6821 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006822 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006823 }
6824 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006825 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006826 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006827 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006828must_cow:
6829 /*
6830 * this will cow the extent, reset the len in case we changed
6831 * it above
6832 */
6833 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006834 free_extent_map(em);
6835 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006836 if (IS_ERR(em)) {
6837 ret = PTR_ERR(em);
6838 goto unlock_err;
6839 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006840 len = min(len, em->len - (start - em->start));
6841unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006842 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6843 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006844 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006845 bh_result->b_bdev = em->bdev;
6846 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006847 if (create) {
6848 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6849 set_buffer_new(bh_result);
6850
6851 /*
6852 * Need to update the i_size under the extent lock so buffered
6853 * readers will get the updated i_size when we unlock.
6854 */
6855 if (start + len > i_size_read(inode))
6856 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006857
6858 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6859 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6860 &cached_state, GFP_NOFS);
6861 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006862 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006863
Josef Bacikeb838e72012-07-31 16:28:48 -04006864 /*
6865 * In the case of write we need to clear and unlock the entire range,
6866 * in the case of read we need to unlock only the end area that we
6867 * aren't using if there is any left over space.
6868 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006869 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006870 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6871 lockend, unlock_bits, 1, 0,
6872 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006873 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006874 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006875 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006876
Josef Bacik4b46fce2010-05-23 11:00:55 -04006877 free_extent_map(em);
6878
6879 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006880
6881unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006882 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6883 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6884 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006885}
6886
6887struct btrfs_dio_private {
6888 struct inode *inode;
6889 u64 logical_offset;
6890 u64 disk_bytenr;
6891 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006892 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006893
6894 /* number of bios pending for this dio */
6895 atomic_t pending_bios;
6896
6897 /* IO errors */
6898 int errors;
6899
6900 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006901};
6902
6903static void btrfs_endio_direct_read(struct bio *bio, int err)
6904{
Miao Xiee65e1532010-11-22 03:04:43 +00006905 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006906 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6907 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006908 struct inode *inode = dip->inode;
6909 struct btrfs_root *root = BTRFS_I(inode)->root;
6910 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006911
6912 start = dip->logical_offset;
6913 do {
6914 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6915 struct page *page = bvec->bv_page;
6916 char *kaddr;
6917 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006918 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006919 unsigned long flags;
6920
Josef Bacikc3298612012-08-03 16:49:19 -04006921 if (get_state_private(&BTRFS_I(inode)->io_tree,
6922 start, &private))
6923 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006924 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006925 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006926 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6927 csum, bvec->bv_len);
6928 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006929 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006930 local_irq_restore(flags);
6931
6932 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006933 if (csum != private) {
6934failed:
Li Zefan33345d012011-04-20 10:31:50 +08006935 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006936 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006937 (unsigned long long)btrfs_ino(inode),
6938 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006939 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006940 err = -EIO;
6941 }
6942 }
6943
6944 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006945 bvec++;
6946 } while (bvec <= bvec_end);
6947
6948 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006949 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006950 bio->bi_private = dip->private;
6951
Josef Bacik4b46fce2010-05-23 11:00:55 -04006952 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006953
6954 /* If we had a csum failure make sure to clear the uptodate flag */
6955 if (err)
6956 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006957 dio_end_io(bio, err);
6958}
6959
6960static void btrfs_endio_direct_write(struct bio *bio, int err)
6961{
6962 struct btrfs_dio_private *dip = bio->bi_private;
6963 struct inode *inode = dip->inode;
6964 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006965 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006966 u64 ordered_offset = dip->logical_offset;
6967 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006968 int ret;
6969
6970 if (err)
6971 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006972again:
6973 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6974 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006975 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006976 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006977 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006978
Josef Bacik5fd02042012-05-02 14:00:54 -04006979 ordered->work.func = finish_ordered_fn;
6980 ordered->work.flags = 0;
6981 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6982 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006983out_test:
6984 /*
6985 * our bio might span multiple ordered extents. If we haven't
6986 * completed the accounting for the whole dio, go back and try again
6987 */
6988 if (ordered_offset < dip->logical_offset + dip->bytes) {
6989 ordered_bytes = dip->logical_offset + dip->bytes -
6990 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006991 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006992 goto again;
6993 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006994out_done:
6995 bio->bi_private = dip->private;
6996
Josef Bacik4b46fce2010-05-23 11:00:55 -04006997 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006998
6999 /* If we had an error make sure to clear the uptodate flag */
7000 if (err)
7001 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007002 dio_end_io(bio, err);
7003}
7004
Chris Masoneaf25d92010-05-25 09:48:28 -04007005static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7006 struct bio *bio, int mirror_num,
7007 unsigned long bio_flags, u64 offset)
7008{
7009 int ret;
7010 struct btrfs_root *root = BTRFS_I(inode)->root;
7011 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007012 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007013 return 0;
7014}
7015
Miao Xiee65e1532010-11-22 03:04:43 +00007016static void btrfs_end_dio_bio(struct bio *bio, int err)
7017{
7018 struct btrfs_dio_private *dip = bio->bi_private;
7019
7020 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007021 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007022 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007023 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007024 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007025 dip->errors = 1;
7026
7027 /*
7028 * before atomic variable goto zero, we must make sure
7029 * dip->errors is perceived to be set.
7030 */
7031 smp_mb__before_atomic_dec();
7032 }
7033
7034 /* if there are more bios still pending for this dio, just exit */
7035 if (!atomic_dec_and_test(&dip->pending_bios))
7036 goto out;
7037
7038 if (dip->errors)
7039 bio_io_error(dip->orig_bio);
7040 else {
7041 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
7042 bio_endio(dip->orig_bio, 0);
7043 }
7044out:
7045 bio_put(bio);
7046}
7047
7048static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7049 u64 first_sector, gfp_t gfp_flags)
7050{
7051 int nr_vecs = bio_get_nr_vecs(bdev);
7052 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7053}
7054
7055static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7056 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007057 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007058{
7059 int write = rw & REQ_WRITE;
7060 struct btrfs_root *root = BTRFS_I(inode)->root;
7061 int ret;
7062
Josef Bacikb812ce22012-11-16 13:56:32 -05007063 if (async_submit)
7064 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7065
Miao Xiee65e1532010-11-22 03:04:43 +00007066 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007067
7068 if (!write) {
7069 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7070 if (ret)
7071 goto err;
7072 }
Miao Xiee65e1532010-11-22 03:04:43 +00007073
Josef Bacik1ae39932011-04-06 14:41:34 -04007074 if (skip_sum)
7075 goto map;
7076
7077 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007078 ret = btrfs_wq_submit_bio(root->fs_info,
7079 inode, rw, bio, 0, 0,
7080 file_offset,
7081 __btrfs_submit_bio_start_direct_io,
7082 __btrfs_submit_bio_done);
7083 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007084 } else if (write) {
7085 /*
7086 * If we aren't doing async submit, calculate the csum of the
7087 * bio now.
7088 */
7089 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7090 if (ret)
7091 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007092 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007093 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007094 if (ret)
7095 goto err;
7096 }
Miao Xiee65e1532010-11-22 03:04:43 +00007097
Josef Bacik1ae39932011-04-06 14:41:34 -04007098map:
7099 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007100err:
7101 bio_put(bio);
7102 return ret;
7103}
7104
7105static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7106 int skip_sum)
7107{
7108 struct inode *inode = dip->inode;
7109 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007110 struct bio *bio;
7111 struct bio *orig_bio = dip->orig_bio;
7112 struct bio_vec *bvec = orig_bio->bi_io_vec;
7113 u64 start_sector = orig_bio->bi_sector;
7114 u64 file_offset = dip->logical_offset;
7115 u64 submit_len = 0;
7116 u64 map_length;
7117 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007118 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007119 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007120
Miao Xiee65e1532010-11-22 03:04:43 +00007121 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007122 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007123 &map_length, NULL, 0);
7124 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007125 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007126 return -EIO;
7127 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007128 if (map_length >= orig_bio->bi_size) {
7129 bio = orig_bio;
7130 goto submit;
7131 }
7132
David Woodhouse53b381b2013-01-29 18:40:14 -05007133 /* async crcs make it difficult to collect full stripe writes. */
7134 if (btrfs_get_alloc_profile(root, 1) &
7135 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7136 async_submit = 0;
7137 else
7138 async_submit = 1;
7139
Josef Bacik02f57c72011-04-06 14:25:44 -04007140 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7141 if (!bio)
7142 return -ENOMEM;
7143 bio->bi_private = dip;
7144 bio->bi_end_io = btrfs_end_dio_bio;
7145 atomic_inc(&dip->pending_bios);
7146
Miao Xiee65e1532010-11-22 03:04:43 +00007147 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7148 if (unlikely(map_length < submit_len + bvec->bv_len ||
7149 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7150 bvec->bv_offset) < bvec->bv_len)) {
7151 /*
7152 * inc the count before we submit the bio so
7153 * we know the end IO handler won't happen before
7154 * we inc the count. Otherwise, the dip might get freed
7155 * before we're done setting it up
7156 */
7157 atomic_inc(&dip->pending_bios);
7158 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7159 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007160 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007161 if (ret) {
7162 bio_put(bio);
7163 atomic_dec(&dip->pending_bios);
7164 goto out_err;
7165 }
7166
Miao Xiee65e1532010-11-22 03:04:43 +00007167 start_sector += submit_len >> 9;
7168 file_offset += submit_len;
7169
7170 submit_len = 0;
7171 nr_pages = 0;
7172
7173 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7174 start_sector, GFP_NOFS);
7175 if (!bio)
7176 goto out_err;
7177 bio->bi_private = dip;
7178 bio->bi_end_io = btrfs_end_dio_bio;
7179
7180 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007181 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007182 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007183 &map_length, NULL, 0);
7184 if (ret) {
7185 bio_put(bio);
7186 goto out_err;
7187 }
7188 } else {
7189 submit_len += bvec->bv_len;
7190 nr_pages ++;
7191 bvec++;
7192 }
7193 }
7194
Josef Bacik02f57c72011-04-06 14:25:44 -04007195submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007196 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007197 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007198 if (!ret)
7199 return 0;
7200
7201 bio_put(bio);
7202out_err:
7203 dip->errors = 1;
7204 /*
7205 * before atomic variable goto zero, we must
7206 * make sure dip->errors is perceived to be set.
7207 */
7208 smp_mb__before_atomic_dec();
7209 if (atomic_dec_and_test(&dip->pending_bios))
7210 bio_io_error(dip->orig_bio);
7211
7212 /* bio_end_io() will handle error, so we needn't return it */
7213 return 0;
7214}
7215
Josef Bacik4b46fce2010-05-23 11:00:55 -04007216static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
7217 loff_t file_offset)
7218{
7219 struct btrfs_root *root = BTRFS_I(inode)->root;
7220 struct btrfs_dio_private *dip;
7221 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007222 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007223 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007224 int ret = 0;
7225
7226 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7227
7228 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7229 if (!dip) {
7230 ret = -ENOMEM;
7231 goto free_ordered;
7232 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007233
7234 dip->private = bio->bi_private;
7235 dip->inode = inode;
7236 dip->logical_offset = file_offset;
7237
Josef Bacik4b46fce2010-05-23 11:00:55 -04007238 dip->bytes = 0;
7239 do {
7240 dip->bytes += bvec->bv_len;
7241 bvec++;
7242 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
7243
Chris Mason46bfbb52010-05-26 11:04:10 -04007244 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007245 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007246 dip->errors = 0;
7247 dip->orig_bio = bio;
7248 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249
7250 if (write)
7251 bio->bi_end_io = btrfs_endio_direct_write;
7252 else
7253 bio->bi_end_io = btrfs_endio_direct_read;
7254
Miao Xiee65e1532010-11-22 03:04:43 +00007255 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7256 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007257 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007258free_ordered:
7259 /*
7260 * If this is a write, we need to clean up the reserved space and kill
7261 * the ordered extent.
7262 */
7263 if (write) {
7264 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007265 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7267 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7268 btrfs_free_reserved_extent(root, ordered->start,
7269 ordered->disk_len);
7270 btrfs_put_ordered_extent(ordered);
7271 btrfs_put_ordered_extent(ordered);
7272 }
7273 bio_endio(bio, ret);
7274}
7275
Chris Mason5a5f79b2010-05-26 21:33:37 -04007276static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7277 const struct iovec *iov, loff_t offset,
7278 unsigned long nr_segs)
7279{
7280 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007281 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007282 size_t size;
7283 unsigned long addr;
7284 unsigned blocksize_mask = root->sectorsize - 1;
7285 ssize_t retval = -EINVAL;
7286 loff_t end = offset;
7287
7288 if (offset & blocksize_mask)
7289 goto out;
7290
7291 /* Check the memory alignment. Blocks cannot straddle pages */
7292 for (seg = 0; seg < nr_segs; seg++) {
7293 addr = (unsigned long)iov[seg].iov_base;
7294 size = iov[seg].iov_len;
7295 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007296 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007297 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007298
7299 /* If this is a write we don't need to check anymore */
7300 if (rw & WRITE)
7301 continue;
7302
7303 /*
7304 * Check to make sure we don't have duplicate iov_base's in this
7305 * iovec, if so return EINVAL, otherwise we'll get csum errors
7306 * when reading back.
7307 */
7308 for (i = seg + 1; i < nr_segs; i++) {
7309 if (iov[seg].iov_base == iov[i].iov_base)
7310 goto out;
7311 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007312 }
7313 retval = 0;
7314out:
7315 return retval;
7316}
Josef Bacikeb838e72012-07-31 16:28:48 -04007317
Chris Mason16432982008-04-10 10:23:21 -04007318static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7319 const struct iovec *iov, loff_t offset,
7320 unsigned long nr_segs)
7321{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007322 struct file *file = iocb->ki_filp;
7323 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007324 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007325 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007326 bool wakeup = true;
7327 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007328 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007329
Chris Mason5a5f79b2010-05-26 21:33:37 -04007330 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007331 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007332 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007333
Miao Xie38851cc2013-02-08 07:04:11 +00007334 atomic_inc(&inode->i_dio_count);
7335 smp_mb__after_atomic_inc();
7336
Miao Xie09348562013-02-07 10:12:07 +00007337 if (rw & WRITE) {
7338 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007339 /*
7340 * If the write DIO is beyond the EOF, we need update
7341 * the isize, but it is protected by i_mutex. So we can
7342 * not unlock the i_mutex at this case.
7343 */
7344 if (offset + count <= inode->i_size) {
7345 mutex_unlock(&inode->i_mutex);
7346 relock = true;
7347 }
Miao Xie09348562013-02-07 10:12:07 +00007348 ret = btrfs_delalloc_reserve_space(inode, count);
7349 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007350 goto out;
7351 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7352 &BTRFS_I(inode)->runtime_flags))) {
7353 inode_dio_done(inode);
7354 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7355 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007356 }
7357
7358 ret = __blockdev_direct_IO(rw, iocb, inode,
7359 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7360 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007361 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007362 if (rw & WRITE) {
7363 if (ret < 0 && ret != -EIOCBQUEUED)
7364 btrfs_delalloc_release_space(inode, count);
7365 else if (ret > 0 && (size_t)ret < count) {
7366 spin_lock(&BTRFS_I(inode)->lock);
7367 BTRFS_I(inode)->outstanding_extents++;
7368 spin_unlock(&BTRFS_I(inode)->lock);
7369 btrfs_delalloc_release_space(inode,
7370 count - (size_t)ret);
7371 }
7372 btrfs_delalloc_release_metadata(inode, 0);
7373 }
Miao Xie38851cc2013-02-08 07:04:11 +00007374out:
Miao Xie2e60a512013-02-08 07:01:08 +00007375 if (wakeup)
7376 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007377 if (relock)
7378 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007379
7380 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007381}
7382
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007383#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7384
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007385static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7386 __u64 start, __u64 len)
7387{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007388 int ret;
7389
7390 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7391 if (ret)
7392 return ret;
7393
Chris Masonec29ed52011-02-23 16:23:20 -05007394 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007395}
7396
Chris Mason9ebefb182007-06-15 13:50:00 -04007397int btrfs_readpage(struct file *file, struct page *page)
7398{
Chris Masond1310b22008-01-24 16:13:08 -05007399 struct extent_io_tree *tree;
7400 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007401 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007402}
Chris Mason1832a6d2007-12-21 16:27:21 -05007403
Chris Mason39279cc2007-06-12 06:35:45 -04007404static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7405{
Chris Masond1310b22008-01-24 16:13:08 -05007406 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04007407
7408
7409 if (current->flags & PF_MEMALLOC) {
7410 redirty_page_for_writepage(wbc, page);
7411 unlock_page(page);
7412 return 0;
7413 }
Chris Masond1310b22008-01-24 16:13:08 -05007414 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007415 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7416}
Chris Mason39279cc2007-06-12 06:35:45 -04007417
Chris Masonf4219502008-07-22 11:18:09 -04007418int btrfs_writepages(struct address_space *mapping,
7419 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007420{
Chris Masond1310b22008-01-24 16:13:08 -05007421 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007422
Chris Masond1310b22008-01-24 16:13:08 -05007423 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007424 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7425}
7426
Chris Mason3ab2fb52007-11-08 10:59:22 -05007427static int
7428btrfs_readpages(struct file *file, struct address_space *mapping,
7429 struct list_head *pages, unsigned nr_pages)
7430{
Chris Masond1310b22008-01-24 16:13:08 -05007431 struct extent_io_tree *tree;
7432 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007433 return extent_readpages(tree, mapping, pages, nr_pages,
7434 btrfs_get_extent);
7435}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007436static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007437{
Chris Masond1310b22008-01-24 16:13:08 -05007438 struct extent_io_tree *tree;
7439 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007440 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007441
Chris Masond1310b22008-01-24 16:13:08 -05007442 tree = &BTRFS_I(page->mapping->host)->io_tree;
7443 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007444 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007445 if (ret == 1) {
7446 ClearPagePrivate(page);
7447 set_page_private(page, 0);
7448 page_cache_release(page);
7449 }
7450 return ret;
7451}
Chris Mason39279cc2007-06-12 06:35:45 -04007452
Chris Masone6dcd2d2008-07-17 12:53:50 -04007453static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7454{
Chris Mason98509cf2008-09-11 15:51:43 -04007455 if (PageWriteback(page) || PageDirty(page))
7456 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007457 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007458}
7459
Chris Masona52d9a82007-08-27 16:49:44 -04007460static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7461{
Josef Bacik5fd02042012-05-02 14:00:54 -04007462 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007463 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007464 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007465 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007466 u64 page_start = page_offset(page);
7467 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007468
Chris Mason8b62b722009-09-02 16:53:46 -04007469 /*
7470 * we have the page locked, so new writeback can't start,
7471 * and the dirty bit won't be cleared while we are here.
7472 *
7473 * Wait for IO on this page so that we can safely clear
7474 * the PagePrivate2 bit and do ordered accounting
7475 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007476 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007477
Josef Bacik5fd02042012-05-02 14:00:54 -04007478 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007479 if (offset) {
7480 btrfs_releasepage(page, GFP_NOFS);
7481 return;
7482 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007483 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007484 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007485 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007486 /*
7487 * IO on this page will never be started, so we need
7488 * to account for any ordered extents now
7489 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007490 clear_extent_bit(tree, page_start, page_end,
7491 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007492 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7493 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007494 /*
7495 * whoever cleared the private bit is responsible
7496 * for the finish_ordered_io
7497 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007498 if (TestClearPagePrivate2(page) &&
7499 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7500 PAGE_CACHE_SIZE, 1)) {
7501 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007502 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007503 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007504 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007505 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007506 }
7507 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007508 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007509 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7510 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007511 __btrfs_releasepage(page, GFP_NOFS);
7512
Chris Mason4a096752008-07-21 10:29:44 -04007513 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007514 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007515 ClearPagePrivate(page);
7516 set_page_private(page, 0);
7517 page_cache_release(page);
7518 }
Chris Mason39279cc2007-06-12 06:35:45 -04007519}
7520
Chris Mason9ebefb182007-06-15 13:50:00 -04007521/*
7522 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7523 * called from a page fault handler when a page is first dirtied. Hence we must
7524 * be careful to check for EOF conditions here. We set the page up correctly
7525 * for a written page which means we get ENOSPC checking when writing into
7526 * holes and correct delalloc and unwritten extent mapping on filesystems that
7527 * support these features.
7528 *
7529 * We are not allowed to take the i_mutex here so we have to play games to
7530 * protect against truncate races as the page could now be beyond EOF. Because
7531 * vmtruncate() writes the inode size before removing pages, once we have the
7532 * page lock we can determine safely if the page is beyond EOF. If it is not
7533 * beyond EOF, then the page is guaranteed safe against truncation until we
7534 * unlock the page.
7535 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007536int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007537{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007538 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05007539 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05007540 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007541 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7542 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007543 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007544 char *kaddr;
7545 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007546 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007547 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007548 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007549 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007550 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007551
Jan Karab2b5ef52012-06-12 16:20:45 +02007552 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007553 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007554 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007555 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007556 reserved = 1;
7557 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007558 if (ret) {
7559 if (ret == -ENOMEM)
7560 ret = VM_FAULT_OOM;
7561 else /* -ENOSPC, -EIO, etc */
7562 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007563 if (reserved)
7564 goto out;
7565 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007566 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007567
Nick Piggin56a76f82009-03-31 15:23:23 -07007568 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007569again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007570 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007571 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007572 page_start = page_offset(page);
7573 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007574
Chris Mason9ebefb182007-06-15 13:50:00 -04007575 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007576 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007577 /* page got truncated out from underneath us */
7578 goto out_unlock;
7579 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007580 wait_on_page_writeback(page);
7581
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007582 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007583 set_page_extent_mapped(page);
7584
Chris Masoneb84ae02008-07-17 13:53:27 -04007585 /*
7586 * we can't set the delalloc bits if there are pending ordered
7587 * extents. Drop our locks and wait for them to finish
7588 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007589 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7590 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007591 unlock_extent_cached(io_tree, page_start, page_end,
7592 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007593 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007594 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007595 btrfs_put_ordered_extent(ordered);
7596 goto again;
7597 }
7598
Josef Bacikfbf19082009-10-01 17:10:23 -04007599 /*
7600 * XXX - page_mkwrite gets called every time the page is dirtied, even
7601 * if it was already dirty, so for space accounting reasons we need to
7602 * clear any delalloc bits for the range we are fixing to save. There
7603 * is probably a better way to do this, but for now keep consistent with
7604 * prepare_pages in the normal write path.
7605 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007606 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007607 EXTENT_DIRTY | EXTENT_DELALLOC |
7608 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007609 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007610
Josef Bacik2ac55d42010-02-03 19:33:23 +00007611 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7612 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007613 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007614 unlock_extent_cached(io_tree, page_start, page_end,
7615 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007616 ret = VM_FAULT_SIGBUS;
7617 goto out_unlock;
7618 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007619 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007620
7621 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007622 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007623 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007624 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007625 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007626
Chris Masone6dcd2d2008-07-17 12:53:50 -04007627 if (zero_start != PAGE_CACHE_SIZE) {
7628 kaddr = kmap(page);
7629 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7630 flush_dcache_page(page);
7631 kunmap(page);
7632 }
Chris Mason247e7432008-07-17 12:53:51 -04007633 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007634 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007635 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007636
Chris Mason257c62e2009-10-13 13:21:08 -04007637 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7638 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007639 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007640
Josef Bacik2ac55d42010-02-03 19:33:23 +00007641 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007642
7643out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007644 if (!ret) {
7645 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007646 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007647 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007648 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007649out:
Josef Bacikec39e182012-01-12 19:10:12 -05007650 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007651out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007652 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007653 return ret;
7654}
7655
Josef Bacika41ad392011-01-31 15:30:16 -05007656static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007657{
7658 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007659 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007660 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007661 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007662 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007663 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007664 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007665
Josef Bacik2aaa6652012-08-29 14:27:18 -04007666 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007667 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007668 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007669
Chris Mason4a096752008-07-21 10:29:44 -04007670 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007671 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007672
Josef Bacikfcb80c22011-05-03 10:40:22 -04007673 /*
7674 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7675 * 3 things going on here
7676 *
7677 * 1) We need to reserve space for our orphan item and the space to
7678 * delete our orphan item. Lord knows we don't want to have a dangling
7679 * orphan item because we didn't reserve space to remove it.
7680 *
7681 * 2) We need to reserve space to update our inode.
7682 *
7683 * 3) We need to have something to cache all the space that is going to
7684 * be free'd up by the truncate operation, but also have some slack
7685 * space reserved in case it uses space during the truncate (thank you
7686 * very much snapshotting).
7687 *
7688 * And we need these to all be seperate. The fact is we can use alot of
7689 * space doing the truncate, and we have no earthly idea how much space
7690 * we will use, so we need the truncate reservation to be seperate so it
7691 * doesn't end up using space reserved for updating the inode or
7692 * removing the orphan item. We also need to be able to stop the
7693 * transaction and start a new one, which means we need to be able to
7694 * update the inode several times, and we have no idea of knowing how
7695 * many times that will be, so we can't just reserve 1 item for the
7696 * entirety of the opration, so that has to be done seperately as well.
7697 * Then there is the orphan item, which does indeed need to be held on
7698 * to for the whole operation, and we need nobody to touch this reserved
7699 * space except the orphan code.
7700 *
7701 * So that leaves us with
7702 *
7703 * 1) root->orphan_block_rsv - for the orphan deletion.
7704 * 2) rsv - for the truncate reservation, which we will steal from the
7705 * transaction reservation.
7706 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7707 * updating the inode.
7708 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007709 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007710 if (!rsv)
7711 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007712 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007713 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007714
Josef Bacik907cbce2011-08-08 13:46:15 -04007715 /*
Josef Bacik07127182011-08-19 10:29:59 -04007716 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007717 * 1 for updating the inode.
7718 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007719 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007720 if (IS_ERR(trans)) {
7721 err = PTR_ERR(trans);
7722 goto out;
7723 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007724
Josef Bacik907cbce2011-08-08 13:46:15 -04007725 /* Migrate the slack space for the truncate to our reserve */
7726 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7727 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007728 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007729
Chris Mason5a3f23d2009-03-31 13:27:11 -04007730 /*
7731 * setattr is responsible for setting the ordered_data_close flag,
7732 * but that is only tested during the last file release. That
7733 * could happen well after the next commit, leaving a great big
7734 * window where new writes may get lost if someone chooses to write
7735 * to this file after truncating to zero
7736 *
7737 * The inode doesn't have any dirty data here, and so if we commit
7738 * this is a noop. If someone immediately starts writing to the inode
7739 * it is very likely we'll catch some of their writes in this
7740 * transaction, and the commit will find this file on the ordered
7741 * data list with good things to send down.
7742 *
7743 * This is a best effort solution, there is still a window where
7744 * using truncate to replace the contents of the file will
7745 * end up with a zero length file after a crash.
7746 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007747 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7748 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007749 btrfs_add_ordered_operation(trans, root, inode);
7750
Josef Bacik5dc562c2012-08-17 13:14:17 -04007751 /*
7752 * So if we truncate and then write and fsync we normally would just
7753 * write the extents that changed, which is a problem if we need to
7754 * first truncate that entire inode. So set this flag so we write out
7755 * all of the extents in the inode to the sync log so we're completely
7756 * safe.
7757 */
7758 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007759 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007760
Yan, Zheng80825102009-11-12 09:35:36 +00007761 while (1) {
7762 ret = btrfs_truncate_inode_items(trans, root, inode,
7763 inode->i_size,
7764 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007765 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007766 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007767 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007768 }
Chris Mason39279cc2007-06-12 06:35:45 -04007769
Josef Bacikfcb80c22011-05-03 10:40:22 -04007770 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007771 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007772 if (ret) {
7773 err = ret;
7774 break;
7775 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007776
Yan, Zheng80825102009-11-12 09:35:36 +00007777 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007778 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007779
7780 trans = btrfs_start_transaction(root, 2);
7781 if (IS_ERR(trans)) {
7782 ret = err = PTR_ERR(trans);
7783 trans = NULL;
7784 break;
7785 }
7786
7787 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7788 rsv, min_size);
7789 BUG_ON(ret); /* shouldn't happen */
7790 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007791 }
7792
7793 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007794 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007795 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007796 if (ret)
7797 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007798 }
7799
Chris Mason917c16b2011-11-08 14:49:59 -05007800 if (trans) {
7801 trans->block_rsv = &root->fs_info->trans_block_rsv;
7802 ret = btrfs_update_inode(trans, root, inode);
7803 if (ret && !err)
7804 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007805
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007806 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007807 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007808 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007809
7810out:
7811 btrfs_free_block_rsv(root, rsv);
7812
Josef Bacik3893e332011-01-31 16:03:11 -05007813 if (ret && !err)
7814 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007815
Josef Bacik3893e332011-01-31 16:03:11 -05007816 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007817}
7818
Sven Wegener3b963622008-06-09 21:57:42 -04007819/*
Chris Masond352ac62008-09-29 15:18:18 -04007820 * create a new subvolume directory/inode (helper for the ioctl).
7821 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007822int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007823 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007824{
Chris Mason39279cc2007-06-12 06:35:45 -04007825 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007826 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007827 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007828
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007829 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7830 new_dirid, new_dirid,
7831 S_IFDIR | (~current_umask() & S_IRWXUGO),
7832 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007833 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007834 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007835 inode->i_op = &btrfs_dir_inode_operations;
7836 inode->i_fop = &btrfs_dir_file_operations;
7837
Miklos Szeredibfe86842011-10-28 14:13:29 +02007838 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007839 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007840
Yan, Zheng76dda932009-09-21 16:00:26 -04007841 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007842
Yan, Zheng76dda932009-09-21 16:00:26 -04007843 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007844 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007845}
7846
Chris Mason39279cc2007-06-12 06:35:45 -04007847struct inode *btrfs_alloc_inode(struct super_block *sb)
7848{
7849 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007850 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007851
7852 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7853 if (!ei)
7854 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007855
7856 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007857 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007858 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007859 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007860 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007861 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007862 ei->disk_i_size = 0;
7863 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007864 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007865 ei->index_cnt = (u64)-1;
7866 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007867 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007868
Josef Bacik9e0baf62011-07-15 15:16:44 +00007869 spin_lock_init(&ei->lock);
7870 ei->outstanding_extents = 0;
7871 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007872
Josef Bacik72ac3c02012-05-23 14:13:11 -04007873 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007874 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007875
Miao Xie16cdcec2011-04-22 18:12:22 +08007876 ei->delayed_node = NULL;
7877
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007878 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007879 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007880 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7881 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007882 ei->io_tree.track_uptodate = 1;
7883 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007884 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007885 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007886 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007887 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007888 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007889 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007890 RB_CLEAR_NODE(&ei->rb_node);
7891
7892 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007893}
7894
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007895static void btrfs_i_callback(struct rcu_head *head)
7896{
7897 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007898 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7899}
7900
Chris Mason39279cc2007-06-12 06:35:45 -04007901void btrfs_destroy_inode(struct inode *inode)
7902{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007903 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007904 struct btrfs_root *root = BTRFS_I(inode)->root;
7905
Al Virob3d9b7a2012-06-09 13:51:19 -04007906 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007907 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007908 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7909 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007910 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7911 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007912
Chris Mason5a3f23d2009-03-31 13:27:11 -04007913 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007914 * This can happen where we create an inode, but somebody else also
7915 * created the same inode and we need to destroy the one we already
7916 * created.
7917 */
7918 if (!root)
7919 goto free;
7920
7921 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007922 * Make sure we're properly removed from the ordered operation
7923 * lists.
7924 */
7925 smp_mb();
7926 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7927 spin_lock(&root->fs_info->ordered_extent_lock);
7928 list_del_init(&BTRFS_I(inode)->ordered_operations);
7929 spin_unlock(&root->fs_info->ordered_extent_lock);
7930 }
7931
Josef Bacik8a35d952012-05-23 14:26:42 -04007932 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7933 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007934 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7935 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007936 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007937 }
Josef Bacik7b128762008-07-24 12:17:14 -04007938
Chris Masond3977122009-01-05 21:25:51 -05007939 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007940 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7941 if (!ordered)
7942 break;
7943 else {
Chris Masond3977122009-01-05 21:25:51 -05007944 printk(KERN_ERR "btrfs found ordered "
7945 "extent %llu %llu on inode cleanup\n",
7946 (unsigned long long)ordered->file_offset,
7947 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007948 btrfs_remove_ordered_extent(inode, ordered);
7949 btrfs_put_ordered_extent(ordered);
7950 btrfs_put_ordered_extent(ordered);
7951 }
7952 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007953 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007954 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007955free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007956 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007957 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007958}
7959
Al Viro45321ac2010-06-07 13:43:19 -04007960int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007961{
7962 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007963
Liu Bofa6ac872013-02-20 14:10:23 +00007964 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007965 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007966 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007967 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007968 else
Al Viro45321ac2010-06-07 13:43:19 -04007969 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007970}
7971
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007972static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007973{
7974 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7975
7976 inode_init_once(&ei->vfs_inode);
7977}
7978
7979void btrfs_destroy_cachep(void)
7980{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007981 /*
7982 * Make sure all delayed rcu free inodes are flushed before we
7983 * destroy cache.
7984 */
7985 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007986 if (btrfs_inode_cachep)
7987 kmem_cache_destroy(btrfs_inode_cachep);
7988 if (btrfs_trans_handle_cachep)
7989 kmem_cache_destroy(btrfs_trans_handle_cachep);
7990 if (btrfs_transaction_cachep)
7991 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007992 if (btrfs_path_cachep)
7993 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007994 if (btrfs_free_space_cachep)
7995 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007996 if (btrfs_delalloc_work_cachep)
7997 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007998}
7999
8000int btrfs_init_cachep(void)
8001{
David Sterba837e1972012-09-07 03:00:48 -06008002 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008003 sizeof(struct btrfs_inode), 0,
8004 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008005 if (!btrfs_inode_cachep)
8006 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008007
David Sterba837e1972012-09-07 03:00:48 -06008008 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008009 sizeof(struct btrfs_trans_handle), 0,
8010 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008011 if (!btrfs_trans_handle_cachep)
8012 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008013
David Sterba837e1972012-09-07 03:00:48 -06008014 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008015 sizeof(struct btrfs_transaction), 0,
8016 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008017 if (!btrfs_transaction_cachep)
8018 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008019
David Sterba837e1972012-09-07 03:00:48 -06008020 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008021 sizeof(struct btrfs_path), 0,
8022 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008023 if (!btrfs_path_cachep)
8024 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008025
David Sterba837e1972012-09-07 03:00:48 -06008026 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008027 sizeof(struct btrfs_free_space), 0,
8028 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8029 if (!btrfs_free_space_cachep)
8030 goto fail;
8031
Miao Xie8ccf6f192012-10-25 09:28:04 +00008032 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8033 sizeof(struct btrfs_delalloc_work), 0,
8034 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8035 NULL);
8036 if (!btrfs_delalloc_work_cachep)
8037 goto fail;
8038
Chris Mason39279cc2007-06-12 06:35:45 -04008039 return 0;
8040fail:
8041 btrfs_destroy_cachep();
8042 return -ENOMEM;
8043}
8044
8045static int btrfs_getattr(struct vfsmount *mnt,
8046 struct dentry *dentry, struct kstat *stat)
8047{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008048 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008049 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008050 u32 blocksize = inode->i_sb->s_blocksize;
8051
Chris Mason39279cc2007-06-12 06:35:45 -04008052 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008053 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008054 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008055
8056 spin_lock(&BTRFS_I(inode)->lock);
8057 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8058 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008059 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008060 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008061 return 0;
8062}
8063
Liu Bo75e7cb72011-03-22 10:12:20 +00008064/*
8065 * If a file is moved, it will inherit the cow and compression flags of the new
8066 * directory.
8067 */
8068static void fixup_inode_flags(struct inode *dir, struct inode *inode)
8069{
8070 struct btrfs_inode *b_dir = BTRFS_I(dir);
8071 struct btrfs_inode *b_inode = BTRFS_I(inode);
8072
8073 if (b_dir->flags & BTRFS_INODE_NODATACOW)
8074 b_inode->flags |= BTRFS_INODE_NODATACOW;
8075 else
8076 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
8077
Liu Bobc178232012-06-14 02:23:18 -06008078 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00008079 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06008080 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
8081 } else {
8082 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
8083 BTRFS_INODE_NOCOMPRESS);
8084 }
Liu Bo75e7cb72011-03-22 10:12:20 +00008085}
8086
Chris Masond3977122009-01-05 21:25:51 -05008087static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8088 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008089{
8090 struct btrfs_trans_handle *trans;
8091 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008092 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008093 struct inode *new_inode = new_dentry->d_inode;
8094 struct inode *old_inode = old_dentry->d_inode;
8095 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008096 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008097 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008098 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008099 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008100
Li Zefan33345d012011-04-20 10:31:50 +08008101 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008102 return -EPERM;
8103
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008104 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008105 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008106 return -EXDEV;
8107
Li Zefan33345d012011-04-20 10:31:50 +08008108 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8109 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008110 return -ENOTEMPTY;
8111
Chris Mason39279cc2007-06-12 06:35:45 -04008112 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008113 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008114 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008115
8116
8117 /* check for collisions, even if the name isn't there */
8118 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8119 new_dentry->d_name.name,
8120 new_dentry->d_name.len);
8121
8122 if (ret) {
8123 if (ret == -EEXIST) {
8124 /* we shouldn't get
8125 * eexist without a new_inode */
8126 if (!new_inode) {
8127 WARN_ON(1);
8128 return ret;
8129 }
8130 } else {
8131 /* maybe -EOVERFLOW */
8132 return ret;
8133 }
8134 }
8135 ret = 0;
8136
Chris Mason5a3f23d2009-03-31 13:27:11 -04008137 /*
8138 * we're using rename to replace one file with another.
8139 * and the replacement file is large. Start IO on it now so
8140 * we don't add too much work to the end of the transaction
8141 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008142 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008143 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8144 filemap_flush(old_inode->i_mapping);
8145
Yan, Zheng76dda932009-09-21 16:00:26 -04008146 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008147 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008148 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008149 /*
8150 * We want to reserve the absolute worst case amount of items. So if
8151 * both inodes are subvols and we need to unlink them then that would
8152 * require 4 item modifications, but if they are both normal inodes it
8153 * would require 5 item modifications, so we'll assume their normal
8154 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8155 * should cover the worst case number of items we'll modify.
8156 */
8157 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008158 if (IS_ERR(trans)) {
8159 ret = PTR_ERR(trans);
8160 goto out_notrans;
8161 }
Chris Mason5f39d392007-10-15 16:14:19 -04008162
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008163 if (dest != root)
8164 btrfs_record_root_in_trans(trans, dest);
8165
Yan, Zhenga5719522009-09-24 09:17:31 -04008166 ret = btrfs_set_inode_index(new_dir, &index);
8167 if (ret)
8168 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008169
Li Zefan33345d012011-04-20 10:31:50 +08008170 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008171 /* force full log commit if subvolume involved. */
8172 root->fs_info->last_trans_log_full_commit = trans->transid;
8173 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008174 ret = btrfs_insert_inode_ref(trans, dest,
8175 new_dentry->d_name.name,
8176 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008177 old_ino,
8178 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008179 if (ret)
8180 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008181 /*
8182 * this is an ugly little race, but the rename is required
8183 * to make sure that if we crash, the inode is either at the
8184 * old name or the new one. pinning the log transaction lets
8185 * us make sure we don't allow a log commit to come in after
8186 * we unlink the name but before we add the new name back in.
8187 */
8188 btrfs_pin_log_trans(root);
8189 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008190 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008191 * make sure the inode gets flushed if it is replacing
8192 * something.
8193 */
Li Zefan33345d012011-04-20 10:31:50 +08008194 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008195 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008196
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008197 inode_inc_iversion(old_dir);
8198 inode_inc_iversion(new_dir);
8199 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008200 old_dir->i_ctime = old_dir->i_mtime = ctime;
8201 new_dir->i_ctime = new_dir->i_mtime = ctime;
8202 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008203
Chris Mason12fcfd22009-03-24 10:24:20 -04008204 if (old_dentry->d_parent != new_dentry->d_parent)
8205 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8206
Li Zefan33345d012011-04-20 10:31:50 +08008207 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008208 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8209 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8210 old_dentry->d_name.name,
8211 old_dentry->d_name.len);
8212 } else {
Al Viro92986792011-03-04 17:14:37 +00008213 ret = __btrfs_unlink_inode(trans, root, old_dir,
8214 old_dentry->d_inode,
8215 old_dentry->d_name.name,
8216 old_dentry->d_name.len);
8217 if (!ret)
8218 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008219 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008220 if (ret) {
8221 btrfs_abort_transaction(trans, root, ret);
8222 goto out_fail;
8223 }
Chris Mason39279cc2007-06-12 06:35:45 -04008224
8225 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008226 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008227 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008228 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008229 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8230 root_objectid = BTRFS_I(new_inode)->location.objectid;
8231 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8232 root_objectid,
8233 new_dentry->d_name.name,
8234 new_dentry->d_name.len);
8235 BUG_ON(new_inode->i_nlink == 0);
8236 } else {
8237 ret = btrfs_unlink_inode(trans, dest, new_dir,
8238 new_dentry->d_inode,
8239 new_dentry->d_name.name,
8240 new_dentry->d_name.len);
8241 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008242 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008243 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008244 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008245 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008246 if (ret) {
8247 btrfs_abort_transaction(trans, root, ret);
8248 goto out_fail;
8249 }
Chris Mason39279cc2007-06-12 06:35:45 -04008250 }
Josef Bacikaec74772008-07-24 12:12:38 -04008251
Liu Bo75e7cb72011-03-22 10:12:20 +00008252 fixup_inode_flags(new_dir, old_inode);
8253
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008254 ret = btrfs_add_link(trans, new_dir, old_inode,
8255 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008256 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008257 if (ret) {
8258 btrfs_abort_transaction(trans, root, ret);
8259 goto out_fail;
8260 }
Chris Mason39279cc2007-06-12 06:35:45 -04008261
Li Zefan33345d012011-04-20 10:31:50 +08008262 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008263 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008264 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008265 btrfs_end_log_trans(root);
8266 }
Chris Mason39279cc2007-06-12 06:35:45 -04008267out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008268 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008269out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008270 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008271 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008272
Chris Mason39279cc2007-06-12 06:35:45 -04008273 return ret;
8274}
8275
Miao Xie8ccf6f192012-10-25 09:28:04 +00008276static void btrfs_run_delalloc_work(struct btrfs_work *work)
8277{
8278 struct btrfs_delalloc_work *delalloc_work;
8279
8280 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8281 work);
8282 if (delalloc_work->wait)
8283 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8284 else
8285 filemap_flush(delalloc_work->inode->i_mapping);
8286
8287 if (delalloc_work->delay_iput)
8288 btrfs_add_delayed_iput(delalloc_work->inode);
8289 else
8290 iput(delalloc_work->inode);
8291 complete(&delalloc_work->completion);
8292}
8293
8294struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8295 int wait, int delay_iput)
8296{
8297 struct btrfs_delalloc_work *work;
8298
8299 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8300 if (!work)
8301 return NULL;
8302
8303 init_completion(&work->completion);
8304 INIT_LIST_HEAD(&work->list);
8305 work->inode = inode;
8306 work->wait = wait;
8307 work->delay_iput = delay_iput;
8308 work->work.func = btrfs_run_delalloc_work;
8309
8310 return work;
8311}
8312
8313void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8314{
8315 wait_for_completion(&work->completion);
8316 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8317}
8318
Chris Masond352ac62008-09-29 15:18:18 -04008319/*
8320 * some fairly slow code that needs optimization. This walks the list
8321 * of all the inodes with pending delalloc and forces them to disk.
8322 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008323int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008324{
Chris Masonea8c2812008-08-04 23:17:27 -04008325 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008326 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008327 struct btrfs_delalloc_work *work, *next;
8328 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008329 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008330 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008331
Yan Zhengc146afa2008-11-12 14:34:12 -05008332 if (root->fs_info->sb->s_flags & MS_RDONLY)
8333 return -EROFS;
8334
Miao Xie8ccf6f192012-10-25 09:28:04 +00008335 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008336 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008337
Chris Mason75eff682008-12-15 15:54:40 -05008338 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008339 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8340 while (!list_empty(&splice)) {
8341 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008342 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008343
8344 list_del_init(&binode->delalloc_inodes);
8345
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008346 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008347 if (!inode) {
8348 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8349 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008350 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008351 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008352
8353 list_add_tail(&binode->delalloc_inodes,
8354 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008355 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008356
8357 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8358 if (unlikely(!work)) {
8359 ret = -ENOMEM;
8360 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008361 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008362 list_add_tail(&work->list, &works);
8363 btrfs_queue_worker(&root->fs_info->flush_workers,
8364 &work->work);
8365
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008366 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008367 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008368 }
Chris Mason75eff682008-12-15 15:54:40 -05008369 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008370
Miao Xie1eafa6c2013-01-22 10:49:00 +00008371 list_for_each_entry_safe(work, next, &works, list) {
8372 list_del_init(&work->list);
8373 btrfs_wait_and_free_delalloc_work(work);
8374 }
8375
Chris Mason8c8bee12008-09-29 11:19:10 -04008376 /* the filemap_flush will queue IO into the worker threads, but
8377 * we have to make sure the IO is actually started and that
8378 * ordered extents get created before we return
8379 */
8380 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008381 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008382 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008383 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008384 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8385 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008386 }
8387 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008388 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008389out:
8390 list_for_each_entry_safe(work, next, &works, list) {
8391 list_del_init(&work->list);
8392 btrfs_wait_and_free_delalloc_work(work);
8393 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008394
8395 if (!list_empty_careful(&splice)) {
8396 spin_lock(&root->fs_info->delalloc_lock);
8397 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8398 spin_unlock(&root->fs_info->delalloc_lock);
8399 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008400 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008401}
8402
Chris Mason39279cc2007-06-12 06:35:45 -04008403static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8404 const char *symname)
8405{
8406 struct btrfs_trans_handle *trans;
8407 struct btrfs_root *root = BTRFS_I(dir)->root;
8408 struct btrfs_path *path;
8409 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008410 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008411 int err;
8412 int drop_inode = 0;
8413 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008414 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008415 int name_len;
8416 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008417 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008418 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008419 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008420
8421 name_len = strlen(symname) + 1;
8422 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8423 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008424
Josef Bacik9ed74f22009-09-11 16:12:44 -04008425 /*
8426 * 2 items for inode item and ref
8427 * 2 items for dir items
8428 * 1 item for xattr if selinux is on
8429 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008430 trans = btrfs_start_transaction(root, 5);
8431 if (IS_ERR(trans))
8432 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008433
Li Zefan581bb052011-04-20 10:06:11 +08008434 err = btrfs_find_free_ino(root, &objectid);
8435 if (err)
8436 goto out_unlock;
8437
Josef Bacikaec74772008-07-24 12:12:38 -04008438 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008439 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008440 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008441 if (IS_ERR(inode)) {
8442 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008443 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008444 }
Chris Mason39279cc2007-06-12 06:35:45 -04008445
Eric Paris2a7dba32011-02-01 11:05:39 -05008446 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008447 if (err) {
8448 drop_inode = 1;
8449 goto out_unlock;
8450 }
8451
Casey Schauflerad19db72011-12-15 10:09:07 -05008452 /*
8453 * If the active LSM wants to access the inode during
8454 * d_instantiate it needs these. Smack checks to see
8455 * if the filesystem supports xattrs by looking at the
8456 * ops vector.
8457 */
8458 inode->i_fop = &btrfs_file_operations;
8459 inode->i_op = &btrfs_file_inode_operations;
8460
Josef Bacika1b075d2010-11-19 20:36:11 +00008461 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008462 if (err)
8463 drop_inode = 1;
8464 else {
8465 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008466 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008467 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008468 }
Chris Mason39279cc2007-06-12 06:35:45 -04008469 if (drop_inode)
8470 goto out_unlock;
8471
8472 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008473 if (!path) {
8474 err = -ENOMEM;
8475 drop_inode = 1;
8476 goto out_unlock;
8477 }
Li Zefan33345d012011-04-20 10:31:50 +08008478 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008479 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008480 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8481 datasize = btrfs_file_extent_calc_inline_size(name_len);
8482 err = btrfs_insert_empty_item(trans, root, path, &key,
8483 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008484 if (err) {
8485 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008486 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008487 goto out_unlock;
8488 }
Chris Mason5f39d392007-10-15 16:14:19 -04008489 leaf = path->nodes[0];
8490 ei = btrfs_item_ptr(leaf, path->slots[0],
8491 struct btrfs_file_extent_item);
8492 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8493 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008494 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008495 btrfs_set_file_extent_encryption(leaf, ei, 0);
8496 btrfs_set_file_extent_compression(leaf, ei, 0);
8497 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8498 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8499
Chris Mason39279cc2007-06-12 06:35:45 -04008500 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008501 write_extent_buffer(leaf, symname, ptr, name_len);
8502 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008503 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008504
Chris Mason39279cc2007-06-12 06:35:45 -04008505 inode->i_op = &btrfs_symlink_inode_operations;
8506 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008507 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008508 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008509 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008510 err = btrfs_update_inode(trans, root, inode);
8511 if (err)
8512 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008513
8514out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008515 if (!err)
8516 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008517 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008518 if (drop_inode) {
8519 inode_dec_link_count(inode);
8520 iput(inode);
8521 }
Liu Bob53d3f52012-11-14 14:34:34 +00008522 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008523 return err;
8524}
Chris Mason16432982008-04-10 10:23:21 -04008525
Josef Bacik0af3d002010-06-21 14:48:16 -04008526static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8527 u64 start, u64 num_bytes, u64 min_size,
8528 loff_t actual_len, u64 *alloc_hint,
8529 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008530{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008531 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8532 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008533 struct btrfs_root *root = BTRFS_I(inode)->root;
8534 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008535 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008536 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04008537 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008538 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008539
Josef Bacik0af3d002010-06-21 14:48:16 -04008540 if (trans)
8541 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008542 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008543 if (own_trans) {
8544 trans = btrfs_start_transaction(root, 3);
8545 if (IS_ERR(trans)) {
8546 ret = PTR_ERR(trans);
8547 break;
8548 }
Yan Zhengd899e052008-10-30 14:25:28 -04008549 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008550
Zach Brown24542bf2012-11-16 00:04:43 +00008551 ret = btrfs_reserve_extent(trans, root,
8552 min(num_bytes, 256ULL * 1024 * 1024),
8553 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008554 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008555 if (own_trans)
8556 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008557 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008558 }
8559
Yan Zhengd899e052008-10-30 14:25:28 -04008560 ret = insert_reserved_file_extent(trans, inode,
8561 cur_offset, ins.objectid,
8562 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008563 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008564 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008565 if (ret) {
8566 btrfs_abort_transaction(trans, root, ret);
8567 if (own_trans)
8568 btrfs_end_transaction(trans, root);
8569 break;
8570 }
Chris Masona1ed8352009-09-11 12:27:37 -04008571 btrfs_drop_extent_cache(inode, cur_offset,
8572 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008573
Josef Bacik5dc562c2012-08-17 13:14:17 -04008574 em = alloc_extent_map();
8575 if (!em) {
8576 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8577 &BTRFS_I(inode)->runtime_flags);
8578 goto next;
8579 }
8580
8581 em->start = cur_offset;
8582 em->orig_start = cur_offset;
8583 em->len = ins.offset;
8584 em->block_start = ins.objectid;
8585 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008586 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008587 em->bdev = root->fs_info->fs_devices->latest_bdev;
8588 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8589 em->generation = trans->transid;
8590
8591 while (1) {
8592 write_lock(&em_tree->lock);
8593 ret = add_extent_mapping(em_tree, em);
8594 if (!ret)
8595 list_move(&em->list,
8596 &em_tree->modified_extents);
8597 write_unlock(&em_tree->lock);
8598 if (ret != -EEXIST)
8599 break;
8600 btrfs_drop_extent_cache(inode, cur_offset,
8601 cur_offset + ins.offset - 1,
8602 0);
8603 }
8604 free_extent_map(em);
8605next:
Yan Zhengd899e052008-10-30 14:25:28 -04008606 num_bytes -= ins.offset;
8607 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008608 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008609
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008610 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008611 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008612 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008613 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008614 (actual_len > inode->i_size) &&
8615 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008616 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008617 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008618 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008619 i_size = cur_offset;
8620 i_size_write(inode, i_size);
8621 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008622 }
8623
Yan Zhengd899e052008-10-30 14:25:28 -04008624 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008625
8626 if (ret) {
8627 btrfs_abort_transaction(trans, root, ret);
8628 if (own_trans)
8629 btrfs_end_transaction(trans, root);
8630 break;
8631 }
Yan Zhengd899e052008-10-30 14:25:28 -04008632
Josef Bacik0af3d002010-06-21 14:48:16 -04008633 if (own_trans)
8634 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008635 }
Yan Zhengd899e052008-10-30 14:25:28 -04008636 return ret;
8637}
8638
Josef Bacik0af3d002010-06-21 14:48:16 -04008639int btrfs_prealloc_file_range(struct inode *inode, int mode,
8640 u64 start, u64 num_bytes, u64 min_size,
8641 loff_t actual_len, u64 *alloc_hint)
8642{
8643 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8644 min_size, actual_len, alloc_hint,
8645 NULL);
8646}
8647
8648int btrfs_prealloc_file_range_trans(struct inode *inode,
8649 struct btrfs_trans_handle *trans, int mode,
8650 u64 start, u64 num_bytes, u64 min_size,
8651 loff_t actual_len, u64 *alloc_hint)
8652{
8653 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8654 min_size, actual_len, alloc_hint, trans);
8655}
8656
Chris Masone6dcd2d2008-07-17 12:53:50 -04008657static int btrfs_set_page_dirty(struct page *page)
8658{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008659 return __set_page_dirty_nobuffers(page);
8660}
8661
Al Viro10556cb2011-06-20 19:28:19 -04008662static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008663{
Li Zefanb83cc962010-12-20 16:04:08 +08008664 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008665 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008666
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008667 if (mask & MAY_WRITE &&
8668 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8669 if (btrfs_root_readonly(root))
8670 return -EROFS;
8671 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8672 return -EACCES;
8673 }
Al Viro2830ba72011-06-20 19:16:29 -04008674 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008675}
Chris Mason39279cc2007-06-12 06:35:45 -04008676
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008677static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008678 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008679 .lookup = btrfs_lookup,
8680 .create = btrfs_create,
8681 .unlink = btrfs_unlink,
8682 .link = btrfs_link,
8683 .mkdir = btrfs_mkdir,
8684 .rmdir = btrfs_rmdir,
8685 .rename = btrfs_rename,
8686 .symlink = btrfs_symlink,
8687 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008688 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008689 .setxattr = btrfs_setxattr,
8690 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008691 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008692 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008693 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008694 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008695};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008696static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008697 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008698 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008699 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008700};
Yan, Zheng76dda932009-09-21 16:00:26 -04008701
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008702static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008703 .llseek = generic_file_llseek,
8704 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008705 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008706 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008707#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008708 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008709#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008710 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008711 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008712};
8713
Chris Masond1310b22008-01-24 16:13:08 -05008714static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008715 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008716 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008717 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008718 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008719 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008720 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008721 .set_bit_hook = btrfs_set_bit_hook,
8722 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008723 .merge_extent_hook = btrfs_merge_extent_hook,
8724 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008725};
8726
Chris Mason35054392009-01-21 13:11:13 -05008727/*
8728 * btrfs doesn't support the bmap operation because swapfiles
8729 * use bmap to make a mapping of extents in the file. They assume
8730 * these extents won't change over the life of the file and they
8731 * use the bmap result to do IO directly to the drive.
8732 *
8733 * the btrfs bmap call would return logical addresses that aren't
8734 * suitable for IO and they also will change frequently as COW
8735 * operations happen. So, swapfile + btrfs == corruption.
8736 *
8737 * For now we're avoiding this by dropping bmap.
8738 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008739static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008740 .readpage = btrfs_readpage,
8741 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008742 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008743 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008744 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008745 .invalidatepage = btrfs_invalidatepage,
8746 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008747 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008748 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008749};
8750
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008751static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008752 .readpage = btrfs_readpage,
8753 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008754 .invalidatepage = btrfs_invalidatepage,
8755 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008756};
8757
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008758static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008759 .getattr = btrfs_getattr,
8760 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008761 .setxattr = btrfs_setxattr,
8762 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008763 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008764 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008765 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008766 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008767 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008768 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008769};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008770static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008771 .getattr = btrfs_getattr,
8772 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008773 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008774 .setxattr = btrfs_setxattr,
8775 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008776 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008777 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008778 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008779 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008780};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008781static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008782 .readlink = generic_readlink,
8783 .follow_link = page_follow_link_light,
8784 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008785 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008786 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008787 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008788 .setxattr = btrfs_setxattr,
8789 .getxattr = btrfs_getxattr,
8790 .listxattr = btrfs_listxattr,
8791 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008792 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008793 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008794};
Yan, Zheng76dda932009-09-21 16:00:26 -04008795
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008796const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008797 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008798 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008799};