blob: 9ef7a5b1b77ecf78bfeeed6feab4bc2652d8f9c5 [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, Zhengc21677542009-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, Zhengc21677542009-11-12 09:34:21 +0000214
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100215 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400216fail:
217 btrfs_free_path(path);
218 return err;
219}
220
221
222/*
223 * conditionally insert an inline extent into the file. This
224 * does the checks required to make sure the data is small enough
225 * to fit as an inline extent.
226 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400227static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400228 struct btrfs_root *root,
229 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000230 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400231 struct page **compressed_pages)
232{
233 u64 isize = i_size_read(inode);
234 u64 actual_end = min(end + 1, isize);
235 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000236 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 u64 data_len = inline_len;
238 int ret;
239
240 if (compressed_size)
241 data_len = compressed_size;
242
243 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400244 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400245 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
246 (!compressed_size &&
247 (actual_end & (root->sectorsize - 1)) == 0) ||
248 end + 1 < isize ||
249 data_len > root->fs_info->max_inline) {
250 return 1;
251 }
252
Josef Bacik26714852012-08-29 12:24:27 -0400253 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100254 if (ret)
255 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400256
257 if (isize > actual_end)
258 inline_len = min_t(u64, isize, actual_end);
259 ret = insert_inline_extent(trans, root, inode, start,
260 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000261 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400262 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100263 btrfs_abort_transaction(trans, root, ret);
264 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400265 } else if (ret == -ENOSPC) {
266 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400268
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400269 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400270 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400271 return 0;
272}
273
Chris Mason771ed682008-11-06 22:02:51 -0500274struct async_extent {
275 u64 start;
276 u64 ram_size;
277 u64 compressed_size;
278 struct page **pages;
279 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800280 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500281 struct list_head list;
282};
283
284struct async_cow {
285 struct inode *inode;
286 struct btrfs_root *root;
287 struct page *locked_page;
288 u64 start;
289 u64 end;
290 struct list_head extents;
291 struct btrfs_work work;
292};
293
294static noinline int add_async_extent(struct async_cow *cow,
295 u64 start, u64 ram_size,
296 u64 compressed_size,
297 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800298 unsigned long nr_pages,
299 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500300{
301 struct async_extent *async_extent;
302
303 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100304 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500305 async_extent->start = start;
306 async_extent->ram_size = ram_size;
307 async_extent->compressed_size = compressed_size;
308 async_extent->pages = pages;
309 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800310 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500311 list_add_tail(&async_extent->list, &cow->extents);
312 return 0;
313}
314
Chris Masonc8b97812008-10-29 14:49:59 -0400315/*
Chris Mason771ed682008-11-06 22:02:51 -0500316 * we create compressed extents in two phases. The first
317 * phase compresses a range of pages that have already been
318 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400319 *
Chris Mason771ed682008-11-06 22:02:51 -0500320 * This is done inside an ordered work queue, and the compression
321 * is spread across many cpus. The actual IO submission is step
322 * two, and the ordered work queue takes care of making sure that
323 * happens in the same order things were put onto the queue by
324 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400325 *
Chris Mason771ed682008-11-06 22:02:51 -0500326 * If this code finds it can't get good compression, it puts an
327 * entry onto the work queue to write the uncompressed bytes. This
328 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300329 * are written in the same order that the flusher thread sent them
330 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400331 */
Chris Mason771ed682008-11-06 22:02:51 -0500332static noinline int compress_file_range(struct inode *inode,
333 struct page *locked_page,
334 u64 start, u64 end,
335 struct async_cow *async_cow,
336 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400337{
338 struct btrfs_root *root = BTRFS_I(inode)->root;
339 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400340 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400341 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400342 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500343 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400344 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400345 struct page **pages = NULL;
346 unsigned long nr_pages;
347 unsigned long nr_pages_ret = 0;
348 unsigned long total_compressed = 0;
349 unsigned long total_in = 0;
350 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500351 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400352 int i;
353 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800354 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400355
Liu Bo4cb13e52012-03-29 09:57:45 -0400356 /* if this is a small write inside eof, kick off a defrag */
357 if ((end - start + 1) < 16 * 1024 &&
358 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400359 btrfs_add_inode_defrag(NULL, inode);
360
Chris Mason42dc7ba2008-12-15 11:44:56 -0500361 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400362again:
363 will_compress = 0;
364 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
365 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
366
Chris Masonf03d9302009-02-04 09:31:06 -0500367 /*
368 * we don't want to send crud past the end of i_size through
369 * compression, that's just a waste of CPU time. So, if the
370 * end of the file is before the start of our current
371 * requested range of bytes, we bail out to the uncompressed
372 * cleanup code that can deal with all of this.
373 *
374 * It isn't really the fastest way to fix things, but this is a
375 * very uncommon corner.
376 */
377 if (actual_end <= start)
378 goto cleanup_and_bail_uncompressed;
379
Chris Masonc8b97812008-10-29 14:49:59 -0400380 total_compressed = actual_end - start;
381
382 /* we want to make sure that amount of ram required to uncompress
383 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500384 * of a compressed extent to 128k. This is a crucial number
385 * because it also controls how easily we can spread reads across
386 * cpus for decompression.
387 *
388 * We also want to make sure the amount of IO required to do
389 * a random read is reasonably small, so we limit the size of
390 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400391 */
392 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000393 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500394 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400395 total_in = 0;
396 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400397
Chris Mason771ed682008-11-06 22:02:51 -0500398 /*
399 * we do compression for mount -o compress and when the
400 * inode has not been flagged as nocompress. This flag can
401 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400402 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200403 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500404 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000405 (BTRFS_I(inode)->force_compress) ||
406 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400407 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400408 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800409 if (!pages) {
410 /* just bail out to the uncompressed code */
411 goto cont;
412 }
Chris Mason179e29e2007-11-01 11:28:41 -0400413
Li Zefan261507a02010-12-17 14:21:50 +0800414 if (BTRFS_I(inode)->force_compress)
415 compress_type = BTRFS_I(inode)->force_compress;
416
417 ret = btrfs_compress_pages(compress_type,
418 inode->i_mapping, start,
419 total_compressed, pages,
420 nr_pages, &nr_pages_ret,
421 &total_in,
422 &total_compressed,
423 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400424
425 if (!ret) {
426 unsigned long offset = total_compressed &
427 (PAGE_CACHE_SIZE - 1);
428 struct page *page = pages[nr_pages_ret - 1];
429 char *kaddr;
430
431 /* zero the tail end of the last page, we might be
432 * sending it down to disk
433 */
434 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800435 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400436 memset(kaddr + offset, 0,
437 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800438 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400439 }
440 will_compress = 1;
441 }
442 }
Li Zefan560f7d72011-09-08 10:22:01 +0800443cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400444 if (start == 0) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -0400445 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100446 if (IS_ERR(trans)) {
447 ret = PTR_ERR(trans);
448 trans = NULL;
449 goto cleanup_and_out;
450 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400451 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500452
Chris Masonc8b97812008-10-29 14:49:59 -0400453 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500454 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400455 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500456 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400457 */
458 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000459 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400460 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500461 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400462 ret = cow_file_range_inline(trans, root, inode,
463 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000464 total_compressed,
465 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400466 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100467 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500468 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100469 * inline extent creation worked or returned error,
470 * we don't need to create any more async work items.
471 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500472 */
Chris Masonc8b97812008-10-29 14:49:59 -0400473 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400474 &BTRFS_I(inode)->io_tree,
475 start, end, NULL,
476 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400477 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400478 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000479
480 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400481 goto free_pages_out;
482 }
Yan, Zhengc21677542009-11-12 09:34:21 +0000483 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400484 }
485
486 if (will_compress) {
487 /*
488 * we aren't doing an inline extent round the compressed size
489 * up to a block size boundary so the allocator does sane
490 * things
491 */
Qu Wenruofda28322013-02-26 08:10:22 +0000492 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400493
494 /*
495 * one last check to make sure the compression is really a
496 * win, compare the page count read with the blocks on disk
497 */
Qu Wenruofda28322013-02-26 08:10:22 +0000498 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400499 if (total_compressed >= total_in) {
500 will_compress = 0;
501 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400502 num_bytes = total_in;
503 }
504 }
505 if (!will_compress && pages) {
506 /*
507 * the compression code ran but failed to make things smaller,
508 * free any pages it allocated and our page pointer array
509 */
510 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400511 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400512 page_cache_release(pages[i]);
513 }
514 kfree(pages);
515 pages = NULL;
516 total_compressed = 0;
517 nr_pages_ret = 0;
518
519 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500520 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
521 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500522 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500523 }
Chris Masonc8b97812008-10-29 14:49:59 -0400524 }
Chris Mason771ed682008-11-06 22:02:51 -0500525 if (will_compress) {
526 *num_added += 1;
527
528 /* the async work queues will take care of doing actual
529 * allocation on disk for these compressed pages,
530 * and will submit them to the elevator.
531 */
532 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800533 total_compressed, pages, nr_pages_ret,
534 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500535
Yan, Zheng24ae6362010-12-06 07:02:36 +0000536 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500537 start += num_bytes;
538 pages = NULL;
539 cond_resched();
540 goto again;
541 }
542 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500543cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500544 /*
545 * No compression, but we still need to write the pages in
546 * the file we've been given so far. redirty the locked
547 * page if it corresponds to our extent and set things up
548 * for the async work queue to run cow_file_range to do
549 * the normal delalloc dance
550 */
551 if (page_offset(locked_page) >= start &&
552 page_offset(locked_page) <= end) {
553 __set_page_dirty_nobuffers(locked_page);
554 /* unlocked later on in the async handlers */
555 }
Li Zefan261507a02010-12-17 14:21:50 +0800556 add_async_extent(async_cow, start, end - start + 1,
557 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500558 *num_added += 1;
559 }
560
561out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100562 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500563
564free_pages_out:
565 for (i = 0; i < nr_pages_ret; i++) {
566 WARN_ON(pages[i]->mapping);
567 page_cache_release(pages[i]);
568 }
Chris Masond3977122009-01-05 21:25:51 -0500569 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500570
571 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100572
573cleanup_and_out:
574 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
575 start, end, NULL,
576 EXTENT_CLEAR_UNLOCK_PAGE |
577 EXTENT_CLEAR_DIRTY |
578 EXTENT_CLEAR_DELALLOC |
579 EXTENT_SET_WRITEBACK |
580 EXTENT_END_WRITEBACK);
581 if (!trans || IS_ERR(trans))
582 btrfs_error(root->fs_info, ret, "Failed to join transaction");
583 else
584 btrfs_abort_transaction(trans, root, ret);
585 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500586}
587
588/*
589 * phase two of compressed writeback. This is the ordered portion
590 * of the code, which only gets called in the order the work was
591 * queued. We walk all the async extents created by compress_file_range
592 * and send them down to the disk.
593 */
594static noinline int submit_compressed_extents(struct inode *inode,
595 struct async_cow *async_cow)
596{
597 struct async_extent *async_extent;
598 u64 alloc_hint = 0;
599 struct btrfs_trans_handle *trans;
600 struct btrfs_key ins;
601 struct extent_map *em;
602 struct btrfs_root *root = BTRFS_I(inode)->root;
603 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
604 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500605 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500606
607 if (list_empty(&async_cow->extents))
608 return 0;
609
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500610again:
Chris Masond3977122009-01-05 21:25:51 -0500611 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500612 async_extent = list_entry(async_cow->extents.next,
613 struct async_extent, list);
614 list_del(&async_extent->list);
615
616 io_tree = &BTRFS_I(inode)->io_tree;
617
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500618retry:
Chris Mason771ed682008-11-06 22:02:51 -0500619 /* did the compression code fall back to uncompressed IO? */
620 if (!async_extent->pages) {
621 int page_started = 0;
622 unsigned long nr_written = 0;
623
624 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000625 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100626 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500627
628 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500629 ret = cow_file_range(inode, async_cow->locked_page,
630 async_extent->start,
631 async_extent->start +
632 async_extent->ram_size - 1,
633 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500634
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100635 /* JDM XXX */
636
Chris Mason771ed682008-11-06 22:02:51 -0500637 /*
638 * if page_started, cow_file_range inserted an
639 * inline extent and took care of all the unlocking
640 * and IO for us. Otherwise, we need to submit
641 * all those pages down to the drive.
642 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500643 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500644 extent_write_locked_range(io_tree,
645 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500646 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500647 async_extent->ram_size - 1,
648 btrfs_get_extent,
649 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500650 else if (ret)
651 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500652 kfree(async_extent);
653 cond_resched();
654 continue;
655 }
656
657 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100658 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500659
Josef Bacik7a7eaa402011-04-13 12:54:33 -0400660 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100661 if (IS_ERR(trans)) {
662 ret = PTR_ERR(trans);
663 } else {
664 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
665 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500666 async_extent->compressed_size,
667 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500668 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600669 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100670 btrfs_abort_transaction(trans, root, ret);
671 btrfs_end_transaction(trans, root);
672 }
Yan, Zhengc21677542009-11-12 09:34:21 +0000673
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500674 if (ret) {
675 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500676
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500677 for (i = 0; i < async_extent->nr_pages; i++) {
678 WARN_ON(async_extent->pages[i]->mapping);
679 page_cache_release(async_extent->pages[i]);
680 }
681 kfree(async_extent->pages);
682 async_extent->nr_pages = 0;
683 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500684
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100685 if (ret == -ENOSPC)
686 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500687 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500688 }
689
Yan, Zhengc21677542009-11-12 09:34:21 +0000690 /*
691 * here we're doing allocation and writeback of the
692 * compressed pages
693 */
694 btrfs_drop_extent_cache(inode, async_extent->start,
695 async_extent->start +
696 async_extent->ram_size - 1, 0);
697
David Sterba172ddd62011-04-21 00:48:27 +0200698 em = alloc_extent_map();
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500699 if (!em)
700 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500701 em->start = async_extent->start;
702 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500703 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400704 em->mod_start = em->start;
705 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500706
707 em->block_start = ins.objectid;
708 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500709 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500710 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800711 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500712 set_bit(EXTENT_FLAG_PINNED, &em->flags);
713 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400714 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500715
Chris Masond3977122009-01-05 21:25:51 -0500716 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400717 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500718 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400719 if (!ret)
720 list_move(&em->list,
721 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400722 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500723 if (ret != -EEXIST) {
724 free_extent_map(em);
725 break;
726 }
727 btrfs_drop_extent_cache(inode, async_extent->start,
728 async_extent->start +
729 async_extent->ram_size - 1, 0);
730 }
731
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500732 if (ret)
733 goto out_free_reserve;
734
Li Zefan261507a02010-12-17 14:21:50 +0800735 ret = btrfs_add_ordered_extent_compress(inode,
736 async_extent->start,
737 ins.objectid,
738 async_extent->ram_size,
739 ins.offset,
740 BTRFS_ORDERED_COMPRESSED,
741 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500742 if (ret)
743 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500744
Chris Mason771ed682008-11-06 22:02:51 -0500745 /*
746 * clear dirty, set writeback and unlock the pages.
747 */
748 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400749 &BTRFS_I(inode)->io_tree,
750 async_extent->start,
751 async_extent->start +
752 async_extent->ram_size - 1,
753 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
754 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400755 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400756 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500757
758 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500759 async_extent->start,
760 async_extent->ram_size,
761 ins.objectid,
762 ins.offset, async_extent->pages,
763 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500764 alloc_hint = ins.objectid + ins.offset;
765 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500766 if (ret)
767 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500768 cond_resched();
769 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100770 ret = 0;
771out:
772 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500773out_free_reserve:
774 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100775out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500776 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
777 async_extent->start,
778 async_extent->start +
779 async_extent->ram_size - 1,
780 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
781 EXTENT_CLEAR_UNLOCK |
782 EXTENT_CLEAR_DELALLOC |
783 EXTENT_CLEAR_DIRTY |
784 EXTENT_SET_WRITEBACK |
785 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100786 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500787 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500788}
789
Josef Bacik4b46fce2010-05-23 11:00:55 -0400790static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
791 u64 num_bytes)
792{
793 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
794 struct extent_map *em;
795 u64 alloc_hint = 0;
796
797 read_lock(&em_tree->lock);
798 em = search_extent_mapping(em_tree, start, num_bytes);
799 if (em) {
800 /*
801 * if block start isn't an actual block number then find the
802 * first block in this inode and use that as a hint. If that
803 * block is also bogus then just don't worry about it.
804 */
805 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
806 free_extent_map(em);
807 em = search_extent_mapping(em_tree, 0, 0);
808 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
809 alloc_hint = em->block_start;
810 if (em)
811 free_extent_map(em);
812 } else {
813 alloc_hint = em->block_start;
814 free_extent_map(em);
815 }
816 }
817 read_unlock(&em_tree->lock);
818
819 return alloc_hint;
820}
821
Chris Mason771ed682008-11-06 22:02:51 -0500822/*
823 * when extent_io.c finds a delayed allocation range in the file,
824 * the call backs end up in this code. The basic idea is to
825 * allocate extents on disk for the range, and create ordered data structs
826 * in ram to track those extents.
827 *
828 * locked_page is the page that writepage had locked already. We use
829 * it to make sure we don't do extra locks or unlocks.
830 *
831 * *page_started is set to one if we unlock locked_page and do everything
832 * required to start IO on it. It may be clean and already done with
833 * IO when we return.
834 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000835static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
836 struct inode *inode,
837 struct btrfs_root *root,
838 struct page *locked_page,
839 u64 start, u64 end, int *page_started,
840 unsigned long *nr_written,
841 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500842{
Chris Mason771ed682008-11-06 22:02:51 -0500843 u64 alloc_hint = 0;
844 u64 num_bytes;
845 unsigned long ram_size;
846 u64 disk_num_bytes;
847 u64 cur_alloc_size;
848 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500849 struct btrfs_key ins;
850 struct extent_map *em;
851 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
852 int ret = 0;
853
Liu Bo83eea1f2012-07-10 05:28:39 -0600854 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500855
Qu Wenruofda28322013-02-26 08:10:22 +0000856 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500857 num_bytes = max(blocksize, num_bytes);
858 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500859
Chris Mason4cb53002011-05-24 15:35:30 -0400860 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400861 if (num_bytes < 64 * 1024 &&
862 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400863 btrfs_add_inode_defrag(trans, inode);
864
Chris Mason771ed682008-11-06 22:02:51 -0500865 if (start == 0) {
866 /* lets try to make an inline extent */
867 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000868 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500869 if (ret == 0) {
870 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400871 &BTRFS_I(inode)->io_tree,
872 start, end, NULL,
873 EXTENT_CLEAR_UNLOCK_PAGE |
874 EXTENT_CLEAR_UNLOCK |
875 EXTENT_CLEAR_DELALLOC |
876 EXTENT_CLEAR_DIRTY |
877 EXTENT_SET_WRITEBACK |
878 EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000879
Chris Mason771ed682008-11-06 22:02:51 -0500880 *nr_written = *nr_written +
881 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
882 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500883 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100884 } else if (ret < 0) {
885 btrfs_abort_transaction(trans, root, ret);
886 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500887 }
888 }
Chris Masonc8b97812008-10-29 14:49:59 -0400889
890 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200891 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400892
Josef Bacik4b46fce2010-05-23 11:00:55 -0400893 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400894 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400895
Chris Masond3977122009-01-05 21:25:51 -0500896 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400897 unsigned long op;
898
Josef Bacik287a0ab2010-03-19 18:07:23 +0000899 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400900 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500901 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500902 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100903 if (ret < 0) {
904 btrfs_abort_transaction(trans, root, ret);
905 goto out_unlock;
906 }
Chris Masond3977122009-01-05 21:25:51 -0500907
David Sterba172ddd62011-04-21 00:48:27 +0200908 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100909 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400910 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500911 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500912 ram_size = ins.offset;
913 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400914 em->mod_start = em->start;
915 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400916
Chris Masone6dcd2d2008-07-17 12:53:50 -0400917 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400918 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500919 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400920 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400921 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400922 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400923
Chris Masond3977122009-01-05 21:25:51 -0500924 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400925 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400926 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400927 if (!ret)
928 list_move(&em->list,
929 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400930 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400931 if (ret != -EEXIST) {
932 free_extent_map(em);
933 break;
934 }
935 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400936 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400937 }
938
Chris Mason98d20f62008-04-14 09:46:10 -0400939 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400940 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500941 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100942 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400943
Yan Zheng17d217f2008-12-12 10:03:38 -0500944 if (root->root_key.objectid ==
945 BTRFS_DATA_RELOC_TREE_OBJECTID) {
946 ret = btrfs_reloc_clone_csums(inode, start,
947 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100948 if (ret) {
949 btrfs_abort_transaction(trans, root, ret);
950 goto out_unlock;
951 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500952 }
953
Chris Masond3977122009-01-05 21:25:51 -0500954 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400955 break;
Chris Masond3977122009-01-05 21:25:51 -0500956
Chris Masonc8b97812008-10-29 14:49:59 -0400957 /* we're not doing compressed IO, don't unlock the first
958 * page (which the caller expects to stay locked), don't
959 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400960 *
961 * Do set the Private2 bit so we know this page was properly
962 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400963 */
Chris Masona791e352009-10-08 11:27:10 -0400964 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
965 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
966 EXTENT_SET_PRIVATE2;
967
Chris Masonc8b97812008-10-29 14:49:59 -0400968 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
969 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400970 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400971 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500972 num_bytes -= cur_alloc_size;
973 alloc_hint = ins.objectid + ins.offset;
974 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400975 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100976out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500977 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000978
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100979out_unlock:
980 extent_clear_unlock_delalloc(inode,
981 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400982 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100983 EXTENT_CLEAR_UNLOCK_PAGE |
984 EXTENT_CLEAR_UNLOCK |
985 EXTENT_CLEAR_DELALLOC |
986 EXTENT_CLEAR_DIRTY |
987 EXTENT_SET_WRITEBACK |
988 EXTENT_END_WRITEBACK);
989
990 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500991}
Chris Masonc8b97812008-10-29 14:49:59 -0400992
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000993static noinline int cow_file_range(struct inode *inode,
994 struct page *locked_page,
995 u64 start, u64 end, int *page_started,
996 unsigned long *nr_written,
997 int unlock)
998{
999 struct btrfs_trans_handle *trans;
1000 struct btrfs_root *root = BTRFS_I(inode)->root;
1001 int ret;
1002
1003 trans = btrfs_join_transaction(root);
1004 if (IS_ERR(trans)) {
1005 extent_clear_unlock_delalloc(inode,
1006 &BTRFS_I(inode)->io_tree,
1007 start, end, locked_page,
1008 EXTENT_CLEAR_UNLOCK_PAGE |
1009 EXTENT_CLEAR_UNLOCK |
1010 EXTENT_CLEAR_DELALLOC |
1011 EXTENT_CLEAR_DIRTY |
1012 EXTENT_SET_WRITEBACK |
1013 EXTENT_END_WRITEBACK);
1014 return PTR_ERR(trans);
1015 }
1016 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1017
1018 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1019 page_started, nr_written, unlock);
1020
1021 btrfs_end_transaction(trans, root);
1022
1023 return ret;
1024}
1025
Chris Mason771ed682008-11-06 22:02:51 -05001026/*
1027 * work queue call back to started compression on a file and pages
1028 */
1029static noinline void async_cow_start(struct btrfs_work *work)
1030{
1031 struct async_cow *async_cow;
1032 int num_added = 0;
1033 async_cow = container_of(work, struct async_cow, work);
1034
1035 compress_file_range(async_cow->inode, async_cow->locked_page,
1036 async_cow->start, async_cow->end, async_cow,
1037 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001038 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001039 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001040 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001041 }
Chris Mason771ed682008-11-06 22:02:51 -05001042}
1043
1044/*
1045 * work queue call back to submit previously compressed pages
1046 */
1047static noinline void async_cow_submit(struct btrfs_work *work)
1048{
1049 struct async_cow *async_cow;
1050 struct btrfs_root *root;
1051 unsigned long nr_pages;
1052
1053 async_cow = container_of(work, struct async_cow, work);
1054
1055 root = async_cow->root;
1056 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1057 PAGE_CACHE_SHIFT;
1058
Josef Bacik66657b32012-08-01 15:36:24 -04001059 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001060 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001061 waitqueue_active(&root->fs_info->async_submit_wait))
1062 wake_up(&root->fs_info->async_submit_wait);
1063
Chris Masond3977122009-01-05 21:25:51 -05001064 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001065 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001066}
Chris Masonc8b97812008-10-29 14:49:59 -04001067
Chris Mason771ed682008-11-06 22:02:51 -05001068static noinline void async_cow_free(struct btrfs_work *work)
1069{
1070 struct async_cow *async_cow;
1071 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001072 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001073 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001074 kfree(async_cow);
1075}
1076
1077static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1078 u64 start, u64 end, int *page_started,
1079 unsigned long *nr_written)
1080{
1081 struct async_cow *async_cow;
1082 struct btrfs_root *root = BTRFS_I(inode)->root;
1083 unsigned long nr_pages;
1084 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001085 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001086
Chris Masona3429ab2009-10-08 12:30:20 -04001087 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1088 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001089 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001090 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001091 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001092 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001093 async_cow->root = root;
1094 async_cow->locked_page = locked_page;
1095 async_cow->start = start;
1096
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001097 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001098 cur_end = end;
1099 else
1100 cur_end = min(end, start + 512 * 1024 - 1);
1101
1102 async_cow->end = cur_end;
1103 INIT_LIST_HEAD(&async_cow->extents);
1104
1105 async_cow->work.func = async_cow_start;
1106 async_cow->work.ordered_func = async_cow_submit;
1107 async_cow->work.ordered_free = async_cow_free;
1108 async_cow->work.flags = 0;
1109
Chris Mason771ed682008-11-06 22:02:51 -05001110 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1111 PAGE_CACHE_SHIFT;
1112 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1113
1114 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1115 &async_cow->work);
1116
1117 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1118 wait_event(root->fs_info->async_submit_wait,
1119 (atomic_read(&root->fs_info->async_delalloc_pages) <
1120 limit));
1121 }
1122
Chris Masond3977122009-01-05 21:25:51 -05001123 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001124 atomic_read(&root->fs_info->async_delalloc_pages)) {
1125 wait_event(root->fs_info->async_submit_wait,
1126 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1127 0));
1128 }
1129
1130 *nr_written += nr_pages;
1131 start = cur_end + 1;
1132 }
1133 *page_started = 1;
1134 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001135}
1136
Chris Masond3977122009-01-05 21:25:51 -05001137static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001138 u64 bytenr, u64 num_bytes)
1139{
1140 int ret;
1141 struct btrfs_ordered_sum *sums;
1142 LIST_HEAD(list);
1143
Yan Zheng07d400a2009-01-06 11:42:00 -05001144 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001145 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001146 if (ret == 0 && list_empty(&list))
1147 return 0;
1148
1149 while (!list_empty(&list)) {
1150 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1151 list_del(&sums->list);
1152 kfree(sums);
1153 }
1154 return 1;
1155}
1156
Chris Masond352ac62008-09-29 15:18:18 -04001157/*
1158 * when nowcow writeback call back. This checks for snapshots or COW copies
1159 * of the extents that exist in the file, and COWs the file as required.
1160 *
1161 * If no cow copies or snapshots exist, we write directly to the existing
1162 * blocks on disk
1163 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001164static noinline int run_delalloc_nocow(struct inode *inode,
1165 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001166 u64 start, u64 end, int *page_started, int force,
1167 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001168{
Chris Masonbe20aa92007-12-17 20:14:01 -05001169 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001170 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001171 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001172 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001173 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001174 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001175 u64 cow_start;
1176 u64 cur_offset;
1177 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001178 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001179 u64 disk_bytenr;
1180 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001181 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001182 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001183 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001184 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001185 int nocow;
1186 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001187 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001188 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001189
1190 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001191 if (!path) {
1192 extent_clear_unlock_delalloc(inode,
1193 &BTRFS_I(inode)->io_tree,
1194 start, end, locked_page,
1195 EXTENT_CLEAR_UNLOCK_PAGE |
1196 EXTENT_CLEAR_UNLOCK |
1197 EXTENT_CLEAR_DELALLOC |
1198 EXTENT_CLEAR_DIRTY |
1199 EXTENT_SET_WRITEBACK |
1200 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001201 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001202 }
Li Zefan82d59022011-04-20 10:33:24 +08001203
Liu Bo83eea1f2012-07-10 05:28:39 -06001204 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001205
1206 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001207 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001208 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001209 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001210
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001211 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001212 extent_clear_unlock_delalloc(inode,
1213 &BTRFS_I(inode)->io_tree,
1214 start, end, locked_page,
1215 EXTENT_CLEAR_UNLOCK_PAGE |
1216 EXTENT_CLEAR_UNLOCK |
1217 EXTENT_CLEAR_DELALLOC |
1218 EXTENT_CLEAR_DIRTY |
1219 EXTENT_SET_WRITEBACK |
1220 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001221 btrfs_free_path(path);
1222 return PTR_ERR(trans);
1223 }
1224
Josef Bacik74b21072011-04-13 12:02:53 -04001225 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001226
Yan Zheng80ff3852008-10-30 14:20:02 -04001227 cow_start = (u64)-1;
1228 cur_offset = start;
1229 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001230 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001231 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001232 if (ret < 0) {
1233 btrfs_abort_transaction(trans, root, ret);
1234 goto error;
1235 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1237 leaf = path->nodes[0];
1238 btrfs_item_key_to_cpu(leaf, &found_key,
1239 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001240 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 found_key.type == BTRFS_EXTENT_DATA_KEY)
1242 path->slots[0]--;
1243 }
1244 check_prev = 0;
1245next_slot:
1246 leaf = path->nodes[0];
1247 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1248 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001249 if (ret < 0) {
1250 btrfs_abort_transaction(trans, root, ret);
1251 goto error;
1252 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001253 if (ret > 0)
1254 break;
1255 leaf = path->nodes[0];
1256 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001257
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 nocow = 0;
1259 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001260 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001262
Li Zefan33345d012011-04-20 10:31:50 +08001263 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001264 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1265 found_key.offset > end)
1266 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001267
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 if (found_key.offset > cur_offset) {
1269 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001270 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001271 goto out_check;
1272 }
Chris Masonc31f8832008-01-08 15:46:31 -05001273
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 fi = btrfs_item_ptr(leaf, path->slots[0],
1275 struct btrfs_file_extent_item);
1276 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001277
Yan Zhengd899e052008-10-30 14:25:28 -04001278 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1279 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001281 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 extent_end = found_key.offset +
1283 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001284 disk_num_bytes =
1285 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 if (extent_end <= start) {
1287 path->slots[0]++;
1288 goto next_slot;
1289 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001290 if (disk_bytenr == 0)
1291 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 if (btrfs_file_extent_compression(leaf, fi) ||
1293 btrfs_file_extent_encryption(leaf, fi) ||
1294 btrfs_file_extent_other_encoding(leaf, fi))
1295 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001296 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1297 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001298 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001299 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001300 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001301 found_key.offset -
1302 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001303 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001304 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001305 disk_bytenr += cur_offset - found_key.offset;
1306 num_bytes = min(end + 1, extent_end) - cur_offset;
1307 /*
1308 * force cow if csum exists in the range.
1309 * this ensure that csum for a given extent are
1310 * either valid or do not exist.
1311 */
1312 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1313 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 nocow = 1;
1315 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1316 extent_end = found_key.offset +
1317 btrfs_file_extent_inline_len(leaf, fi);
1318 extent_end = ALIGN(extent_end, root->sectorsize);
1319 } else {
1320 BUG_ON(1);
1321 }
1322out_check:
1323 if (extent_end <= start) {
1324 path->slots[0]++;
1325 goto next_slot;
1326 }
1327 if (!nocow) {
1328 if (cow_start == (u64)-1)
1329 cow_start = cur_offset;
1330 cur_offset = extent_end;
1331 if (cur_offset > end)
1332 break;
1333 path->slots[0]++;
1334 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001335 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001336
David Sterbab3b4aa72011-04-21 01:20:15 +02001337 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001339 ret = __cow_file_range(trans, inode, root, locked_page,
1340 cow_start, found_key.offset - 1,
1341 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001342 if (ret) {
1343 btrfs_abort_transaction(trans, root, ret);
1344 goto error;
1345 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001347 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001348
Yan Zhengd899e052008-10-30 14:25:28 -04001349 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1350 struct extent_map *em;
1351 struct extent_map_tree *em_tree;
1352 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001353 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001354 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001355 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001356 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001357 em->len = num_bytes;
1358 em->block_len = num_bytes;
1359 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001360 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001361 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001362 em->mod_start = em->start;
1363 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001364 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001365 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001366 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001367 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001368 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001369 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001370 if (!ret)
1371 list_move(&em->list,
1372 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001373 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001374 if (ret != -EEXIST) {
1375 free_extent_map(em);
1376 break;
1377 }
1378 btrfs_drop_extent_cache(inode, em->start,
1379 em->start + em->len - 1, 0);
1380 }
1381 type = BTRFS_ORDERED_PREALLOC;
1382 } else {
1383 type = BTRFS_ORDERED_NOCOW;
1384 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001385
1386 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001387 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001388 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001389
Yan, Zhengefa56462010-05-16 10:49:59 -04001390 if (root->root_key.objectid ==
1391 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1392 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1393 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001394 if (ret) {
1395 btrfs_abort_transaction(trans, root, ret);
1396 goto error;
1397 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001398 }
1399
Yan Zhengd899e052008-10-30 14:25:28 -04001400 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001401 cur_offset, cur_offset + num_bytes - 1,
1402 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1403 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1404 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001405 cur_offset = extent_end;
1406 if (cur_offset > end)
1407 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001408 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001409 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001410
Josef Bacik17ca04a2012-05-31 15:58:55 -04001411 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001412 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001413 cur_offset = end;
1414 }
1415
Yan Zheng80ff3852008-10-30 14:20:02 -04001416 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001417 ret = __cow_file_range(trans, inode, root, locked_page,
1418 cow_start, end,
1419 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001420 if (ret) {
1421 btrfs_abort_transaction(trans, root, ret);
1422 goto error;
1423 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001424 }
1425
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001426error:
Miao Xiea698d0752012-09-20 01:51:59 -06001427 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001428 if (!ret)
1429 ret = err;
1430
Josef Bacik17ca04a2012-05-31 15:58:55 -04001431 if (ret && cur_offset < end)
1432 extent_clear_unlock_delalloc(inode,
1433 &BTRFS_I(inode)->io_tree,
1434 cur_offset, end, locked_page,
1435 EXTENT_CLEAR_UNLOCK_PAGE |
1436 EXTENT_CLEAR_UNLOCK |
1437 EXTENT_CLEAR_DELALLOC |
1438 EXTENT_CLEAR_DIRTY |
1439 EXTENT_SET_WRITEBACK |
1440 EXTENT_END_WRITEBACK);
1441
Yan Zheng7ea394f2008-08-05 13:05:02 -04001442 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001443 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001444}
1445
Chris Masond352ac62008-09-29 15:18:18 -04001446/*
1447 * extent_io.c call back to do delayed allocation processing
1448 */
Chris Masonc8b97812008-10-29 14:49:59 -04001449static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001450 u64 start, u64 end, int *page_started,
1451 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001452{
Chris Masonbe20aa92007-12-17 20:14:01 -05001453 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001454 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001455
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001456 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001457 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001458 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001459 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001460 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001461 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001462 } else if (!btrfs_test_opt(root, COMPRESS) &&
1463 !(BTRFS_I(inode)->force_compress) &&
1464 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001465 ret = cow_file_range(inode, locked_page, start, end,
1466 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001467 } else {
1468 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1469 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001470 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001471 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001472 }
Chris Masonb888db22007-08-27 16:49:44 -04001473 return ret;
1474}
1475
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001476static void btrfs_split_extent_hook(struct inode *inode,
1477 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001478{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001479 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001480 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001481 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001482
Josef Bacik9e0baf62011-07-15 15:16:44 +00001483 spin_lock(&BTRFS_I(inode)->lock);
1484 BTRFS_I(inode)->outstanding_extents++;
1485 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001486}
1487
1488/*
1489 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1490 * extents so we can keep track of new extents that are just merged onto old
1491 * extents, such as when we are doing sequential writes, so we can properly
1492 * account for the metadata space we'll need.
1493 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001494static void btrfs_merge_extent_hook(struct inode *inode,
1495 struct extent_state *new,
1496 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001497{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001498 /* not delalloc, ignore it */
1499 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001500 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001501
Josef Bacik9e0baf62011-07-15 15:16:44 +00001502 spin_lock(&BTRFS_I(inode)->lock);
1503 BTRFS_I(inode)->outstanding_extents--;
1504 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001505}
1506
Chris Masond352ac62008-09-29 15:18:18 -04001507/*
1508 * extent_io.c set_bit_hook, used to track delayed allocation
1509 * bytes in this file, and to maintain the list of inodes that
1510 * have pending delalloc work to be done.
1511 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001512static void btrfs_set_bit_hook(struct inode *inode,
1513 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001514{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001515
Chris Mason75eff682008-12-15 15:54:40 -05001516 /*
1517 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001518 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001519 * bit, which is only set or cleared with irqs on
1520 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001521 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001522 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001523 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001524 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001525
Josef Bacik9e0baf62011-07-15 15:16:44 +00001526 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001527 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001528 } else {
1529 spin_lock(&BTRFS_I(inode)->lock);
1530 BTRFS_I(inode)->outstanding_extents++;
1531 spin_unlock(&BTRFS_I(inode)->lock);
1532 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001533
Miao Xie963d6782013-01-29 10:10:51 +00001534 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1535 root->fs_info->delalloc_batch);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001536 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001537 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a52013-01-29 10:11:59 +00001538 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1539 &BTRFS_I(inode)->runtime_flags)) {
1540 spin_lock(&root->fs_info->delalloc_lock);
1541 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1542 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1543 &root->fs_info->delalloc_inodes);
1544 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1545 &BTRFS_I(inode)->runtime_flags);
1546 }
1547 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001548 }
Miao Xiedf0af1a52013-01-29 10:11:59 +00001549 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001550 }
Chris Mason291d6732008-01-29 15:55:23 -05001551}
1552
Chris Masond352ac62008-09-29 15:18:18 -04001553/*
1554 * extent_io.c clear_bit_hook, see set_bit_hook for why
1555 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001556static void btrfs_clear_bit_hook(struct inode *inode,
1557 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001558{
Chris Mason75eff682008-12-15 15:54:40 -05001559 /*
1560 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001561 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001562 * bit, which is only set or cleared with irqs on
1563 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001564 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001565 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001566 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001567 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001568
Josef Bacik9e0baf62011-07-15 15:16:44 +00001569 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001570 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001571 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1572 spin_lock(&BTRFS_I(inode)->lock);
1573 BTRFS_I(inode)->outstanding_extents--;
1574 spin_unlock(&BTRFS_I(inode)->lock);
1575 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001576
1577 if (*bits & EXTENT_DO_ACCOUNTING)
1578 btrfs_delalloc_release_metadata(inode, len);
1579
Josef Bacik0cb59c92010-07-02 12:14:14 -04001580 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1581 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001582 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001583
Miao Xie963d6782013-01-29 10:10:51 +00001584 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1585 root->fs_info->delalloc_batch);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001586 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001587 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001588 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a52013-01-29 10:11:59 +00001589 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1590 &BTRFS_I(inode)->runtime_flags)) {
1591 spin_lock(&root->fs_info->delalloc_lock);
1592 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1593 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1594 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1595 &BTRFS_I(inode)->runtime_flags);
1596 }
1597 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001598 }
Miao Xiedf0af1a52013-01-29 10:11:59 +00001599 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001600 }
Chris Mason291d6732008-01-29 15:55:23 -05001601}
1602
Chris Masond352ac62008-09-29 15:18:18 -04001603/*
1604 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1605 * we don't create bios that span stripes or chunks
1606 */
David Woodhouse64a16702009-07-15 23:29:37 +01001607int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001608 size_t size, struct bio *bio,
1609 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001610{
1611 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001612 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001613 u64 length = 0;
1614 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001615 int ret;
1616
Chris Mason771ed682008-11-06 22:02:51 -05001617 if (bio_flags & EXTENT_BIO_COMPRESSED)
1618 return 0;
1619
Chris Masonf2d8d742008-04-21 10:03:05 -04001620 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001621 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001622 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001623 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001624 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001625 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001626 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001627 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001628 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001629}
1630
Chris Masond352ac62008-09-29 15:18:18 -04001631/*
1632 * in order to insert checksums into the metadata in large chunks,
1633 * we wait until bio submission time. All the pages in the bio are
1634 * checksummed and sums are attached onto the ordered extent record.
1635 *
1636 * At IO completion time the cums attached on the ordered extent record
1637 * are inserted into the btree
1638 */
Chris Masond3977122009-01-05 21:25:51 -05001639static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1640 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001641 unsigned long bio_flags,
1642 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001643{
Chris Mason065631f2008-02-20 12:07:25 -05001644 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001645 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001646
Chris Masond20f7042008-12-08 16:58:54 -05001647 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001648 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001649 return 0;
1650}
Chris Masone0156402008-04-16 11:15:20 -04001651
Chris Mason4a69a412008-11-06 22:03:00 -05001652/*
1653 * in order to insert checksums into the metadata in large chunks,
1654 * we wait until bio submission time. All the pages in the bio are
1655 * checksummed and sums are attached onto the ordered extent record.
1656 *
1657 * At IO completion time the cums attached on the ordered extent record
1658 * are inserted into the btree
1659 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001660static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001661 int mirror_num, unsigned long bio_flags,
1662 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001663{
1664 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001665 int ret;
1666
1667 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1668 if (ret)
1669 bio_endio(bio, ret);
1670 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001671}
1672
Chris Masond352ac62008-09-29 15:18:18 -04001673/*
Chris Masoncad321a2008-12-17 14:51:42 -05001674 * extent_io.c submission hook. This does the right thing for csum calculation
1675 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001676 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001677static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001678 int mirror_num, unsigned long bio_flags,
1679 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001680{
1681 struct btrfs_root *root = BTRFS_I(inode)->root;
1682 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001683 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001684 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001685 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001686
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001687 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001688
Liu Bo83eea1f2012-07-10 05:28:39 -06001689 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001690 metadata = 2;
1691
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001692 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001693 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1694 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001695 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001696
Chris Masond20f7042008-12-08 16:58:54 -05001697 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001698 ret = btrfs_submit_compressed_read(inode, bio,
1699 mirror_num,
1700 bio_flags);
1701 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001702 } else if (!skip_sum) {
1703 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1704 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001705 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001706 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001707 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001708 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001709 /* csum items have already been cloned */
1710 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1711 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001712 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001713 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001714 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001715 bio_flags, bio_offset,
1716 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001717 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001718 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001719 } else if (!skip_sum) {
1720 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1721 if (ret)
1722 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001723 }
1724
Chris Mason0b86a832008-03-24 15:01:56 -04001725mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001726 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1727
1728out:
1729 if (ret < 0)
1730 bio_endio(bio, ret);
1731 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001732}
Chris Mason6885f302008-02-20 16:11:05 -05001733
Chris Masond352ac62008-09-29 15:18:18 -04001734/*
1735 * given a list of ordered sums record them in the inode. This happens
1736 * at IO completion time based on sums calculated at bio submission time.
1737 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001738static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001739 struct inode *inode, u64 file_offset,
1740 struct list_head *list)
1741{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001742 struct btrfs_ordered_sum *sum;
1743
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001744 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001745 btrfs_csum_file_blocks(trans,
1746 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001747 }
1748 return 0;
1749}
1750
Josef Bacik2ac55d42010-02-03 19:33:23 +00001751int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1752 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001753{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001754 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001755 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001756 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001757}
1758
Chris Masond352ac62008-09-29 15:18:18 -04001759/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001760struct btrfs_writepage_fixup {
1761 struct page *page;
1762 struct btrfs_work work;
1763};
1764
Christoph Hellwigb2950862008-12-02 09:54:17 -05001765static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001766{
1767 struct btrfs_writepage_fixup *fixup;
1768 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001769 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001770 struct page *page;
1771 struct inode *inode;
1772 u64 page_start;
1773 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001774 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001775
1776 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1777 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001778again:
Chris Mason247e7432008-07-17 12:53:51 -04001779 lock_page(page);
1780 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1781 ClearPageChecked(page);
1782 goto out_page;
1783 }
1784
1785 inode = page->mapping->host;
1786 page_start = page_offset(page);
1787 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1788
Josef Bacik2ac55d42010-02-03 19:33:23 +00001789 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001790 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001791
1792 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001793 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001794 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001795
1796 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1797 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001798 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1799 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001800 unlock_page(page);
1801 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001802 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001803 goto again;
1804 }
Chris Mason247e7432008-07-17 12:53:51 -04001805
Jeff Mahoney87826df2012-02-15 16:23:57 +01001806 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1807 if (ret) {
1808 mapping_set_error(page->mapping, ret);
1809 end_extent_writepage(page, ret, page_start, page_end);
1810 ClearPageChecked(page);
1811 goto out;
1812 }
1813
Josef Bacik2ac55d42010-02-03 19:33:23 +00001814 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001815 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001816 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001817out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001818 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1819 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001820out_page:
1821 unlock_page(page);
1822 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001823 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001824}
1825
1826/*
1827 * There are a few paths in the higher layers of the kernel that directly
1828 * set the page dirty bit without asking the filesystem if it is a
1829 * good idea. This causes problems because we want to make sure COW
1830 * properly happens and the data=ordered rules are followed.
1831 *
Chris Masonc8b97812008-10-29 14:49:59 -04001832 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001833 * hasn't been properly setup for IO. We kick off an async process
1834 * to fix it up. The async helper will wait for ordered extents, set
1835 * the delalloc bit and make it safe to write the page.
1836 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001837static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001838{
1839 struct inode *inode = page->mapping->host;
1840 struct btrfs_writepage_fixup *fixup;
1841 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001842
Chris Mason8b62b722009-09-02 16:53:46 -04001843 /* this page is properly in the ordered list */
1844 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001845 return 0;
1846
1847 if (PageChecked(page))
1848 return -EAGAIN;
1849
1850 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1851 if (!fixup)
1852 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001853
Chris Mason247e7432008-07-17 12:53:51 -04001854 SetPageChecked(page);
1855 page_cache_get(page);
1856 fixup->work.func = btrfs_writepage_fixup_worker;
1857 fixup->page = page;
1858 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001859 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001860}
1861
Yan Zhengd899e052008-10-30 14:25:28 -04001862static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1863 struct inode *inode, u64 file_pos,
1864 u64 disk_bytenr, u64 disk_num_bytes,
1865 u64 num_bytes, u64 ram_bytes,
1866 u8 compression, u8 encryption,
1867 u16 other_encoding, int extent_type)
1868{
1869 struct btrfs_root *root = BTRFS_I(inode)->root;
1870 struct btrfs_file_extent_item *fi;
1871 struct btrfs_path *path;
1872 struct extent_buffer *leaf;
1873 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001874 int ret;
1875
1876 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001877 if (!path)
1878 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001879
Chris Masonb9473432009-03-13 11:00:37 -04001880 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001881
1882 /*
1883 * we may be replacing one extent in the tree with another.
1884 * The new extent is pinned in the extent map, and we don't want
1885 * to drop it from the cache until it is completely in the btree.
1886 *
1887 * So, tell btrfs_drop_extents to leave this extent in the cache.
1888 * the caller is expected to unpin it and allow it to be merged
1889 * with the others.
1890 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001891 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001892 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001893 if (ret)
1894 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001895
Li Zefan33345d012011-04-20 10:31:50 +08001896 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001897 ins.offset = file_pos;
1898 ins.type = BTRFS_EXTENT_DATA_KEY;
1899 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001900 if (ret)
1901 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001902 leaf = path->nodes[0];
1903 fi = btrfs_item_ptr(leaf, path->slots[0],
1904 struct btrfs_file_extent_item);
1905 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1906 btrfs_set_file_extent_type(leaf, fi, extent_type);
1907 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1908 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1909 btrfs_set_file_extent_offset(leaf, fi, 0);
1910 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1911 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1912 btrfs_set_file_extent_compression(leaf, fi, compression);
1913 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1914 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001915
Yan Zhengd899e052008-10-30 14:25:28 -04001916 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001917 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001918
1919 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001920
1921 ins.objectid = disk_bytenr;
1922 ins.offset = disk_num_bytes;
1923 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001924 ret = btrfs_alloc_reserved_file_extent(trans, root,
1925 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001926 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001927out:
Yan Zhengd899e052008-10-30 14:25:28 -04001928 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001929
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001930 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001931}
1932
Liu Bo38c227d2013-01-29 03:18:40 +00001933/* snapshot-aware defrag */
1934struct sa_defrag_extent_backref {
1935 struct rb_node node;
1936 struct old_sa_defrag_extent *old;
1937 u64 root_id;
1938 u64 inum;
1939 u64 file_pos;
1940 u64 extent_offset;
1941 u64 num_bytes;
1942 u64 generation;
1943};
1944
1945struct old_sa_defrag_extent {
1946 struct list_head list;
1947 struct new_sa_defrag_extent *new;
1948
1949 u64 extent_offset;
1950 u64 bytenr;
1951 u64 offset;
1952 u64 len;
1953 int count;
1954};
1955
1956struct new_sa_defrag_extent {
1957 struct rb_root root;
1958 struct list_head head;
1959 struct btrfs_path *path;
1960 struct inode *inode;
1961 u64 file_pos;
1962 u64 len;
1963 u64 bytenr;
1964 u64 disk_len;
1965 u8 compress_type;
1966};
1967
1968static int backref_comp(struct sa_defrag_extent_backref *b1,
1969 struct sa_defrag_extent_backref *b2)
1970{
1971 if (b1->root_id < b2->root_id)
1972 return -1;
1973 else if (b1->root_id > b2->root_id)
1974 return 1;
1975
1976 if (b1->inum < b2->inum)
1977 return -1;
1978 else if (b1->inum > b2->inum)
1979 return 1;
1980
1981 if (b1->file_pos < b2->file_pos)
1982 return -1;
1983 else if (b1->file_pos > b2->file_pos)
1984 return 1;
1985
1986 /*
1987 * [------------------------------] ===> (a range of space)
1988 * |<--->| |<---->| =============> (fs/file tree A)
1989 * |<---------------------------->| ===> (fs/file tree B)
1990 *
1991 * A range of space can refer to two file extents in one tree while
1992 * refer to only one file extent in another tree.
1993 *
1994 * So we may process a disk offset more than one time(two extents in A)
1995 * and locate at the same extent(one extent in B), then insert two same
1996 * backrefs(both refer to the extent in B).
1997 */
1998 return 0;
1999}
2000
2001static void backref_insert(struct rb_root *root,
2002 struct sa_defrag_extent_backref *backref)
2003{
2004 struct rb_node **p = &root->rb_node;
2005 struct rb_node *parent = NULL;
2006 struct sa_defrag_extent_backref *entry;
2007 int ret;
2008
2009 while (*p) {
2010 parent = *p;
2011 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2012
2013 ret = backref_comp(backref, entry);
2014 if (ret < 0)
2015 p = &(*p)->rb_left;
2016 else
2017 p = &(*p)->rb_right;
2018 }
2019
2020 rb_link_node(&backref->node, parent, p);
2021 rb_insert_color(&backref->node, root);
2022}
2023
2024/*
2025 * Note the backref might has changed, and in this case we just return 0.
2026 */
2027static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2028 void *ctx)
2029{
2030 struct btrfs_file_extent_item *extent;
2031 struct btrfs_fs_info *fs_info;
2032 struct old_sa_defrag_extent *old = ctx;
2033 struct new_sa_defrag_extent *new = old->new;
2034 struct btrfs_path *path = new->path;
2035 struct btrfs_key key;
2036 struct btrfs_root *root;
2037 struct sa_defrag_extent_backref *backref;
2038 struct extent_buffer *leaf;
2039 struct inode *inode = new->inode;
2040 int slot;
2041 int ret;
2042 u64 extent_offset;
2043 u64 num_bytes;
2044
2045 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2046 inum == btrfs_ino(inode))
2047 return 0;
2048
2049 key.objectid = root_id;
2050 key.type = BTRFS_ROOT_ITEM_KEY;
2051 key.offset = (u64)-1;
2052
2053 fs_info = BTRFS_I(inode)->root->fs_info;
2054 root = btrfs_read_fs_root_no_name(fs_info, &key);
2055 if (IS_ERR(root)) {
2056 if (PTR_ERR(root) == -ENOENT)
2057 return 0;
2058 WARN_ON(1);
2059 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2060 inum, offset, root_id);
2061 return PTR_ERR(root);
2062 }
2063
2064 key.objectid = inum;
2065 key.type = BTRFS_EXTENT_DATA_KEY;
2066 if (offset > (u64)-1 << 32)
2067 key.offset = 0;
2068 else
2069 key.offset = offset;
2070
2071 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2072 if (ret < 0) {
2073 WARN_ON(1);
2074 return ret;
2075 }
2076
2077 while (1) {
2078 cond_resched();
2079
2080 leaf = path->nodes[0];
2081 slot = path->slots[0];
2082
2083 if (slot >= btrfs_header_nritems(leaf)) {
2084 ret = btrfs_next_leaf(root, path);
2085 if (ret < 0) {
2086 goto out;
2087 } else if (ret > 0) {
2088 ret = 0;
2089 goto out;
2090 }
2091 continue;
2092 }
2093
2094 path->slots[0]++;
2095
2096 btrfs_item_key_to_cpu(leaf, &key, slot);
2097
2098 if (key.objectid > inum)
2099 goto out;
2100
2101 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2102 continue;
2103
2104 extent = btrfs_item_ptr(leaf, slot,
2105 struct btrfs_file_extent_item);
2106
2107 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2108 continue;
2109
2110 extent_offset = btrfs_file_extent_offset(leaf, extent);
2111 if (key.offset - extent_offset != offset)
2112 continue;
2113
2114 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2115 if (extent_offset >= old->extent_offset + old->offset +
2116 old->len || extent_offset + num_bytes <=
2117 old->extent_offset + old->offset)
2118 continue;
2119
2120 break;
2121 }
2122
2123 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2124 if (!backref) {
2125 ret = -ENOENT;
2126 goto out;
2127 }
2128
2129 backref->root_id = root_id;
2130 backref->inum = inum;
2131 backref->file_pos = offset + extent_offset;
2132 backref->num_bytes = num_bytes;
2133 backref->extent_offset = extent_offset;
2134 backref->generation = btrfs_file_extent_generation(leaf, extent);
2135 backref->old = old;
2136 backref_insert(&new->root, backref);
2137 old->count++;
2138out:
2139 btrfs_release_path(path);
2140 WARN_ON(ret);
2141 return ret;
2142}
2143
2144static noinline bool record_extent_backrefs(struct btrfs_path *path,
2145 struct new_sa_defrag_extent *new)
2146{
2147 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2148 struct old_sa_defrag_extent *old, *tmp;
2149 int ret;
2150
2151 new->path = path;
2152
2153 list_for_each_entry_safe(old, tmp, &new->head, list) {
2154 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2155 path, record_one_backref,
2156 old);
2157 BUG_ON(ret < 0 && ret != -ENOENT);
2158
2159 /* no backref to be processed for this extent */
2160 if (!old->count) {
2161 list_del(&old->list);
2162 kfree(old);
2163 }
2164 }
2165
2166 if (list_empty(&new->head))
2167 return false;
2168
2169 return true;
2170}
2171
2172static int relink_is_mergable(struct extent_buffer *leaf,
2173 struct btrfs_file_extent_item *fi,
2174 u64 disk_bytenr)
2175{
2176 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2177 return 0;
2178
2179 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2180 return 0;
2181
2182 if (btrfs_file_extent_compression(leaf, fi) ||
2183 btrfs_file_extent_encryption(leaf, fi) ||
2184 btrfs_file_extent_other_encoding(leaf, fi))
2185 return 0;
2186
2187 return 1;
2188}
2189
2190/*
2191 * Note the backref might has changed, and in this case we just return 0.
2192 */
2193static noinline int relink_extent_backref(struct btrfs_path *path,
2194 struct sa_defrag_extent_backref *prev,
2195 struct sa_defrag_extent_backref *backref)
2196{
2197 struct btrfs_file_extent_item *extent;
2198 struct btrfs_file_extent_item *item;
2199 struct btrfs_ordered_extent *ordered;
2200 struct btrfs_trans_handle *trans;
2201 struct btrfs_fs_info *fs_info;
2202 struct btrfs_root *root;
2203 struct btrfs_key key;
2204 struct extent_buffer *leaf;
2205 struct old_sa_defrag_extent *old = backref->old;
2206 struct new_sa_defrag_extent *new = old->new;
2207 struct inode *src_inode = new->inode;
2208 struct inode *inode;
2209 struct extent_state *cached = NULL;
2210 int ret = 0;
2211 u64 start;
2212 u64 len;
2213 u64 lock_start;
2214 u64 lock_end;
2215 bool merge = false;
2216 int index;
2217
2218 if (prev && prev->root_id == backref->root_id &&
2219 prev->inum == backref->inum &&
2220 prev->file_pos + prev->num_bytes == backref->file_pos)
2221 merge = true;
2222
2223 /* step 1: get root */
2224 key.objectid = backref->root_id;
2225 key.type = BTRFS_ROOT_ITEM_KEY;
2226 key.offset = (u64)-1;
2227
2228 fs_info = BTRFS_I(src_inode)->root->fs_info;
2229 index = srcu_read_lock(&fs_info->subvol_srcu);
2230
2231 root = btrfs_read_fs_root_no_name(fs_info, &key);
2232 if (IS_ERR(root)) {
2233 srcu_read_unlock(&fs_info->subvol_srcu, index);
2234 if (PTR_ERR(root) == -ENOENT)
2235 return 0;
2236 return PTR_ERR(root);
2237 }
2238 if (btrfs_root_refs(&root->root_item) == 0) {
2239 srcu_read_unlock(&fs_info->subvol_srcu, index);
2240 /* parse ENOENT to 0 */
2241 return 0;
2242 }
2243
2244 /* step 2: get inode */
2245 key.objectid = backref->inum;
2246 key.type = BTRFS_INODE_ITEM_KEY;
2247 key.offset = 0;
2248
2249 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2250 if (IS_ERR(inode)) {
2251 srcu_read_unlock(&fs_info->subvol_srcu, index);
2252 return 0;
2253 }
2254
2255 srcu_read_unlock(&fs_info->subvol_srcu, index);
2256
2257 /* step 3: relink backref */
2258 lock_start = backref->file_pos;
2259 lock_end = backref->file_pos + backref->num_bytes - 1;
2260 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2261 0, &cached);
2262
2263 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2264 if (ordered) {
2265 btrfs_put_ordered_extent(ordered);
2266 goto out_unlock;
2267 }
2268
2269 trans = btrfs_join_transaction(root);
2270 if (IS_ERR(trans)) {
2271 ret = PTR_ERR(trans);
2272 goto out_unlock;
2273 }
2274
2275 key.objectid = backref->inum;
2276 key.type = BTRFS_EXTENT_DATA_KEY;
2277 key.offset = backref->file_pos;
2278
2279 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2280 if (ret < 0) {
2281 goto out_free_path;
2282 } else if (ret > 0) {
2283 ret = 0;
2284 goto out_free_path;
2285 }
2286
2287 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2288 struct btrfs_file_extent_item);
2289
2290 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2291 backref->generation)
2292 goto out_free_path;
2293
2294 btrfs_release_path(path);
2295
2296 start = backref->file_pos;
2297 if (backref->extent_offset < old->extent_offset + old->offset)
2298 start += old->extent_offset + old->offset -
2299 backref->extent_offset;
2300
2301 len = min(backref->extent_offset + backref->num_bytes,
2302 old->extent_offset + old->offset + old->len);
2303 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2304
2305 ret = btrfs_drop_extents(trans, root, inode, start,
2306 start + len, 1);
2307 if (ret)
2308 goto out_free_path;
2309again:
2310 key.objectid = btrfs_ino(inode);
2311 key.type = BTRFS_EXTENT_DATA_KEY;
2312 key.offset = start;
2313
2314 if (merge) {
2315 struct btrfs_file_extent_item *fi;
2316 u64 extent_len;
2317 struct btrfs_key found_key;
2318
2319 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2320 if (ret < 0)
2321 goto out_free_path;
2322
2323 path->slots[0]--;
2324 leaf = path->nodes[0];
2325 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2326
2327 fi = btrfs_item_ptr(leaf, path->slots[0],
2328 struct btrfs_file_extent_item);
2329 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2330
2331 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2332 extent_len + found_key.offset == start) {
2333 btrfs_set_file_extent_num_bytes(leaf, fi,
2334 extent_len + len);
2335 btrfs_mark_buffer_dirty(leaf);
2336 inode_add_bytes(inode, len);
2337
2338 ret = 1;
2339 goto out_free_path;
2340 } else {
2341 merge = false;
2342 btrfs_release_path(path);
2343 goto again;
2344 }
2345 }
2346
2347 ret = btrfs_insert_empty_item(trans, root, path, &key,
2348 sizeof(*extent));
2349 if (ret) {
2350 btrfs_abort_transaction(trans, root, ret);
2351 goto out_free_path;
2352 }
2353
2354 leaf = path->nodes[0];
2355 item = btrfs_item_ptr(leaf, path->slots[0],
2356 struct btrfs_file_extent_item);
2357 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2358 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2359 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2360 btrfs_set_file_extent_num_bytes(leaf, item, len);
2361 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2362 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2363 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2364 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2365 btrfs_set_file_extent_encryption(leaf, item, 0);
2366 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2367
2368 btrfs_mark_buffer_dirty(leaf);
2369 inode_add_bytes(inode, len);
2370
2371 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2372 new->disk_len, 0,
2373 backref->root_id, backref->inum,
2374 new->file_pos, 0); /* start - extent_offset */
2375 if (ret) {
2376 btrfs_abort_transaction(trans, root, ret);
2377 goto out_free_path;
2378 }
2379
2380 ret = 1;
2381out_free_path:
2382 btrfs_release_path(path);
2383 btrfs_end_transaction(trans, root);
2384out_unlock:
2385 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2386 &cached, GFP_NOFS);
2387 iput(inode);
2388 return ret;
2389}
2390
2391static void relink_file_extents(struct new_sa_defrag_extent *new)
2392{
2393 struct btrfs_path *path;
2394 struct old_sa_defrag_extent *old, *tmp;
2395 struct sa_defrag_extent_backref *backref;
2396 struct sa_defrag_extent_backref *prev = NULL;
2397 struct inode *inode;
2398 struct btrfs_root *root;
2399 struct rb_node *node;
2400 int ret;
2401
2402 inode = new->inode;
2403 root = BTRFS_I(inode)->root;
2404
2405 path = btrfs_alloc_path();
2406 if (!path)
2407 return;
2408
2409 if (!record_extent_backrefs(path, new)) {
2410 btrfs_free_path(path);
2411 goto out;
2412 }
2413 btrfs_release_path(path);
2414
2415 while (1) {
2416 node = rb_first(&new->root);
2417 if (!node)
2418 break;
2419 rb_erase(node, &new->root);
2420
2421 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2422
2423 ret = relink_extent_backref(path, prev, backref);
2424 WARN_ON(ret < 0);
2425
2426 kfree(prev);
2427
2428 if (ret == 1)
2429 prev = backref;
2430 else
2431 prev = NULL;
2432 cond_resched();
2433 }
2434 kfree(prev);
2435
2436 btrfs_free_path(path);
2437
2438 list_for_each_entry_safe(old, tmp, &new->head, list) {
2439 list_del(&old->list);
2440 kfree(old);
2441 }
2442out:
2443 atomic_dec(&root->fs_info->defrag_running);
2444 wake_up(&root->fs_info->transaction_wait);
2445
2446 kfree(new);
2447}
2448
2449static struct new_sa_defrag_extent *
2450record_old_file_extents(struct inode *inode,
2451 struct btrfs_ordered_extent *ordered)
2452{
2453 struct btrfs_root *root = BTRFS_I(inode)->root;
2454 struct btrfs_path *path;
2455 struct btrfs_key key;
2456 struct old_sa_defrag_extent *old, *tmp;
2457 struct new_sa_defrag_extent *new;
2458 int ret;
2459
2460 new = kmalloc(sizeof(*new), GFP_NOFS);
2461 if (!new)
2462 return NULL;
2463
2464 new->inode = inode;
2465 new->file_pos = ordered->file_offset;
2466 new->len = ordered->len;
2467 new->bytenr = ordered->start;
2468 new->disk_len = ordered->disk_len;
2469 new->compress_type = ordered->compress_type;
2470 new->root = RB_ROOT;
2471 INIT_LIST_HEAD(&new->head);
2472
2473 path = btrfs_alloc_path();
2474 if (!path)
2475 goto out_kfree;
2476
2477 key.objectid = btrfs_ino(inode);
2478 key.type = BTRFS_EXTENT_DATA_KEY;
2479 key.offset = new->file_pos;
2480
2481 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2482 if (ret < 0)
2483 goto out_free_path;
2484 if (ret > 0 && path->slots[0] > 0)
2485 path->slots[0]--;
2486
2487 /* find out all the old extents for the file range */
2488 while (1) {
2489 struct btrfs_file_extent_item *extent;
2490 struct extent_buffer *l;
2491 int slot;
2492 u64 num_bytes;
2493 u64 offset;
2494 u64 end;
2495 u64 disk_bytenr;
2496 u64 extent_offset;
2497
2498 l = path->nodes[0];
2499 slot = path->slots[0];
2500
2501 if (slot >= btrfs_header_nritems(l)) {
2502 ret = btrfs_next_leaf(root, path);
2503 if (ret < 0)
2504 goto out_free_list;
2505 else if (ret > 0)
2506 break;
2507 continue;
2508 }
2509
2510 btrfs_item_key_to_cpu(l, &key, slot);
2511
2512 if (key.objectid != btrfs_ino(inode))
2513 break;
2514 if (key.type != BTRFS_EXTENT_DATA_KEY)
2515 break;
2516 if (key.offset >= new->file_pos + new->len)
2517 break;
2518
2519 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2520
2521 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2522 if (key.offset + num_bytes < new->file_pos)
2523 goto next;
2524
2525 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2526 if (!disk_bytenr)
2527 goto next;
2528
2529 extent_offset = btrfs_file_extent_offset(l, extent);
2530
2531 old = kmalloc(sizeof(*old), GFP_NOFS);
2532 if (!old)
2533 goto out_free_list;
2534
2535 offset = max(new->file_pos, key.offset);
2536 end = min(new->file_pos + new->len, key.offset + num_bytes);
2537
2538 old->bytenr = disk_bytenr;
2539 old->extent_offset = extent_offset;
2540 old->offset = offset - key.offset;
2541 old->len = end - offset;
2542 old->new = new;
2543 old->count = 0;
2544 list_add_tail(&old->list, &new->head);
2545next:
2546 path->slots[0]++;
2547 cond_resched();
2548 }
2549
2550 btrfs_free_path(path);
2551 atomic_inc(&root->fs_info->defrag_running);
2552
2553 return new;
2554
2555out_free_list:
2556 list_for_each_entry_safe(old, tmp, &new->head, list) {
2557 list_del(&old->list);
2558 kfree(old);
2559 }
2560out_free_path:
2561 btrfs_free_path(path);
2562out_kfree:
2563 kfree(new);
2564 return NULL;
2565}
2566
Chris Mason5d13a982009-03-13 11:41:46 -04002567/*
2568 * helper function for btrfs_finish_ordered_io, this
2569 * just reads in some of the csum leaves to prime them into ram
2570 * before we start the transaction. It limits the amount of btree
2571 * reads required while inside the transaction.
2572 */
Chris Masond352ac62008-09-29 15:18:18 -04002573/* as ordered data IO finishes, this gets called so we can finish
2574 * an ordered extent if the range of bytes in the file it covers are
2575 * fully written.
2576 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002577static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002578{
Josef Bacik5fd02042012-05-02 14:00:54 -04002579 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002580 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002581 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002582 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002583 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002584 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002585 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002586 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002587 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002588
Liu Bo83eea1f2012-07-10 05:28:39 -06002589 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002590
Josef Bacik5fd02042012-05-02 14:00:54 -04002591 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2592 ret = -EIO;
2593 goto out;
2594 }
2595
Yan, Zhengc21677542009-11-12 09:34:21 +00002596 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002597 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002598 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2599 if (nolock)
2600 trans = btrfs_join_transaction_nolock(root);
2601 else
2602 trans = btrfs_join_transaction(root);
2603 if (IS_ERR(trans)) {
2604 ret = PTR_ERR(trans);
2605 trans = NULL;
2606 goto out;
Yan, Zhengc21677542009-11-12 09:34:21 +00002607 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002608 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2609 ret = btrfs_update_inode_fallback(trans, root, inode);
2610 if (ret) /* -ENOMEM or corruption */
2611 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00002612 goto out;
2613 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002614
Josef Bacik2ac55d42010-02-03 19:33:23 +00002615 lock_extent_bits(io_tree, ordered_extent->file_offset,
2616 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002617 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002618
Liu Bo38c227d2013-01-29 03:18:40 +00002619 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2620 ordered_extent->file_offset + ordered_extent->len - 1,
2621 EXTENT_DEFRAG, 1, cached_state);
2622 if (ret) {
2623 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2624 if (last_snapshot >= BTRFS_I(inode)->generation)
2625 /* the inode is shared */
2626 new = record_old_file_extents(inode, ordered_extent);
2627
2628 clear_extent_bit(io_tree, ordered_extent->file_offset,
2629 ordered_extent->file_offset + ordered_extent->len - 1,
2630 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2631 }
2632
Josef Bacik0cb59c92010-07-02 12:14:14 -04002633 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002634 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002635 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002636 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002637 if (IS_ERR(trans)) {
2638 ret = PTR_ERR(trans);
2639 trans = NULL;
2640 goto out_unlock;
2641 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002642 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00002643
Chris Masonc8b97812008-10-29 14:49:59 -04002644 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002645 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002646 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002647 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002648 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002649 ordered_extent->file_offset,
2650 ordered_extent->file_offset +
2651 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002652 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002653 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002654 ret = insert_reserved_file_extent(trans, inode,
2655 ordered_extent->file_offset,
2656 ordered_extent->start,
2657 ordered_extent->disk_len,
2658 ordered_extent->len,
2659 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002660 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002661 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002662 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002663 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2664 ordered_extent->file_offset, ordered_extent->len,
2665 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002666 if (ret < 0) {
2667 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002668 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002669 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002670
Chris Masone6dcd2d2008-07-17 12:53:50 -04002671 add_pending_csums(trans, inode, ordered_extent->file_offset,
2672 &ordered_extent->list);
2673
Josef Bacik6c760c02012-11-09 10:53:21 -05002674 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2675 ret = btrfs_update_inode_fallback(trans, root, inode);
2676 if (ret) { /* -ENOMEM or corruption */
2677 btrfs_abort_transaction(trans, root, ret);
2678 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002679 }
2680 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002681out_unlock:
2682 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2683 ordered_extent->file_offset +
2684 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc21677542009-11-12 09:34:21 +00002685out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002686 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002687 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002688 if (trans)
2689 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002690
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002691 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002692 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2693 ordered_extent->file_offset +
2694 ordered_extent->len - 1, NULL, GFP_NOFS);
2695
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002696 /*
2697 * If the ordered extent had an IOERR or something else went
2698 * wrong we need to return the space for this ordered extent
2699 * back to the allocator.
2700 */
2701 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2702 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2703 btrfs_free_reserved_extent(root, ordered_extent->start,
2704 ordered_extent->disk_len);
2705 }
2706
2707
Josef Bacik5fd02042012-05-02 14:00:54 -04002708 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002709 * This needs to be done to make sure anybody waiting knows we are done
2710 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002711 */
2712 btrfs_remove_ordered_extent(inode, ordered_extent);
2713
Liu Bo38c227d2013-01-29 03:18:40 +00002714 /* for snapshot-aware defrag */
2715 if (new)
2716 relink_file_extents(new);
2717
Chris Masone6dcd2d2008-07-17 12:53:50 -04002718 /* once for us */
2719 btrfs_put_ordered_extent(ordered_extent);
2720 /* once for the tree */
2721 btrfs_put_ordered_extent(ordered_extent);
2722
Josef Bacik5fd02042012-05-02 14:00:54 -04002723 return ret;
2724}
2725
2726static void finish_ordered_fn(struct btrfs_work *work)
2727{
2728 struct btrfs_ordered_extent *ordered_extent;
2729 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2730 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002731}
2732
Christoph Hellwigb2950862008-12-02 09:54:17 -05002733static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002734 struct extent_state *state, int uptodate)
2735{
Josef Bacik5fd02042012-05-02 14:00:54 -04002736 struct inode *inode = page->mapping->host;
2737 struct btrfs_root *root = BTRFS_I(inode)->root;
2738 struct btrfs_ordered_extent *ordered_extent = NULL;
2739 struct btrfs_workers *workers;
2740
liubo1abe9b82011-03-24 11:18:59 +00002741 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2742
Chris Mason8b62b722009-09-02 16:53:46 -04002743 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002744 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2745 end - start + 1, uptodate))
2746 return 0;
2747
2748 ordered_extent->work.func = finish_ordered_fn;
2749 ordered_extent->work.flags = 0;
2750
Liu Bo83eea1f2012-07-10 05:28:39 -06002751 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002752 workers = &root->fs_info->endio_freespace_worker;
2753 else
2754 workers = &root->fs_info->endio_write_workers;
2755 btrfs_queue_worker(workers, &ordered_extent->work);
2756
2757 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002758}
2759
Chris Masond352ac62008-09-29 15:18:18 -04002760/*
Chris Masond352ac62008-09-29 15:18:18 -04002761 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002762 * if there's a match, we allow the bio to finish. If not, the code in
2763 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002764 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002765static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002766 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002767{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002768 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002769 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002770 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002771 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002772 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002773 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002774 struct btrfs_root *root = BTRFS_I(inode)->root;
2775 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002776
Chris Masond20f7042008-12-08 16:58:54 -05002777 if (PageChecked(page)) {
2778 ClearPageChecked(page);
2779 goto good;
2780 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002781
2782 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002783 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002784
Yan Zheng17d217f2008-12-12 10:03:38 -05002785 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002786 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002787 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2788 GFP_NOFS);
2789 return 0;
2790 }
2791
Yanc2e639f2008-02-04 08:57:25 -05002792 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002793 private = state->private;
2794 ret = 0;
2795 } else {
2796 ret = get_state_private(io_tree, start, &private);
2797 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002798 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002799 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002800 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002801
Chris Masonff79f812007-10-15 16:22:25 -04002802 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2803 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002804 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002805 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002806
Cong Wang7ac687d2011-11-25 23:14:28 +08002807 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002808good:
Chris Mason07157aa2007-08-30 08:50:51 -04002809 return 0;
2810
2811zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002812 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002813 "private %llu\n",
2814 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002815 (unsigned long long)start, csum,
2816 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002817 memset(kaddr + offset, 1, end - start + 1);
2818 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002819 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002820 if (private == 0)
2821 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002822 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002823}
Chris Masonb888db22007-08-27 16:49:44 -04002824
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002825struct delayed_iput {
2826 struct list_head list;
2827 struct inode *inode;
2828};
2829
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002830/* JDM: If this is fs-wide, why can't we add a pointer to
2831 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002832void btrfs_add_delayed_iput(struct inode *inode)
2833{
2834 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2835 struct delayed_iput *delayed;
2836
2837 if (atomic_add_unless(&inode->i_count, -1, 1))
2838 return;
2839
2840 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2841 delayed->inode = inode;
2842
2843 spin_lock(&fs_info->delayed_iput_lock);
2844 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2845 spin_unlock(&fs_info->delayed_iput_lock);
2846}
2847
2848void btrfs_run_delayed_iputs(struct btrfs_root *root)
2849{
2850 LIST_HEAD(list);
2851 struct btrfs_fs_info *fs_info = root->fs_info;
2852 struct delayed_iput *delayed;
2853 int empty;
2854
2855 spin_lock(&fs_info->delayed_iput_lock);
2856 empty = list_empty(&fs_info->delayed_iputs);
2857 spin_unlock(&fs_info->delayed_iput_lock);
2858 if (empty)
2859 return;
2860
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002861 spin_lock(&fs_info->delayed_iput_lock);
2862 list_splice_init(&fs_info->delayed_iputs, &list);
2863 spin_unlock(&fs_info->delayed_iput_lock);
2864
2865 while (!list_empty(&list)) {
2866 delayed = list_entry(list.next, struct delayed_iput, list);
2867 list_del(&delayed->list);
2868 iput(delayed->inode);
2869 kfree(delayed);
2870 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002871}
2872
Yan, Zhengd68fc572010-05-16 10:49:58 -04002873/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002874 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002875 * files in the subvolume, it removes orphan item and frees block_rsv
2876 * structure.
2877 */
2878void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2879 struct btrfs_root *root)
2880{
Josef Bacik90290e12011-12-02 15:44:12 -05002881 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002882 int ret;
2883
Josef Bacik8a35d952012-05-23 14:26:42 -04002884 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002885 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2886 return;
2887
Josef Bacik90290e12011-12-02 15:44:12 -05002888 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002889 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002890 spin_unlock(&root->orphan_lock);
2891 return;
2892 }
2893
2894 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2895 spin_unlock(&root->orphan_lock);
2896 return;
2897 }
2898
2899 block_rsv = root->orphan_block_rsv;
2900 root->orphan_block_rsv = NULL;
2901 spin_unlock(&root->orphan_lock);
2902
Yan, Zhengd68fc572010-05-16 10:49:58 -04002903 if (root->orphan_item_inserted &&
2904 btrfs_root_refs(&root->root_item) > 0) {
2905 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2906 root->root_key.objectid);
2907 BUG_ON(ret);
2908 root->orphan_item_inserted = 0;
2909 }
2910
Josef Bacik90290e12011-12-02 15:44:12 -05002911 if (block_rsv) {
2912 WARN_ON(block_rsv->size > 0);
2913 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002914 }
2915}
2916
2917/*
Josef Bacik7b128762008-07-24 12:17:14 -04002918 * This creates an orphan entry for the given inode in case something goes
2919 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002920 *
2921 * NOTE: caller of this function should reserve 5 units of metadata for
2922 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002923 */
2924int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2925{
2926 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002927 struct btrfs_block_rsv *block_rsv = NULL;
2928 int reserve = 0;
2929 int insert = 0;
2930 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002931
Yan, Zhengd68fc572010-05-16 10:49:58 -04002932 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002933 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002934 if (!block_rsv)
2935 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002936 }
2937
Yan, Zhengd68fc572010-05-16 10:49:58 -04002938 spin_lock(&root->orphan_lock);
2939 if (!root->orphan_block_rsv) {
2940 root->orphan_block_rsv = block_rsv;
2941 } else if (block_rsv) {
2942 btrfs_free_block_rsv(root, block_rsv);
2943 block_rsv = NULL;
2944 }
Josef Bacik7b128762008-07-24 12:17:14 -04002945
Josef Bacik8a35d952012-05-23 14:26:42 -04002946 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2947 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002948#if 0
2949 /*
2950 * For proper ENOSPC handling, we should do orphan
2951 * cleanup when mounting. But this introduces backward
2952 * compatibility issue.
2953 */
2954 if (!xchg(&root->orphan_item_inserted, 1))
2955 insert = 2;
2956 else
2957 insert = 1;
2958#endif
2959 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002960 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002961 }
Josef Bacik7b128762008-07-24 12:17:14 -04002962
Josef Bacik72ac3c02012-05-23 14:13:11 -04002963 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2964 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002965 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002966 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002967
Yan, Zhengd68fc572010-05-16 10:49:58 -04002968 /* grab metadata reservation from transaction handle */
2969 if (reserve) {
2970 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002971 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002972 }
2973
2974 /* insert an orphan item to track this unlinked/truncated file */
2975 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002976 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002977 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002978 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2979 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002980 btrfs_abort_transaction(trans, root, ret);
2981 return ret;
2982 }
2983 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002984 }
2985
2986 /* insert an orphan item to track subvolume contains orphan files */
2987 if (insert >= 2) {
2988 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2989 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002990 if (ret && ret != -EEXIST) {
2991 btrfs_abort_transaction(trans, root, ret);
2992 return ret;
2993 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002994 }
2995 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002996}
2997
2998/*
2999 * We have done the truncate/delete so we can go ahead and remove the orphan
3000 * item for this particular inode.
3001 */
3002int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
3003{
3004 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003005 int delete_item = 0;
3006 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003007 int ret = 0;
3008
Yan, Zhengd68fc572010-05-16 10:49:58 -04003009 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003010 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3011 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003012 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003013
Josef Bacik72ac3c02012-05-23 14:13:11 -04003014 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3015 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003016 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003017 spin_unlock(&root->orphan_lock);
3018
3019 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003020 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003021 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003022 }
3023
Josef Bacik8a35d952012-05-23 14:26:42 -04003024 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003025 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003026 atomic_dec(&root->orphan_inodes);
3027 }
Josef Bacik7b128762008-07-24 12:17:14 -04003028
Yan, Zhengd68fc572010-05-16 10:49:58 -04003029 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003030}
3031
3032/*
3033 * this cleans up any orphans that may be left on the list from the last use
3034 * of this root.
3035 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003036int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003037{
3038 struct btrfs_path *path;
3039 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003040 struct btrfs_key key, found_key;
3041 struct btrfs_trans_handle *trans;
3042 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003043 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003044 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3045
Yan, Zhengd68fc572010-05-16 10:49:58 -04003046 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003047 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003048
3049 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003050 if (!path) {
3051 ret = -ENOMEM;
3052 goto out;
3053 }
Josef Bacik7b128762008-07-24 12:17:14 -04003054 path->reada = -1;
3055
3056 key.objectid = BTRFS_ORPHAN_OBJECTID;
3057 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3058 key.offset = (u64)-1;
3059
Josef Bacik7b128762008-07-24 12:17:14 -04003060 while (1) {
3061 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003062 if (ret < 0)
3063 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003064
3065 /*
3066 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003067 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003068 * find the key and see if we have stuff that matches
3069 */
3070 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003071 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003072 if (path->slots[0] == 0)
3073 break;
3074 path->slots[0]--;
3075 }
3076
3077 /* pull out the item */
3078 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003079 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3080
3081 /* make sure the item matches what we want */
3082 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3083 break;
3084 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3085 break;
3086
3087 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003088 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003089
3090 /*
3091 * this is where we are basically btrfs_lookup, without the
3092 * crossing root thing. we store the inode number in the
3093 * offset of the orphan item.
3094 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003095
3096 if (found_key.offset == last_objectid) {
3097 printk(KERN_ERR "btrfs: Error removing orphan entry, "
3098 "stopping orphan cleanup\n");
3099 ret = -EINVAL;
3100 goto out;
3101 }
3102
3103 last_objectid = found_key.offset;
3104
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003105 found_key.objectid = found_key.offset;
3106 found_key.type = BTRFS_INODE_ITEM_KEY;
3107 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003108 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003109 ret = PTR_RET(inode);
3110 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003111 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003112
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003113 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3114 struct btrfs_root *dead_root;
3115 struct btrfs_fs_info *fs_info = root->fs_info;
3116 int is_dead_root = 0;
3117
3118 /*
3119 * this is an orphan in the tree root. Currently these
3120 * could come from 2 sources:
3121 * a) a snapshot deletion in progress
3122 * b) a free space cache inode
3123 * We need to distinguish those two, as the snapshot
3124 * orphan must not get deleted.
3125 * find_dead_roots already ran before us, so if this
3126 * is a snapshot deletion, we should find the root
3127 * in the dead_roots list
3128 */
3129 spin_lock(&fs_info->trans_lock);
3130 list_for_each_entry(dead_root, &fs_info->dead_roots,
3131 root_list) {
3132 if (dead_root->root_key.objectid ==
3133 found_key.objectid) {
3134 is_dead_root = 1;
3135 break;
3136 }
3137 }
3138 spin_unlock(&fs_info->trans_lock);
3139 if (is_dead_root) {
3140 /* prevent this orphan from being found again */
3141 key.offset = found_key.objectid - 1;
3142 continue;
3143 }
3144 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003145 /*
3146 * Inode is already gone but the orphan item is still there,
3147 * kill the orphan item.
3148 */
3149 if (ret == -ESTALE) {
3150 trans = btrfs_start_transaction(root, 1);
3151 if (IS_ERR(trans)) {
3152 ret = PTR_ERR(trans);
3153 goto out;
3154 }
Josef Bacik8a35d952012-05-23 14:26:42 -04003155 printk(KERN_ERR "auto deleting %Lu\n",
3156 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003157 ret = btrfs_del_orphan_item(trans, root,
3158 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003159 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003160 btrfs_end_transaction(trans, root);
3161 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003162 }
Josef Bacik7b128762008-07-24 12:17:14 -04003163
Josef Bacik7b128762008-07-24 12:17:14 -04003164 /*
3165 * add this inode to the orphan list so btrfs_orphan_del does
3166 * the proper thing when we hit it
3167 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003168 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3169 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003170 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003171
Josef Bacik7b128762008-07-24 12:17:14 -04003172 /* if we have links, this was a truncate, lets do that */
3173 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003174 if (!S_ISREG(inode->i_mode)) {
3175 WARN_ON(1);
3176 iput(inode);
3177 continue;
3178 }
Josef Bacik7b128762008-07-24 12:17:14 -04003179 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003180
3181 /* 1 for the orphan item deletion. */
3182 trans = btrfs_start_transaction(root, 1);
3183 if (IS_ERR(trans)) {
3184 ret = PTR_ERR(trans);
3185 goto out;
3186 }
3187 ret = btrfs_orphan_add(trans, inode);
3188 btrfs_end_transaction(trans, root);
3189 if (ret)
3190 goto out;
3191
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003192 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003193 if (ret)
3194 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003195 } else {
3196 nr_unlink++;
3197 }
3198
3199 /* this will do delete_inode and everything for us */
3200 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003201 if (ret)
3202 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003203 }
Miao Xie3254c872011-11-10 20:45:05 -05003204 /* release the path since we're done with it */
3205 btrfs_release_path(path);
3206
Yan, Zhengd68fc572010-05-16 10:49:58 -04003207 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3208
3209 if (root->orphan_block_rsv)
3210 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3211 (u64)-1);
3212
3213 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003214 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003215 if (!IS_ERR(trans))
3216 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003217 }
Josef Bacik7b128762008-07-24 12:17:14 -04003218
3219 if (nr_unlink)
3220 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
3221 if (nr_truncate)
3222 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003223
3224out:
3225 if (ret)
3226 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
3227 btrfs_free_path(path);
3228 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003229}
3230
Chris Masond352ac62008-09-29 15:18:18 -04003231/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003232 * very simple check to peek ahead in the leaf looking for xattrs. If we
3233 * don't find any xattrs, we know there can't be any acls.
3234 *
3235 * slot is the slot the inode is in, objectid is the objectid of the inode
3236 */
3237static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3238 int slot, u64 objectid)
3239{
3240 u32 nritems = btrfs_header_nritems(leaf);
3241 struct btrfs_key found_key;
3242 int scanned = 0;
3243
3244 slot++;
3245 while (slot < nritems) {
3246 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3247
3248 /* we found a different objectid, there must not be acls */
3249 if (found_key.objectid != objectid)
3250 return 0;
3251
3252 /* we found an xattr, assume we've got an acl */
3253 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3254 return 1;
3255
3256 /*
3257 * we found a key greater than an xattr key, there can't
3258 * be any acls later on
3259 */
3260 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3261 return 0;
3262
3263 slot++;
3264 scanned++;
3265
3266 /*
3267 * it goes inode, inode backrefs, xattrs, extents,
3268 * so if there are a ton of hard links to an inode there can
3269 * be a lot of backrefs. Don't waste time searching too hard,
3270 * this is just an optimization
3271 */
3272 if (scanned >= 8)
3273 break;
3274 }
3275 /* we hit the end of the leaf before we found an xattr or
3276 * something larger than an xattr. We have to assume the inode
3277 * has acls
3278 */
3279 return 1;
3280}
3281
3282/*
Chris Masond352ac62008-09-29 15:18:18 -04003283 * read an inode from the btree into the in-memory inode
3284 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003285static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003286{
3287 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003288 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003289 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003290 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003291 struct btrfs_root *root = BTRFS_I(inode)->root;
3292 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003293 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003294 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003295 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003296 bool filled = false;
3297
3298 ret = btrfs_fill_inode(inode, &rdev);
3299 if (!ret)
3300 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003301
3302 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003303 if (!path)
3304 goto make_bad;
3305
Josef Bacikd90c7322011-05-17 09:50:54 -04003306 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003307 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003308
Chris Mason39279cc2007-06-12 06:35:45 -04003309 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003310 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003311 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003312
Chris Mason5f39d392007-10-15 16:14:19 -04003313 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003314
3315 if (filled)
3316 goto cache_acl;
3317
Chris Mason5f39d392007-10-15 16:14:19 -04003318 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3319 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003320 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003321 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003322 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3323 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003324 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003325
3326 tspec = btrfs_inode_atime(inode_item);
3327 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3328 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3329
3330 tspec = btrfs_inode_mtime(inode_item);
3331 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3332 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3333
3334 tspec = btrfs_inode_ctime(inode_item);
3335 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3336 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3337
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003338 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003339 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003340 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3341
3342 /*
3343 * If we were modified in the current generation and evicted from memory
3344 * and then re-read we need to do a full sync since we don't have any
3345 * idea about which extents were modified before we were evicted from
3346 * cache.
3347 */
3348 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3349 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3350 &BTRFS_I(inode)->runtime_flags);
3351
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003352 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003353 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003354 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003355 rdev = btrfs_inode_rdev(leaf, inode_item);
3356
Josef Bacikaec74772008-07-24 12:12:38 -04003357 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003358 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003359cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003360 /*
3361 * try to precache a NULL acl entry for files that don't have
3362 * any xattrs or acls
3363 */
Li Zefan33345d012011-04-20 10:31:50 +08003364 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3365 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003366 if (!maybe_acls)
3367 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003368
Chris Mason39279cc2007-06-12 06:35:45 -04003369 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003370
Chris Mason39279cc2007-06-12 06:35:45 -04003371 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003372 case S_IFREG:
3373 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003374 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003375 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003376 inode->i_fop = &btrfs_file_operations;
3377 inode->i_op = &btrfs_file_inode_operations;
3378 break;
3379 case S_IFDIR:
3380 inode->i_fop = &btrfs_dir_file_operations;
3381 if (root == root->fs_info->tree_root)
3382 inode->i_op = &btrfs_dir_ro_inode_operations;
3383 else
3384 inode->i_op = &btrfs_dir_inode_operations;
3385 break;
3386 case S_IFLNK:
3387 inode->i_op = &btrfs_symlink_inode_operations;
3388 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003389 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003390 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003391 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003392 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003393 init_special_inode(inode, inode->i_mode, rdev);
3394 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003395 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003396
3397 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003398 return;
3399
3400make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003401 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003402 make_bad_inode(inode);
3403}
3404
Chris Masond352ac62008-09-29 15:18:18 -04003405/*
3406 * given a leaf and an inode, copy the inode fields into the leaf
3407 */
Chris Masone02119d2008-09-05 16:13:11 -04003408static void fill_inode_item(struct btrfs_trans_handle *trans,
3409 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003410 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003411 struct inode *inode)
3412{
Liu Bo51fab692012-12-27 09:01:21 +00003413 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003414
Liu Bo51fab692012-12-27 09:01:21 +00003415 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003416
Liu Bo51fab692012-12-27 09:01:21 +00003417 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3418 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3419 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3420 &token);
3421 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3422 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003423
Liu Bo51fab692012-12-27 09:01:21 +00003424 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3425 inode->i_atime.tv_sec, &token);
3426 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3427 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003428
Liu Bo51fab692012-12-27 09:01:21 +00003429 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3430 inode->i_mtime.tv_sec, &token);
3431 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3432 inode->i_mtime.tv_nsec, &token);
3433
3434 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3435 inode->i_ctime.tv_sec, &token);
3436 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3437 inode->i_ctime.tv_nsec, &token);
3438
3439 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3440 &token);
3441 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3442 &token);
3443 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3444 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3445 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3446 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3447 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003448}
3449
Chris Masond352ac62008-09-29 15:18:18 -04003450/*
3451 * copy everything in the in-memory inode into the btree.
3452 */
Chris Mason21151332011-11-10 20:39:08 -05003453static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003454 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003455{
3456 struct btrfs_inode_item *inode_item;
3457 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003458 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003459 int ret;
3460
3461 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003462 if (!path)
3463 return -ENOMEM;
3464
Chris Masonb9473432009-03-13 11:00:37 -04003465 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003466 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3467 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003468 if (ret) {
3469 if (ret > 0)
3470 ret = -ENOENT;
3471 goto failed;
3472 }
3473
Chris Masonb4ce94d2009-02-04 09:25:08 -05003474 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003475 leaf = path->nodes[0];
3476 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003477 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003478
Chris Masone02119d2008-09-05 16:13:11 -04003479 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003480 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003481 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003482 ret = 0;
3483failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003484 btrfs_free_path(path);
3485 return ret;
3486}
3487
Chris Masond352ac62008-09-29 15:18:18 -04003488/*
Chris Mason21151332011-11-10 20:39:08 -05003489 * copy everything in the in-memory inode into the btree.
3490 */
3491noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3492 struct btrfs_root *root, struct inode *inode)
3493{
3494 int ret;
3495
3496 /*
3497 * If the inode is a free space inode, we can deadlock during commit
3498 * if we put it into the delayed code.
3499 *
3500 * The data relocation inode should also be directly updated
3501 * without delay
3502 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003503 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003504 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003505 btrfs_update_root_times(trans, root);
3506
Chris Mason21151332011-11-10 20:39:08 -05003507 ret = btrfs_delayed_update_inode(trans, root, inode);
3508 if (!ret)
3509 btrfs_set_inode_last_trans(trans, inode);
3510 return ret;
3511 }
3512
3513 return btrfs_update_inode_item(trans, root, inode);
3514}
3515
Josef Bacikbe6aef62012-10-22 15:43:12 -04003516noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3517 struct btrfs_root *root,
3518 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003519{
3520 int ret;
3521
3522 ret = btrfs_update_inode(trans, root, inode);
3523 if (ret == -ENOSPC)
3524 return btrfs_update_inode_item(trans, root, inode);
3525 return ret;
3526}
3527
3528/*
Chris Masond352ac62008-09-29 15:18:18 -04003529 * unlink helper that gets used here in inode.c and in the tree logging
3530 * recovery code. It remove a link in a directory with a given name, and
3531 * also drops the back refs in the inode to the directory
3532 */
Al Viro92986792011-03-04 17:14:37 +00003533static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3534 struct btrfs_root *root,
3535 struct inode *dir, struct inode *inode,
3536 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003537{
3538 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003539 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003540 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003541 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003542 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003543 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003544 u64 ino = btrfs_ino(inode);
3545 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003546
3547 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003548 if (!path) {
3549 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003550 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003551 }
3552
Chris Masonb9473432009-03-13 11:00:37 -04003553 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003554 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003555 name, name_len, -1);
3556 if (IS_ERR(di)) {
3557 ret = PTR_ERR(di);
3558 goto err;
3559 }
3560 if (!di) {
3561 ret = -ENOENT;
3562 goto err;
3563 }
Chris Mason5f39d392007-10-15 16:14:19 -04003564 leaf = path->nodes[0];
3565 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003566 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003567 if (ret)
3568 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003569 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003570
Li Zefan33345d012011-04-20 10:31:50 +08003571 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3572 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003573 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05003574 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08003575 "inode %llu parent %llu\n", name_len, name,
3576 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003577 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003578 goto err;
3579 }
3580
Miao Xie16cdcec2011-04-22 18:12:22 +08003581 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003582 if (ret) {
3583 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003584 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003585 }
Chris Mason39279cc2007-06-12 06:35:45 -04003586
Chris Masone02119d2008-09-05 16:13:11 -04003587 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003588 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003589 if (ret != 0 && ret != -ENOENT) {
3590 btrfs_abort_transaction(trans, root, ret);
3591 goto err;
3592 }
Chris Masone02119d2008-09-05 16:13:11 -04003593
3594 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3595 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003596 if (ret == -ENOENT)
3597 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003598err:
3599 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003600 if (ret)
3601 goto out;
3602
3603 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003604 inode_inc_iversion(inode);
3605 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003606 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003607 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003608out:
Chris Mason39279cc2007-06-12 06:35:45 -04003609 return ret;
3610}
3611
Al Viro92986792011-03-04 17:14:37 +00003612int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3613 struct btrfs_root *root,
3614 struct inode *dir, struct inode *inode,
3615 const char *name, int name_len)
3616{
3617 int ret;
3618 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3619 if (!ret) {
3620 btrfs_drop_nlink(inode);
3621 ret = btrfs_update_inode(trans, root, inode);
3622 }
3623 return ret;
3624}
3625
3626
Yan, Zhenga22285a2010-05-16 10:48:46 -04003627/* helper to check if there is any shared block in the path */
3628static int check_path_shared(struct btrfs_root *root,
3629 struct btrfs_path *path)
3630{
3631 struct extent_buffer *eb;
3632 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00003633 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003634
3635 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003636 int ret;
3637
Yan, Zhenga22285a2010-05-16 10:48:46 -04003638 if (!path->nodes[level])
3639 break;
3640 eb = path->nodes[level];
3641 if (!btrfs_block_can_be_shared(root, eb))
3642 continue;
3643 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
3644 &refs, NULL);
3645 if (refs > 1)
3646 return 1;
3647 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003648 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003649}
3650
3651/*
3652 * helper to start transaction for unlink and rmdir.
3653 *
3654 * unlink and rmdir are special in btrfs, they do not always free space.
3655 * so in enospc case, we should make sure they will free space before
3656 * allowing them to use the global metadata reservation.
3657 */
3658static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3659 struct dentry *dentry)
3660{
3661 struct btrfs_trans_handle *trans;
3662 struct btrfs_root *root = BTRFS_I(dir)->root;
3663 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003664 struct btrfs_dir_item *di;
3665 struct inode *inode = dentry->d_inode;
3666 u64 index;
3667 int check_link = 1;
3668 int err = -ENOSPC;
3669 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003670 u64 ino = btrfs_ino(inode);
3671 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003672
Josef Bacike70bea52011-10-11 14:18:24 -04003673 /*
3674 * 1 for the possible orphan item
3675 * 1 for the dir item
3676 * 1 for the dir index
3677 * 1 for the inode ref
3678 * 1 for the inode ref in the tree log
3679 * 2 for the dir entries in the log
3680 * 1 for the inode
3681 */
3682 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003683 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3684 return trans;
3685
Li Zefan33345d012011-04-20 10:31:50 +08003686 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003687 return ERR_PTR(-ENOSPC);
3688
3689 /* check if there is someone else holds reference */
3690 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3691 return ERR_PTR(-ENOSPC);
3692
3693 if (atomic_read(&inode->i_count) > 2)
3694 return ERR_PTR(-ENOSPC);
3695
3696 if (xchg(&root->fs_info->enospc_unlink, 1))
3697 return ERR_PTR(-ENOSPC);
3698
3699 path = btrfs_alloc_path();
3700 if (!path) {
3701 root->fs_info->enospc_unlink = 0;
3702 return ERR_PTR(-ENOMEM);
3703 }
3704
Josef Bacik3880a1b2011-10-14 14:46:51 -04003705 /* 1 for the orphan item */
3706 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003707 if (IS_ERR(trans)) {
3708 btrfs_free_path(path);
3709 root->fs_info->enospc_unlink = 0;
3710 return trans;
3711 }
3712
3713 path->skip_locking = 1;
3714 path->search_commit_root = 1;
3715
3716 ret = btrfs_lookup_inode(trans, root, path,
3717 &BTRFS_I(dir)->location, 0);
3718 if (ret < 0) {
3719 err = ret;
3720 goto out;
3721 }
3722 if (ret == 0) {
3723 if (check_path_shared(root, path))
3724 goto out;
3725 } else {
3726 check_link = 0;
3727 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003728 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003729
3730 ret = btrfs_lookup_inode(trans, root, path,
3731 &BTRFS_I(inode)->location, 0);
3732 if (ret < 0) {
3733 err = ret;
3734 goto out;
3735 }
3736 if (ret == 0) {
3737 if (check_path_shared(root, path))
3738 goto out;
3739 } else {
3740 check_link = 0;
3741 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003742 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003743
3744 if (ret == 0 && S_ISREG(inode->i_mode)) {
3745 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003746 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003747 if (ret < 0) {
3748 err = ret;
3749 goto out;
3750 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003751 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003752 if (check_path_shared(root, path))
3753 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003754 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003755 }
3756
3757 if (!check_link) {
3758 err = 0;
3759 goto out;
3760 }
3761
Li Zefan33345d012011-04-20 10:31:50 +08003762 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003763 dentry->d_name.name, dentry->d_name.len, 0);
3764 if (IS_ERR(di)) {
3765 err = PTR_ERR(di);
3766 goto out;
3767 }
3768 if (di) {
3769 if (check_path_shared(root, path))
3770 goto out;
3771 } else {
3772 err = 0;
3773 goto out;
3774 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003775 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003776
Mark Fashehf1863732012-08-08 11:32:27 -07003777 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3778 dentry->d_name.len, ino, dir_ino, 0,
3779 &index);
3780 if (ret) {
3781 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003782 goto out;
3783 }
Mark Fashehf1863732012-08-08 11:32:27 -07003784
Yan, Zhenga22285a2010-05-16 10:48:46 -04003785 if (check_path_shared(root, path))
3786 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003787
David Sterbab3b4aa72011-04-21 01:20:15 +02003788 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003789
Miao Xie16cdcec2011-04-22 18:12:22 +08003790 /*
3791 * This is a commit root search, if we can lookup inode item and other
3792 * relative items in the commit root, it means the transaction of
3793 * dir/file creation has been committed, and the dir index item that we
3794 * delay to insert has also been inserted into the commit root. So
3795 * we needn't worry about the delayed insertion of the dir index item
3796 * here.
3797 */
Li Zefan33345d012011-04-20 10:31:50 +08003798 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003799 dentry->d_name.name, dentry->d_name.len, 0);
3800 if (IS_ERR(di)) {
3801 err = PTR_ERR(di);
3802 goto out;
3803 }
3804 BUG_ON(ret == -ENOENT);
3805 if (check_path_shared(root, path))
3806 goto out;
3807
3808 err = 0;
3809out:
3810 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003811 /* Migrate the orphan reservation over */
3812 if (!err)
3813 err = btrfs_block_rsv_migrate(trans->block_rsv,
3814 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003815 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003816
Yan, Zhenga22285a2010-05-16 10:48:46 -04003817 if (err) {
3818 btrfs_end_transaction(trans, root);
3819 root->fs_info->enospc_unlink = 0;
3820 return ERR_PTR(err);
3821 }
3822
3823 trans->block_rsv = &root->fs_info->global_block_rsv;
3824 return trans;
3825}
3826
3827static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3828 struct btrfs_root *root)
3829{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003830 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003831 btrfs_block_rsv_release(root, trans->block_rsv,
3832 trans->bytes_reserved);
3833 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003834 BUG_ON(!root->fs_info->enospc_unlink);
3835 root->fs_info->enospc_unlink = 0;
3836 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003837 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003838}
3839
Chris Mason39279cc2007-06-12 06:35:45 -04003840static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3841{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003842 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003843 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003844 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003845 int ret;
3846
Yan, Zhenga22285a2010-05-16 10:48:46 -04003847 trans = __unlink_start_trans(dir, dentry);
3848 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003849 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003850
Chris Mason12fcfd22009-03-24 10:24:20 -04003851 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3852
Chris Masone02119d2008-09-05 16:13:11 -04003853 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3854 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003855 if (ret)
3856 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003857
Yan, Zhenga22285a2010-05-16 10:48:46 -04003858 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003859 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003860 if (ret)
3861 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003862 }
Josef Bacik7b128762008-07-24 12:17:14 -04003863
Tsutomu Itohb5324022011-07-19 07:27:20 +00003864out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003865 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003866 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003867 return ret;
3868}
3869
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003870int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3871 struct btrfs_root *root,
3872 struct inode *dir, u64 objectid,
3873 const char *name, int name_len)
3874{
3875 struct btrfs_path *path;
3876 struct extent_buffer *leaf;
3877 struct btrfs_dir_item *di;
3878 struct btrfs_key key;
3879 u64 index;
3880 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003881 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003882
3883 path = btrfs_alloc_path();
3884 if (!path)
3885 return -ENOMEM;
3886
Li Zefan33345d012011-04-20 10:31:50 +08003887 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003888 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003889 if (IS_ERR_OR_NULL(di)) {
3890 if (!di)
3891 ret = -ENOENT;
3892 else
3893 ret = PTR_ERR(di);
3894 goto out;
3895 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003896
3897 leaf = path->nodes[0];
3898 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3899 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3900 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003901 if (ret) {
3902 btrfs_abort_transaction(trans, root, ret);
3903 goto out;
3904 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003905 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003906
3907 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3908 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003909 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003910 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003911 if (ret != -ENOENT) {
3912 btrfs_abort_transaction(trans, root, ret);
3913 goto out;
3914 }
Li Zefan33345d012011-04-20 10:31:50 +08003915 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003916 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003917 if (IS_ERR_OR_NULL(di)) {
3918 if (!di)
3919 ret = -ENOENT;
3920 else
3921 ret = PTR_ERR(di);
3922 btrfs_abort_transaction(trans, root, ret);
3923 goto out;
3924 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003925
3926 leaf = path->nodes[0];
3927 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003928 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003929 index = key.offset;
3930 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003931 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003932
Miao Xie16cdcec2011-04-22 18:12:22 +08003933 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003934 if (ret) {
3935 btrfs_abort_transaction(trans, root, ret);
3936 goto out;
3937 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003938
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003939 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003940 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003941 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003942 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003943 if (ret)
3944 btrfs_abort_transaction(trans, root, ret);
3945out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003946 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003947 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003948}
3949
Chris Mason39279cc2007-06-12 06:35:45 -04003950static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3951{
3952 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003953 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003954 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003955 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003956
David Sterbab3ae2442012-09-13 16:04:34 -06003957 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003958 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003959 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3960 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003961
Yan, Zhenga22285a2010-05-16 10:48:46 -04003962 trans = __unlink_start_trans(dir, dentry);
3963 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003964 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003965
Li Zefan33345d012011-04-20 10:31:50 +08003966 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003967 err = btrfs_unlink_subvol(trans, root, dir,
3968 BTRFS_I(inode)->location.objectid,
3969 dentry->d_name.name,
3970 dentry->d_name.len);
3971 goto out;
3972 }
3973
Josef Bacik7b128762008-07-24 12:17:14 -04003974 err = btrfs_orphan_add(trans, inode);
3975 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003976 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003977
Chris Mason39279cc2007-06-12 06:35:45 -04003978 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003979 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3980 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003981 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003982 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003983out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003984 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003985 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003986
Chris Mason39279cc2007-06-12 06:35:45 -04003987 return err;
3988}
3989
Chris Mason323ac952008-10-01 19:05:46 -04003990/*
Chris Mason39279cc2007-06-12 06:35:45 -04003991 * this can truncate away extent items, csum items and directory items.
3992 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003993 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003994 *
3995 * csum items that cross the new i_size are truncated to the new size
3996 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003997 *
3998 * min_type is the minimum key type to truncate down to. If set to 0, this
3999 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004000 */
Yan, Zheng80825102009-11-12 09:35:36 +00004001int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4002 struct btrfs_root *root,
4003 struct inode *inode,
4004 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004005{
Chris Mason39279cc2007-06-12 06:35:45 -04004006 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004007 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004008 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004009 struct btrfs_key key;
4010 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004011 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004012 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004013 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004014 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004015 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004016 int found_extent;
4017 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004018 int pending_del_nr = 0;
4019 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004020 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004021 int ret;
4022 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004023 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004024
4025 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004026
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004027 path = btrfs_alloc_path();
4028 if (!path)
4029 return -ENOMEM;
4030 path->reada = -1;
4031
Josef Bacik5dc562c2012-08-17 13:14:17 -04004032 /*
4033 * We want to drop from the next block forward in case this new size is
4034 * not block aligned since we will be keeping the last block of the
4035 * extent just the way it is.
4036 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004037 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004038 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4039 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004040
Miao Xie16cdcec2011-04-22 18:12:22 +08004041 /*
4042 * This function is also used to drop the items in the log tree before
4043 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4044 * it is used to drop the loged items. So we shouldn't kill the delayed
4045 * items.
4046 */
4047 if (min_type == 0 && root == BTRFS_I(inode)->root)
4048 btrfs_kill_delayed_inode_items(inode);
4049
Li Zefan33345d012011-04-20 10:31:50 +08004050 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004051 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004052 key.type = (u8)-1;
4053
Chris Mason85e21ba2008-01-29 15:11:36 -05004054search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004055 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004056 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004057 if (ret < 0) {
4058 err = ret;
4059 goto out;
4060 }
Chris Masond3977122009-01-05 21:25:51 -05004061
Chris Mason85e21ba2008-01-29 15:11:36 -05004062 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004063 /* there are no items in the tree for us to truncate, we're
4064 * done
4065 */
Yan, Zheng80825102009-11-12 09:35:36 +00004066 if (path->slots[0] == 0)
4067 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004068 path->slots[0]--;
4069 }
4070
Chris Masond3977122009-01-05 21:25:51 -05004071 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004072 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004073 leaf = path->nodes[0];
4074 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4075 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004076
Li Zefan33345d012011-04-20 10:31:50 +08004077 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004078 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004079
Chris Mason85e21ba2008-01-29 15:11:36 -05004080 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004081 break;
4082
Chris Mason5f39d392007-10-15 16:14:19 -04004083 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004084 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004085 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004086 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004087 extent_type = btrfs_file_extent_type(leaf, fi);
4088 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004089 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004090 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004091 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004092 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004093 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004094 }
Yan008630c2007-11-07 13:31:09 -05004095 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004096 }
Yan, Zheng80825102009-11-12 09:35:36 +00004097 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004098 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004099 } else {
4100 if (item_end < new_size)
4101 break;
4102 if (found_key.offset >= new_size)
4103 del_item = 1;
4104 else
4105 del_item = 0;
4106 }
Chris Mason39279cc2007-06-12 06:35:45 -04004107 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004108 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004109 if (found_type != BTRFS_EXTENT_DATA_KEY)
4110 goto delete;
4111
4112 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004113 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004114 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004115 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004116 u64 orig_num_bytes =
4117 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004118 extent_num_bytes = ALIGN(new_size -
4119 found_key.offset,
4120 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004121 btrfs_set_file_extent_num_bytes(leaf, fi,
4122 extent_num_bytes);
4123 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004124 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004125 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004126 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004127 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004128 } else {
Chris Masondb945352007-10-15 16:15:53 -04004129 extent_num_bytes =
4130 btrfs_file_extent_disk_num_bytes(leaf,
4131 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004132 extent_offset = found_key.offset -
4133 btrfs_file_extent_offset(leaf, fi);
4134
Chris Mason39279cc2007-06-12 06:35:45 -04004135 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004136 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004137 if (extent_start != 0) {
4138 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004139 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004140 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004141 }
4142 }
Chris Mason90692182008-02-08 13:49:28 -05004143 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004144 /*
4145 * we can't truncate inline items that have had
4146 * special encodings
4147 */
4148 if (!del_item &&
4149 btrfs_file_extent_compression(leaf, fi) == 0 &&
4150 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4151 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004152 u32 size = new_size - found_key.offset;
4153
4154 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004155 inode_sub_bytes(inode, item_end + 1 -
4156 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004157 }
4158 size =
4159 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004160 btrfs_truncate_item(trans, root, path,
4161 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004162 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004163 inode_sub_bytes(inode, item_end + 1 -
4164 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004165 }
Chris Mason39279cc2007-06-12 06:35:45 -04004166 }
Chris Mason179e29e2007-11-01 11:28:41 -04004167delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004168 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004169 if (!pending_del_nr) {
4170 /* no pending yet, add ourselves */
4171 pending_del_slot = path->slots[0];
4172 pending_del_nr = 1;
4173 } else if (pending_del_nr &&
4174 path->slots[0] + 1 == pending_del_slot) {
4175 /* hop on the pending chunk */
4176 pending_del_nr++;
4177 pending_del_slot = path->slots[0];
4178 } else {
Chris Masond3977122009-01-05 21:25:51 -05004179 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004180 }
Chris Mason39279cc2007-06-12 06:35:45 -04004181 } else {
4182 break;
4183 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004184 if (found_extent && (root->ref_cows ||
4185 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004186 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004187 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004188 extent_num_bytes, 0,
4189 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004190 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004191 BUG_ON(ret);
4192 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004193
Yan, Zheng80825102009-11-12 09:35:36 +00004194 if (found_type == BTRFS_INODE_ITEM_KEY)
4195 break;
4196
4197 if (path->slots[0] == 0 ||
4198 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004199 if (pending_del_nr) {
4200 ret = btrfs_del_items(trans, root, path,
4201 pending_del_slot,
4202 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004203 if (ret) {
4204 btrfs_abort_transaction(trans,
4205 root, ret);
4206 goto error;
4207 }
Yan, Zheng80825102009-11-12 09:35:36 +00004208 pending_del_nr = 0;
4209 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004210 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004211 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004212 } else {
4213 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004214 }
Chris Mason39279cc2007-06-12 06:35:45 -04004215 }
Yan, Zheng80825102009-11-12 09:35:36 +00004216out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004217 if (pending_del_nr) {
4218 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4219 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004220 if (ret)
4221 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004222 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004223error:
Chris Mason39279cc2007-06-12 06:35:45 -04004224 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004225 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004226}
4227
Chris Masona52d9a82007-08-27 16:49:44 -04004228/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004229 * btrfs_truncate_page - read, zero a chunk and write a page
4230 * @inode - inode that we're zeroing
4231 * @from - the offset to start zeroing
4232 * @len - the length to zero, 0 to zero the entire range respective to the
4233 * offset
4234 * @front - zero up to the offset instead of from the offset on
4235 *
4236 * This will find the page for the "from" offset and cow the page and zero the
4237 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004238 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004239int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4240 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004241{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004242 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004243 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004244 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4245 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004246 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004247 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004248 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004249 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4250 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4251 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004252 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004253 int ret = 0;
4254 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004255 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004256
Josef Bacik2aaa6652012-08-29 14:27:18 -04004257 if ((offset & (blocksize - 1)) == 0 &&
4258 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004259 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004260 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004261 if (ret)
4262 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004263
Chris Mason211c17f2008-05-15 09:13:45 -04004264again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004265 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004266 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004267 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004268 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004269 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004270 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004271
4272 page_start = page_offset(page);
4273 page_end = page_start + PAGE_CACHE_SIZE - 1;
4274
Chris Masona52d9a82007-08-27 16:49:44 -04004275 if (!PageUptodate(page)) {
4276 ret = btrfs_readpage(NULL, page);
4277 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004278 if (page->mapping != mapping) {
4279 unlock_page(page);
4280 page_cache_release(page);
4281 goto again;
4282 }
Chris Masona52d9a82007-08-27 16:49:44 -04004283 if (!PageUptodate(page)) {
4284 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004285 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004286 }
4287 }
Chris Mason211c17f2008-05-15 09:13:45 -04004288 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004289
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004290 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004291 set_page_extent_mapped(page);
4292
4293 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4294 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004295 unlock_extent_cached(io_tree, page_start, page_end,
4296 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004297 unlock_page(page);
4298 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004299 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004300 btrfs_put_ordered_extent(ordered);
4301 goto again;
4302 }
4303
Josef Bacik2ac55d42010-02-03 19:33:23 +00004304 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004305 EXTENT_DIRTY | EXTENT_DELALLOC |
4306 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004307 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004308
Josef Bacik2ac55d42010-02-03 19:33:23 +00004309 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4310 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004311 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004312 unlock_extent_cached(io_tree, page_start, page_end,
4313 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004314 goto out_unlock;
4315 }
4316
Chris Masone6dcd2d2008-07-17 12:53:50 -04004317 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004318 if (!len)
4319 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004320 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004321 if (front)
4322 memset(kaddr, 0, offset);
4323 else
4324 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004325 flush_dcache_page(page);
4326 kunmap(page);
4327 }
Chris Mason247e7432008-07-17 12:53:51 -04004328 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004329 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004330 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4331 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004332
Chris Mason89642222008-07-24 09:41:53 -04004333out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004334 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004335 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004336 unlock_page(page);
4337 page_cache_release(page);
4338out:
4339 return ret;
4340}
4341
Josef Bacik695a0d02011-03-04 15:46:53 -05004342/*
4343 * This function puts in dummy file extents for the area we're creating a hole
4344 * for. So if we are truncating this file to a larger size we need to insert
4345 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4346 * the range between oldsize and size
4347 */
Josef Bacika41ad392011-01-31 15:30:16 -05004348int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004349{
4350 struct btrfs_trans_handle *trans;
4351 struct btrfs_root *root = BTRFS_I(inode)->root;
4352 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004353 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004354 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004355 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004356 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4357 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004358 u64 last_byte;
4359 u64 cur_offset;
4360 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004361 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004362
4363 if (size <= hole_start)
4364 return 0;
4365
Yan Zheng9036c102008-10-30 14:19:41 -04004366 while (1) {
4367 struct btrfs_ordered_extent *ordered;
4368 btrfs_wait_ordered_range(inode, hole_start,
4369 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004370 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004371 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004372 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4373 if (!ordered)
4374 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004375 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4376 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004377 btrfs_put_ordered_extent(ordered);
4378 }
4379
Yan Zheng9036c102008-10-30 14:19:41 -04004380 cur_offset = hole_start;
4381 while (1) {
4382 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4383 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004384 if (IS_ERR(em)) {
4385 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004386 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004387 break;
4388 }
Yan Zheng9036c102008-10-30 14:19:41 -04004389 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004390 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004391 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004392 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004393 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004394
Miao Xie36423202011-12-14 20:12:02 -05004395 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004396 if (IS_ERR(trans)) {
4397 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004398 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004399 }
Yan, Zheng80825102009-11-12 09:35:36 +00004400
Josef Bacik5dc562c2012-08-17 13:14:17 -04004401 err = btrfs_drop_extents(trans, root, inode,
4402 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004403 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004404 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004405 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004406 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004407 break;
Miao Xie5b397372011-09-11 10:52:24 -04004408 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004409
Yan Zheng9036c102008-10-30 14:19:41 -04004410 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004411 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004412 0, hole_size, 0, hole_size,
4413 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004414 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004415 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004416 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004417 break;
Miao Xie5b397372011-09-11 10:52:24 -04004418 }
Yan, Zheng80825102009-11-12 09:35:36 +00004419
Josef Bacik5dc562c2012-08-17 13:14:17 -04004420 btrfs_drop_extent_cache(inode, cur_offset,
4421 cur_offset + hole_size - 1, 0);
4422 hole_em = alloc_extent_map();
4423 if (!hole_em) {
4424 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4425 &BTRFS_I(inode)->runtime_flags);
4426 goto next;
4427 }
4428 hole_em->start = cur_offset;
4429 hole_em->len = hole_size;
4430 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004431
Josef Bacik5dc562c2012-08-17 13:14:17 -04004432 hole_em->block_start = EXTENT_MAP_HOLE;
4433 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004434 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004435 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4436 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4437 hole_em->generation = trans->transid;
4438
4439 while (1) {
4440 write_lock(&em_tree->lock);
4441 err = add_extent_mapping(em_tree, hole_em);
4442 if (!err)
4443 list_move(&hole_em->list,
4444 &em_tree->modified_extents);
4445 write_unlock(&em_tree->lock);
4446 if (err != -EEXIST)
4447 break;
4448 btrfs_drop_extent_cache(inode, cur_offset,
4449 cur_offset +
4450 hole_size - 1, 0);
4451 }
4452 free_extent_map(hole_em);
4453next:
Miao Xie36423202011-12-14 20:12:02 -05004454 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004455 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004456 }
4457 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004458 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004459 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004460 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004461 break;
4462 }
4463
Yan, Zhenga22285a2010-05-16 10:48:46 -04004464 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004465 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4466 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004467 return err;
4468}
4469
Eric Sandeen3972f262013-01-12 02:57:22 +00004470static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004471{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004472 struct btrfs_root *root = BTRFS_I(inode)->root;
4473 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004474 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004475 loff_t newsize = attr->ia_size;
4476 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004477 int ret;
4478
Josef Bacika41ad392011-01-31 15:30:16 -05004479 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004480 return 0;
4481
Eric Sandeen3972f262013-01-12 02:57:22 +00004482 /*
4483 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4484 * special case where we need to update the times despite not having
4485 * these flags set. For all other operations the VFS set these flags
4486 * explicitly if it wants a timestamp update.
4487 */
4488 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4489 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4490
Josef Bacika41ad392011-01-31 15:30:16 -05004491 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004492 truncate_pagecache(inode, oldsize, newsize);
4493 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004494 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004495 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004496
Miao Xief4a2f4c2011-12-14 20:12:01 -05004497 trans = btrfs_start_transaction(root, 1);
4498 if (IS_ERR(trans))
4499 return PTR_ERR(trans);
4500
4501 i_size_write(inode, newsize);
4502 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4503 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004504 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004505 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004506
Josef Bacika41ad392011-01-31 15:30:16 -05004507 /*
4508 * We're truncating a file that used to have good data down to
4509 * zero. Make sure it gets into the ordered flush list so that
4510 * any new writes get down to disk quickly.
4511 */
4512 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004513 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4514 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004515
Josef Bacikf3fe8202013-01-07 17:03:21 -05004516 /*
4517 * 1 for the orphan item we're going to add
4518 * 1 for the orphan item deletion.
4519 */
4520 trans = btrfs_start_transaction(root, 2);
4521 if (IS_ERR(trans))
4522 return PTR_ERR(trans);
4523
4524 /*
4525 * We need to do this in case we fail at _any_ point during the
4526 * actual truncate. Once we do the truncate_setsize we could
4527 * invalidate pages which forces any outstanding ordered io to
4528 * be instantly completed which will give us extents that need
4529 * to be truncated. If we fail to get an orphan inode down we
4530 * could have left over extents that were never meant to live,
4531 * so we need to garuntee from this point on that everything
4532 * will be consistent.
4533 */
4534 ret = btrfs_orphan_add(trans, inode);
4535 btrfs_end_transaction(trans, root);
4536 if (ret)
4537 return ret;
4538
Josef Bacika41ad392011-01-31 15:30:16 -05004539 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4540 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004541
4542 /* Disable nonlocked read DIO to avoid the end less truncate */
4543 btrfs_inode_block_unlocked_dio(inode);
4544 inode_dio_wait(inode);
4545 btrfs_inode_resume_unlocked_dio(inode);
4546
Josef Bacika41ad392011-01-31 15:30:16 -05004547 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004548 if (ret && inode->i_nlink)
4549 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004550 }
4551
Josef Bacika41ad392011-01-31 15:30:16 -05004552 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004553}
4554
Chris Mason39279cc2007-06-12 06:35:45 -04004555static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4556{
4557 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004558 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004559 int err;
4560
Li Zefanb83cc962010-12-20 16:04:08 +08004561 if (btrfs_root_readonly(root))
4562 return -EROFS;
4563
Chris Mason39279cc2007-06-12 06:35:45 -04004564 err = inode_change_ok(inode, attr);
4565 if (err)
4566 return err;
4567
Chris Mason5a3f23d2009-03-31 13:27:11 -04004568 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004569 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004570 if (err)
4571 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004572 }
Yan Zheng9036c102008-10-30 14:19:41 -04004573
Christoph Hellwig10257742010-06-04 11:30:02 +02004574 if (attr->ia_valid) {
4575 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004576 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004577 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004578
Josef Bacik22c44fe2011-11-30 10:45:38 -05004579 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004580 err = btrfs_acl_chmod(inode);
4581 }
4582
Chris Mason39279cc2007-06-12 06:35:45 -04004583 return err;
4584}
Chris Mason61295eb2008-01-14 16:24:38 -05004585
Al Virobd555972010-06-07 11:35:40 -04004586void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004587{
4588 struct btrfs_trans_handle *trans;
4589 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004590 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004591 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004592 int ret;
4593
liubo1abe9b82011-03-24 11:18:59 +00004594 trace_btrfs_inode_evict(inode);
4595
Chris Mason39279cc2007-06-12 06:35:45 -04004596 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004597 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004598 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004599 goto no_delete;
4600
Chris Mason39279cc2007-06-12 06:35:45 -04004601 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004602 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004603 goto no_delete;
4604 }
Al Virobd555972010-06-07 11:35:40 -04004605 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004606 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004607
Yan, Zhengc71bf092009-11-12 09:34:40 +00004608 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004609 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004610 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004611 goto no_delete;
4612 }
4613
Yan, Zheng76dda932009-09-21 16:00:26 -04004614 if (inode->i_nlink > 0) {
4615 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4616 goto no_delete;
4617 }
4618
Miao Xie0e8c36a2012-12-19 06:59:51 +00004619 ret = btrfs_commit_inode_delayed_inode(inode);
4620 if (ret) {
4621 btrfs_orphan_del(NULL, inode);
4622 goto no_delete;
4623 }
4624
Miao Xie66d8f3d2012-09-06 04:02:28 -06004625 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004626 if (!rsv) {
4627 btrfs_orphan_del(NULL, inode);
4628 goto no_delete;
4629 }
Josef Bacik4a338542011-08-29 11:01:31 -04004630 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004631 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004632 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004633
Chris Masondbe674a2008-07-17 12:54:05 -04004634 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004635
Josef Bacik4289a662011-08-05 13:22:24 -04004636 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004637 * This is a bit simpler than btrfs_truncate since we've already
4638 * reserved our space for our orphan item in the unlink, so we just
4639 * need to reserve some slack space in case we add bytes and update
4640 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004641 */
Yan, Zheng80825102009-11-12 09:35:36 +00004642 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004643 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4644 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004645
Josef Bacik726c35f2011-09-26 15:46:06 -04004646 /*
4647 * Try and steal from the global reserve since we will
4648 * likely not use this space anyway, we want to try as
4649 * hard as possible to get this to work.
4650 */
4651 if (ret)
4652 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4653
Yan, Zhengd68fc572010-05-16 10:49:58 -04004654 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04004655 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04004656 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004657 btrfs_orphan_del(NULL, inode);
4658 btrfs_free_block_rsv(root, rsv);
4659 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004660 }
4661
Miao Xie0e8c36a2012-12-19 06:59:51 +00004662 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004663 if (IS_ERR(trans)) {
4664 btrfs_orphan_del(NULL, inode);
4665 btrfs_free_block_rsv(root, rsv);
4666 goto no_delete;
4667 }
4668
4669 trans->block_rsv = rsv;
4670
Yan, Zhengd68fc572010-05-16 10:49:58 -04004671 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004672 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004673 break;
4674
Miao Xie8407aa42012-09-07 01:43:32 -06004675 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004676 btrfs_end_transaction(trans, root);
4677 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004678 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004679 }
4680
Josef Bacik4289a662011-08-05 13:22:24 -04004681 btrfs_free_block_rsv(root, rsv);
4682
Yan, Zheng80825102009-11-12 09:35:36 +00004683 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004684 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004685 ret = btrfs_orphan_del(trans, inode);
4686 BUG_ON(ret);
4687 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004688
Josef Bacik4289a662011-08-05 13:22:24 -04004689 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004690 if (!(root == root->fs_info->tree_root ||
4691 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004692 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004693
Chris Mason54aa1f42007-06-22 14:16:25 -04004694 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004695 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004696no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004697 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004698 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004699}
4700
4701/*
4702 * this returns the key found in the dir entry in the location pointer.
4703 * If no dir entries were found, location->objectid is 0.
4704 */
4705static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4706 struct btrfs_key *location)
4707{
4708 const char *name = dentry->d_name.name;
4709 int namelen = dentry->d_name.len;
4710 struct btrfs_dir_item *di;
4711 struct btrfs_path *path;
4712 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004713 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004714
4715 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004716 if (!path)
4717 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004718
Li Zefan33345d012011-04-20 10:31:50 +08004719 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004720 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004721 if (IS_ERR(di))
4722 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004723
David Sterbac7040052011-04-19 18:00:01 +02004724 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004725 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004726
Chris Mason5f39d392007-10-15 16:14:19 -04004727 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004728out:
Chris Mason39279cc2007-06-12 06:35:45 -04004729 btrfs_free_path(path);
4730 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004731out_err:
4732 location->objectid = 0;
4733 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004734}
4735
4736/*
4737 * when we hit a tree root in a directory, the btrfs part of the inode
4738 * needs to be changed to reflect the root directory of the tree root. This
4739 * is kind of like crossing a mount point.
4740 */
4741static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004742 struct inode *dir,
4743 struct dentry *dentry,
4744 struct btrfs_key *location,
4745 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004746{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004747 struct btrfs_path *path;
4748 struct btrfs_root *new_root;
4749 struct btrfs_root_ref *ref;
4750 struct extent_buffer *leaf;
4751 int ret;
4752 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004753
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004754 path = btrfs_alloc_path();
4755 if (!path) {
4756 err = -ENOMEM;
4757 goto out;
4758 }
Chris Mason39279cc2007-06-12 06:35:45 -04004759
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004760 err = -ENOENT;
4761 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4762 BTRFS_I(dir)->root->root_key.objectid,
4763 location->objectid);
4764 if (ret) {
4765 if (ret < 0)
4766 err = ret;
4767 goto out;
4768 }
Chris Mason39279cc2007-06-12 06:35:45 -04004769
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004770 leaf = path->nodes[0];
4771 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004772 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004773 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4774 goto out;
4775
4776 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4777 (unsigned long)(ref + 1),
4778 dentry->d_name.len);
4779 if (ret)
4780 goto out;
4781
David Sterbab3b4aa72011-04-21 01:20:15 +02004782 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004783
4784 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4785 if (IS_ERR(new_root)) {
4786 err = PTR_ERR(new_root);
4787 goto out;
4788 }
4789
4790 if (btrfs_root_refs(&new_root->root_item) == 0) {
4791 err = -ENOENT;
4792 goto out;
4793 }
4794
4795 *sub_root = new_root;
4796 location->objectid = btrfs_root_dirid(&new_root->root_item);
4797 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004798 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004799 err = 0;
4800out:
4801 btrfs_free_path(path);
4802 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004803}
4804
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004805static void inode_tree_add(struct inode *inode)
4806{
4807 struct btrfs_root *root = BTRFS_I(inode)->root;
4808 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004809 struct rb_node **p;
4810 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004811 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004812again:
4813 p = &root->inode_tree.rb_node;
4814 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004815
Al Viro1d3382c2010-10-23 15:19:20 -04004816 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004817 return;
4818
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004819 spin_lock(&root->inode_lock);
4820 while (*p) {
4821 parent = *p;
4822 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4823
Li Zefan33345d012011-04-20 10:31:50 +08004824 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004825 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004826 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004827 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004828 else {
4829 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004830 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004831 rb_erase(parent, &root->inode_tree);
4832 RB_CLEAR_NODE(parent);
4833 spin_unlock(&root->inode_lock);
4834 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004835 }
4836 }
4837 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4838 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4839 spin_unlock(&root->inode_lock);
4840}
4841
4842static void inode_tree_del(struct inode *inode)
4843{
4844 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004845 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004846
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004847 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004848 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004849 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004850 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004851 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004852 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004853 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004854
Josef Bacik0af3d002010-06-21 14:48:16 -04004855 /*
4856 * Free space cache has inodes in the tree root, but the tree root has a
4857 * root_refs of 0, so this could end up dropping the tree root as a
4858 * snapshot, so we need the extra !root->fs_info->tree_root check to
4859 * make sure we don't drop it.
4860 */
4861 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4862 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004863 synchronize_srcu(&root->fs_info->subvol_srcu);
4864 spin_lock(&root->inode_lock);
4865 empty = RB_EMPTY_ROOT(&root->inode_tree);
4866 spin_unlock(&root->inode_lock);
4867 if (empty)
4868 btrfs_add_dead_root(root);
4869 }
4870}
4871
Jeff Mahoney143bede2012-03-01 14:56:26 +01004872void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004873{
4874 struct rb_node *node;
4875 struct rb_node *prev;
4876 struct btrfs_inode *entry;
4877 struct inode *inode;
4878 u64 objectid = 0;
4879
4880 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4881
4882 spin_lock(&root->inode_lock);
4883again:
4884 node = root->inode_tree.rb_node;
4885 prev = NULL;
4886 while (node) {
4887 prev = node;
4888 entry = rb_entry(node, struct btrfs_inode, rb_node);
4889
Li Zefan33345d012011-04-20 10:31:50 +08004890 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004891 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004892 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004893 node = node->rb_right;
4894 else
4895 break;
4896 }
4897 if (!node) {
4898 while (prev) {
4899 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004900 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004901 node = prev;
4902 break;
4903 }
4904 prev = rb_next(prev);
4905 }
4906 }
4907 while (node) {
4908 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004909 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004910 inode = igrab(&entry->vfs_inode);
4911 if (inode) {
4912 spin_unlock(&root->inode_lock);
4913 if (atomic_read(&inode->i_count) > 1)
4914 d_prune_aliases(inode);
4915 /*
Al Viro45321ac2010-06-07 13:43:19 -04004916 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004917 * the inode cache when its usage count
4918 * hits zero.
4919 */
4920 iput(inode);
4921 cond_resched();
4922 spin_lock(&root->inode_lock);
4923 goto again;
4924 }
4925
4926 if (cond_resched_lock(&root->inode_lock))
4927 goto again;
4928
4929 node = rb_next(node);
4930 }
4931 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004932}
4933
Chris Masone02119d2008-09-05 16:13:11 -04004934static int btrfs_init_locked_inode(struct inode *inode, void *p)
4935{
4936 struct btrfs_iget_args *args = p;
4937 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004938 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004939 return 0;
4940}
4941
4942static int btrfs_find_actor(struct inode *inode, void *opaque)
4943{
4944 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004945 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004946 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004947}
4948
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004949static struct inode *btrfs_iget_locked(struct super_block *s,
4950 u64 objectid,
4951 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004952{
4953 struct inode *inode;
4954 struct btrfs_iget_args args;
4955 args.ino = objectid;
4956 args.root = root;
4957
4958 inode = iget5_locked(s, objectid, btrfs_find_actor,
4959 btrfs_init_locked_inode,
4960 (void *)&args);
4961 return inode;
4962}
4963
Balaji Rao1a54ef82008-07-21 02:01:04 +05304964/* Get an inode object given its location and corresponding root.
4965 * Returns in *is_new if the inode was read from disk
4966 */
4967struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004968 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304969{
4970 struct inode *inode;
4971
4972 inode = btrfs_iget_locked(s, location->objectid, root);
4973 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004974 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304975
4976 if (inode->i_state & I_NEW) {
4977 BTRFS_I(inode)->root = root;
4978 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4979 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004980 if (!is_bad_inode(inode)) {
4981 inode_tree_add(inode);
4982 unlock_new_inode(inode);
4983 if (new)
4984 *new = 1;
4985 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004986 unlock_new_inode(inode);
4987 iput(inode);
4988 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004989 }
4990 }
4991
Balaji Rao1a54ef82008-07-21 02:01:04 +05304992 return inode;
4993}
4994
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004995static struct inode *new_simple_dir(struct super_block *s,
4996 struct btrfs_key *key,
4997 struct btrfs_root *root)
4998{
4999 struct inode *inode = new_inode(s);
5000
5001 if (!inode)
5002 return ERR_PTR(-ENOMEM);
5003
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005004 BTRFS_I(inode)->root = root;
5005 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005006 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005007
5008 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005009 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005010 inode->i_fop = &simple_dir_operations;
5011 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5012 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5013
5014 return inode;
5015}
5016
Chris Mason3de45862008-11-17 21:02:50 -05005017struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005018{
Chris Masond3977122009-01-05 21:25:51 -05005019 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005020 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005021 struct btrfs_root *sub_root = root;
5022 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005023 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005024 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005025
5026 if (dentry->d_name.len > BTRFS_NAME_LEN)
5027 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005028
Jeff Layton39e3c952012-11-28 11:30:53 -05005029 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005030 if (ret < 0)
5031 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005032
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005033 if (location.objectid == 0)
5034 return NULL;
5035
5036 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005037 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005038 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005039 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005040
5041 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5042
Yan, Zheng76dda932009-09-21 16:00:26 -04005043 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005044 ret = fixup_tree_root_location(root, dir, dentry,
5045 &location, &sub_root);
5046 if (ret < 0) {
5047 if (ret != -ENOENT)
5048 inode = ERR_PTR(ret);
5049 else
5050 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5051 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005052 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005053 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005054 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5055
Julia Lawall34d19ba2011-01-24 19:55:19 +00005056 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005057 down_read(&root->fs_info->cleanup_work_sem);
5058 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005059 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005060 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005061 if (ret)
5062 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005063 }
5064
Chris Mason3de45862008-11-17 21:02:50 -05005065 return inode;
5066}
5067
Nick Pigginfe15ce42011-01-07 17:49:23 +11005068static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005069{
5070 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005071 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005072
Li Zefan848cce02012-02-21 17:04:28 +08005073 if (!inode && !IS_ROOT(dentry))
5074 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005075
Li Zefan848cce02012-02-21 17:04:28 +08005076 if (inode) {
5077 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005078 if (btrfs_root_refs(&root->root_item) == 0)
5079 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005080
5081 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5082 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005083 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005084 return 0;
5085}
5086
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005087static void btrfs_dentry_release(struct dentry *dentry)
5088{
5089 if (dentry->d_fsdata)
5090 kfree(dentry->d_fsdata);
5091}
5092
Chris Mason3de45862008-11-17 21:02:50 -05005093static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005094 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005095{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005096 struct dentry *ret;
5097
5098 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005099 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005100}
5101
Miao Xie16cdcec2011-04-22 18:12:22 +08005102unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005103 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5104};
5105
David Woodhousecbdf5a22008-08-06 19:42:33 +01005106static int btrfs_real_readdir(struct file *filp, void *dirent,
5107 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005108{
Chris Mason6da6aba2007-12-18 16:15:09 -05005109 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005110 struct btrfs_root *root = BTRFS_I(inode)->root;
5111 struct btrfs_item *item;
5112 struct btrfs_dir_item *di;
5113 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005114 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005115 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005116 struct list_head ins_list;
5117 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005118 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005119 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005120 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005121 unsigned char d_type;
5122 int over = 0;
5123 u32 di_cur;
5124 u32 di_total;
5125 u32 di_len;
5126 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005127 char tmp_name[32];
5128 char *name_ptr;
5129 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005130 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005131
5132 /* FIXME, use a real flag for deciding about the key type */
5133 if (root->fs_info->tree_root == root)
5134 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005135
Chris Mason39544012007-12-12 14:38:19 -05005136 /* special case for "." */
5137 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005138 over = filldir(dirent, ".", 1,
5139 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005140 if (over)
5141 return 0;
5142 filp->f_pos = 1;
5143 }
Chris Mason39544012007-12-12 14:38:19 -05005144 /* special case for .., just use the back ref */
5145 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005146 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005147 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005148 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005149 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005150 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005151 filp->f_pos = 2;
5152 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005153 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005154 if (!path)
5155 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005156
Josef Bacik026fd312011-05-13 10:32:11 -04005157 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005158
Miao Xie16cdcec2011-04-22 18:12:22 +08005159 if (key_type == BTRFS_DIR_INDEX_KEY) {
5160 INIT_LIST_HEAD(&ins_list);
5161 INIT_LIST_HEAD(&del_list);
5162 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5163 }
5164
Chris Mason39279cc2007-06-12 06:35:45 -04005165 btrfs_set_key_type(&key, key_type);
5166 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005167 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005168
Chris Mason39279cc2007-06-12 06:35:45 -04005169 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5170 if (ret < 0)
5171 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005172
5173 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005174 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005175 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005176 if (slot >= btrfs_header_nritems(leaf)) {
5177 ret = btrfs_next_leaf(root, path);
5178 if (ret < 0)
5179 goto err;
5180 else if (ret > 0)
5181 break;
5182 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005183 }
Chris Mason3de45862008-11-17 21:02:50 -05005184
Chris Mason5f39d392007-10-15 16:14:19 -04005185 item = btrfs_item_nr(leaf, slot);
5186 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5187
5188 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005189 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005190 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005191 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005192 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005193 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005194 if (key_type == BTRFS_DIR_INDEX_KEY &&
5195 btrfs_should_delete_dir_index(&del_list,
5196 found_key.offset))
5197 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005198
5199 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005200 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005201
Chris Mason39279cc2007-06-12 06:35:45 -04005202 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5203 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005204 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005205
5206 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005207 struct btrfs_key location;
5208
Josef Bacik22a94d42011-03-16 16:47:17 -04005209 if (verify_dir_item(root, leaf, di))
5210 break;
5211
Chris Mason5f39d392007-10-15 16:14:19 -04005212 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005213 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005214 name_ptr = tmp_name;
5215 } else {
5216 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005217 if (!name_ptr) {
5218 ret = -ENOMEM;
5219 goto err;
5220 }
Chris Mason5f39d392007-10-15 16:14:19 -04005221 }
5222 read_extent_buffer(leaf, name_ptr,
5223 (unsigned long)(di + 1), name_len);
5224
5225 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5226 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005227
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005228
Chris Mason3de45862008-11-17 21:02:50 -05005229 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005230 * skip it.
5231 *
5232 * In contrast to old kernels, we insert the snapshot's
5233 * dir item and dir index after it has been created, so
5234 * we won't find a reference to our own snapshot. We
5235 * still keep the following code for backward
5236 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005237 */
5238 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5239 location.objectid == root->root_key.objectid) {
5240 over = 0;
5241 goto skip;
5242 }
Chris Mason5f39d392007-10-15 16:14:19 -04005243 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005244 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005245 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005246
Chris Mason3de45862008-11-17 21:02:50 -05005247skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005248 if (name_ptr != tmp_name)
5249 kfree(name_ptr);
5250
Chris Mason39279cc2007-06-12 06:35:45 -04005251 if (over)
5252 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005253 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005254 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005255 di_cur += di_len;
5256 di = (struct btrfs_dir_item *)((char *)di + di_len);
5257 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005258next:
5259 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005260 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005261
Miao Xie16cdcec2011-04-22 18:12:22 +08005262 if (key_type == BTRFS_DIR_INDEX_KEY) {
5263 if (is_curr)
5264 filp->f_pos++;
5265 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5266 &ins_list);
5267 if (ret)
5268 goto nopos;
5269 }
5270
David Woodhouse49593bf2008-08-17 17:08:36 +01005271 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005272 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005273 /*
5274 * 32-bit glibc will use getdents64, but then strtol -
5275 * so the last number we can serve is this.
5276 */
5277 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005278 else
5279 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005280nopos:
5281 ret = 0;
5282err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005283 if (key_type == BTRFS_DIR_INDEX_KEY)
5284 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005285 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005286 return ret;
5287}
5288
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005289int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005290{
5291 struct btrfs_root *root = BTRFS_I(inode)->root;
5292 struct btrfs_trans_handle *trans;
5293 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005294 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005295
Josef Bacik72ac3c02012-05-23 14:13:11 -04005296 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005297 return 0;
5298
Liu Bo83eea1f2012-07-10 05:28:39 -06005299 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005300 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005301
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005302 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005303 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005304 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005305 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005306 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005307 if (IS_ERR(trans))
5308 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005309 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005310 }
5311 return ret;
5312}
5313
5314/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005315 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005316 * inode changes. But, it is most likely to find the inode in cache.
5317 * FIXME, needs more benchmarking...there are no reasons other than performance
5318 * to keep or drop this code.
5319 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05005320int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005321{
5322 struct btrfs_root *root = BTRFS_I(inode)->root;
5323 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005324 int ret;
5325
Josef Bacik72ac3c02012-05-23 14:13:11 -04005326 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005327 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005328
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005329 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005330 if (IS_ERR(trans))
5331 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005332
5333 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005334 if (ret && ret == -ENOSPC) {
5335 /* whoops, lets try again with the full transaction */
5336 btrfs_end_transaction(trans, root);
5337 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005338 if (IS_ERR(trans))
5339 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005340
Chris Mason94b60442010-05-26 11:02:00 -04005341 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005342 }
Chris Mason39279cc2007-06-12 06:35:45 -04005343 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005344 if (BTRFS_I(inode)->delayed_node)
5345 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005346
5347 return ret;
5348}
5349
5350/*
5351 * This is a copy of file_update_time. We need this so we can return error on
5352 * ENOSPC for updating the inode in the case of file write and mmap writes.
5353 */
Josef Bacike41f9412012-03-26 09:46:47 -04005354static int btrfs_update_time(struct inode *inode, struct timespec *now,
5355 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005356{
Alexander Block2bc5565282012-06-15 09:49:33 +02005357 struct btrfs_root *root = BTRFS_I(inode)->root;
5358
5359 if (btrfs_root_readonly(root))
5360 return -EROFS;
5361
Josef Bacike41f9412012-03-26 09:46:47 -04005362 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005363 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005364 if (flags & S_CTIME)
5365 inode->i_ctime = *now;
5366 if (flags & S_MTIME)
5367 inode->i_mtime = *now;
5368 if (flags & S_ATIME)
5369 inode->i_atime = *now;
5370 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005371}
5372
Chris Masond352ac62008-09-29 15:18:18 -04005373/*
5374 * find the highest existing sequence number in a directory
5375 * and then set the in-memory index_cnt variable to reflect
5376 * free sequence numbers
5377 */
Josef Bacikaec74772008-07-24 12:12:38 -04005378static int btrfs_set_inode_index_count(struct inode *inode)
5379{
5380 struct btrfs_root *root = BTRFS_I(inode)->root;
5381 struct btrfs_key key, found_key;
5382 struct btrfs_path *path;
5383 struct extent_buffer *leaf;
5384 int ret;
5385
Li Zefan33345d012011-04-20 10:31:50 +08005386 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005387 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5388 key.offset = (u64)-1;
5389
5390 path = btrfs_alloc_path();
5391 if (!path)
5392 return -ENOMEM;
5393
5394 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5395 if (ret < 0)
5396 goto out;
5397 /* FIXME: we should be able to handle this */
5398 if (ret == 0)
5399 goto out;
5400 ret = 0;
5401
5402 /*
5403 * MAGIC NUMBER EXPLANATION:
5404 * since we search a directory based on f_pos we have to start at 2
5405 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5406 * else has to start at 2
5407 */
5408 if (path->slots[0] == 0) {
5409 BTRFS_I(inode)->index_cnt = 2;
5410 goto out;
5411 }
5412
5413 path->slots[0]--;
5414
5415 leaf = path->nodes[0];
5416 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5417
Li Zefan33345d012011-04-20 10:31:50 +08005418 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005419 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5420 BTRFS_I(inode)->index_cnt = 2;
5421 goto out;
5422 }
5423
5424 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5425out:
5426 btrfs_free_path(path);
5427 return ret;
5428}
5429
Chris Masond352ac62008-09-29 15:18:18 -04005430/*
5431 * helper to find a free sequence number in a given directory. This current
5432 * code is very simple, later versions will do smarter things in the btree
5433 */
Chris Mason3de45862008-11-17 21:02:50 -05005434int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005435{
5436 int ret = 0;
5437
5438 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005439 ret = btrfs_inode_delayed_dir_index_count(dir);
5440 if (ret) {
5441 ret = btrfs_set_inode_index_count(dir);
5442 if (ret)
5443 return ret;
5444 }
Josef Bacikaec74772008-07-24 12:12:38 -04005445 }
5446
Chris Mason00e4e6b2008-08-05 11:18:09 -04005447 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005448 BTRFS_I(dir)->index_cnt++;
5449
5450 return ret;
5451}
5452
Chris Mason39279cc2007-06-12 06:35:45 -04005453static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5454 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005455 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005456 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005457 u64 ref_objectid, u64 objectid,
5458 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005459{
5460 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005461 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005462 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005463 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005464 struct btrfs_inode_ref *ref;
5465 struct btrfs_key key[2];
5466 u32 sizes[2];
5467 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005468 int ret;
5469 int owner;
5470
Chris Mason5f39d392007-10-15 16:14:19 -04005471 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005472 if (!path)
5473 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005474
Chris Mason39279cc2007-06-12 06:35:45 -04005475 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005476 if (!inode) {
5477 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005478 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005479 }
Chris Mason39279cc2007-06-12 06:35:45 -04005480
Li Zefan581bb052011-04-20 10:06:11 +08005481 /*
5482 * we have to initialize this early, so we can reclaim the inode
5483 * number if we fail afterwards in this function.
5484 */
5485 inode->i_ino = objectid;
5486
Josef Bacikaec74772008-07-24 12:12:38 -04005487 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005488 trace_btrfs_inode_request(dir);
5489
Chris Mason3de45862008-11-17 21:02:50 -05005490 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005491 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005492 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005493 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005494 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005495 }
Josef Bacikaec74772008-07-24 12:12:38 -04005496 }
5497 /*
5498 * index_cnt is ignored for everything but a dir,
5499 * btrfs_get_inode_index_count has an explanation for the magic
5500 * number
5501 */
5502 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005503 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005504 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005505 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005506
Josef Bacik5dc562c2012-08-17 13:14:17 -04005507 /*
5508 * We could have gotten an inode number from somebody who was fsynced
5509 * and then removed in this same transaction, so let's just set full
5510 * sync since it will be a full sync anyway and this will blow away the
5511 * old info in the log.
5512 */
5513 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5514
Al Viro569254b2011-07-24 17:08:40 -04005515 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005516 owner = 0;
5517 else
5518 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005519
5520 key[0].objectid = objectid;
5521 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5522 key[0].offset = 0;
5523
Mark Fashehf1863732012-08-08 11:32:27 -07005524 /*
5525 * Start new inodes with an inode_ref. This is slightly more
5526 * efficient for small numbers of hard links since they will
5527 * be packed into one item. Extended refs will kick in if we
5528 * add more hard links than can fit in the ref item.
5529 */
Chris Mason9c583092008-01-29 15:15:18 -05005530 key[1].objectid = objectid;
5531 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5532 key[1].offset = ref_objectid;
5533
5534 sizes[0] = sizeof(struct btrfs_inode_item);
5535 sizes[1] = name_len + sizeof(*ref);
5536
Chris Masonb9473432009-03-13 11:00:37 -04005537 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005538 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5539 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005540 goto fail;
5541
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005542 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005543 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005544 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005545 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5546 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005547 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5548 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005549 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005550
5551 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5552 struct btrfs_inode_ref);
5553 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005554 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005555 ptr = (unsigned long)(ref + 1);
5556 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5557
Chris Mason5f39d392007-10-15 16:14:19 -04005558 btrfs_mark_buffer_dirty(path->nodes[0]);
5559 btrfs_free_path(path);
5560
Chris Mason39279cc2007-06-12 06:35:45 -04005561 location = &BTRFS_I(inode)->location;
5562 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005563 location->offset = 0;
5564 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5565
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005566 btrfs_inherit_iflags(inode, dir);
5567
Al Viro569254b2011-07-24 17:08:40 -04005568 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005569 if (btrfs_test_opt(root, NODATASUM))
5570 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005571 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005572 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5573 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005574 }
5575
Chris Mason39279cc2007-06-12 06:35:45 -04005576 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005577 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005578
5579 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005580 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005581
Alexander Block8ea05e32012-07-25 17:35:53 +02005582 btrfs_update_root_times(trans, root);
5583
Chris Mason39279cc2007-06-12 06:35:45 -04005584 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005585fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005586 if (dir)
5587 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005588 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005589 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005590 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005591}
5592
5593static inline u8 btrfs_inode_type(struct inode *inode)
5594{
5595 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5596}
5597
Chris Masond352ac62008-09-29 15:18:18 -04005598/*
5599 * utility function to add 'inode' into 'parent_inode' with
5600 * a give name and a given sequence number.
5601 * if 'add_backref' is true, also insert a backref from the
5602 * inode to the parent directory.
5603 */
Chris Masone02119d2008-09-05 16:13:11 -04005604int btrfs_add_link(struct btrfs_trans_handle *trans,
5605 struct inode *parent_inode, struct inode *inode,
5606 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005607{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005608 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005609 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005610 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005611 u64 ino = btrfs_ino(inode);
5612 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005613
Li Zefan33345d012011-04-20 10:31:50 +08005614 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005615 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5616 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005617 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005618 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5619 key.offset = 0;
5620 }
Chris Mason39279cc2007-06-12 06:35:45 -04005621
Li Zefan33345d012011-04-20 10:31:50 +08005622 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005623 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5624 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005625 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005626 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005627 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5628 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005629 }
5630
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005631 /* Nothing to clean up yet */
5632 if (ret)
5633 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005634
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005635 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5636 parent_inode, &key,
5637 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005638 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005639 goto fail_dir_item;
5640 else if (ret) {
5641 btrfs_abort_transaction(trans, root, ret);
5642 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005643 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005644
5645 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5646 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005647 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005648 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5649 ret = btrfs_update_inode(trans, root, parent_inode);
5650 if (ret)
5651 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005652 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005653
5654fail_dir_item:
5655 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5656 u64 local_index;
5657 int err;
5658 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5659 key.objectid, root->root_key.objectid,
5660 parent_ino, &local_index, name, name_len);
5661
5662 } else if (add_backref) {
5663 u64 local_index;
5664 int err;
5665
5666 err = btrfs_del_inode_ref(trans, root, name, name_len,
5667 ino, parent_ino, &local_index);
5668 }
5669 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005670}
5671
5672static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005673 struct inode *dir, struct dentry *dentry,
5674 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005675{
Josef Bacika1b075d2010-11-19 20:36:11 +00005676 int err = btrfs_add_link(trans, dir, inode,
5677 dentry->d_name.name, dentry->d_name.len,
5678 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005679 if (err > 0)
5680 err = -EEXIST;
5681 return err;
5682}
5683
Josef Bacik618e21d2007-07-11 10:18:17 -04005684static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005685 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005686{
5687 struct btrfs_trans_handle *trans;
5688 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005689 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005690 int err;
5691 int drop_inode = 0;
5692 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005693 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005694
5695 if (!new_valid_dev(rdev))
5696 return -EINVAL;
5697
Josef Bacik9ed74f22009-09-11 16:12:44 -04005698 /*
5699 * 2 for inode item and ref
5700 * 2 for dir items
5701 * 1 for xattr if selinux is on
5702 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005703 trans = btrfs_start_transaction(root, 5);
5704 if (IS_ERR(trans))
5705 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005706
Li Zefan581bb052011-04-20 10:06:11 +08005707 err = btrfs_find_free_ino(root, &objectid);
5708 if (err)
5709 goto out_unlock;
5710
Josef Bacikaec74772008-07-24 12:12:38 -04005711 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005712 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005713 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005714 if (IS_ERR(inode)) {
5715 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005716 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005717 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005718
Eric Paris2a7dba32011-02-01 11:05:39 -05005719 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005720 if (err) {
5721 drop_inode = 1;
5722 goto out_unlock;
5723 }
5724
Casey Schauflerad19db72011-12-15 10:09:07 -05005725 /*
5726 * If the active LSM wants to access the inode during
5727 * d_instantiate it needs these. Smack checks to see
5728 * if the filesystem supports xattrs by looking at the
5729 * ops vector.
5730 */
5731
5732 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005733 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005734 if (err)
5735 drop_inode = 1;
5736 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005737 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005738 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005739 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005740 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005741out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005742 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005743 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005744 if (drop_inode) {
5745 inode_dec_link_count(inode);
5746 iput(inode);
5747 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005748 return err;
5749}
5750
Chris Mason39279cc2007-06-12 06:35:45 -04005751static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005752 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005753{
5754 struct btrfs_trans_handle *trans;
5755 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005756 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005757 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005758 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005759 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005760 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005761
Josef Bacik9ed74f22009-09-11 16:12:44 -04005762 /*
5763 * 2 for inode item and ref
5764 * 2 for dir items
5765 * 1 for xattr if selinux is on
5766 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005767 trans = btrfs_start_transaction(root, 5);
5768 if (IS_ERR(trans))
5769 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005770
Li Zefan581bb052011-04-20 10:06:11 +08005771 err = btrfs_find_free_ino(root, &objectid);
5772 if (err)
5773 goto out_unlock;
5774
Josef Bacikaec74772008-07-24 12:12:38 -04005775 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005776 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005777 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005778 if (IS_ERR(inode)) {
5779 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005780 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005781 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005782 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005783
Eric Paris2a7dba32011-02-01 11:05:39 -05005784 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005785 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005786 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005787
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005788 err = btrfs_update_inode(trans, root, inode);
5789 if (err)
5790 goto out_unlock;
5791
Casey Schauflerad19db72011-12-15 10:09:07 -05005792 /*
5793 * If the active LSM wants to access the inode during
5794 * d_instantiate it needs these. Smack checks to see
5795 * if the filesystem supports xattrs by looking at the
5796 * ops vector.
5797 */
5798 inode->i_fop = &btrfs_file_operations;
5799 inode->i_op = &btrfs_file_inode_operations;
5800
Josef Bacika1b075d2010-11-19 20:36:11 +00005801 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005802 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005803 goto out_unlock;
5804
5805 inode->i_mapping->a_ops = &btrfs_aops;
5806 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5807 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5808 d_instantiate(dentry, inode);
5809
Chris Mason39279cc2007-06-12 06:35:45 -04005810out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005811 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005812 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005813 inode_dec_link_count(inode);
5814 iput(inode);
5815 }
Liu Bob53d3f52012-11-14 14:34:34 +00005816 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005817 return err;
5818}
5819
5820static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5821 struct dentry *dentry)
5822{
5823 struct btrfs_trans_handle *trans;
5824 struct btrfs_root *root = BTRFS_I(dir)->root;
5825 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005826 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005827 int err;
5828 int drop_inode = 0;
5829
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005830 /* do not allow sys_link's with other subvols of the same device */
5831 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005832 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005833
Mark Fashehf1863732012-08-08 11:32:27 -07005834 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005835 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005836
Chris Mason3de45862008-11-17 21:02:50 -05005837 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005838 if (err)
5839 goto fail;
5840
Yan, Zhenga22285a2010-05-16 10:48:46 -04005841 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005842 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005843 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005844 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005845 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005846 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005847 if (IS_ERR(trans)) {
5848 err = PTR_ERR(trans);
5849 goto fail;
5850 }
Chris Mason5f39d392007-10-15 16:14:19 -04005851
Miao Xie31534952011-04-13 13:19:21 +08005852 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005853 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005854 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04005855 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005856 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005857
Josef Bacika1b075d2010-11-19 20:36:11 +00005858 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005859
Yan, Zhenga5719522009-09-24 09:17:31 -04005860 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005861 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005862 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005863 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005864 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005865 if (err)
5866 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005867 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005868 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005869 }
Chris Mason39279cc2007-06-12 06:35:45 -04005870
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005871 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005872fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005873 if (drop_inode) {
5874 inode_dec_link_count(inode);
5875 iput(inode);
5876 }
Liu Bob53d3f52012-11-14 14:34:34 +00005877 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005878 return err;
5879}
5880
Al Viro18bb1db2011-07-26 01:41:39 -04005881static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005882{
Chris Masonb9d86662008-05-02 16:13:49 -04005883 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005884 struct btrfs_trans_handle *trans;
5885 struct btrfs_root *root = BTRFS_I(dir)->root;
5886 int err = 0;
5887 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005888 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005889 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005890
Josef Bacik9ed74f22009-09-11 16:12:44 -04005891 /*
5892 * 2 items for inode and ref
5893 * 2 items for dir items
5894 * 1 for xattr if selinux is on
5895 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005896 trans = btrfs_start_transaction(root, 5);
5897 if (IS_ERR(trans))
5898 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005899
Li Zefan581bb052011-04-20 10:06:11 +08005900 err = btrfs_find_free_ino(root, &objectid);
5901 if (err)
5902 goto out_fail;
5903
Josef Bacikaec74772008-07-24 12:12:38 -04005904 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005905 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005906 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005907 if (IS_ERR(inode)) {
5908 err = PTR_ERR(inode);
5909 goto out_fail;
5910 }
Chris Mason5f39d392007-10-15 16:14:19 -04005911
Chris Mason39279cc2007-06-12 06:35:45 -04005912 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005913
Eric Paris2a7dba32011-02-01 11:05:39 -05005914 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005915 if (err)
5916 goto out_fail;
5917
Chris Mason39279cc2007-06-12 06:35:45 -04005918 inode->i_op = &btrfs_dir_inode_operations;
5919 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005920
Chris Masondbe674a2008-07-17 12:54:05 -04005921 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005922 err = btrfs_update_inode(trans, root, inode);
5923 if (err)
5924 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005925
Josef Bacika1b075d2010-11-19 20:36:11 +00005926 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5927 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005928 if (err)
5929 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005930
Chris Mason39279cc2007-06-12 06:35:45 -04005931 d_instantiate(dentry, inode);
5932 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005933
5934out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005935 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005936 if (drop_on_err)
5937 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005938 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005939 return err;
5940}
5941
Chris Masond352ac62008-09-29 15:18:18 -04005942/* helper for btfs_get_extent. Given an existing extent in the tree,
5943 * and an extent that you want to insert, deal with overlap and insert
5944 * the new extent into the tree.
5945 */
Chris Mason3b951512008-04-17 11:29:12 -04005946static int merge_extent_mapping(struct extent_map_tree *em_tree,
5947 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005948 struct extent_map *em,
5949 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005950{
5951 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005952
Chris Masone6dcd2d2008-07-17 12:53:50 -04005953 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5954 start_diff = map_start - em->start;
5955 em->start = map_start;
5956 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005957 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5958 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005959 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005960 em->block_len -= start_diff;
5961 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005962 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005963}
5964
Chris Masonc8b97812008-10-29 14:49:59 -04005965static noinline int uncompress_inline(struct btrfs_path *path,
5966 struct inode *inode, struct page *page,
5967 size_t pg_offset, u64 extent_offset,
5968 struct btrfs_file_extent_item *item)
5969{
5970 int ret;
5971 struct extent_buffer *leaf = path->nodes[0];
5972 char *tmp;
5973 size_t max_size;
5974 unsigned long inline_size;
5975 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005976 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005977
5978 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005979 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005980 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5981 inline_size = btrfs_file_extent_inline_item_len(leaf,
5982 btrfs_item_nr(leaf, path->slots[0]));
5983 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005984 if (!tmp)
5985 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005986 ptr = btrfs_file_extent_inline_start(item);
5987
5988 read_extent_buffer(leaf, tmp, ptr, inline_size);
5989
Chris Mason5b050f02008-11-11 09:34:41 -05005990 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005991 ret = btrfs_decompress(compress_type, tmp, page,
5992 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005993 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005994 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005995 unsigned long copy_size = min_t(u64,
5996 PAGE_CACHE_SIZE - pg_offset,
5997 max_size - extent_offset);
5998 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005999 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006000 }
6001 kfree(tmp);
6002 return 0;
6003}
6004
Chris Masond352ac62008-09-29 15:18:18 -04006005/*
6006 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006007 * the ugly parts come from merging extents from the disk with the in-ram
6008 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006009 * where the in-ram extents might be locked pending data=ordered completion.
6010 *
6011 * This also copies inline extents directly into the page.
6012 */
Chris Masond3977122009-01-05 21:25:51 -05006013
Chris Masona52d9a82007-08-27 16:49:44 -04006014struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006015 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006016 int create)
6017{
6018 int ret;
6019 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006020 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006021 u64 extent_start = 0;
6022 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006023 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006024 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006025 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006026 struct btrfs_root *root = BTRFS_I(inode)->root;
6027 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006028 struct extent_buffer *leaf;
6029 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006030 struct extent_map *em = NULL;
6031 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006032 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006033 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006034 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006035
Chris Masona52d9a82007-08-27 16:49:44 -04006036again:
Chris Mason890871b2009-09-02 16:24:52 -04006037 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006038 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006039 if (em)
6040 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006041 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006042
Chris Masona52d9a82007-08-27 16:49:44 -04006043 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006044 if (em->start > start || em->start + em->len <= start)
6045 free_extent_map(em);
6046 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006047 free_extent_map(em);
6048 else
6049 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006050 }
David Sterba172ddd62011-04-21 00:48:27 +02006051 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006052 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006053 err = -ENOMEM;
6054 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006055 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006056 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006057 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006058 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006059 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006060 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006061
6062 if (!path) {
6063 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006064 if (!path) {
6065 err = -ENOMEM;
6066 goto out;
6067 }
6068 /*
6069 * Chances are we'll be called again, so go ahead and do
6070 * readahead
6071 */
6072 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006073 }
6074
Chris Mason179e29e2007-11-01 11:28:41 -04006075 ret = btrfs_lookup_file_extent(trans, root, path,
6076 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006077 if (ret < 0) {
6078 err = ret;
6079 goto out;
6080 }
6081
6082 if (ret != 0) {
6083 if (path->slots[0] == 0)
6084 goto not_found;
6085 path->slots[0]--;
6086 }
6087
Chris Mason5f39d392007-10-15 16:14:19 -04006088 leaf = path->nodes[0];
6089 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006090 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006091 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006092 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6093 found_type = btrfs_key_type(&found_key);
6094 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006095 found_type != BTRFS_EXTENT_DATA_KEY) {
6096 goto not_found;
6097 }
6098
Chris Mason5f39d392007-10-15 16:14:19 -04006099 found_type = btrfs_file_extent_type(leaf, item);
6100 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006101 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006102 if (found_type == BTRFS_FILE_EXTENT_REG ||
6103 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006104 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006105 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006106 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6107 size_t size;
6108 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006109 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006110 }
6111
6112 if (start >= extent_end) {
6113 path->slots[0]++;
6114 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6115 ret = btrfs_next_leaf(root, path);
6116 if (ret < 0) {
6117 err = ret;
6118 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006119 }
Yan Zheng9036c102008-10-30 14:19:41 -04006120 if (ret > 0)
6121 goto not_found;
6122 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006123 }
Yan Zheng9036c102008-10-30 14:19:41 -04006124 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6125 if (found_key.objectid != objectid ||
6126 found_key.type != BTRFS_EXTENT_DATA_KEY)
6127 goto not_found;
6128 if (start + len <= found_key.offset)
6129 goto not_found;
6130 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006131 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006132 em->len = found_key.offset - start;
6133 goto not_found_em;
6134 }
6135
Yan Zhengd899e052008-10-30 14:25:28 -04006136 if (found_type == BTRFS_FILE_EXTENT_REG ||
6137 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006138 em->start = extent_start;
6139 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006140 em->orig_start = extent_start -
6141 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006142 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6143 item);
Chris Masondb945352007-10-15 16:15:53 -04006144 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6145 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006146 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006147 goto insert;
6148 }
Li Zefan261507a02010-12-17 14:21:50 +08006149 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006150 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006151 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006152 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006153 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006154 } else {
6155 bytenr += btrfs_file_extent_offset(leaf, item);
6156 em->block_start = bytenr;
6157 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006158 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6159 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006160 }
Chris Masona52d9a82007-08-27 16:49:44 -04006161 goto insert;
6162 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006163 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006164 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006165 size_t size;
6166 size_t extent_offset;
6167 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006168
Chris Masona52d9a82007-08-27 16:49:44 -04006169 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006170 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006171 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006172 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006173 goto out;
6174 }
6175
Yan Zheng9036c102008-10-30 14:19:41 -04006176 size = btrfs_file_extent_inline_len(leaf, item);
6177 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006178 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006179 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006180 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006181 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006182 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006183 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006184 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006185 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006186 em->compress_type = compress_type;
6187 }
Yan689f9342007-10-29 11:41:07 -04006188 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006189 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006190 if (btrfs_file_extent_compression(leaf, item) !=
6191 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006192 ret = uncompress_inline(path, inode, page,
6193 pg_offset,
6194 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006195 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006196 } else {
6197 map = kmap(page);
6198 read_extent_buffer(leaf, map + pg_offset, ptr,
6199 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006200 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6201 memset(map + pg_offset + copy_size, 0,
6202 PAGE_CACHE_SIZE - pg_offset -
6203 copy_size);
6204 }
Chris Masonc8b97812008-10-29 14:49:59 -04006205 kunmap(page);
6206 }
Chris Mason179e29e2007-11-01 11:28:41 -04006207 flush_dcache_page(page);
6208 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006209 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006210 if (!trans) {
6211 kunmap(page);
6212 free_extent_map(em);
6213 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006214
David Sterbab3b4aa72011-04-21 01:20:15 +02006215 btrfs_release_path(path);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006216 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006217
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006218 if (IS_ERR(trans))
6219 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006220 goto again;
6221 }
Chris Masonc8b97812008-10-29 14:49:59 -04006222 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006223 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006224 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006225 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006226 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006227 }
Chris Masond1310b22008-01-24 16:13:08 -05006228 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006229 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006230 goto insert;
6231 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006232 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006233 }
6234not_found:
6235 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006236 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006237 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006238not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006239 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006240 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006241insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006242 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006243 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05006244 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
6245 "[%llu %llu]\n", (unsigned long long)em->start,
6246 (unsigned long long)em->len,
6247 (unsigned long long)start,
6248 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006249 err = -EIO;
6250 goto out;
6251 }
Chris Masond1310b22008-01-24 16:13:08 -05006252
6253 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006254 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006255 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04006256 /* it is possible that someone inserted the extent into the tree
6257 * while we had the lock dropped. It is also possible that
6258 * an overlapping map exists in the tree
6259 */
Chris Masona52d9a82007-08-27 16:49:44 -04006260 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006261 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006262
6263 ret = 0;
6264
Chris Mason3b951512008-04-17 11:29:12 -04006265 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006266 if (existing && (existing->start > start ||
6267 existing->start + existing->len <= start)) {
6268 free_extent_map(existing);
6269 existing = NULL;
6270 }
Chris Mason3b951512008-04-17 11:29:12 -04006271 if (!existing) {
6272 existing = lookup_extent_mapping(em_tree, em->start,
6273 em->len);
6274 if (existing) {
6275 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006276 em, start,
6277 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006278 free_extent_map(existing);
6279 if (err) {
6280 free_extent_map(em);
6281 em = NULL;
6282 }
6283 } else {
6284 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006285 free_extent_map(em);
6286 em = NULL;
6287 }
6288 } else {
6289 free_extent_map(em);
6290 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006291 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006292 }
Chris Masona52d9a82007-08-27 16:49:44 -04006293 }
Chris Mason890871b2009-09-02 16:24:52 -04006294 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006295out:
liubo1abe9b82011-03-24 11:18:59 +00006296
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006297 if (em)
6298 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006299
Chris Masonf4219502008-07-22 11:18:09 -04006300 if (path)
6301 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006302 if (trans) {
6303 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006304 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006305 err = ret;
6306 }
Chris Masona52d9a82007-08-27 16:49:44 -04006307 if (err) {
6308 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006309 return ERR_PTR(err);
6310 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006311 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006312 return em;
6313}
6314
Chris Masonec29ed52011-02-23 16:23:20 -05006315struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6316 size_t pg_offset, u64 start, u64 len,
6317 int create)
6318{
6319 struct extent_map *em;
6320 struct extent_map *hole_em = NULL;
6321 u64 range_start = start;
6322 u64 end;
6323 u64 found;
6324 u64 found_end;
6325 int err = 0;
6326
6327 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6328 if (IS_ERR(em))
6329 return em;
6330 if (em) {
6331 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006332 * if our em maps to
6333 * - a hole or
6334 * - a pre-alloc extent,
6335 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006336 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006337 if (em->block_start != EXTENT_MAP_HOLE &&
6338 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006339 return em;
6340 else
6341 hole_em = em;
6342 }
6343
6344 /* check to see if we've wrapped (len == -1 or similar) */
6345 end = start + len;
6346 if (end < start)
6347 end = (u64)-1;
6348 else
6349 end -= 1;
6350
6351 em = NULL;
6352
6353 /* ok, we didn't find anything, lets look for delalloc */
6354 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6355 end, len, EXTENT_DELALLOC, 1);
6356 found_end = range_start + found;
6357 if (found_end < range_start)
6358 found_end = (u64)-1;
6359
6360 /*
6361 * we didn't find anything useful, return
6362 * the original results from get_extent()
6363 */
6364 if (range_start > end || found_end <= start) {
6365 em = hole_em;
6366 hole_em = NULL;
6367 goto out;
6368 }
6369
6370 /* adjust the range_start to make sure it doesn't
6371 * go backwards from the start they passed in
6372 */
6373 range_start = max(start,range_start);
6374 found = found_end - range_start;
6375
6376 if (found > 0) {
6377 u64 hole_start = start;
6378 u64 hole_len = len;
6379
David Sterba172ddd62011-04-21 00:48:27 +02006380 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006381 if (!em) {
6382 err = -ENOMEM;
6383 goto out;
6384 }
6385 /*
6386 * when btrfs_get_extent can't find anything it
6387 * returns one huge hole
6388 *
6389 * make sure what it found really fits our range, and
6390 * adjust to make sure it is based on the start from
6391 * the caller
6392 */
6393 if (hole_em) {
6394 u64 calc_end = extent_map_end(hole_em);
6395
6396 if (calc_end <= start || (hole_em->start > end)) {
6397 free_extent_map(hole_em);
6398 hole_em = NULL;
6399 } else {
6400 hole_start = max(hole_em->start, start);
6401 hole_len = calc_end - hole_start;
6402 }
6403 }
6404 em->bdev = NULL;
6405 if (hole_em && range_start > hole_start) {
6406 /* our hole starts before our delalloc, so we
6407 * have to return just the parts of the hole
6408 * that go until the delalloc starts
6409 */
6410 em->len = min(hole_len,
6411 range_start - hole_start);
6412 em->start = hole_start;
6413 em->orig_start = hole_start;
6414 /*
6415 * don't adjust block start at all,
6416 * it is fixed at EXTENT_MAP_HOLE
6417 */
6418 em->block_start = hole_em->block_start;
6419 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006420 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6421 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006422 } else {
6423 em->start = range_start;
6424 em->len = found;
6425 em->orig_start = range_start;
6426 em->block_start = EXTENT_MAP_DELALLOC;
6427 em->block_len = found;
6428 }
6429 } else if (hole_em) {
6430 return hole_em;
6431 }
6432out:
6433
6434 free_extent_map(hole_em);
6435 if (err) {
6436 free_extent_map(em);
6437 return ERR_PTR(err);
6438 }
6439 return em;
6440}
6441
Josef Bacik4b46fce2010-05-23 11:00:55 -04006442static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6443 u64 start, u64 len)
6444{
6445 struct btrfs_root *root = BTRFS_I(inode)->root;
6446 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006447 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006448 struct btrfs_key ins;
6449 u64 alloc_hint;
6450 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006451
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006452 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006453 if (IS_ERR(trans))
6454 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006455
6456 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6457
6458 alloc_hint = get_extent_allocation_hint(inode, start, len);
6459 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006460 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006461 if (ret) {
6462 em = ERR_PTR(ret);
6463 goto out;
6464 }
6465
Josef Bacik70c8a912012-10-11 16:54:30 -04006466 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
6467 ins.offset, ins.offset, 0);
6468 if (IS_ERR(em))
6469 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006470
6471 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6472 ins.offset, ins.offset, 0);
6473 if (ret) {
6474 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6475 em = ERR_PTR(ret);
6476 }
6477out:
6478 btrfs_end_transaction(trans, root);
6479 return em;
6480}
6481
Chris Mason46bfbb52010-05-26 11:04:10 -04006482/*
6483 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6484 * block must be cow'd
6485 */
6486static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
6487 struct inode *inode, u64 offset, u64 len)
6488{
6489 struct btrfs_path *path;
6490 int ret;
6491 struct extent_buffer *leaf;
6492 struct btrfs_root *root = BTRFS_I(inode)->root;
6493 struct btrfs_file_extent_item *fi;
6494 struct btrfs_key key;
6495 u64 disk_bytenr;
6496 u64 backref_offset;
6497 u64 extent_end;
6498 u64 num_bytes;
6499 int slot;
6500 int found_type;
6501
6502 path = btrfs_alloc_path();
6503 if (!path)
6504 return -ENOMEM;
6505
Li Zefan33345d012011-04-20 10:31:50 +08006506 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006507 offset, 0);
6508 if (ret < 0)
6509 goto out;
6510
6511 slot = path->slots[0];
6512 if (ret == 1) {
6513 if (slot == 0) {
6514 /* can't find the item, must cow */
6515 ret = 0;
6516 goto out;
6517 }
6518 slot--;
6519 }
6520 ret = 0;
6521 leaf = path->nodes[0];
6522 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006523 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006524 key.type != BTRFS_EXTENT_DATA_KEY) {
6525 /* not our file or wrong item type, must cow */
6526 goto out;
6527 }
6528
6529 if (key.offset > offset) {
6530 /* Wrong offset, must cow */
6531 goto out;
6532 }
6533
6534 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6535 found_type = btrfs_file_extent_type(leaf, fi);
6536 if (found_type != BTRFS_FILE_EXTENT_REG &&
6537 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6538 /* not a regular extent, must cow */
6539 goto out;
6540 }
6541 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6542 backref_offset = btrfs_file_extent_offset(leaf, fi);
6543
6544 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6545 if (extent_end < offset + len) {
6546 /* extent doesn't include our full range, must cow */
6547 goto out;
6548 }
6549
6550 if (btrfs_extent_readonly(root, disk_bytenr))
6551 goto out;
6552
6553 /*
6554 * look for other files referencing this extent, if we
6555 * find any we must cow
6556 */
Li Zefan33345d012011-04-20 10:31:50 +08006557 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006558 key.offset - backref_offset, disk_bytenr))
6559 goto out;
6560
6561 /*
6562 * adjust disk_bytenr and num_bytes to cover just the bytes
6563 * in this extent we are about to write. If there
6564 * are any csums in that range we have to cow in order
6565 * to keep the csums correct
6566 */
6567 disk_bytenr += backref_offset;
6568 disk_bytenr += offset - key.offset;
6569 num_bytes = min(offset + len, extent_end) - offset;
6570 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6571 goto out;
6572 /*
6573 * all of the above have passed, it is safe to overwrite this extent
6574 * without cow
6575 */
6576 ret = 1;
6577out:
6578 btrfs_free_path(path);
6579 return ret;
6580}
6581
Josef Bacikeb838e72012-07-31 16:28:48 -04006582static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6583 struct extent_state **cached_state, int writing)
6584{
6585 struct btrfs_ordered_extent *ordered;
6586 int ret = 0;
6587
6588 while (1) {
6589 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6590 0, cached_state);
6591 /*
6592 * We're concerned with the entire range that we're going to be
6593 * doing DIO to, so we need to make sure theres no ordered
6594 * extents in this range.
6595 */
6596 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6597 lockend - lockstart + 1);
6598
6599 /*
6600 * We need to make sure there are no buffered pages in this
6601 * range either, we could have raced between the invalidate in
6602 * generic_file_direct_write and locking the extent. The
6603 * invalidate needs to happen so that reads after a write do not
6604 * get stale data.
6605 */
6606 if (!ordered && (!writing ||
6607 !test_range_bit(&BTRFS_I(inode)->io_tree,
6608 lockstart, lockend, EXTENT_UPTODATE, 0,
6609 *cached_state)))
6610 break;
6611
6612 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6613 cached_state, GFP_NOFS);
6614
6615 if (ordered) {
6616 btrfs_start_ordered_extent(inode, ordered, 1);
6617 btrfs_put_ordered_extent(ordered);
6618 } else {
6619 /* Screw you mmap */
6620 ret = filemap_write_and_wait_range(inode->i_mapping,
6621 lockstart,
6622 lockend);
6623 if (ret)
6624 break;
6625
6626 /*
6627 * If we found a page that couldn't be invalidated just
6628 * fall back to buffered.
6629 */
6630 ret = invalidate_inode_pages2_range(inode->i_mapping,
6631 lockstart >> PAGE_CACHE_SHIFT,
6632 lockend >> PAGE_CACHE_SHIFT);
6633 if (ret)
6634 break;
6635 }
6636
6637 cond_resched();
6638 }
6639
6640 return ret;
6641}
6642
Josef Bacik69ffb542012-09-11 15:40:07 -04006643static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6644 u64 len, u64 orig_start,
6645 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05006646 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006647{
6648 struct extent_map_tree *em_tree;
6649 struct extent_map *em;
6650 struct btrfs_root *root = BTRFS_I(inode)->root;
6651 int ret;
6652
6653 em_tree = &BTRFS_I(inode)->extent_tree;
6654 em = alloc_extent_map();
6655 if (!em)
6656 return ERR_PTR(-ENOMEM);
6657
6658 em->start = start;
6659 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006660 em->mod_start = start;
6661 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006662 em->len = len;
6663 em->block_len = block_len;
6664 em->block_start = block_start;
6665 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006666 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006667 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006668 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6669 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006670 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006671
6672 do {
6673 btrfs_drop_extent_cache(inode, em->start,
6674 em->start + em->len - 1, 0);
6675 write_lock(&em_tree->lock);
6676 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04006677 if (!ret)
6678 list_move(&em->list,
6679 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006680 write_unlock(&em_tree->lock);
6681 } while (ret == -EEXIST);
6682
6683 if (ret) {
6684 free_extent_map(em);
6685 return ERR_PTR(ret);
6686 }
6687
6688 return em;
6689}
6690
6691
Josef Bacik4b46fce2010-05-23 11:00:55 -04006692static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6693 struct buffer_head *bh_result, int create)
6694{
6695 struct extent_map *em;
6696 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006697 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006698 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006699 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006700 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006701 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006702 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006703 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006704
Miao Xie172a5042013-02-21 02:48:22 -07006705 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006706 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006707 else
Josef Bacikc3298612012-08-03 16:49:19 -04006708 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006709
Josef Bacikc3298612012-08-03 16:49:19 -04006710 lockstart = start;
6711 lockend = start + len - 1;
6712
Josef Bacikeb838e72012-07-31 16:28:48 -04006713 /*
6714 * If this errors out it's because we couldn't invalidate pagecache for
6715 * this range and we need to fallback to buffered.
6716 */
6717 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6718 return -ENOTBLK;
6719
Josef Bacik4b46fce2010-05-23 11:00:55 -04006720 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006721 if (IS_ERR(em)) {
6722 ret = PTR_ERR(em);
6723 goto unlock_err;
6724 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006725
6726 /*
6727 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6728 * io. INLINE is special, and we could probably kludge it in here, but
6729 * it's still buffered so for safety lets just fall back to the generic
6730 * buffered path.
6731 *
6732 * For COMPRESSED we _have_ to read the entire extent in so we can
6733 * decompress it, so there will be buffering required no matter what we
6734 * do, so go ahead and fallback to buffered.
6735 *
6736 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6737 * to buffered IO. Don't blame me, this is the price we pay for using
6738 * the generic code.
6739 */
6740 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6741 em->block_start == EXTENT_MAP_INLINE) {
6742 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006743 ret = -ENOTBLK;
6744 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006745 }
6746
6747 /* Just a good old fashioned hole, return */
6748 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6749 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6750 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006751 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006752 }
6753
6754 /*
6755 * We don't allocate a new extent in the following cases
6756 *
6757 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6758 * existing extent.
6759 * 2) The extent is marked as PREALLOC. We're good to go here and can
6760 * just use the extent.
6761 *
6762 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006763 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006764 len = min(len, em->len - (start - em->start));
6765 lockstart = start + len;
6766 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006767 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006768
6769 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6770 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6771 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006772 int type;
6773 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006774 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006775
6776 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6777 type = BTRFS_ORDERED_PREALLOC;
6778 else
6779 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006780 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006781 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006782
6783 /*
6784 * we're not going to log anything, but we do need
6785 * to make sure the current transaction stays open
6786 * while we look for nocow cross refs
6787 */
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006788 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006789 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006790 goto must_cow;
6791
6792 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006793 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006794 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006795
6796 if (type == BTRFS_ORDERED_PREALLOC) {
6797 free_extent_map(em);
6798 em = create_pinned_em(inode, start, len,
6799 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006800 block_start, len,
6801 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006802 if (IS_ERR(em)) {
6803 btrfs_end_transaction(trans, root);
6804 goto unlock_err;
6805 }
6806 }
6807
Chris Mason46bfbb52010-05-26 11:04:10 -04006808 ret = btrfs_add_ordered_extent_dio(inode, start,
6809 block_start, len, len, type);
6810 btrfs_end_transaction(trans, root);
6811 if (ret) {
6812 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006813 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006814 }
6815 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006816 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006817 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006818 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006819must_cow:
6820 /*
6821 * this will cow the extent, reset the len in case we changed
6822 * it above
6823 */
6824 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006825 free_extent_map(em);
6826 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006827 if (IS_ERR(em)) {
6828 ret = PTR_ERR(em);
6829 goto unlock_err;
6830 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006831 len = min(len, em->len - (start - em->start));
6832unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006833 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6834 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006835 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006836 bh_result->b_bdev = em->bdev;
6837 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006838 if (create) {
6839 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6840 set_buffer_new(bh_result);
6841
6842 /*
6843 * Need to update the i_size under the extent lock so buffered
6844 * readers will get the updated i_size when we unlock.
6845 */
6846 if (start + len > i_size_read(inode))
6847 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006848
Miao Xie172a5042013-02-21 02:48:22 -07006849 spin_lock(&BTRFS_I(inode)->lock);
6850 BTRFS_I(inode)->outstanding_extents++;
6851 spin_unlock(&BTRFS_I(inode)->lock);
6852
Miao Xie09348562013-02-07 10:12:07 +00006853 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6854 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6855 &cached_state, GFP_NOFS);
6856 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006857 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006858
Josef Bacikeb838e72012-07-31 16:28:48 -04006859 /*
6860 * In the case of write we need to clear and unlock the entire range,
6861 * in the case of read we need to unlock only the end area that we
6862 * aren't using if there is any left over space.
6863 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006864 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006865 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6866 lockend, unlock_bits, 1, 0,
6867 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006868 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006869 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006870 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006871
Josef Bacik4b46fce2010-05-23 11:00:55 -04006872 free_extent_map(em);
6873
6874 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006875
6876unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006877 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6878 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6879 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006880}
6881
6882struct btrfs_dio_private {
6883 struct inode *inode;
6884 u64 logical_offset;
6885 u64 disk_bytenr;
6886 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006887 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006888
6889 /* number of bios pending for this dio */
6890 atomic_t pending_bios;
6891
6892 /* IO errors */
6893 int errors;
6894
6895 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006896};
6897
6898static void btrfs_endio_direct_read(struct bio *bio, int err)
6899{
Miao Xiee65e1532010-11-22 03:04:43 +00006900 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006901 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6902 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006903 struct inode *inode = dip->inode;
6904 struct btrfs_root *root = BTRFS_I(inode)->root;
6905 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006906
6907 start = dip->logical_offset;
6908 do {
6909 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6910 struct page *page = bvec->bv_page;
6911 char *kaddr;
6912 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006913 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006914 unsigned long flags;
6915
Josef Bacikc3298612012-08-03 16:49:19 -04006916 if (get_state_private(&BTRFS_I(inode)->io_tree,
6917 start, &private))
6918 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006919 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006920 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006921 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6922 csum, bvec->bv_len);
6923 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006924 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006925 local_irq_restore(flags);
6926
6927 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006928 if (csum != private) {
6929failed:
Li Zefan33345d012011-04-20 10:31:50 +08006930 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006931 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006932 (unsigned long long)btrfs_ino(inode),
6933 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006934 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006935 err = -EIO;
6936 }
6937 }
6938
6939 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006940 bvec++;
6941 } while (bvec <= bvec_end);
6942
6943 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006944 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006945 bio->bi_private = dip->private;
6946
Josef Bacik4b46fce2010-05-23 11:00:55 -04006947 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006948
6949 /* If we had a csum failure make sure to clear the uptodate flag */
6950 if (err)
6951 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006952 dio_end_io(bio, err);
6953}
6954
6955static void btrfs_endio_direct_write(struct bio *bio, int err)
6956{
6957 struct btrfs_dio_private *dip = bio->bi_private;
6958 struct inode *inode = dip->inode;
6959 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006960 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006961 u64 ordered_offset = dip->logical_offset;
6962 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006963 int ret;
6964
6965 if (err)
6966 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006967again:
6968 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6969 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006970 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006971 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006972 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006973
Josef Bacik5fd02042012-05-02 14:00:54 -04006974 ordered->work.func = finish_ordered_fn;
6975 ordered->work.flags = 0;
6976 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6977 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006978out_test:
6979 /*
6980 * our bio might span multiple ordered extents. If we haven't
6981 * completed the accounting for the whole dio, go back and try again
6982 */
6983 if (ordered_offset < dip->logical_offset + dip->bytes) {
6984 ordered_bytes = dip->logical_offset + dip->bytes -
6985 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006986 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006987 goto again;
6988 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006989out_done:
6990 bio->bi_private = dip->private;
6991
Josef Bacik4b46fce2010-05-23 11:00:55 -04006992 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006993
6994 /* If we had an error make sure to clear the uptodate flag */
6995 if (err)
6996 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006997 dio_end_io(bio, err);
6998}
6999
Chris Masoneaf25d92010-05-25 09:48:28 -04007000static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7001 struct bio *bio, int mirror_num,
7002 unsigned long bio_flags, u64 offset)
7003{
7004 int ret;
7005 struct btrfs_root *root = BTRFS_I(inode)->root;
7006 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007007 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007008 return 0;
7009}
7010
Miao Xiee65e1532010-11-22 03:04:43 +00007011static void btrfs_end_dio_bio(struct bio *bio, int err)
7012{
7013 struct btrfs_dio_private *dip = bio->bi_private;
7014
7015 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007016 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007017 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007018 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007019 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007020 dip->errors = 1;
7021
7022 /*
7023 * before atomic variable goto zero, we must make sure
7024 * dip->errors is perceived to be set.
7025 */
7026 smp_mb__before_atomic_dec();
7027 }
7028
7029 /* if there are more bios still pending for this dio, just exit */
7030 if (!atomic_dec_and_test(&dip->pending_bios))
7031 goto out;
7032
7033 if (dip->errors)
7034 bio_io_error(dip->orig_bio);
7035 else {
7036 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
7037 bio_endio(dip->orig_bio, 0);
7038 }
7039out:
7040 bio_put(bio);
7041}
7042
7043static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7044 u64 first_sector, gfp_t gfp_flags)
7045{
7046 int nr_vecs = bio_get_nr_vecs(bdev);
7047 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7048}
7049
7050static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7051 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007052 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007053{
7054 int write = rw & REQ_WRITE;
7055 struct btrfs_root *root = BTRFS_I(inode)->root;
7056 int ret;
7057
Josef Bacikb812ce22012-11-16 13:56:32 -05007058 if (async_submit)
7059 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7060
Miao Xiee65e1532010-11-22 03:04:43 +00007061 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007062
7063 if (!write) {
7064 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7065 if (ret)
7066 goto err;
7067 }
Miao Xiee65e1532010-11-22 03:04:43 +00007068
Josef Bacik1ae39932011-04-06 14:41:34 -04007069 if (skip_sum)
7070 goto map;
7071
7072 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007073 ret = btrfs_wq_submit_bio(root->fs_info,
7074 inode, rw, bio, 0, 0,
7075 file_offset,
7076 __btrfs_submit_bio_start_direct_io,
7077 __btrfs_submit_bio_done);
7078 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007079 } else if (write) {
7080 /*
7081 * If we aren't doing async submit, calculate the csum of the
7082 * bio now.
7083 */
7084 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7085 if (ret)
7086 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007087 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007088 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007089 if (ret)
7090 goto err;
7091 }
Miao Xiee65e1532010-11-22 03:04:43 +00007092
Josef Bacik1ae39932011-04-06 14:41:34 -04007093map:
7094 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007095err:
7096 bio_put(bio);
7097 return ret;
7098}
7099
7100static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7101 int skip_sum)
7102{
7103 struct inode *inode = dip->inode;
7104 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007105 struct bio *bio;
7106 struct bio *orig_bio = dip->orig_bio;
7107 struct bio_vec *bvec = orig_bio->bi_io_vec;
7108 u64 start_sector = orig_bio->bi_sector;
7109 u64 file_offset = dip->logical_offset;
7110 u64 submit_len = 0;
7111 u64 map_length;
7112 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007113 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007114 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007115
Miao Xiee65e1532010-11-22 03:04:43 +00007116 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007117 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007118 &map_length, NULL, 0);
7119 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007120 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007121 return -EIO;
7122 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007123 if (map_length >= orig_bio->bi_size) {
7124 bio = orig_bio;
7125 goto submit;
7126 }
7127
David Woodhouse53b381b2013-01-29 18:40:14 -05007128 /* async crcs make it difficult to collect full stripe writes. */
7129 if (btrfs_get_alloc_profile(root, 1) &
7130 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7131 async_submit = 0;
7132 else
7133 async_submit = 1;
7134
Josef Bacik02f57c72011-04-06 14:25:44 -04007135 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7136 if (!bio)
7137 return -ENOMEM;
7138 bio->bi_private = dip;
7139 bio->bi_end_io = btrfs_end_dio_bio;
7140 atomic_inc(&dip->pending_bios);
7141
Miao Xiee65e1532010-11-22 03:04:43 +00007142 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7143 if (unlikely(map_length < submit_len + bvec->bv_len ||
7144 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7145 bvec->bv_offset) < bvec->bv_len)) {
7146 /*
7147 * inc the count before we submit the bio so
7148 * we know the end IO handler won't happen before
7149 * we inc the count. Otherwise, the dip might get freed
7150 * before we're done setting it up
7151 */
7152 atomic_inc(&dip->pending_bios);
7153 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7154 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007155 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007156 if (ret) {
7157 bio_put(bio);
7158 atomic_dec(&dip->pending_bios);
7159 goto out_err;
7160 }
7161
Miao Xiee65e1532010-11-22 03:04:43 +00007162 start_sector += submit_len >> 9;
7163 file_offset += submit_len;
7164
7165 submit_len = 0;
7166 nr_pages = 0;
7167
7168 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7169 start_sector, GFP_NOFS);
7170 if (!bio)
7171 goto out_err;
7172 bio->bi_private = dip;
7173 bio->bi_end_io = btrfs_end_dio_bio;
7174
7175 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007176 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007177 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007178 &map_length, NULL, 0);
7179 if (ret) {
7180 bio_put(bio);
7181 goto out_err;
7182 }
7183 } else {
7184 submit_len += bvec->bv_len;
7185 nr_pages ++;
7186 bvec++;
7187 }
7188 }
7189
Josef Bacik02f57c72011-04-06 14:25:44 -04007190submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007191 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007192 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007193 if (!ret)
7194 return 0;
7195
7196 bio_put(bio);
7197out_err:
7198 dip->errors = 1;
7199 /*
7200 * before atomic variable goto zero, we must
7201 * make sure dip->errors is perceived to be set.
7202 */
7203 smp_mb__before_atomic_dec();
7204 if (atomic_dec_and_test(&dip->pending_bios))
7205 bio_io_error(dip->orig_bio);
7206
7207 /* bio_end_io() will handle error, so we needn't return it */
7208 return 0;
7209}
7210
Josef Bacik4b46fce2010-05-23 11:00:55 -04007211static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
7212 loff_t file_offset)
7213{
7214 struct btrfs_root *root = BTRFS_I(inode)->root;
7215 struct btrfs_dio_private *dip;
7216 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007217 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007218 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007219 int ret = 0;
7220
7221 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7222
7223 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7224 if (!dip) {
7225 ret = -ENOMEM;
7226 goto free_ordered;
7227 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007228
7229 dip->private = bio->bi_private;
7230 dip->inode = inode;
7231 dip->logical_offset = file_offset;
7232
Josef Bacik4b46fce2010-05-23 11:00:55 -04007233 dip->bytes = 0;
7234 do {
7235 dip->bytes += bvec->bv_len;
7236 bvec++;
7237 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
7238
Chris Mason46bfbb52010-05-26 11:04:10 -04007239 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007240 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007241 dip->errors = 0;
7242 dip->orig_bio = bio;
7243 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007244
7245 if (write)
7246 bio->bi_end_io = btrfs_endio_direct_write;
7247 else
7248 bio->bi_end_io = btrfs_endio_direct_read;
7249
Miao Xiee65e1532010-11-22 03:04:43 +00007250 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7251 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007252 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007253free_ordered:
7254 /*
7255 * If this is a write, we need to clean up the reserved space and kill
7256 * the ordered extent.
7257 */
7258 if (write) {
7259 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007260 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007261 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7262 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7263 btrfs_free_reserved_extent(root, ordered->start,
7264 ordered->disk_len);
7265 btrfs_put_ordered_extent(ordered);
7266 btrfs_put_ordered_extent(ordered);
7267 }
7268 bio_endio(bio, ret);
7269}
7270
Chris Mason5a5f79b2010-05-26 21:33:37 -04007271static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7272 const struct iovec *iov, loff_t offset,
7273 unsigned long nr_segs)
7274{
7275 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007276 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007277 size_t size;
7278 unsigned long addr;
7279 unsigned blocksize_mask = root->sectorsize - 1;
7280 ssize_t retval = -EINVAL;
7281 loff_t end = offset;
7282
7283 if (offset & blocksize_mask)
7284 goto out;
7285
7286 /* Check the memory alignment. Blocks cannot straddle pages */
7287 for (seg = 0; seg < nr_segs; seg++) {
7288 addr = (unsigned long)iov[seg].iov_base;
7289 size = iov[seg].iov_len;
7290 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007291 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007292 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007293
7294 /* If this is a write we don't need to check anymore */
7295 if (rw & WRITE)
7296 continue;
7297
7298 /*
7299 * Check to make sure we don't have duplicate iov_base's in this
7300 * iovec, if so return EINVAL, otherwise we'll get csum errors
7301 * when reading back.
7302 */
7303 for (i = seg + 1; i < nr_segs; i++) {
7304 if (iov[seg].iov_base == iov[i].iov_base)
7305 goto out;
7306 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007307 }
7308 retval = 0;
7309out:
7310 return retval;
7311}
Josef Bacikeb838e72012-07-31 16:28:48 -04007312
Chris Mason16432982008-04-10 10:23:21 -04007313static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7314 const struct iovec *iov, loff_t offset,
7315 unsigned long nr_segs)
7316{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007317 struct file *file = iocb->ki_filp;
7318 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007319 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007320 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007321 bool wakeup = true;
7322 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007323 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007324
Chris Mason5a5f79b2010-05-26 21:33:37 -04007325 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007326 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007327 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007328
Miao Xie38851cc2013-02-08 07:04:11 +00007329 atomic_inc(&inode->i_dio_count);
7330 smp_mb__after_atomic_inc();
7331
Miao Xie09348562013-02-07 10:12:07 +00007332 if (rw & WRITE) {
7333 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007334 /*
7335 * If the write DIO is beyond the EOF, we need update
7336 * the isize, but it is protected by i_mutex. So we can
7337 * not unlock the i_mutex at this case.
7338 */
7339 if (offset + count <= inode->i_size) {
7340 mutex_unlock(&inode->i_mutex);
7341 relock = true;
7342 }
Miao Xie09348562013-02-07 10:12:07 +00007343 ret = btrfs_delalloc_reserve_space(inode, count);
7344 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007345 goto out;
7346 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7347 &BTRFS_I(inode)->runtime_flags))) {
7348 inode_dio_done(inode);
7349 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7350 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007351 }
7352
7353 ret = __blockdev_direct_IO(rw, iocb, inode,
7354 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7355 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007356 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007357 if (rw & WRITE) {
7358 if (ret < 0 && ret != -EIOCBQUEUED)
7359 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007360 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007361 btrfs_delalloc_release_space(inode,
7362 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007363 else
7364 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007365 }
Miao Xie38851cc2013-02-08 07:04:11 +00007366out:
Miao Xie2e60a512013-02-08 07:01:08 +00007367 if (wakeup)
7368 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007369 if (relock)
7370 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007371
7372 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007373}
7374
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007375#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7376
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007377static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7378 __u64 start, __u64 len)
7379{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007380 int ret;
7381
7382 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7383 if (ret)
7384 return ret;
7385
Chris Masonec29ed52011-02-23 16:23:20 -05007386 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007387}
7388
Chris Mason9ebefb182007-06-15 13:50:00 -04007389int btrfs_readpage(struct file *file, struct page *page)
7390{
Chris Masond1310b22008-01-24 16:13:08 -05007391 struct extent_io_tree *tree;
7392 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007393 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007394}
Chris Mason1832a6d2007-12-21 16:27:21 -05007395
Chris Mason39279cc2007-06-12 06:35:45 -04007396static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7397{
Chris Masond1310b22008-01-24 16:13:08 -05007398 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007399
7400
7401 if (current->flags & PF_MEMALLOC) {
7402 redirty_page_for_writepage(wbc, page);
7403 unlock_page(page);
7404 return 0;
7405 }
Chris Masond1310b22008-01-24 16:13:08 -05007406 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007407 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7408}
Chris Mason39279cc2007-06-12 06:35:45 -04007409
Chris Masonf4219502008-07-22 11:18:09 -04007410int btrfs_writepages(struct address_space *mapping,
7411 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007412{
Chris Masond1310b22008-01-24 16:13:08 -05007413 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007414
Chris Masond1310b22008-01-24 16:13:08 -05007415 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007416 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7417}
7418
Chris Mason3ab2fb52007-11-08 10:59:22 -05007419static int
7420btrfs_readpages(struct file *file, struct address_space *mapping,
7421 struct list_head *pages, unsigned nr_pages)
7422{
Chris Masond1310b22008-01-24 16:13:08 -05007423 struct extent_io_tree *tree;
7424 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007425 return extent_readpages(tree, mapping, pages, nr_pages,
7426 btrfs_get_extent);
7427}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007428static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007429{
Chris Masond1310b22008-01-24 16:13:08 -05007430 struct extent_io_tree *tree;
7431 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007432 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007433
Chris Masond1310b22008-01-24 16:13:08 -05007434 tree = &BTRFS_I(page->mapping->host)->io_tree;
7435 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007436 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007437 if (ret == 1) {
7438 ClearPagePrivate(page);
7439 set_page_private(page, 0);
7440 page_cache_release(page);
7441 }
7442 return ret;
7443}
Chris Mason39279cc2007-06-12 06:35:45 -04007444
Chris Masone6dcd2d2008-07-17 12:53:50 -04007445static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7446{
Chris Mason98509cf2008-09-11 15:51:43 -04007447 if (PageWriteback(page) || PageDirty(page))
7448 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007449 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007450}
7451
Chris Masona52d9a82007-08-27 16:49:44 -04007452static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7453{
Josef Bacik5fd02042012-05-02 14:00:54 -04007454 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007455 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007456 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007457 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007458 u64 page_start = page_offset(page);
7459 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007460
Chris Mason8b62b722009-09-02 16:53:46 -04007461 /*
7462 * we have the page locked, so new writeback can't start,
7463 * and the dirty bit won't be cleared while we are here.
7464 *
7465 * Wait for IO on this page so that we can safely clear
7466 * the PagePrivate2 bit and do ordered accounting
7467 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007468 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007469
Josef Bacik5fd02042012-05-02 14:00:54 -04007470 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007471 if (offset) {
7472 btrfs_releasepage(page, GFP_NOFS);
7473 return;
7474 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007475 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007476 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007477 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007478 /*
7479 * IO on this page will never be started, so we need
7480 * to account for any ordered extents now
7481 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007482 clear_extent_bit(tree, page_start, page_end,
7483 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007484 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7485 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007486 /*
7487 * whoever cleared the private bit is responsible
7488 * for the finish_ordered_io
7489 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007490 if (TestClearPagePrivate2(page) &&
7491 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7492 PAGE_CACHE_SIZE, 1)) {
7493 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007494 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007495 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007496 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007497 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007498 }
7499 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007500 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007501 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7502 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007503 __btrfs_releasepage(page, GFP_NOFS);
7504
Chris Mason4a096752008-07-21 10:29:44 -04007505 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007506 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007507 ClearPagePrivate(page);
7508 set_page_private(page, 0);
7509 page_cache_release(page);
7510 }
Chris Mason39279cc2007-06-12 06:35:45 -04007511}
7512
Chris Mason9ebefb182007-06-15 13:50:00 -04007513/*
7514 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7515 * called from a page fault handler when a page is first dirtied. Hence we must
7516 * be careful to check for EOF conditions here. We set the page up correctly
7517 * for a written page which means we get ENOSPC checking when writing into
7518 * holes and correct delalloc and unwritten extent mapping on filesystems that
7519 * support these features.
7520 *
7521 * We are not allowed to take the i_mutex here so we have to play games to
7522 * protect against truncate races as the page could now be beyond EOF. Because
7523 * vmtruncate() writes the inode size before removing pages, once we have the
7524 * page lock we can determine safely if the page is beyond EOF. If it is not
7525 * beyond EOF, then the page is guaranteed safe against truncation until we
7526 * unlock the page.
7527 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007528int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007529{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007530 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05007531 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05007532 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007533 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7534 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007535 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007536 char *kaddr;
7537 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007538 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007539 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007540 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007541 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007542 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007543
Jan Karab2b5ef52012-06-12 16:20:45 +02007544 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007545 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007546 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007547 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007548 reserved = 1;
7549 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007550 if (ret) {
7551 if (ret == -ENOMEM)
7552 ret = VM_FAULT_OOM;
7553 else /* -ENOSPC, -EIO, etc */
7554 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007555 if (reserved)
7556 goto out;
7557 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007558 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007559
Nick Piggin56a76f82009-03-31 15:23:23 -07007560 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007561again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007562 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007563 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007564 page_start = page_offset(page);
7565 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007566
Chris Mason9ebefb182007-06-15 13:50:00 -04007567 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007568 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007569 /* page got truncated out from underneath us */
7570 goto out_unlock;
7571 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007572 wait_on_page_writeback(page);
7573
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007574 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007575 set_page_extent_mapped(page);
7576
Chris Masoneb84ae02008-07-17 13:53:27 -04007577 /*
7578 * we can't set the delalloc bits if there are pending ordered
7579 * extents. Drop our locks and wait for them to finish
7580 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007581 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7582 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007583 unlock_extent_cached(io_tree, page_start, page_end,
7584 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007585 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007586 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007587 btrfs_put_ordered_extent(ordered);
7588 goto again;
7589 }
7590
Josef Bacikfbf19082009-10-01 17:10:23 -04007591 /*
7592 * XXX - page_mkwrite gets called every time the page is dirtied, even
7593 * if it was already dirty, so for space accounting reasons we need to
7594 * clear any delalloc bits for the range we are fixing to save. There
7595 * is probably a better way to do this, but for now keep consistent with
7596 * prepare_pages in the normal write path.
7597 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007598 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007599 EXTENT_DIRTY | EXTENT_DELALLOC |
7600 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007601 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007602
Josef Bacik2ac55d42010-02-03 19:33:23 +00007603 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7604 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007605 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007606 unlock_extent_cached(io_tree, page_start, page_end,
7607 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007608 ret = VM_FAULT_SIGBUS;
7609 goto out_unlock;
7610 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007611 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007612
7613 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007614 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007615 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007616 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007617 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007618
Chris Masone6dcd2d2008-07-17 12:53:50 -04007619 if (zero_start != PAGE_CACHE_SIZE) {
7620 kaddr = kmap(page);
7621 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7622 flush_dcache_page(page);
7623 kunmap(page);
7624 }
Chris Mason247e7432008-07-17 12:53:51 -04007625 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007626 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007627 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007628
Chris Mason257c62e2009-10-13 13:21:08 -04007629 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7630 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007631 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007632
Josef Bacik2ac55d42010-02-03 19:33:23 +00007633 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007634
7635out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007636 if (!ret) {
7637 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007638 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007639 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007640 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007641out:
Josef Bacikec39e182012-01-12 19:10:12 -05007642 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007643out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007644 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007645 return ret;
7646}
7647
Josef Bacika41ad392011-01-31 15:30:16 -05007648static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007649{
7650 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007651 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007652 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007653 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007654 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007655 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007656 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007657
Josef Bacik2aaa6652012-08-29 14:27:18 -04007658 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007659 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007660 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007661
Chris Mason4a096752008-07-21 10:29:44 -04007662 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007663 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007664
Josef Bacikfcb80c22011-05-03 10:40:22 -04007665 /*
7666 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7667 * 3 things going on here
7668 *
7669 * 1) We need to reserve space for our orphan item and the space to
7670 * delete our orphan item. Lord knows we don't want to have a dangling
7671 * orphan item because we didn't reserve space to remove it.
7672 *
7673 * 2) We need to reserve space to update our inode.
7674 *
7675 * 3) We need to have something to cache all the space that is going to
7676 * be free'd up by the truncate operation, but also have some slack
7677 * space reserved in case it uses space during the truncate (thank you
7678 * very much snapshotting).
7679 *
7680 * And we need these to all be seperate. The fact is we can use alot of
7681 * space doing the truncate, and we have no earthly idea how much space
7682 * we will use, so we need the truncate reservation to be seperate so it
7683 * doesn't end up using space reserved for updating the inode or
7684 * removing the orphan item. We also need to be able to stop the
7685 * transaction and start a new one, which means we need to be able to
7686 * update the inode several times, and we have no idea of knowing how
7687 * many times that will be, so we can't just reserve 1 item for the
7688 * entirety of the opration, so that has to be done seperately as well.
7689 * Then there is the orphan item, which does indeed need to be held on
7690 * to for the whole operation, and we need nobody to touch this reserved
7691 * space except the orphan code.
7692 *
7693 * So that leaves us with
7694 *
7695 * 1) root->orphan_block_rsv - for the orphan deletion.
7696 * 2) rsv - for the truncate reservation, which we will steal from the
7697 * transaction reservation.
7698 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7699 * updating the inode.
7700 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007701 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007702 if (!rsv)
7703 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007704 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007705 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007706
Josef Bacik907cbce2011-08-08 13:46:15 -04007707 /*
Josef Bacik07127182011-08-19 10:29:59 -04007708 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007709 * 1 for updating the inode.
7710 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007711 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007712 if (IS_ERR(trans)) {
7713 err = PTR_ERR(trans);
7714 goto out;
7715 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007716
Josef Bacik907cbce2011-08-08 13:46:15 -04007717 /* Migrate the slack space for the truncate to our reserve */
7718 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7719 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007720 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007721
Chris Mason5a3f23d2009-03-31 13:27:11 -04007722 /*
7723 * setattr is responsible for setting the ordered_data_close flag,
7724 * but that is only tested during the last file release. That
7725 * could happen well after the next commit, leaving a great big
7726 * window where new writes may get lost if someone chooses to write
7727 * to this file after truncating to zero
7728 *
7729 * The inode doesn't have any dirty data here, and so if we commit
7730 * this is a noop. If someone immediately starts writing to the inode
7731 * it is very likely we'll catch some of their writes in this
7732 * transaction, and the commit will find this file on the ordered
7733 * data list with good things to send down.
7734 *
7735 * This is a best effort solution, there is still a window where
7736 * using truncate to replace the contents of the file will
7737 * end up with a zero length file after a crash.
7738 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007739 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7740 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007741 btrfs_add_ordered_operation(trans, root, inode);
7742
Josef Bacik5dc562c2012-08-17 13:14:17 -04007743 /*
7744 * So if we truncate and then write and fsync we normally would just
7745 * write the extents that changed, which is a problem if we need to
7746 * first truncate that entire inode. So set this flag so we write out
7747 * all of the extents in the inode to the sync log so we're completely
7748 * safe.
7749 */
7750 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007751 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007752
Yan, Zheng80825102009-11-12 09:35:36 +00007753 while (1) {
7754 ret = btrfs_truncate_inode_items(trans, root, inode,
7755 inode->i_size,
7756 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007757 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007758 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007759 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007760 }
Chris Mason39279cc2007-06-12 06:35:45 -04007761
Josef Bacikfcb80c22011-05-03 10:40:22 -04007762 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007763 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007764 if (ret) {
7765 err = ret;
7766 break;
7767 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007768
Yan, Zheng80825102009-11-12 09:35:36 +00007769 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007770 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007771
7772 trans = btrfs_start_transaction(root, 2);
7773 if (IS_ERR(trans)) {
7774 ret = err = PTR_ERR(trans);
7775 trans = NULL;
7776 break;
7777 }
7778
7779 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7780 rsv, min_size);
7781 BUG_ON(ret); /* shouldn't happen */
7782 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007783 }
7784
7785 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007786 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007787 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007788 if (ret)
7789 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007790 }
7791
Chris Mason917c16b2011-11-08 14:49:59 -05007792 if (trans) {
7793 trans->block_rsv = &root->fs_info->trans_block_rsv;
7794 ret = btrfs_update_inode(trans, root, inode);
7795 if (ret && !err)
7796 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007797
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007798 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007799 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007800 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007801
7802out:
7803 btrfs_free_block_rsv(root, rsv);
7804
Josef Bacik3893e332011-01-31 16:03:11 -05007805 if (ret && !err)
7806 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007807
Josef Bacik3893e332011-01-31 16:03:11 -05007808 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007809}
7810
Sven Wegener3b963622008-06-09 21:57:42 -04007811/*
Chris Masond352ac62008-09-29 15:18:18 -04007812 * create a new subvolume directory/inode (helper for the ioctl).
7813 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007814int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007815 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007816{
Chris Mason39279cc2007-06-12 06:35:45 -04007817 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007818 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007819 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007820
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007821 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7822 new_dirid, new_dirid,
7823 S_IFDIR | (~current_umask() & S_IRWXUGO),
7824 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007825 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007826 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007827 inode->i_op = &btrfs_dir_inode_operations;
7828 inode->i_fop = &btrfs_dir_file_operations;
7829
Miklos Szeredibfe86842011-10-28 14:13:29 +02007830 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007831 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007832
Yan, Zheng76dda932009-09-21 16:00:26 -04007833 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007834
Yan, Zheng76dda932009-09-21 16:00:26 -04007835 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007836 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007837}
7838
Chris Mason39279cc2007-06-12 06:35:45 -04007839struct inode *btrfs_alloc_inode(struct super_block *sb)
7840{
7841 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007842 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007843
7844 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7845 if (!ei)
7846 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007847
7848 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007849 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007850 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007851 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007852 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007853 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007854 ei->disk_i_size = 0;
7855 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007856 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007857 ei->index_cnt = (u64)-1;
7858 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007859 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007860
Josef Bacik9e0baf62011-07-15 15:16:44 +00007861 spin_lock_init(&ei->lock);
7862 ei->outstanding_extents = 0;
7863 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007864
Josef Bacik72ac3c02012-05-23 14:13:11 -04007865 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007866 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007867
Miao Xie16cdcec2011-04-22 18:12:22 +08007868 ei->delayed_node = NULL;
7869
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007870 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007871 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007872 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7873 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007874 ei->io_tree.track_uptodate = 1;
7875 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007876 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007877 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007878 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007879 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007880 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007881 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007882 RB_CLEAR_NODE(&ei->rb_node);
7883
7884 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007885}
7886
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007887static void btrfs_i_callback(struct rcu_head *head)
7888{
7889 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007890 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7891}
7892
Chris Mason39279cc2007-06-12 06:35:45 -04007893void btrfs_destroy_inode(struct inode *inode)
7894{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007895 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007896 struct btrfs_root *root = BTRFS_I(inode)->root;
7897
Al Virob3d9b7a2012-06-09 13:51:19 -04007898 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007899 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007900 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7901 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007902 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7903 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007904
Chris Mason5a3f23d2009-03-31 13:27:11 -04007905 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007906 * This can happen where we create an inode, but somebody else also
7907 * created the same inode and we need to destroy the one we already
7908 * created.
7909 */
7910 if (!root)
7911 goto free;
7912
7913 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007914 * Make sure we're properly removed from the ordered operation
7915 * lists.
7916 */
7917 smp_mb();
7918 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7919 spin_lock(&root->fs_info->ordered_extent_lock);
7920 list_del_init(&BTRFS_I(inode)->ordered_operations);
7921 spin_unlock(&root->fs_info->ordered_extent_lock);
7922 }
7923
Josef Bacik8a35d952012-05-23 14:26:42 -04007924 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7925 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007926 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7927 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007928 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007929 }
Josef Bacik7b128762008-07-24 12:17:14 -04007930
Chris Masond3977122009-01-05 21:25:51 -05007931 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007932 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7933 if (!ordered)
7934 break;
7935 else {
Chris Masond3977122009-01-05 21:25:51 -05007936 printk(KERN_ERR "btrfs found ordered "
7937 "extent %llu %llu on inode cleanup\n",
7938 (unsigned long long)ordered->file_offset,
7939 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007940 btrfs_remove_ordered_extent(inode, ordered);
7941 btrfs_put_ordered_extent(ordered);
7942 btrfs_put_ordered_extent(ordered);
7943 }
7944 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007945 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007946 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007947free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007948 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007949 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007950}
7951
Al Viro45321ac2010-06-07 13:43:19 -04007952int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007953{
7954 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007955
Liu Bofa6ac872013-02-20 14:10:23 +00007956 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007957 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007958 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007959 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007960 else
Al Viro45321ac2010-06-07 13:43:19 -04007961 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007962}
7963
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007964static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007965{
7966 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7967
7968 inode_init_once(&ei->vfs_inode);
7969}
7970
7971void btrfs_destroy_cachep(void)
7972{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007973 /*
7974 * Make sure all delayed rcu free inodes are flushed before we
7975 * destroy cache.
7976 */
7977 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007978 if (btrfs_inode_cachep)
7979 kmem_cache_destroy(btrfs_inode_cachep);
7980 if (btrfs_trans_handle_cachep)
7981 kmem_cache_destroy(btrfs_trans_handle_cachep);
7982 if (btrfs_transaction_cachep)
7983 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007984 if (btrfs_path_cachep)
7985 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007986 if (btrfs_free_space_cachep)
7987 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007988 if (btrfs_delalloc_work_cachep)
7989 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007990}
7991
7992int btrfs_init_cachep(void)
7993{
David Sterba837e1972012-09-07 03:00:48 -06007994 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007995 sizeof(struct btrfs_inode), 0,
7996 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007997 if (!btrfs_inode_cachep)
7998 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007999
David Sterba837e1972012-09-07 03:00:48 -06008000 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008001 sizeof(struct btrfs_trans_handle), 0,
8002 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008003 if (!btrfs_trans_handle_cachep)
8004 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008005
David Sterba837e1972012-09-07 03:00:48 -06008006 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008007 sizeof(struct btrfs_transaction), 0,
8008 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008009 if (!btrfs_transaction_cachep)
8010 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008011
David Sterba837e1972012-09-07 03:00:48 -06008012 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008013 sizeof(struct btrfs_path), 0,
8014 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008015 if (!btrfs_path_cachep)
8016 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008017
David Sterba837e1972012-09-07 03:00:48 -06008018 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008019 sizeof(struct btrfs_free_space), 0,
8020 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8021 if (!btrfs_free_space_cachep)
8022 goto fail;
8023
Miao Xie8ccf6f192012-10-25 09:28:04 +00008024 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8025 sizeof(struct btrfs_delalloc_work), 0,
8026 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8027 NULL);
8028 if (!btrfs_delalloc_work_cachep)
8029 goto fail;
8030
Chris Mason39279cc2007-06-12 06:35:45 -04008031 return 0;
8032fail:
8033 btrfs_destroy_cachep();
8034 return -ENOMEM;
8035}
8036
8037static int btrfs_getattr(struct vfsmount *mnt,
8038 struct dentry *dentry, struct kstat *stat)
8039{
Miao Xiedf0af1a52013-01-29 10:11:59 +00008040 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008041 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008042 u32 blocksize = inode->i_sb->s_blocksize;
8043
Chris Mason39279cc2007-06-12 06:35:45 -04008044 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008045 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008046 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a52013-01-29 10:11:59 +00008047
8048 spin_lock(&BTRFS_I(inode)->lock);
8049 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8050 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008051 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a52013-01-29 10:11:59 +00008052 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008053 return 0;
8054}
8055
Chris Masond3977122009-01-05 21:25:51 -05008056static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8057 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008058{
8059 struct btrfs_trans_handle *trans;
8060 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008061 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008062 struct inode *new_inode = new_dentry->d_inode;
8063 struct inode *old_inode = old_dentry->d_inode;
8064 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008065 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008066 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008067 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008068 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008069
Li Zefan33345d012011-04-20 10:31:50 +08008070 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008071 return -EPERM;
8072
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008073 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008074 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008075 return -EXDEV;
8076
Li Zefan33345d012011-04-20 10:31:50 +08008077 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8078 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008079 return -ENOTEMPTY;
8080
Chris Mason39279cc2007-06-12 06:35:45 -04008081 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008082 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008083 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008084
8085
8086 /* check for collisions, even if the name isn't there */
8087 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8088 new_dentry->d_name.name,
8089 new_dentry->d_name.len);
8090
8091 if (ret) {
8092 if (ret == -EEXIST) {
8093 /* we shouldn't get
8094 * eexist without a new_inode */
8095 if (!new_inode) {
8096 WARN_ON(1);
8097 return ret;
8098 }
8099 } else {
8100 /* maybe -EOVERFLOW */
8101 return ret;
8102 }
8103 }
8104 ret = 0;
8105
Chris Mason5a3f23d2009-03-31 13:27:11 -04008106 /*
8107 * we're using rename to replace one file with another.
8108 * and the replacement file is large. Start IO on it now so
8109 * we don't add too much work to the end of the transaction
8110 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008111 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008112 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8113 filemap_flush(old_inode->i_mapping);
8114
Yan, Zheng76dda932009-09-21 16:00:26 -04008115 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008116 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008117 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008118 /*
8119 * We want to reserve the absolute worst case amount of items. So if
8120 * both inodes are subvols and we need to unlink them then that would
8121 * require 4 item modifications, but if they are both normal inodes it
8122 * would require 5 item modifications, so we'll assume their normal
8123 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8124 * should cover the worst case number of items we'll modify.
8125 */
8126 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008127 if (IS_ERR(trans)) {
8128 ret = PTR_ERR(trans);
8129 goto out_notrans;
8130 }
Chris Mason5f39d392007-10-15 16:14:19 -04008131
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008132 if (dest != root)
8133 btrfs_record_root_in_trans(trans, dest);
8134
Yan, Zhenga5719522009-09-24 09:17:31 -04008135 ret = btrfs_set_inode_index(new_dir, &index);
8136 if (ret)
8137 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008138
Li Zefan33345d012011-04-20 10:31:50 +08008139 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008140 /* force full log commit if subvolume involved. */
8141 root->fs_info->last_trans_log_full_commit = trans->transid;
8142 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008143 ret = btrfs_insert_inode_ref(trans, dest,
8144 new_dentry->d_name.name,
8145 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008146 old_ino,
8147 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008148 if (ret)
8149 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008150 /*
8151 * this is an ugly little race, but the rename is required
8152 * to make sure that if we crash, the inode is either at the
8153 * old name or the new one. pinning the log transaction lets
8154 * us make sure we don't allow a log commit to come in after
8155 * we unlink the name but before we add the new name back in.
8156 */
8157 btrfs_pin_log_trans(root);
8158 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008159 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008160 * make sure the inode gets flushed if it is replacing
8161 * something.
8162 */
Li Zefan33345d012011-04-20 10:31:50 +08008163 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008164 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008165
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008166 inode_inc_iversion(old_dir);
8167 inode_inc_iversion(new_dir);
8168 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008169 old_dir->i_ctime = old_dir->i_mtime = ctime;
8170 new_dir->i_ctime = new_dir->i_mtime = ctime;
8171 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008172
Chris Mason12fcfd22009-03-24 10:24:20 -04008173 if (old_dentry->d_parent != new_dentry->d_parent)
8174 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8175
Li Zefan33345d012011-04-20 10:31:50 +08008176 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008177 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8178 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8179 old_dentry->d_name.name,
8180 old_dentry->d_name.len);
8181 } else {
Al Viro92986792011-03-04 17:14:37 +00008182 ret = __btrfs_unlink_inode(trans, root, old_dir,
8183 old_dentry->d_inode,
8184 old_dentry->d_name.name,
8185 old_dentry->d_name.len);
8186 if (!ret)
8187 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008188 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008189 if (ret) {
8190 btrfs_abort_transaction(trans, root, ret);
8191 goto out_fail;
8192 }
Chris Mason39279cc2007-06-12 06:35:45 -04008193
8194 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008195 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008196 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008197 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008198 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8199 root_objectid = BTRFS_I(new_inode)->location.objectid;
8200 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8201 root_objectid,
8202 new_dentry->d_name.name,
8203 new_dentry->d_name.len);
8204 BUG_ON(new_inode->i_nlink == 0);
8205 } else {
8206 ret = btrfs_unlink_inode(trans, dest, new_dir,
8207 new_dentry->d_inode,
8208 new_dentry->d_name.name,
8209 new_dentry->d_name.len);
8210 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008211 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008212 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008213 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008214 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008215 if (ret) {
8216 btrfs_abort_transaction(trans, root, ret);
8217 goto out_fail;
8218 }
Chris Mason39279cc2007-06-12 06:35:45 -04008219 }
Josef Bacikaec74772008-07-24 12:12:38 -04008220
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008221 ret = btrfs_add_link(trans, new_dir, old_inode,
8222 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008223 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008224 if (ret) {
8225 btrfs_abort_transaction(trans, root, ret);
8226 goto out_fail;
8227 }
Chris Mason39279cc2007-06-12 06:35:45 -04008228
Li Zefan33345d012011-04-20 10:31:50 +08008229 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008230 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008231 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008232 btrfs_end_log_trans(root);
8233 }
Chris Mason39279cc2007-06-12 06:35:45 -04008234out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008235 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008236out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008237 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008238 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008239
Chris Mason39279cc2007-06-12 06:35:45 -04008240 return ret;
8241}
8242
Miao Xie8ccf6f192012-10-25 09:28:04 +00008243static void btrfs_run_delalloc_work(struct btrfs_work *work)
8244{
8245 struct btrfs_delalloc_work *delalloc_work;
8246
8247 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8248 work);
8249 if (delalloc_work->wait)
8250 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8251 else
8252 filemap_flush(delalloc_work->inode->i_mapping);
8253
8254 if (delalloc_work->delay_iput)
8255 btrfs_add_delayed_iput(delalloc_work->inode);
8256 else
8257 iput(delalloc_work->inode);
8258 complete(&delalloc_work->completion);
8259}
8260
8261struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8262 int wait, int delay_iput)
8263{
8264 struct btrfs_delalloc_work *work;
8265
8266 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8267 if (!work)
8268 return NULL;
8269
8270 init_completion(&work->completion);
8271 INIT_LIST_HEAD(&work->list);
8272 work->inode = inode;
8273 work->wait = wait;
8274 work->delay_iput = delay_iput;
8275 work->work.func = btrfs_run_delalloc_work;
8276
8277 return work;
8278}
8279
8280void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8281{
8282 wait_for_completion(&work->completion);
8283 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8284}
8285
Chris Masond352ac62008-09-29 15:18:18 -04008286/*
8287 * some fairly slow code that needs optimization. This walks the list
8288 * of all the inodes with pending delalloc and forces them to disk.
8289 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008290int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008291{
Chris Masonea8c2812008-08-04 23:17:27 -04008292 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008293 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008294 struct btrfs_delalloc_work *work, *next;
8295 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008296 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008297 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008298
Yan Zhengc146afa2008-11-12 14:34:12 -05008299 if (root->fs_info->sb->s_flags & MS_RDONLY)
8300 return -EROFS;
8301
Miao Xie8ccf6f192012-10-25 09:28:04 +00008302 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008303 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008304
Chris Mason75eff682008-12-15 15:54:40 -05008305 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008306 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8307 while (!list_empty(&splice)) {
8308 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008309 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008310
8311 list_del_init(&binode->delalloc_inodes);
8312
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008313 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a52013-01-29 10:11:59 +00008314 if (!inode) {
8315 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8316 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008317 continue;
Miao Xiedf0af1a52013-01-29 10:11:59 +00008318 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008319
8320 list_add_tail(&binode->delalloc_inodes,
8321 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008322 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008323
8324 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8325 if (unlikely(!work)) {
8326 ret = -ENOMEM;
8327 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008328 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008329 list_add_tail(&work->list, &works);
8330 btrfs_queue_worker(&root->fs_info->flush_workers,
8331 &work->work);
8332
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008333 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008334 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008335 }
Chris Mason75eff682008-12-15 15:54:40 -05008336 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008337
Miao Xie1eafa6c2013-01-22 10:49:00 +00008338 list_for_each_entry_safe(work, next, &works, list) {
8339 list_del_init(&work->list);
8340 btrfs_wait_and_free_delalloc_work(work);
8341 }
8342
Chris Mason8c8bee12008-09-29 11:19:10 -04008343 /* the filemap_flush will queue IO into the worker threads, but
8344 * we have to make sure the IO is actually started and that
8345 * ordered extents get created before we return
8346 */
8347 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008348 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008349 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008350 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008351 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8352 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008353 }
8354 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008355 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008356out:
8357 list_for_each_entry_safe(work, next, &works, list) {
8358 list_del_init(&work->list);
8359 btrfs_wait_and_free_delalloc_work(work);
8360 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008361
8362 if (!list_empty_careful(&splice)) {
8363 spin_lock(&root->fs_info->delalloc_lock);
8364 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8365 spin_unlock(&root->fs_info->delalloc_lock);
8366 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008367 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008368}
8369
Chris Mason39279cc2007-06-12 06:35:45 -04008370static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8371 const char *symname)
8372{
8373 struct btrfs_trans_handle *trans;
8374 struct btrfs_root *root = BTRFS_I(dir)->root;
8375 struct btrfs_path *path;
8376 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008377 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008378 int err;
8379 int drop_inode = 0;
8380 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008381 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008382 int name_len;
8383 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008384 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008385 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008386 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008387
8388 name_len = strlen(symname) + 1;
8389 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8390 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008391
Josef Bacik9ed74f22009-09-11 16:12:44 -04008392 /*
8393 * 2 items for inode item and ref
8394 * 2 items for dir items
8395 * 1 item for xattr if selinux is on
8396 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008397 trans = btrfs_start_transaction(root, 5);
8398 if (IS_ERR(trans))
8399 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008400
Li Zefan581bb052011-04-20 10:06:11 +08008401 err = btrfs_find_free_ino(root, &objectid);
8402 if (err)
8403 goto out_unlock;
8404
Josef Bacikaec74772008-07-24 12:12:38 -04008405 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008406 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008407 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008408 if (IS_ERR(inode)) {
8409 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008410 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008411 }
Chris Mason39279cc2007-06-12 06:35:45 -04008412
Eric Paris2a7dba32011-02-01 11:05:39 -05008413 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008414 if (err) {
8415 drop_inode = 1;
8416 goto out_unlock;
8417 }
8418
Casey Schauflerad19db72011-12-15 10:09:07 -05008419 /*
8420 * If the active LSM wants to access the inode during
8421 * d_instantiate it needs these. Smack checks to see
8422 * if the filesystem supports xattrs by looking at the
8423 * ops vector.
8424 */
8425 inode->i_fop = &btrfs_file_operations;
8426 inode->i_op = &btrfs_file_inode_operations;
8427
Josef Bacika1b075d2010-11-19 20:36:11 +00008428 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008429 if (err)
8430 drop_inode = 1;
8431 else {
8432 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008433 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008434 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008435 }
Chris Mason39279cc2007-06-12 06:35:45 -04008436 if (drop_inode)
8437 goto out_unlock;
8438
8439 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008440 if (!path) {
8441 err = -ENOMEM;
8442 drop_inode = 1;
8443 goto out_unlock;
8444 }
Li Zefan33345d012011-04-20 10:31:50 +08008445 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008446 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008447 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8448 datasize = btrfs_file_extent_calc_inline_size(name_len);
8449 err = btrfs_insert_empty_item(trans, root, path, &key,
8450 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008451 if (err) {
8452 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008453 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008454 goto out_unlock;
8455 }
Chris Mason5f39d392007-10-15 16:14:19 -04008456 leaf = path->nodes[0];
8457 ei = btrfs_item_ptr(leaf, path->slots[0],
8458 struct btrfs_file_extent_item);
8459 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8460 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008461 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008462 btrfs_set_file_extent_encryption(leaf, ei, 0);
8463 btrfs_set_file_extent_compression(leaf, ei, 0);
8464 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8465 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8466
Chris Mason39279cc2007-06-12 06:35:45 -04008467 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008468 write_extent_buffer(leaf, symname, ptr, name_len);
8469 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008470 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008471
Chris Mason39279cc2007-06-12 06:35:45 -04008472 inode->i_op = &btrfs_symlink_inode_operations;
8473 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008474 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008475 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008476 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008477 err = btrfs_update_inode(trans, root, inode);
8478 if (err)
8479 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008480
8481out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008482 if (!err)
8483 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008484 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008485 if (drop_inode) {
8486 inode_dec_link_count(inode);
8487 iput(inode);
8488 }
Liu Bob53d3f52012-11-14 14:34:34 +00008489 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008490 return err;
8491}
Chris Mason16432982008-04-10 10:23:21 -04008492
Josef Bacik0af3d002010-06-21 14:48:16 -04008493static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8494 u64 start, u64 num_bytes, u64 min_size,
8495 loff_t actual_len, u64 *alloc_hint,
8496 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008497{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008498 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8499 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008500 struct btrfs_root *root = BTRFS_I(inode)->root;
8501 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008502 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008503 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04008504 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008505 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008506
Josef Bacik0af3d002010-06-21 14:48:16 -04008507 if (trans)
8508 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008509 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008510 if (own_trans) {
8511 trans = btrfs_start_transaction(root, 3);
8512 if (IS_ERR(trans)) {
8513 ret = PTR_ERR(trans);
8514 break;
8515 }
Yan Zhengd899e052008-10-30 14:25:28 -04008516 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008517
Zach Brown24542bf2012-11-16 00:04:43 +00008518 ret = btrfs_reserve_extent(trans, root,
8519 min(num_bytes, 256ULL * 1024 * 1024),
8520 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008521 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008522 if (own_trans)
8523 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008524 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008525 }
8526
Yan Zhengd899e052008-10-30 14:25:28 -04008527 ret = insert_reserved_file_extent(trans, inode,
8528 cur_offset, ins.objectid,
8529 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008530 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008531 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008532 if (ret) {
8533 btrfs_abort_transaction(trans, root, ret);
8534 if (own_trans)
8535 btrfs_end_transaction(trans, root);
8536 break;
8537 }
Chris Masona1ed8352009-09-11 12:27:37 -04008538 btrfs_drop_extent_cache(inode, cur_offset,
8539 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008540
Josef Bacik5dc562c2012-08-17 13:14:17 -04008541 em = alloc_extent_map();
8542 if (!em) {
8543 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8544 &BTRFS_I(inode)->runtime_flags);
8545 goto next;
8546 }
8547
8548 em->start = cur_offset;
8549 em->orig_start = cur_offset;
8550 em->len = ins.offset;
8551 em->block_start = ins.objectid;
8552 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008553 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008554 em->bdev = root->fs_info->fs_devices->latest_bdev;
8555 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8556 em->generation = trans->transid;
8557
8558 while (1) {
8559 write_lock(&em_tree->lock);
8560 ret = add_extent_mapping(em_tree, em);
8561 if (!ret)
8562 list_move(&em->list,
8563 &em_tree->modified_extents);
8564 write_unlock(&em_tree->lock);
8565 if (ret != -EEXIST)
8566 break;
8567 btrfs_drop_extent_cache(inode, cur_offset,
8568 cur_offset + ins.offset - 1,
8569 0);
8570 }
8571 free_extent_map(em);
8572next:
Yan Zhengd899e052008-10-30 14:25:28 -04008573 num_bytes -= ins.offset;
8574 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008575 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008576
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008577 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008578 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008579 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008580 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008581 (actual_len > inode->i_size) &&
8582 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008583 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008584 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008585 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008586 i_size = cur_offset;
8587 i_size_write(inode, i_size);
8588 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008589 }
8590
Yan Zhengd899e052008-10-30 14:25:28 -04008591 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008592
8593 if (ret) {
8594 btrfs_abort_transaction(trans, root, ret);
8595 if (own_trans)
8596 btrfs_end_transaction(trans, root);
8597 break;
8598 }
Yan Zhengd899e052008-10-30 14:25:28 -04008599
Josef Bacik0af3d002010-06-21 14:48:16 -04008600 if (own_trans)
8601 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008602 }
Yan Zhengd899e052008-10-30 14:25:28 -04008603 return ret;
8604}
8605
Josef Bacik0af3d002010-06-21 14:48:16 -04008606int btrfs_prealloc_file_range(struct inode *inode, int mode,
8607 u64 start, u64 num_bytes, u64 min_size,
8608 loff_t actual_len, u64 *alloc_hint)
8609{
8610 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8611 min_size, actual_len, alloc_hint,
8612 NULL);
8613}
8614
8615int btrfs_prealloc_file_range_trans(struct inode *inode,
8616 struct btrfs_trans_handle *trans, int mode,
8617 u64 start, u64 num_bytes, u64 min_size,
8618 loff_t actual_len, u64 *alloc_hint)
8619{
8620 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8621 min_size, actual_len, alloc_hint, trans);
8622}
8623
Chris Masone6dcd2d2008-07-17 12:53:50 -04008624static int btrfs_set_page_dirty(struct page *page)
8625{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008626 return __set_page_dirty_nobuffers(page);
8627}
8628
Al Viro10556cb2011-06-20 19:28:19 -04008629static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008630{
Li Zefanb83cc962010-12-20 16:04:08 +08008631 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008632 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008633
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008634 if (mask & MAY_WRITE &&
8635 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8636 if (btrfs_root_readonly(root))
8637 return -EROFS;
8638 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8639 return -EACCES;
8640 }
Al Viro2830ba72011-06-20 19:16:29 -04008641 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008642}
Chris Mason39279cc2007-06-12 06:35:45 -04008643
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008644static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008645 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008646 .lookup = btrfs_lookup,
8647 .create = btrfs_create,
8648 .unlink = btrfs_unlink,
8649 .link = btrfs_link,
8650 .mkdir = btrfs_mkdir,
8651 .rmdir = btrfs_rmdir,
8652 .rename = btrfs_rename,
8653 .symlink = btrfs_symlink,
8654 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008655 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008656 .setxattr = btrfs_setxattr,
8657 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008658 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008659 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008660 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008661 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008662};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008663static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008664 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008665 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008666 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008667};
Yan, Zheng76dda932009-09-21 16:00:26 -04008668
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008669static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008670 .llseek = generic_file_llseek,
8671 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008672 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008673 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008674#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008675 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008676#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008677 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008678 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008679};
8680
Chris Masond1310b22008-01-24 16:13:08 -05008681static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008682 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008683 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008684 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008685 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008686 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008687 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008688 .set_bit_hook = btrfs_set_bit_hook,
8689 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008690 .merge_extent_hook = btrfs_merge_extent_hook,
8691 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008692};
8693
Chris Mason35054392009-01-21 13:11:13 -05008694/*
8695 * btrfs doesn't support the bmap operation because swapfiles
8696 * use bmap to make a mapping of extents in the file. They assume
8697 * these extents won't change over the life of the file and they
8698 * use the bmap result to do IO directly to the drive.
8699 *
8700 * the btrfs bmap call would return logical addresses that aren't
8701 * suitable for IO and they also will change frequently as COW
8702 * operations happen. So, swapfile + btrfs == corruption.
8703 *
8704 * For now we're avoiding this by dropping bmap.
8705 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008706static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008707 .readpage = btrfs_readpage,
8708 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008709 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008710 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008711 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008712 .invalidatepage = btrfs_invalidatepage,
8713 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008714 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008715 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008716};
8717
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008718static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008719 .readpage = btrfs_readpage,
8720 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008721 .invalidatepage = btrfs_invalidatepage,
8722 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008723};
8724
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008725static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008726 .getattr = btrfs_getattr,
8727 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008728 .setxattr = btrfs_setxattr,
8729 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008730 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008731 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008732 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008733 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008734 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008735 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008736};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008737static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008738 .getattr = btrfs_getattr,
8739 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008740 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008741 .setxattr = btrfs_setxattr,
8742 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008743 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008744 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008745 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008746 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008747};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008748static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008749 .readlink = generic_readlink,
8750 .follow_link = page_follow_link_light,
8751 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008752 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008753 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008754 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008755 .setxattr = btrfs_setxattr,
8756 .getxattr = btrfs_getxattr,
8757 .listxattr = btrfs_listxattr,
8758 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008759 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008760 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008761};
Yan, Zheng76dda932009-09-21 16:00:26 -04008762
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008763const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008764 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008765 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008766};