blob: ca1b767d51f760672f8de72c48efbf3df8e58a75 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Chris Mason39279cc2007-06-12 06:35:45 -040059
60struct btrfs_iget_args {
61 u64 ino;
62 struct btrfs_root *root;
63};
64
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070065static const struct inode_operations btrfs_dir_inode_operations;
66static const struct inode_operations btrfs_symlink_inode_operations;
67static const struct inode_operations btrfs_dir_ro_inode_operations;
68static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
71static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050073static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040074
75static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000076static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_trans_handle_cachep;
78struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050080struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081
82#define S_SHIFT 12
83static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
84 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
85 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
86 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
87 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
88 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
89 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
90 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
91};
92
Eric Sandeen3972f262013-01-12 02:57:22 +000093static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050094static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040095static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050096static noinline int cow_file_range(struct inode *inode,
97 struct page *locked_page,
98 u64 start, u64 end, int *page_started,
99 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400100static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
101 u64 len, u64 orig_start,
102 u64 block_start, u64 block_len,
103 u64 orig_block_len, int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400104
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000105static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500106 struct inode *inode, struct inode *dir,
107 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500108{
109 int err;
110
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500112 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500113 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500114 return err;
115}
116
Chris Masond352ac62008-09-29 15:18:18 -0400117/*
Chris Masonc8b97812008-10-29 14:49:59 -0400118 * this does all the hard work for inserting an inline extent into
119 * the btree. The caller should have done a btrfs_drop_extents so that
120 * no overlapping inline items exist in the btree
121 */
Chris Masond3977122009-01-05 21:25:51 -0500122static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400123 struct btrfs_root *root, struct inode *inode,
124 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000125 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400126 struct page **compressed_pages)
127{
128 struct btrfs_key key;
129 struct btrfs_path *path;
130 struct extent_buffer *leaf;
131 struct page *page = NULL;
132 char *kaddr;
133 unsigned long ptr;
134 struct btrfs_file_extent_item *ei;
135 int err = 0;
136 int ret;
137 size_t cur_size = size;
138 size_t datasize;
139 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400140
Li Zefanfe3f5662011-03-28 08:30:38 +0000141 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400142 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400143
Chris Masond3977122009-01-05 21:25:51 -0500144 path = btrfs_alloc_path();
145 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400146 return -ENOMEM;
147
Chris Masonb9473432009-03-13 11:00:37 -0400148 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400149
Li Zefan33345d012011-04-20 10:31:50 +0800150 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400151 key.offset = start;
152 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400153 datasize = btrfs_file_extent_calc_inline_size(cur_size);
154
155 inode_add_bytes(inode, size);
156 ret = btrfs_insert_empty_item(trans, root, path, &key,
157 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400158 if (ret) {
159 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400160 goto fail;
161 }
162 leaf = path->nodes[0];
163 ei = btrfs_item_ptr(leaf, path->slots[0],
164 struct btrfs_file_extent_item);
165 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
166 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
167 btrfs_set_file_extent_encryption(leaf, ei, 0);
168 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
169 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
170 ptr = btrfs_file_extent_inline_start(ei);
171
Li Zefan261507a02010-12-17 14:21:50 +0800172 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400173 struct page *cpage;
174 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500175 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400176 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500177 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400178 PAGE_CACHE_SIZE);
179
Cong Wang7ac687d2011-11-25 23:14:28 +0800180 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400181 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800182 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400183
184 i++;
185 ptr += cur_size;
186 compressed_size -= cur_size;
187 }
188 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800189 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400190 } else {
191 page = find_get_page(inode->i_mapping,
192 start >> PAGE_CACHE_SHIFT);
193 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800194 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 offset = start & (PAGE_CACHE_SIZE - 1);
196 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800197 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400198 page_cache_release(page);
199 }
200 btrfs_mark_buffer_dirty(leaf);
201 btrfs_free_path(path);
202
Yan, Zhengc2167752009-11-12 09:34:21 +0000203 /*
204 * we're an inline extent, so nobody can
205 * extend the file past i_size without locking
206 * a page we already have locked.
207 *
208 * We must do any isize and inode updates
209 * before we unlock the pages. Otherwise we
210 * could end up racing with unlink.
211 */
Chris Masonc8b97812008-10-29 14:49:59 -0400212 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100213 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000214
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100215 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400216fail:
217 btrfs_free_path(path);
218 return err;
219}
220
221
222/*
223 * conditionally insert an inline extent into the file. This
224 * does the checks required to make sure the data is small enough
225 * to fit as an inline extent.
226 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400227static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400228 struct btrfs_root *root,
229 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000230 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400231 struct page **compressed_pages)
232{
233 u64 isize = i_size_read(inode);
234 u64 actual_end = min(end + 1, isize);
235 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000236 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 u64 data_len = inline_len;
238 int ret;
239
240 if (compressed_size)
241 data_len = compressed_size;
242
243 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400244 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400245 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
246 (!compressed_size &&
247 (actual_end & (root->sectorsize - 1)) == 0) ||
248 end + 1 < isize ||
249 data_len > root->fs_info->max_inline) {
250 return 1;
251 }
252
Josef Bacik26714852012-08-29 12:24:27 -0400253 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100254 if (ret)
255 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400256
257 if (isize > actual_end)
258 inline_len = min_t(u64, isize, actual_end);
259 ret = insert_inline_extent(trans, root, inode, start,
260 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000261 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400262 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100263 btrfs_abort_transaction(trans, root, ret);
264 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400265 } else if (ret == -ENOSPC) {
266 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400268
Josef Bacikbdc20e62013-02-28 13:23:38 -0500269 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400270 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400271 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400272 return 0;
273}
274
Chris Mason771ed682008-11-06 22:02:51 -0500275struct async_extent {
276 u64 start;
277 u64 ram_size;
278 u64 compressed_size;
279 struct page **pages;
280 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800281 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500282 struct list_head list;
283};
284
285struct async_cow {
286 struct inode *inode;
287 struct btrfs_root *root;
288 struct page *locked_page;
289 u64 start;
290 u64 end;
291 struct list_head extents;
292 struct btrfs_work work;
293};
294
295static noinline int add_async_extent(struct async_cow *cow,
296 u64 start, u64 ram_size,
297 u64 compressed_size,
298 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800299 unsigned long nr_pages,
300 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500301{
302 struct async_extent *async_extent;
303
304 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100305 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500306 async_extent->start = start;
307 async_extent->ram_size = ram_size;
308 async_extent->compressed_size = compressed_size;
309 async_extent->pages = pages;
310 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800311 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500312 list_add_tail(&async_extent->list, &cow->extents);
313 return 0;
314}
315
Chris Masonc8b97812008-10-29 14:49:59 -0400316/*
Chris Mason771ed682008-11-06 22:02:51 -0500317 * we create compressed extents in two phases. The first
318 * phase compresses a range of pages that have already been
319 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400320 *
Chris Mason771ed682008-11-06 22:02:51 -0500321 * This is done inside an ordered work queue, and the compression
322 * is spread across many cpus. The actual IO submission is step
323 * two, and the ordered work queue takes care of making sure that
324 * happens in the same order things were put onto the queue by
325 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400326 *
Chris Mason771ed682008-11-06 22:02:51 -0500327 * If this code finds it can't get good compression, it puts an
328 * entry onto the work queue to write the uncompressed bytes. This
329 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300330 * are written in the same order that the flusher thread sent them
331 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400332 */
Chris Mason771ed682008-11-06 22:02:51 -0500333static noinline int compress_file_range(struct inode *inode,
334 struct page *locked_page,
335 u64 start, u64 end,
336 struct async_cow *async_cow,
337 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400338{
339 struct btrfs_root *root = BTRFS_I(inode)->root;
340 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400341 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400342 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400343 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500344 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400345 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400346 struct page **pages = NULL;
347 unsigned long nr_pages;
348 unsigned long nr_pages_ret = 0;
349 unsigned long total_compressed = 0;
350 unsigned long total_in = 0;
351 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500352 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400353 int i;
354 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800355 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400356
Liu Bo4cb13e52012-03-29 09:57:45 -0400357 /* if this is a small write inside eof, kick off a defrag */
358 if ((end - start + 1) < 16 * 1024 &&
359 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400360 btrfs_add_inode_defrag(NULL, inode);
361
Chris Mason42dc7ba2008-12-15 11:44:56 -0500362 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400363again:
364 will_compress = 0;
365 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
366 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
367
Chris Masonf03d9301f2009-02-04 09:31:06 -0500368 /*
369 * we don't want to send crud past the end of i_size through
370 * compression, that's just a waste of CPU time. So, if the
371 * end of the file is before the start of our current
372 * requested range of bytes, we bail out to the uncompressed
373 * cleanup code that can deal with all of this.
374 *
375 * It isn't really the fastest way to fix things, but this is a
376 * very uncommon corner.
377 */
378 if (actual_end <= start)
379 goto cleanup_and_bail_uncompressed;
380
Chris Masonc8b97812008-10-29 14:49:59 -0400381 total_compressed = actual_end - start;
382
383 /* we want to make sure that amount of ram required to uncompress
384 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500385 * of a compressed extent to 128k. This is a crucial number
386 * because it also controls how easily we can spread reads across
387 * cpus for decompression.
388 *
389 * We also want to make sure the amount of IO required to do
390 * a random read is reasonably small, so we limit the size of
391 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400392 */
393 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000394 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500395 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400396 total_in = 0;
397 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400398
Chris Mason771ed682008-11-06 22:02:51 -0500399 /*
400 * we do compression for mount -o compress and when the
401 * inode has not been flagged as nocompress. This flag can
402 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400403 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200404 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500405 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000406 (BTRFS_I(inode)->force_compress) ||
407 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400408 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400409 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800410 if (!pages) {
411 /* just bail out to the uncompressed code */
412 goto cont;
413 }
Chris Mason179e29e2007-11-01 11:28:41 -0400414
Li Zefan261507a02010-12-17 14:21:50 +0800415 if (BTRFS_I(inode)->force_compress)
416 compress_type = BTRFS_I(inode)->force_compress;
417
418 ret = btrfs_compress_pages(compress_type,
419 inode->i_mapping, start,
420 total_compressed, pages,
421 nr_pages, &nr_pages_ret,
422 &total_in,
423 &total_compressed,
424 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400425
426 if (!ret) {
427 unsigned long offset = total_compressed &
428 (PAGE_CACHE_SIZE - 1);
429 struct page *page = pages[nr_pages_ret - 1];
430 char *kaddr;
431
432 /* zero the tail end of the last page, we might be
433 * sending it down to disk
434 */
435 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800436 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400437 memset(kaddr + offset, 0,
438 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800439 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400440 }
441 will_compress = 1;
442 }
443 }
Li Zefan560f7d72011-09-08 10:22:01 +0800444cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400445 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400446 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100447 if (IS_ERR(trans)) {
448 ret = PTR_ERR(trans);
449 trans = NULL;
450 goto cleanup_and_out;
451 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400452 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500453
Chris Masonc8b97812008-10-29 14:49:59 -0400454 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500455 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400456 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500457 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400458 */
459 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000460 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400461 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500462 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400463 ret = cow_file_range_inline(trans, root, inode,
464 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000465 total_compressed,
466 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400467 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100468 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500469 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100470 * inline extent creation worked or returned error,
471 * we don't need to create any more async work items.
472 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500473 */
Chris Masonc8b97812008-10-29 14:49:59 -0400474 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400475 &BTRFS_I(inode)->io_tree,
476 start, end, NULL,
477 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400478 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400479 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000480
481 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400482 goto free_pages_out;
483 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000484 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400485 }
486
487 if (will_compress) {
488 /*
489 * we aren't doing an inline extent round the compressed size
490 * up to a block size boundary so the allocator does sane
491 * things
492 */
Qu Wenruofda28322013-02-26 08:10:22 +0000493 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400494
495 /*
496 * one last check to make sure the compression is really a
497 * win, compare the page count read with the blocks on disk
498 */
Qu Wenruofda28322013-02-26 08:10:22 +0000499 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400500 if (total_compressed >= total_in) {
501 will_compress = 0;
502 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400503 num_bytes = total_in;
504 }
505 }
506 if (!will_compress && pages) {
507 /*
508 * the compression code ran but failed to make things smaller,
509 * free any pages it allocated and our page pointer array
510 */
511 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400512 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400513 page_cache_release(pages[i]);
514 }
515 kfree(pages);
516 pages = NULL;
517 total_compressed = 0;
518 nr_pages_ret = 0;
519
520 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500521 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
522 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500523 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500524 }
Chris Masonc8b97812008-10-29 14:49:59 -0400525 }
Chris Mason771ed682008-11-06 22:02:51 -0500526 if (will_compress) {
527 *num_added += 1;
528
529 /* the async work queues will take care of doing actual
530 * allocation on disk for these compressed pages,
531 * and will submit them to the elevator.
532 */
533 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800534 total_compressed, pages, nr_pages_ret,
535 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500536
Yan, Zheng24ae6362010-12-06 07:02:36 +0000537 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500538 start += num_bytes;
539 pages = NULL;
540 cond_resched();
541 goto again;
542 }
543 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500544cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500545 /*
546 * No compression, but we still need to write the pages in
547 * the file we've been given so far. redirty the locked
548 * page if it corresponds to our extent and set things up
549 * for the async work queue to run cow_file_range to do
550 * the normal delalloc dance
551 */
552 if (page_offset(locked_page) >= start &&
553 page_offset(locked_page) <= end) {
554 __set_page_dirty_nobuffers(locked_page);
555 /* unlocked later on in the async handlers */
556 }
Li Zefan261507a02010-12-17 14:21:50 +0800557 add_async_extent(async_cow, start, end - start + 1,
558 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500559 *num_added += 1;
560 }
561
562out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100563 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500564
565free_pages_out:
566 for (i = 0; i < nr_pages_ret; i++) {
567 WARN_ON(pages[i]->mapping);
568 page_cache_release(pages[i]);
569 }
Chris Masond3977122009-01-05 21:25:51 -0500570 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500571
572 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100573
574cleanup_and_out:
575 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
576 start, end, NULL,
577 EXTENT_CLEAR_UNLOCK_PAGE |
578 EXTENT_CLEAR_DIRTY |
579 EXTENT_CLEAR_DELALLOC |
580 EXTENT_SET_WRITEBACK |
581 EXTENT_END_WRITEBACK);
582 if (!trans || IS_ERR(trans))
583 btrfs_error(root->fs_info, ret, "Failed to join transaction");
584 else
585 btrfs_abort_transaction(trans, root, ret);
586 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500587}
588
589/*
590 * phase two of compressed writeback. This is the ordered portion
591 * of the code, which only gets called in the order the work was
592 * queued. We walk all the async extents created by compress_file_range
593 * and send them down to the disk.
594 */
595static noinline int submit_compressed_extents(struct inode *inode,
596 struct async_cow *async_cow)
597{
598 struct async_extent *async_extent;
599 u64 alloc_hint = 0;
600 struct btrfs_trans_handle *trans;
601 struct btrfs_key ins;
602 struct extent_map *em;
603 struct btrfs_root *root = BTRFS_I(inode)->root;
604 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
605 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500606 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500607
608 if (list_empty(&async_cow->extents))
609 return 0;
610
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500611again:
Chris Masond3977122009-01-05 21:25:51 -0500612 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500613 async_extent = list_entry(async_cow->extents.next,
614 struct async_extent, list);
615 list_del(&async_extent->list);
616
617 io_tree = &BTRFS_I(inode)->io_tree;
618
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500619retry:
Chris Mason771ed682008-11-06 22:02:51 -0500620 /* did the compression code fall back to uncompressed IO? */
621 if (!async_extent->pages) {
622 int page_started = 0;
623 unsigned long nr_written = 0;
624
625 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000626 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100627 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500628
629 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500630 ret = cow_file_range(inode, async_cow->locked_page,
631 async_extent->start,
632 async_extent->start +
633 async_extent->ram_size - 1,
634 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500635
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100636 /* JDM XXX */
637
Chris Mason771ed682008-11-06 22:02:51 -0500638 /*
639 * if page_started, cow_file_range inserted an
640 * inline extent and took care of all the unlocking
641 * and IO for us. Otherwise, we need to submit
642 * all those pages down to the drive.
643 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500644 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500645 extent_write_locked_range(io_tree,
646 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500647 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500648 async_extent->ram_size - 1,
649 btrfs_get_extent,
650 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500651 else if (ret)
652 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500653 kfree(async_extent);
654 cond_resched();
655 continue;
656 }
657
658 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100659 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500660
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400661 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100662 if (IS_ERR(trans)) {
663 ret = PTR_ERR(trans);
664 } else {
665 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
666 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500667 async_extent->compressed_size,
668 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500669 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600670 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100671 btrfs_abort_transaction(trans, root, ret);
672 btrfs_end_transaction(trans, root);
673 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000674
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500675 if (ret) {
676 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500677
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500678 for (i = 0; i < async_extent->nr_pages; i++) {
679 WARN_ON(async_extent->pages[i]->mapping);
680 page_cache_release(async_extent->pages[i]);
681 }
682 kfree(async_extent->pages);
683 async_extent->nr_pages = 0;
684 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500685
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100686 if (ret == -ENOSPC)
687 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500688 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500689 }
690
Yan, Zhengc2167752009-11-12 09:34:21 +0000691 /*
692 * here we're doing allocation and writeback of the
693 * compressed pages
694 */
695 btrfs_drop_extent_cache(inode, async_extent->start,
696 async_extent->start +
697 async_extent->ram_size - 1, 0);
698
David Sterba172ddd62011-04-21 00:48:27 +0200699 em = alloc_extent_map();
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500700 if (!em)
701 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500702 em->start = async_extent->start;
703 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500704 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400705 em->mod_start = em->start;
706 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500707
708 em->block_start = ins.objectid;
709 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500710 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500711 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800712 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500713 set_bit(EXTENT_FLAG_PINNED, &em->flags);
714 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400715 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500716
Chris Masond3977122009-01-05 21:25:51 -0500717 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400718 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500719 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400720 if (!ret)
721 list_move(&em->list,
722 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400723 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500724 if (ret != -EEXIST) {
725 free_extent_map(em);
726 break;
727 }
728 btrfs_drop_extent_cache(inode, async_extent->start,
729 async_extent->start +
730 async_extent->ram_size - 1, 0);
731 }
732
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500733 if (ret)
734 goto out_free_reserve;
735
Li Zefan261507a02010-12-17 14:21:50 +0800736 ret = btrfs_add_ordered_extent_compress(inode,
737 async_extent->start,
738 ins.objectid,
739 async_extent->ram_size,
740 ins.offset,
741 BTRFS_ORDERED_COMPRESSED,
742 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500743 if (ret)
744 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500745
Chris Mason771ed682008-11-06 22:02:51 -0500746 /*
747 * clear dirty, set writeback and unlock the pages.
748 */
749 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400750 &BTRFS_I(inode)->io_tree,
751 async_extent->start,
752 async_extent->start +
753 async_extent->ram_size - 1,
754 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
755 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400756 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400757 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500758
759 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500760 async_extent->start,
761 async_extent->ram_size,
762 ins.objectid,
763 ins.offset, async_extent->pages,
764 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500765 alloc_hint = ins.objectid + ins.offset;
766 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500767 if (ret)
768 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500769 cond_resched();
770 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100771 ret = 0;
772out:
773 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500774out_free_reserve:
775 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100776out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500777 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
778 async_extent->start,
779 async_extent->start +
780 async_extent->ram_size - 1,
781 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
782 EXTENT_CLEAR_UNLOCK |
783 EXTENT_CLEAR_DELALLOC |
784 EXTENT_CLEAR_DIRTY |
785 EXTENT_SET_WRITEBACK |
786 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100787 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500788 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500789}
790
Josef Bacik4b46fce2010-05-23 11:00:55 -0400791static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
792 u64 num_bytes)
793{
794 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
795 struct extent_map *em;
796 u64 alloc_hint = 0;
797
798 read_lock(&em_tree->lock);
799 em = search_extent_mapping(em_tree, start, num_bytes);
800 if (em) {
801 /*
802 * if block start isn't an actual block number then find the
803 * first block in this inode and use that as a hint. If that
804 * block is also bogus then just don't worry about it.
805 */
806 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
807 free_extent_map(em);
808 em = search_extent_mapping(em_tree, 0, 0);
809 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
810 alloc_hint = em->block_start;
811 if (em)
812 free_extent_map(em);
813 } else {
814 alloc_hint = em->block_start;
815 free_extent_map(em);
816 }
817 }
818 read_unlock(&em_tree->lock);
819
820 return alloc_hint;
821}
822
Chris Mason771ed682008-11-06 22:02:51 -0500823/*
824 * when extent_io.c finds a delayed allocation range in the file,
825 * the call backs end up in this code. The basic idea is to
826 * allocate extents on disk for the range, and create ordered data structs
827 * in ram to track those extents.
828 *
829 * locked_page is the page that writepage had locked already. We use
830 * it to make sure we don't do extra locks or unlocks.
831 *
832 * *page_started is set to one if we unlock locked_page and do everything
833 * required to start IO on it. It may be clean and already done with
834 * IO when we return.
835 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000836static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
837 struct inode *inode,
838 struct btrfs_root *root,
839 struct page *locked_page,
840 u64 start, u64 end, int *page_started,
841 unsigned long *nr_written,
842 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500843{
Chris Mason771ed682008-11-06 22:02:51 -0500844 u64 alloc_hint = 0;
845 u64 num_bytes;
846 unsigned long ram_size;
847 u64 disk_num_bytes;
848 u64 cur_alloc_size;
849 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500850 struct btrfs_key ins;
851 struct extent_map *em;
852 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
853 int ret = 0;
854
Liu Bo83eea1f2012-07-10 05:28:39 -0600855 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500856
Qu Wenruofda28322013-02-26 08:10:22 +0000857 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500858 num_bytes = max(blocksize, num_bytes);
859 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500860
Chris Mason4cb53002011-05-24 15:35:30 -0400861 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400862 if (num_bytes < 64 * 1024 &&
863 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400864 btrfs_add_inode_defrag(trans, inode);
865
Chris Mason771ed682008-11-06 22:02:51 -0500866 if (start == 0) {
867 /* lets try to make an inline extent */
868 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000869 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500870 if (ret == 0) {
871 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400872 &BTRFS_I(inode)->io_tree,
873 start, end, NULL,
874 EXTENT_CLEAR_UNLOCK_PAGE |
875 EXTENT_CLEAR_UNLOCK |
876 EXTENT_CLEAR_DELALLOC |
877 EXTENT_CLEAR_DIRTY |
878 EXTENT_SET_WRITEBACK |
879 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000880
Chris Mason771ed682008-11-06 22:02:51 -0500881 *nr_written = *nr_written +
882 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
883 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500884 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100885 } else if (ret < 0) {
886 btrfs_abort_transaction(trans, root, ret);
887 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500888 }
889 }
Chris Masonc8b97812008-10-29 14:49:59 -0400890
891 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200892 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400893
Josef Bacik4b46fce2010-05-23 11:00:55 -0400894 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400895 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400896
Chris Masond3977122009-01-05 21:25:51 -0500897 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400898 unsigned long op;
899
Josef Bacik287a0ab2010-03-19 18:07:23 +0000900 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400901 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500902 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500903 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100904 if (ret < 0) {
905 btrfs_abort_transaction(trans, root, ret);
906 goto out_unlock;
907 }
Chris Masond3977122009-01-05 21:25:51 -0500908
David Sterba172ddd62011-04-21 00:48:27 +0200909 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100910 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400911 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500912 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500913 ram_size = ins.offset;
914 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400915 em->mod_start = em->start;
916 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400917
Chris Masone6dcd2d2008-07-17 12:53:50 -0400918 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400919 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500920 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400921 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400922 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400923 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400924
Chris Masond3977122009-01-05 21:25:51 -0500925 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400926 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400927 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400928 if (!ret)
929 list_move(&em->list,
930 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400931 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400932 if (ret != -EEXIST) {
933 free_extent_map(em);
934 break;
935 }
936 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400937 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400938 }
939
Chris Mason98d20f62008-04-14 09:46:10 -0400940 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400941 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500942 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100943 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400944
Yan Zheng17d217f2008-12-12 10:03:38 -0500945 if (root->root_key.objectid ==
946 BTRFS_DATA_RELOC_TREE_OBJECTID) {
947 ret = btrfs_reloc_clone_csums(inode, start,
948 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100949 if (ret) {
950 btrfs_abort_transaction(trans, root, ret);
951 goto out_unlock;
952 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500953 }
954
Chris Masond3977122009-01-05 21:25:51 -0500955 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400956 break;
Chris Masond3977122009-01-05 21:25:51 -0500957
Chris Masonc8b97812008-10-29 14:49:59 -0400958 /* we're not doing compressed IO, don't unlock the first
959 * page (which the caller expects to stay locked), don't
960 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400961 *
962 * Do set the Private2 bit so we know this page was properly
963 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400964 */
Chris Masona791e352009-10-08 11:27:10 -0400965 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
966 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
967 EXTENT_SET_PRIVATE2;
968
Chris Masonc8b97812008-10-29 14:49:59 -0400969 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
970 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400971 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400972 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500973 num_bytes -= cur_alloc_size;
974 alloc_hint = ins.objectid + ins.offset;
975 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400976 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500978 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000979
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100980out_unlock:
981 extent_clear_unlock_delalloc(inode,
982 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400983 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100984 EXTENT_CLEAR_UNLOCK_PAGE |
985 EXTENT_CLEAR_UNLOCK |
986 EXTENT_CLEAR_DELALLOC |
987 EXTENT_CLEAR_DIRTY |
988 EXTENT_SET_WRITEBACK |
989 EXTENT_END_WRITEBACK);
990
991 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500992}
Chris Masonc8b97812008-10-29 14:49:59 -0400993
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000994static noinline int cow_file_range(struct inode *inode,
995 struct page *locked_page,
996 u64 start, u64 end, int *page_started,
997 unsigned long *nr_written,
998 int unlock)
999{
1000 struct btrfs_trans_handle *trans;
1001 struct btrfs_root *root = BTRFS_I(inode)->root;
1002 int ret;
1003
1004 trans = btrfs_join_transaction(root);
1005 if (IS_ERR(trans)) {
1006 extent_clear_unlock_delalloc(inode,
1007 &BTRFS_I(inode)->io_tree,
1008 start, end, locked_page,
1009 EXTENT_CLEAR_UNLOCK_PAGE |
1010 EXTENT_CLEAR_UNLOCK |
1011 EXTENT_CLEAR_DELALLOC |
1012 EXTENT_CLEAR_DIRTY |
1013 EXTENT_SET_WRITEBACK |
1014 EXTENT_END_WRITEBACK);
1015 return PTR_ERR(trans);
1016 }
1017 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1018
1019 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1020 page_started, nr_written, unlock);
1021
1022 btrfs_end_transaction(trans, root);
1023
1024 return ret;
1025}
1026
Chris Mason771ed682008-11-06 22:02:51 -05001027/*
1028 * work queue call back to started compression on a file and pages
1029 */
1030static noinline void async_cow_start(struct btrfs_work *work)
1031{
1032 struct async_cow *async_cow;
1033 int num_added = 0;
1034 async_cow = container_of(work, struct async_cow, work);
1035
1036 compress_file_range(async_cow->inode, async_cow->locked_page,
1037 async_cow->start, async_cow->end, async_cow,
1038 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001039 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001040 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001041 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001042 }
Chris Mason771ed682008-11-06 22:02:51 -05001043}
1044
1045/*
1046 * work queue call back to submit previously compressed pages
1047 */
1048static noinline void async_cow_submit(struct btrfs_work *work)
1049{
1050 struct async_cow *async_cow;
1051 struct btrfs_root *root;
1052 unsigned long nr_pages;
1053
1054 async_cow = container_of(work, struct async_cow, work);
1055
1056 root = async_cow->root;
1057 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1058 PAGE_CACHE_SHIFT;
1059
Josef Bacik66657b32012-08-01 15:36:24 -04001060 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001061 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001062 waitqueue_active(&root->fs_info->async_submit_wait))
1063 wake_up(&root->fs_info->async_submit_wait);
1064
Chris Masond3977122009-01-05 21:25:51 -05001065 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001066 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001067}
Chris Masonc8b97812008-10-29 14:49:59 -04001068
Chris Mason771ed682008-11-06 22:02:51 -05001069static noinline void async_cow_free(struct btrfs_work *work)
1070{
1071 struct async_cow *async_cow;
1072 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001073 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001074 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001075 kfree(async_cow);
1076}
1077
1078static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1079 u64 start, u64 end, int *page_started,
1080 unsigned long *nr_written)
1081{
1082 struct async_cow *async_cow;
1083 struct btrfs_root *root = BTRFS_I(inode)->root;
1084 unsigned long nr_pages;
1085 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001086 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001087
Chris Masona3429ab2009-10-08 12:30:20 -04001088 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1089 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001090 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001091 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001092 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001093 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001094 async_cow->root = root;
1095 async_cow->locked_page = locked_page;
1096 async_cow->start = start;
1097
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001098 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001099 cur_end = end;
1100 else
1101 cur_end = min(end, start + 512 * 1024 - 1);
1102
1103 async_cow->end = cur_end;
1104 INIT_LIST_HEAD(&async_cow->extents);
1105
1106 async_cow->work.func = async_cow_start;
1107 async_cow->work.ordered_func = async_cow_submit;
1108 async_cow->work.ordered_free = async_cow_free;
1109 async_cow->work.flags = 0;
1110
Chris Mason771ed682008-11-06 22:02:51 -05001111 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1112 PAGE_CACHE_SHIFT;
1113 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1114
1115 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1116 &async_cow->work);
1117
1118 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1119 wait_event(root->fs_info->async_submit_wait,
1120 (atomic_read(&root->fs_info->async_delalloc_pages) <
1121 limit));
1122 }
1123
Chris Masond3977122009-01-05 21:25:51 -05001124 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001125 atomic_read(&root->fs_info->async_delalloc_pages)) {
1126 wait_event(root->fs_info->async_submit_wait,
1127 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1128 0));
1129 }
1130
1131 *nr_written += nr_pages;
1132 start = cur_end + 1;
1133 }
1134 *page_started = 1;
1135 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001136}
1137
Chris Masond3977122009-01-05 21:25:51 -05001138static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001139 u64 bytenr, u64 num_bytes)
1140{
1141 int ret;
1142 struct btrfs_ordered_sum *sums;
1143 LIST_HEAD(list);
1144
Yan Zheng07d400a2009-01-06 11:42:00 -05001145 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001146 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001147 if (ret == 0 && list_empty(&list))
1148 return 0;
1149
1150 while (!list_empty(&list)) {
1151 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1152 list_del(&sums->list);
1153 kfree(sums);
1154 }
1155 return 1;
1156}
1157
Chris Masond352ac62008-09-29 15:18:18 -04001158/*
1159 * when nowcow writeback call back. This checks for snapshots or COW copies
1160 * of the extents that exist in the file, and COWs the file as required.
1161 *
1162 * If no cow copies or snapshots exist, we write directly to the existing
1163 * blocks on disk
1164 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001165static noinline int run_delalloc_nocow(struct inode *inode,
1166 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001167 u64 start, u64 end, int *page_started, int force,
1168 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001169{
Chris Masonbe20aa92007-12-17 20:14:01 -05001170 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001171 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001172 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001173 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001174 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001175 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001176 u64 cow_start;
1177 u64 cur_offset;
1178 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001179 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001180 u64 disk_bytenr;
1181 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001182 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001183 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001184 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001185 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001186 int nocow;
1187 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001188 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001189 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001190
1191 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001192 if (!path) {
1193 extent_clear_unlock_delalloc(inode,
1194 &BTRFS_I(inode)->io_tree,
1195 start, end, locked_page,
1196 EXTENT_CLEAR_UNLOCK_PAGE |
1197 EXTENT_CLEAR_UNLOCK |
1198 EXTENT_CLEAR_DELALLOC |
1199 EXTENT_CLEAR_DIRTY |
1200 EXTENT_SET_WRITEBACK |
1201 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001202 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001203 }
Li Zefan82d59022011-04-20 10:33:24 +08001204
Liu Bo83eea1f2012-07-10 05:28:39 -06001205 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001206
1207 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001208 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001209 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001210 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001211
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001212 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001213 extent_clear_unlock_delalloc(inode,
1214 &BTRFS_I(inode)->io_tree,
1215 start, end, locked_page,
1216 EXTENT_CLEAR_UNLOCK_PAGE |
1217 EXTENT_CLEAR_UNLOCK |
1218 EXTENT_CLEAR_DELALLOC |
1219 EXTENT_CLEAR_DIRTY |
1220 EXTENT_SET_WRITEBACK |
1221 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001222 btrfs_free_path(path);
1223 return PTR_ERR(trans);
1224 }
1225
Josef Bacik74b21072011-04-13 12:02:53 -04001226 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001227
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 cow_start = (u64)-1;
1229 cur_offset = start;
1230 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001231 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001232 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001233 if (ret < 0) {
1234 btrfs_abort_transaction(trans, root, ret);
1235 goto error;
1236 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1238 leaf = path->nodes[0];
1239 btrfs_item_key_to_cpu(leaf, &found_key,
1240 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001241 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001242 found_key.type == BTRFS_EXTENT_DATA_KEY)
1243 path->slots[0]--;
1244 }
1245 check_prev = 0;
1246next_slot:
1247 leaf = path->nodes[0];
1248 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1249 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001250 if (ret < 0) {
1251 btrfs_abort_transaction(trans, root, ret);
1252 goto error;
1253 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 if (ret > 0)
1255 break;
1256 leaf = path->nodes[0];
1257 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001258
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 nocow = 0;
1260 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001261 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001263
Li Zefan33345d012011-04-20 10:31:50 +08001264 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1266 found_key.offset > end)
1267 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001268
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 if (found_key.offset > cur_offset) {
1270 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001271 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 goto out_check;
1273 }
Chris Masonc31f8832008-01-08 15:46:31 -05001274
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 fi = btrfs_item_ptr(leaf, path->slots[0],
1276 struct btrfs_file_extent_item);
1277 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001278
Yan Zhengd899e052008-10-30 14:25:28 -04001279 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1280 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001282 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 extent_end = found_key.offset +
1284 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001285 disk_num_bytes =
1286 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 if (extent_end <= start) {
1288 path->slots[0]++;
1289 goto next_slot;
1290 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001291 if (disk_bytenr == 0)
1292 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 if (btrfs_file_extent_compression(leaf, fi) ||
1294 btrfs_file_extent_encryption(leaf, fi) ||
1295 btrfs_file_extent_other_encoding(leaf, fi))
1296 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001297 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1298 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001299 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001301 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001302 found_key.offset -
1303 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001304 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001305 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001306 disk_bytenr += cur_offset - found_key.offset;
1307 num_bytes = min(end + 1, extent_end) - cur_offset;
1308 /*
1309 * force cow if csum exists in the range.
1310 * this ensure that csum for a given extent are
1311 * either valid or do not exist.
1312 */
1313 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1314 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 nocow = 1;
1316 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1317 extent_end = found_key.offset +
1318 btrfs_file_extent_inline_len(leaf, fi);
1319 extent_end = ALIGN(extent_end, root->sectorsize);
1320 } else {
1321 BUG_ON(1);
1322 }
1323out_check:
1324 if (extent_end <= start) {
1325 path->slots[0]++;
1326 goto next_slot;
1327 }
1328 if (!nocow) {
1329 if (cow_start == (u64)-1)
1330 cow_start = cur_offset;
1331 cur_offset = extent_end;
1332 if (cur_offset > end)
1333 break;
1334 path->slots[0]++;
1335 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001336 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001337
David Sterbab3b4aa72011-04-21 01:20:15 +02001338 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001340 ret = __cow_file_range(trans, inode, root, locked_page,
1341 cow_start, found_key.offset - 1,
1342 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001343 if (ret) {
1344 btrfs_abort_transaction(trans, root, ret);
1345 goto error;
1346 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001348 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001349
Yan Zhengd899e052008-10-30 14:25:28 -04001350 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1351 struct extent_map *em;
1352 struct extent_map_tree *em_tree;
1353 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001354 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001355 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001356 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001357 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001358 em->len = num_bytes;
1359 em->block_len = num_bytes;
1360 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001361 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001362 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001363 em->mod_start = em->start;
1364 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001365 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001366 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001367 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001368 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001369 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001370 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001371 if (!ret)
1372 list_move(&em->list,
1373 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001374 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001375 if (ret != -EEXIST) {
1376 free_extent_map(em);
1377 break;
1378 }
1379 btrfs_drop_extent_cache(inode, em->start,
1380 em->start + em->len - 1, 0);
1381 }
1382 type = BTRFS_ORDERED_PREALLOC;
1383 } else {
1384 type = BTRFS_ORDERED_NOCOW;
1385 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001386
1387 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001388 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001389 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001390
Yan, Zhengefa56462010-05-16 10:49:59 -04001391 if (root->root_key.objectid ==
1392 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1393 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1394 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001395 if (ret) {
1396 btrfs_abort_transaction(trans, root, ret);
1397 goto error;
1398 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001399 }
1400
Yan Zhengd899e052008-10-30 14:25:28 -04001401 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001402 cur_offset, cur_offset + num_bytes - 1,
1403 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1404 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1405 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001406 cur_offset = extent_end;
1407 if (cur_offset > end)
1408 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001409 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001410 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001411
Josef Bacik17ca04a2012-05-31 15:58:55 -04001412 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001413 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001414 cur_offset = end;
1415 }
1416
Yan Zheng80ff3852008-10-30 14:20:02 -04001417 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001418 ret = __cow_file_range(trans, inode, root, locked_page,
1419 cow_start, end,
1420 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001421 if (ret) {
1422 btrfs_abort_transaction(trans, root, ret);
1423 goto error;
1424 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 }
1426
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001427error:
Miao Xiea698d0752012-09-20 01:51:59 -06001428 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001429 if (!ret)
1430 ret = err;
1431
Josef Bacik17ca04a2012-05-31 15:58:55 -04001432 if (ret && cur_offset < end)
1433 extent_clear_unlock_delalloc(inode,
1434 &BTRFS_I(inode)->io_tree,
1435 cur_offset, end, locked_page,
1436 EXTENT_CLEAR_UNLOCK_PAGE |
1437 EXTENT_CLEAR_UNLOCK |
1438 EXTENT_CLEAR_DELALLOC |
1439 EXTENT_CLEAR_DIRTY |
1440 EXTENT_SET_WRITEBACK |
1441 EXTENT_END_WRITEBACK);
1442
Yan Zheng7ea394f2008-08-05 13:05:02 -04001443 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001445}
1446
Chris Masond352ac62008-09-29 15:18:18 -04001447/*
1448 * extent_io.c call back to do delayed allocation processing
1449 */
Chris Masonc8b97812008-10-29 14:49:59 -04001450static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001451 u64 start, u64 end, int *page_started,
1452 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001453{
Chris Masonbe20aa92007-12-17 20:14:01 -05001454 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001455 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001456
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001457 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001458 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001459 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001460 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001461 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001462 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001463 } else if (!btrfs_test_opt(root, COMPRESS) &&
1464 !(BTRFS_I(inode)->force_compress) &&
1465 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001466 ret = cow_file_range(inode, locked_page, start, end,
1467 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001468 } else {
1469 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1470 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001471 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001472 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001473 }
Chris Masonb888db22007-08-27 16:49:44 -04001474 return ret;
1475}
1476
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001477static void btrfs_split_extent_hook(struct inode *inode,
1478 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001479{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001480 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001481 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001482 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001483
Josef Bacik9e0baf62011-07-15 15:16:44 +00001484 spin_lock(&BTRFS_I(inode)->lock);
1485 BTRFS_I(inode)->outstanding_extents++;
1486 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001487}
1488
1489/*
1490 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1491 * extents so we can keep track of new extents that are just merged onto old
1492 * extents, such as when we are doing sequential writes, so we can properly
1493 * account for the metadata space we'll need.
1494 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001495static void btrfs_merge_extent_hook(struct inode *inode,
1496 struct extent_state *new,
1497 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001498{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001499 /* not delalloc, ignore it */
1500 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001501 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001502
Josef Bacik9e0baf62011-07-15 15:16:44 +00001503 spin_lock(&BTRFS_I(inode)->lock);
1504 BTRFS_I(inode)->outstanding_extents--;
1505 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001506}
1507
Chris Masond352ac62008-09-29 15:18:18 -04001508/*
1509 * extent_io.c set_bit_hook, used to track delayed allocation
1510 * bytes in this file, and to maintain the list of inodes that
1511 * have pending delalloc work to be done.
1512 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001513static void btrfs_set_bit_hook(struct inode *inode,
1514 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001515{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001516
Chris Mason75eff682008-12-15 15:54:40 -05001517 /*
1518 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001519 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001520 * bit, which is only set or cleared with irqs on
1521 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001522 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001523 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001524 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001525 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001526
Josef Bacik9e0baf62011-07-15 15:16:44 +00001527 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001528 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001529 } else {
1530 spin_lock(&BTRFS_I(inode)->lock);
1531 BTRFS_I(inode)->outstanding_extents++;
1532 spin_unlock(&BTRFS_I(inode)->lock);
1533 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001534
Miao Xie963d6782013-01-29 10:10:51 +00001535 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1536 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001537 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001538 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001539 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1540 &BTRFS_I(inode)->runtime_flags)) {
1541 spin_lock(&root->fs_info->delalloc_lock);
1542 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1543 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1544 &root->fs_info->delalloc_inodes);
1545 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1546 &BTRFS_I(inode)->runtime_flags);
1547 }
1548 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001549 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001550 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001551 }
Chris Mason291d6732008-01-29 15:55:23 -05001552}
1553
Chris Masond352ac62008-09-29 15:18:18 -04001554/*
1555 * extent_io.c clear_bit_hook, see set_bit_hook for why
1556 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001557static void btrfs_clear_bit_hook(struct inode *inode,
1558 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001559{
Chris Mason75eff682008-12-15 15:54:40 -05001560 /*
1561 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001562 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001563 * bit, which is only set or cleared with irqs on
1564 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001565 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001566 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001567 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001568 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001569
Josef Bacik9e0baf62011-07-15 15:16:44 +00001570 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001571 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001572 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1573 spin_lock(&BTRFS_I(inode)->lock);
1574 BTRFS_I(inode)->outstanding_extents--;
1575 spin_unlock(&BTRFS_I(inode)->lock);
1576 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001577
1578 if (*bits & EXTENT_DO_ACCOUNTING)
1579 btrfs_delalloc_release_metadata(inode, len);
1580
Josef Bacik0cb59c92010-07-02 12:14:14 -04001581 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1582 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001583 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001584
Miao Xie963d6782013-01-29 10:10:51 +00001585 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1586 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001587 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001588 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001589 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001590 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1591 &BTRFS_I(inode)->runtime_flags)) {
1592 spin_lock(&root->fs_info->delalloc_lock);
1593 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1594 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1595 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1596 &BTRFS_I(inode)->runtime_flags);
1597 }
1598 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001599 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001600 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001601 }
Chris Mason291d6732008-01-29 15:55:23 -05001602}
1603
Chris Masond352ac62008-09-29 15:18:18 -04001604/*
1605 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1606 * we don't create bios that span stripes or chunks
1607 */
David Woodhouse64a16702009-07-15 23:29:37 +01001608int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001609 size_t size, struct bio *bio,
1610 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001611{
1612 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001613 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001614 u64 length = 0;
1615 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001616 int ret;
1617
Chris Mason771ed682008-11-06 22:02:51 -05001618 if (bio_flags & EXTENT_BIO_COMPRESSED)
1619 return 0;
1620
Chris Masonf2d8d742008-04-21 10:03:05 -04001621 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001622 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001623 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001624 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001625 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001626 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001627 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001628 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001629 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001630}
1631
Chris Masond352ac62008-09-29 15:18:18 -04001632/*
1633 * in order to insert checksums into the metadata in large chunks,
1634 * we wait until bio submission time. All the pages in the bio are
1635 * checksummed and sums are attached onto the ordered extent record.
1636 *
1637 * At IO completion time the cums attached on the ordered extent record
1638 * are inserted into the btree
1639 */
Chris Masond3977122009-01-05 21:25:51 -05001640static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1641 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001642 unsigned long bio_flags,
1643 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001644{
Chris Mason065631f2008-02-20 12:07:25 -05001645 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001646 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001647
Chris Masond20f7042008-12-08 16:58:54 -05001648 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001649 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001650 return 0;
1651}
Chris Masone0156402008-04-16 11:15:20 -04001652
Chris Mason4a69a412008-11-06 22:03:00 -05001653/*
1654 * in order to insert checksums into the metadata in large chunks,
1655 * we wait until bio submission time. All the pages in the bio are
1656 * checksummed and sums are attached onto the ordered extent record.
1657 *
1658 * At IO completion time the cums attached on the ordered extent record
1659 * are inserted into the btree
1660 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001661static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001662 int mirror_num, unsigned long bio_flags,
1663 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001664{
1665 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001666 int ret;
1667
1668 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1669 if (ret)
1670 bio_endio(bio, ret);
1671 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001672}
1673
Chris Masond352ac62008-09-29 15:18:18 -04001674/*
Chris Masoncad321a2008-12-17 14:51:42 -05001675 * extent_io.c submission hook. This does the right thing for csum calculation
1676 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001677 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001678static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001679 int mirror_num, unsigned long bio_flags,
1680 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001681{
1682 struct btrfs_root *root = BTRFS_I(inode)->root;
1683 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001684 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001685 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001686 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001687
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001688 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001689
Liu Bo83eea1f2012-07-10 05:28:39 -06001690 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001691 metadata = 2;
1692
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001693 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001694 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1695 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001696 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001697
Chris Masond20f7042008-12-08 16:58:54 -05001698 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001699 ret = btrfs_submit_compressed_read(inode, bio,
1700 mirror_num,
1701 bio_flags);
1702 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001703 } else if (!skip_sum) {
1704 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1705 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001706 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001707 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001708 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001709 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001710 /* csum items have already been cloned */
1711 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1712 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001713 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001714 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001715 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001716 bio_flags, bio_offset,
1717 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001718 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001719 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001720 } else if (!skip_sum) {
1721 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1722 if (ret)
1723 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001724 }
1725
Chris Mason0b86a832008-03-24 15:01:56 -04001726mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001727 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1728
1729out:
1730 if (ret < 0)
1731 bio_endio(bio, ret);
1732 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001733}
Chris Mason6885f302008-02-20 16:11:05 -05001734
Chris Masond352ac62008-09-29 15:18:18 -04001735/*
1736 * given a list of ordered sums record them in the inode. This happens
1737 * at IO completion time based on sums calculated at bio submission time.
1738 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001739static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001740 struct inode *inode, u64 file_offset,
1741 struct list_head *list)
1742{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001743 struct btrfs_ordered_sum *sum;
1744
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001745 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001746 btrfs_csum_file_blocks(trans,
1747 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001748 }
1749 return 0;
1750}
1751
Josef Bacik2ac55d42010-02-03 19:33:23 +00001752int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1753 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001754{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001755 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001756 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001757 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001758}
1759
Chris Masond352ac62008-09-29 15:18:18 -04001760/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001761struct btrfs_writepage_fixup {
1762 struct page *page;
1763 struct btrfs_work work;
1764};
1765
Christoph Hellwigb2950862008-12-02 09:54:17 -05001766static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001767{
1768 struct btrfs_writepage_fixup *fixup;
1769 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001770 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001771 struct page *page;
1772 struct inode *inode;
1773 u64 page_start;
1774 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001775 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001776
1777 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1778 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001779again:
Chris Mason247e7432008-07-17 12:53:51 -04001780 lock_page(page);
1781 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1782 ClearPageChecked(page);
1783 goto out_page;
1784 }
1785
1786 inode = page->mapping->host;
1787 page_start = page_offset(page);
1788 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1789
Josef Bacik2ac55d42010-02-03 19:33:23 +00001790 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001791 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001792
1793 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001794 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001795 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001796
1797 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1798 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001799 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1800 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001801 unlock_page(page);
1802 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001803 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001804 goto again;
1805 }
Chris Mason247e7432008-07-17 12:53:51 -04001806
Jeff Mahoney87826df2012-02-15 16:23:57 +01001807 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1808 if (ret) {
1809 mapping_set_error(page->mapping, ret);
1810 end_extent_writepage(page, ret, page_start, page_end);
1811 ClearPageChecked(page);
1812 goto out;
1813 }
1814
Josef Bacik2ac55d42010-02-03 19:33:23 +00001815 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001816 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001817 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001818out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001819 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1820 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001821out_page:
1822 unlock_page(page);
1823 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001824 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001825}
1826
1827/*
1828 * There are a few paths in the higher layers of the kernel that directly
1829 * set the page dirty bit without asking the filesystem if it is a
1830 * good idea. This causes problems because we want to make sure COW
1831 * properly happens and the data=ordered rules are followed.
1832 *
Chris Masonc8b97812008-10-29 14:49:59 -04001833 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001834 * hasn't been properly setup for IO. We kick off an async process
1835 * to fix it up. The async helper will wait for ordered extents, set
1836 * the delalloc bit and make it safe to write the page.
1837 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001838static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001839{
1840 struct inode *inode = page->mapping->host;
1841 struct btrfs_writepage_fixup *fixup;
1842 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001843
Chris Mason8b62b722009-09-02 16:53:46 -04001844 /* this page is properly in the ordered list */
1845 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001846 return 0;
1847
1848 if (PageChecked(page))
1849 return -EAGAIN;
1850
1851 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1852 if (!fixup)
1853 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001854
Chris Mason247e7432008-07-17 12:53:51 -04001855 SetPageChecked(page);
1856 page_cache_get(page);
1857 fixup->work.func = btrfs_writepage_fixup_worker;
1858 fixup->page = page;
1859 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001860 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001861}
1862
Yan Zhengd899e052008-10-30 14:25:28 -04001863static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1864 struct inode *inode, u64 file_pos,
1865 u64 disk_bytenr, u64 disk_num_bytes,
1866 u64 num_bytes, u64 ram_bytes,
1867 u8 compression, u8 encryption,
1868 u16 other_encoding, int extent_type)
1869{
1870 struct btrfs_root *root = BTRFS_I(inode)->root;
1871 struct btrfs_file_extent_item *fi;
1872 struct btrfs_path *path;
1873 struct extent_buffer *leaf;
1874 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001875 int ret;
1876
1877 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001878 if (!path)
1879 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001880
Chris Masonb9473432009-03-13 11:00:37 -04001881 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001882
1883 /*
1884 * we may be replacing one extent in the tree with another.
1885 * The new extent is pinned in the extent map, and we don't want
1886 * to drop it from the cache until it is completely in the btree.
1887 *
1888 * So, tell btrfs_drop_extents to leave this extent in the cache.
1889 * the caller is expected to unpin it and allow it to be merged
1890 * with the others.
1891 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001892 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001893 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001894 if (ret)
1895 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001896
Li Zefan33345d012011-04-20 10:31:50 +08001897 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001898 ins.offset = file_pos;
1899 ins.type = BTRFS_EXTENT_DATA_KEY;
1900 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001901 if (ret)
1902 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001903 leaf = path->nodes[0];
1904 fi = btrfs_item_ptr(leaf, path->slots[0],
1905 struct btrfs_file_extent_item);
1906 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1907 btrfs_set_file_extent_type(leaf, fi, extent_type);
1908 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1909 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1910 btrfs_set_file_extent_offset(leaf, fi, 0);
1911 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1912 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1913 btrfs_set_file_extent_compression(leaf, fi, compression);
1914 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1915 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001916
Yan Zhengd899e052008-10-30 14:25:28 -04001917 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001918 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001919
1920 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001921
1922 ins.objectid = disk_bytenr;
1923 ins.offset = disk_num_bytes;
1924 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001925 ret = btrfs_alloc_reserved_file_extent(trans, root,
1926 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001927 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928out:
Yan Zhengd899e052008-10-30 14:25:28 -04001929 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001930
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001931 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001932}
1933
Liu Bo38c227d2013-01-29 03:18:40 +00001934/* snapshot-aware defrag */
1935struct sa_defrag_extent_backref {
1936 struct rb_node node;
1937 struct old_sa_defrag_extent *old;
1938 u64 root_id;
1939 u64 inum;
1940 u64 file_pos;
1941 u64 extent_offset;
1942 u64 num_bytes;
1943 u64 generation;
1944};
1945
1946struct old_sa_defrag_extent {
1947 struct list_head list;
1948 struct new_sa_defrag_extent *new;
1949
1950 u64 extent_offset;
1951 u64 bytenr;
1952 u64 offset;
1953 u64 len;
1954 int count;
1955};
1956
1957struct new_sa_defrag_extent {
1958 struct rb_root root;
1959 struct list_head head;
1960 struct btrfs_path *path;
1961 struct inode *inode;
1962 u64 file_pos;
1963 u64 len;
1964 u64 bytenr;
1965 u64 disk_len;
1966 u8 compress_type;
1967};
1968
1969static int backref_comp(struct sa_defrag_extent_backref *b1,
1970 struct sa_defrag_extent_backref *b2)
1971{
1972 if (b1->root_id < b2->root_id)
1973 return -1;
1974 else if (b1->root_id > b2->root_id)
1975 return 1;
1976
1977 if (b1->inum < b2->inum)
1978 return -1;
1979 else if (b1->inum > b2->inum)
1980 return 1;
1981
1982 if (b1->file_pos < b2->file_pos)
1983 return -1;
1984 else if (b1->file_pos > b2->file_pos)
1985 return 1;
1986
1987 /*
1988 * [------------------------------] ===> (a range of space)
1989 * |<--->| |<---->| =============> (fs/file tree A)
1990 * |<---------------------------->| ===> (fs/file tree B)
1991 *
1992 * A range of space can refer to two file extents in one tree while
1993 * refer to only one file extent in another tree.
1994 *
1995 * So we may process a disk offset more than one time(two extents in A)
1996 * and locate at the same extent(one extent in B), then insert two same
1997 * backrefs(both refer to the extent in B).
1998 */
1999 return 0;
2000}
2001
2002static void backref_insert(struct rb_root *root,
2003 struct sa_defrag_extent_backref *backref)
2004{
2005 struct rb_node **p = &root->rb_node;
2006 struct rb_node *parent = NULL;
2007 struct sa_defrag_extent_backref *entry;
2008 int ret;
2009
2010 while (*p) {
2011 parent = *p;
2012 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2013
2014 ret = backref_comp(backref, entry);
2015 if (ret < 0)
2016 p = &(*p)->rb_left;
2017 else
2018 p = &(*p)->rb_right;
2019 }
2020
2021 rb_link_node(&backref->node, parent, p);
2022 rb_insert_color(&backref->node, root);
2023}
2024
2025/*
2026 * Note the backref might has changed, and in this case we just return 0.
2027 */
2028static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2029 void *ctx)
2030{
2031 struct btrfs_file_extent_item *extent;
2032 struct btrfs_fs_info *fs_info;
2033 struct old_sa_defrag_extent *old = ctx;
2034 struct new_sa_defrag_extent *new = old->new;
2035 struct btrfs_path *path = new->path;
2036 struct btrfs_key key;
2037 struct btrfs_root *root;
2038 struct sa_defrag_extent_backref *backref;
2039 struct extent_buffer *leaf;
2040 struct inode *inode = new->inode;
2041 int slot;
2042 int ret;
2043 u64 extent_offset;
2044 u64 num_bytes;
2045
2046 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2047 inum == btrfs_ino(inode))
2048 return 0;
2049
2050 key.objectid = root_id;
2051 key.type = BTRFS_ROOT_ITEM_KEY;
2052 key.offset = (u64)-1;
2053
2054 fs_info = BTRFS_I(inode)->root->fs_info;
2055 root = btrfs_read_fs_root_no_name(fs_info, &key);
2056 if (IS_ERR(root)) {
2057 if (PTR_ERR(root) == -ENOENT)
2058 return 0;
2059 WARN_ON(1);
2060 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2061 inum, offset, root_id);
2062 return PTR_ERR(root);
2063 }
2064
2065 key.objectid = inum;
2066 key.type = BTRFS_EXTENT_DATA_KEY;
2067 if (offset > (u64)-1 << 32)
2068 key.offset = 0;
2069 else
2070 key.offset = offset;
2071
2072 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2073 if (ret < 0) {
2074 WARN_ON(1);
2075 return ret;
2076 }
2077
2078 while (1) {
2079 cond_resched();
2080
2081 leaf = path->nodes[0];
2082 slot = path->slots[0];
2083
2084 if (slot >= btrfs_header_nritems(leaf)) {
2085 ret = btrfs_next_leaf(root, path);
2086 if (ret < 0) {
2087 goto out;
2088 } else if (ret > 0) {
2089 ret = 0;
2090 goto out;
2091 }
2092 continue;
2093 }
2094
2095 path->slots[0]++;
2096
2097 btrfs_item_key_to_cpu(leaf, &key, slot);
2098
2099 if (key.objectid > inum)
2100 goto out;
2101
2102 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2103 continue;
2104
2105 extent = btrfs_item_ptr(leaf, slot,
2106 struct btrfs_file_extent_item);
2107
2108 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2109 continue;
2110
2111 extent_offset = btrfs_file_extent_offset(leaf, extent);
2112 if (key.offset - extent_offset != offset)
2113 continue;
2114
2115 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2116 if (extent_offset >= old->extent_offset + old->offset +
2117 old->len || extent_offset + num_bytes <=
2118 old->extent_offset + old->offset)
2119 continue;
2120
2121 break;
2122 }
2123
2124 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2125 if (!backref) {
2126 ret = -ENOENT;
2127 goto out;
2128 }
2129
2130 backref->root_id = root_id;
2131 backref->inum = inum;
2132 backref->file_pos = offset + extent_offset;
2133 backref->num_bytes = num_bytes;
2134 backref->extent_offset = extent_offset;
2135 backref->generation = btrfs_file_extent_generation(leaf, extent);
2136 backref->old = old;
2137 backref_insert(&new->root, backref);
2138 old->count++;
2139out:
2140 btrfs_release_path(path);
2141 WARN_ON(ret);
2142 return ret;
2143}
2144
2145static noinline bool record_extent_backrefs(struct btrfs_path *path,
2146 struct new_sa_defrag_extent *new)
2147{
2148 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2149 struct old_sa_defrag_extent *old, *tmp;
2150 int ret;
2151
2152 new->path = path;
2153
2154 list_for_each_entry_safe(old, tmp, &new->head, list) {
2155 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2156 path, record_one_backref,
2157 old);
2158 BUG_ON(ret < 0 && ret != -ENOENT);
2159
2160 /* no backref to be processed for this extent */
2161 if (!old->count) {
2162 list_del(&old->list);
2163 kfree(old);
2164 }
2165 }
2166
2167 if (list_empty(&new->head))
2168 return false;
2169
2170 return true;
2171}
2172
2173static int relink_is_mergable(struct extent_buffer *leaf,
2174 struct btrfs_file_extent_item *fi,
2175 u64 disk_bytenr)
2176{
2177 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2178 return 0;
2179
2180 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2181 return 0;
2182
2183 if (btrfs_file_extent_compression(leaf, fi) ||
2184 btrfs_file_extent_encryption(leaf, fi) ||
2185 btrfs_file_extent_other_encoding(leaf, fi))
2186 return 0;
2187
2188 return 1;
2189}
2190
2191/*
2192 * Note the backref might has changed, and in this case we just return 0.
2193 */
2194static noinline int relink_extent_backref(struct btrfs_path *path,
2195 struct sa_defrag_extent_backref *prev,
2196 struct sa_defrag_extent_backref *backref)
2197{
2198 struct btrfs_file_extent_item *extent;
2199 struct btrfs_file_extent_item *item;
2200 struct btrfs_ordered_extent *ordered;
2201 struct btrfs_trans_handle *trans;
2202 struct btrfs_fs_info *fs_info;
2203 struct btrfs_root *root;
2204 struct btrfs_key key;
2205 struct extent_buffer *leaf;
2206 struct old_sa_defrag_extent *old = backref->old;
2207 struct new_sa_defrag_extent *new = old->new;
2208 struct inode *src_inode = new->inode;
2209 struct inode *inode;
2210 struct extent_state *cached = NULL;
2211 int ret = 0;
2212 u64 start;
2213 u64 len;
2214 u64 lock_start;
2215 u64 lock_end;
2216 bool merge = false;
2217 int index;
2218
2219 if (prev && prev->root_id == backref->root_id &&
2220 prev->inum == backref->inum &&
2221 prev->file_pos + prev->num_bytes == backref->file_pos)
2222 merge = true;
2223
2224 /* step 1: get root */
2225 key.objectid = backref->root_id;
2226 key.type = BTRFS_ROOT_ITEM_KEY;
2227 key.offset = (u64)-1;
2228
2229 fs_info = BTRFS_I(src_inode)->root->fs_info;
2230 index = srcu_read_lock(&fs_info->subvol_srcu);
2231
2232 root = btrfs_read_fs_root_no_name(fs_info, &key);
2233 if (IS_ERR(root)) {
2234 srcu_read_unlock(&fs_info->subvol_srcu, index);
2235 if (PTR_ERR(root) == -ENOENT)
2236 return 0;
2237 return PTR_ERR(root);
2238 }
2239 if (btrfs_root_refs(&root->root_item) == 0) {
2240 srcu_read_unlock(&fs_info->subvol_srcu, index);
2241 /* parse ENOENT to 0 */
2242 return 0;
2243 }
2244
2245 /* step 2: get inode */
2246 key.objectid = backref->inum;
2247 key.type = BTRFS_INODE_ITEM_KEY;
2248 key.offset = 0;
2249
2250 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2251 if (IS_ERR(inode)) {
2252 srcu_read_unlock(&fs_info->subvol_srcu, index);
2253 return 0;
2254 }
2255
2256 srcu_read_unlock(&fs_info->subvol_srcu, index);
2257
2258 /* step 3: relink backref */
2259 lock_start = backref->file_pos;
2260 lock_end = backref->file_pos + backref->num_bytes - 1;
2261 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2262 0, &cached);
2263
2264 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2265 if (ordered) {
2266 btrfs_put_ordered_extent(ordered);
2267 goto out_unlock;
2268 }
2269
2270 trans = btrfs_join_transaction(root);
2271 if (IS_ERR(trans)) {
2272 ret = PTR_ERR(trans);
2273 goto out_unlock;
2274 }
2275
2276 key.objectid = backref->inum;
2277 key.type = BTRFS_EXTENT_DATA_KEY;
2278 key.offset = backref->file_pos;
2279
2280 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2281 if (ret < 0) {
2282 goto out_free_path;
2283 } else if (ret > 0) {
2284 ret = 0;
2285 goto out_free_path;
2286 }
2287
2288 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2289 struct btrfs_file_extent_item);
2290
2291 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2292 backref->generation)
2293 goto out_free_path;
2294
2295 btrfs_release_path(path);
2296
2297 start = backref->file_pos;
2298 if (backref->extent_offset < old->extent_offset + old->offset)
2299 start += old->extent_offset + old->offset -
2300 backref->extent_offset;
2301
2302 len = min(backref->extent_offset + backref->num_bytes,
2303 old->extent_offset + old->offset + old->len);
2304 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2305
2306 ret = btrfs_drop_extents(trans, root, inode, start,
2307 start + len, 1);
2308 if (ret)
2309 goto out_free_path;
2310again:
2311 key.objectid = btrfs_ino(inode);
2312 key.type = BTRFS_EXTENT_DATA_KEY;
2313 key.offset = start;
2314
Liu Boa09a0a72013-03-11 09:20:58 +00002315 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002316 if (merge) {
2317 struct btrfs_file_extent_item *fi;
2318 u64 extent_len;
2319 struct btrfs_key found_key;
2320
2321 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2322 if (ret < 0)
2323 goto out_free_path;
2324
2325 path->slots[0]--;
2326 leaf = path->nodes[0];
2327 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2328
2329 fi = btrfs_item_ptr(leaf, path->slots[0],
2330 struct btrfs_file_extent_item);
2331 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2332
2333 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2334 extent_len + found_key.offset == start) {
2335 btrfs_set_file_extent_num_bytes(leaf, fi,
2336 extent_len + len);
2337 btrfs_mark_buffer_dirty(leaf);
2338 inode_add_bytes(inode, len);
2339
2340 ret = 1;
2341 goto out_free_path;
2342 } else {
2343 merge = false;
2344 btrfs_release_path(path);
2345 goto again;
2346 }
2347 }
2348
2349 ret = btrfs_insert_empty_item(trans, root, path, &key,
2350 sizeof(*extent));
2351 if (ret) {
2352 btrfs_abort_transaction(trans, root, ret);
2353 goto out_free_path;
2354 }
2355
2356 leaf = path->nodes[0];
2357 item = btrfs_item_ptr(leaf, path->slots[0],
2358 struct btrfs_file_extent_item);
2359 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2360 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2361 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2362 btrfs_set_file_extent_num_bytes(leaf, item, len);
2363 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2364 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2365 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2366 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2367 btrfs_set_file_extent_encryption(leaf, item, 0);
2368 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2369
2370 btrfs_mark_buffer_dirty(leaf);
2371 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002372 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002373
2374 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2375 new->disk_len, 0,
2376 backref->root_id, backref->inum,
2377 new->file_pos, 0); /* start - extent_offset */
2378 if (ret) {
2379 btrfs_abort_transaction(trans, root, ret);
2380 goto out_free_path;
2381 }
2382
2383 ret = 1;
2384out_free_path:
2385 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002386 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002387 btrfs_end_transaction(trans, root);
2388out_unlock:
2389 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2390 &cached, GFP_NOFS);
2391 iput(inode);
2392 return ret;
2393}
2394
2395static void relink_file_extents(struct new_sa_defrag_extent *new)
2396{
2397 struct btrfs_path *path;
2398 struct old_sa_defrag_extent *old, *tmp;
2399 struct sa_defrag_extent_backref *backref;
2400 struct sa_defrag_extent_backref *prev = NULL;
2401 struct inode *inode;
2402 struct btrfs_root *root;
2403 struct rb_node *node;
2404 int ret;
2405
2406 inode = new->inode;
2407 root = BTRFS_I(inode)->root;
2408
2409 path = btrfs_alloc_path();
2410 if (!path)
2411 return;
2412
2413 if (!record_extent_backrefs(path, new)) {
2414 btrfs_free_path(path);
2415 goto out;
2416 }
2417 btrfs_release_path(path);
2418
2419 while (1) {
2420 node = rb_first(&new->root);
2421 if (!node)
2422 break;
2423 rb_erase(node, &new->root);
2424
2425 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2426
2427 ret = relink_extent_backref(path, prev, backref);
2428 WARN_ON(ret < 0);
2429
2430 kfree(prev);
2431
2432 if (ret == 1)
2433 prev = backref;
2434 else
2435 prev = NULL;
2436 cond_resched();
2437 }
2438 kfree(prev);
2439
2440 btrfs_free_path(path);
2441
2442 list_for_each_entry_safe(old, tmp, &new->head, list) {
2443 list_del(&old->list);
2444 kfree(old);
2445 }
2446out:
2447 atomic_dec(&root->fs_info->defrag_running);
2448 wake_up(&root->fs_info->transaction_wait);
2449
2450 kfree(new);
2451}
2452
2453static struct new_sa_defrag_extent *
2454record_old_file_extents(struct inode *inode,
2455 struct btrfs_ordered_extent *ordered)
2456{
2457 struct btrfs_root *root = BTRFS_I(inode)->root;
2458 struct btrfs_path *path;
2459 struct btrfs_key key;
2460 struct old_sa_defrag_extent *old, *tmp;
2461 struct new_sa_defrag_extent *new;
2462 int ret;
2463
2464 new = kmalloc(sizeof(*new), GFP_NOFS);
2465 if (!new)
2466 return NULL;
2467
2468 new->inode = inode;
2469 new->file_pos = ordered->file_offset;
2470 new->len = ordered->len;
2471 new->bytenr = ordered->start;
2472 new->disk_len = ordered->disk_len;
2473 new->compress_type = ordered->compress_type;
2474 new->root = RB_ROOT;
2475 INIT_LIST_HEAD(&new->head);
2476
2477 path = btrfs_alloc_path();
2478 if (!path)
2479 goto out_kfree;
2480
2481 key.objectid = btrfs_ino(inode);
2482 key.type = BTRFS_EXTENT_DATA_KEY;
2483 key.offset = new->file_pos;
2484
2485 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2486 if (ret < 0)
2487 goto out_free_path;
2488 if (ret > 0 && path->slots[0] > 0)
2489 path->slots[0]--;
2490
2491 /* find out all the old extents for the file range */
2492 while (1) {
2493 struct btrfs_file_extent_item *extent;
2494 struct extent_buffer *l;
2495 int slot;
2496 u64 num_bytes;
2497 u64 offset;
2498 u64 end;
2499 u64 disk_bytenr;
2500 u64 extent_offset;
2501
2502 l = path->nodes[0];
2503 slot = path->slots[0];
2504
2505 if (slot >= btrfs_header_nritems(l)) {
2506 ret = btrfs_next_leaf(root, path);
2507 if (ret < 0)
2508 goto out_free_list;
2509 else if (ret > 0)
2510 break;
2511 continue;
2512 }
2513
2514 btrfs_item_key_to_cpu(l, &key, slot);
2515
2516 if (key.objectid != btrfs_ino(inode))
2517 break;
2518 if (key.type != BTRFS_EXTENT_DATA_KEY)
2519 break;
2520 if (key.offset >= new->file_pos + new->len)
2521 break;
2522
2523 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2524
2525 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2526 if (key.offset + num_bytes < new->file_pos)
2527 goto next;
2528
2529 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2530 if (!disk_bytenr)
2531 goto next;
2532
2533 extent_offset = btrfs_file_extent_offset(l, extent);
2534
2535 old = kmalloc(sizeof(*old), GFP_NOFS);
2536 if (!old)
2537 goto out_free_list;
2538
2539 offset = max(new->file_pos, key.offset);
2540 end = min(new->file_pos + new->len, key.offset + num_bytes);
2541
2542 old->bytenr = disk_bytenr;
2543 old->extent_offset = extent_offset;
2544 old->offset = offset - key.offset;
2545 old->len = end - offset;
2546 old->new = new;
2547 old->count = 0;
2548 list_add_tail(&old->list, &new->head);
2549next:
2550 path->slots[0]++;
2551 cond_resched();
2552 }
2553
2554 btrfs_free_path(path);
2555 atomic_inc(&root->fs_info->defrag_running);
2556
2557 return new;
2558
2559out_free_list:
2560 list_for_each_entry_safe(old, tmp, &new->head, list) {
2561 list_del(&old->list);
2562 kfree(old);
2563 }
2564out_free_path:
2565 btrfs_free_path(path);
2566out_kfree:
2567 kfree(new);
2568 return NULL;
2569}
2570
Chris Mason5d13a982009-03-13 11:41:46 -04002571/*
2572 * helper function for btrfs_finish_ordered_io, this
2573 * just reads in some of the csum leaves to prime them into ram
2574 * before we start the transaction. It limits the amount of btree
2575 * reads required while inside the transaction.
2576 */
Chris Masond352ac62008-09-29 15:18:18 -04002577/* as ordered data IO finishes, this gets called so we can finish
2578 * an ordered extent if the range of bytes in the file it covers are
2579 * fully written.
2580 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002581static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002582{
Josef Bacik5fd02042012-05-02 14:00:54 -04002583 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002584 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002585 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002586 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002587 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002588 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002589 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002590 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002591 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002592
Liu Bo83eea1f2012-07-10 05:28:39 -06002593 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002594
Josef Bacik5fd02042012-05-02 14:00:54 -04002595 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2596 ret = -EIO;
2597 goto out;
2598 }
2599
Yan, Zhengc2167752009-11-12 09:34:21 +00002600 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002601 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002602 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2603 if (nolock)
2604 trans = btrfs_join_transaction_nolock(root);
2605 else
2606 trans = btrfs_join_transaction(root);
2607 if (IS_ERR(trans)) {
2608 ret = PTR_ERR(trans);
2609 trans = NULL;
2610 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002611 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002612 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2613 ret = btrfs_update_inode_fallback(trans, root, inode);
2614 if (ret) /* -ENOMEM or corruption */
2615 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002616 goto out;
2617 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002618
Josef Bacik2ac55d42010-02-03 19:33:23 +00002619 lock_extent_bits(io_tree, ordered_extent->file_offset,
2620 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002621 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002622
Liu Bo38c227d2013-01-29 03:18:40 +00002623 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2624 ordered_extent->file_offset + ordered_extent->len - 1,
2625 EXTENT_DEFRAG, 1, cached_state);
2626 if (ret) {
2627 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2628 if (last_snapshot >= BTRFS_I(inode)->generation)
2629 /* the inode is shared */
2630 new = record_old_file_extents(inode, ordered_extent);
2631
2632 clear_extent_bit(io_tree, ordered_extent->file_offset,
2633 ordered_extent->file_offset + ordered_extent->len - 1,
2634 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2635 }
2636
Josef Bacik0cb59c92010-07-02 12:14:14 -04002637 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002638 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002639 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002640 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002641 if (IS_ERR(trans)) {
2642 ret = PTR_ERR(trans);
2643 trans = NULL;
2644 goto out_unlock;
2645 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002646 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002647
Chris Masonc8b97812008-10-29 14:49:59 -04002648 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002649 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002650 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002651 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002652 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002653 ordered_extent->file_offset,
2654 ordered_extent->file_offset +
2655 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002656 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002657 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002658 ret = insert_reserved_file_extent(trans, inode,
2659 ordered_extent->file_offset,
2660 ordered_extent->start,
2661 ordered_extent->disk_len,
2662 ordered_extent->len,
2663 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002664 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002665 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002666 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002667 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2668 ordered_extent->file_offset, ordered_extent->len,
2669 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002670 if (ret < 0) {
2671 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002672 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002673 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002674
Chris Masone6dcd2d2008-07-17 12:53:50 -04002675 add_pending_csums(trans, inode, ordered_extent->file_offset,
2676 &ordered_extent->list);
2677
Josef Bacik6c760c02012-11-09 10:53:21 -05002678 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2679 ret = btrfs_update_inode_fallback(trans, root, inode);
2680 if (ret) { /* -ENOMEM or corruption */
2681 btrfs_abort_transaction(trans, root, ret);
2682 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002683 }
2684 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002685out_unlock:
2686 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2687 ordered_extent->file_offset +
2688 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002689out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002690 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002691 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002692 if (trans)
2693 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002694
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002695 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002696 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2697 ordered_extent->file_offset +
2698 ordered_extent->len - 1, NULL, GFP_NOFS);
2699
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002700 /*
2701 * If the ordered extent had an IOERR or something else went
2702 * wrong we need to return the space for this ordered extent
2703 * back to the allocator.
2704 */
2705 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2706 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2707 btrfs_free_reserved_extent(root, ordered_extent->start,
2708 ordered_extent->disk_len);
2709 }
2710
2711
Josef Bacik5fd02042012-05-02 14:00:54 -04002712 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002713 * This needs to be done to make sure anybody waiting knows we are done
2714 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002715 */
2716 btrfs_remove_ordered_extent(inode, ordered_extent);
2717
Liu Bo38c227d2013-01-29 03:18:40 +00002718 /* for snapshot-aware defrag */
2719 if (new)
2720 relink_file_extents(new);
2721
Chris Masone6dcd2d2008-07-17 12:53:50 -04002722 /* once for us */
2723 btrfs_put_ordered_extent(ordered_extent);
2724 /* once for the tree */
2725 btrfs_put_ordered_extent(ordered_extent);
2726
Josef Bacik5fd02042012-05-02 14:00:54 -04002727 return ret;
2728}
2729
2730static void finish_ordered_fn(struct btrfs_work *work)
2731{
2732 struct btrfs_ordered_extent *ordered_extent;
2733 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2734 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002735}
2736
Christoph Hellwigb2950862008-12-02 09:54:17 -05002737static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002738 struct extent_state *state, int uptodate)
2739{
Josef Bacik5fd02042012-05-02 14:00:54 -04002740 struct inode *inode = page->mapping->host;
2741 struct btrfs_root *root = BTRFS_I(inode)->root;
2742 struct btrfs_ordered_extent *ordered_extent = NULL;
2743 struct btrfs_workers *workers;
2744
liubo1abe9b82011-03-24 11:18:59 +00002745 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2746
Chris Mason8b62b722009-09-02 16:53:46 -04002747 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002748 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2749 end - start + 1, uptodate))
2750 return 0;
2751
2752 ordered_extent->work.func = finish_ordered_fn;
2753 ordered_extent->work.flags = 0;
2754
Liu Bo83eea1f2012-07-10 05:28:39 -06002755 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002756 workers = &root->fs_info->endio_freespace_worker;
2757 else
2758 workers = &root->fs_info->endio_write_workers;
2759 btrfs_queue_worker(workers, &ordered_extent->work);
2760
2761 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002762}
2763
Chris Masond352ac62008-09-29 15:18:18 -04002764/*
Chris Masond352ac62008-09-29 15:18:18 -04002765 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002766 * if there's a match, we allow the bio to finish. If not, the code in
2767 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002768 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002769static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002770 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002771{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002772 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002773 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002774 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002775 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002776 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002777 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002778 struct btrfs_root *root = BTRFS_I(inode)->root;
2779 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002780
Chris Masond20f7042008-12-08 16:58:54 -05002781 if (PageChecked(page)) {
2782 ClearPageChecked(page);
2783 goto good;
2784 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002785
2786 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002787 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002788
Yan Zheng17d217f2008-12-12 10:03:38 -05002789 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002790 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002791 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2792 GFP_NOFS);
2793 return 0;
2794 }
2795
Yanc2e639f2008-02-04 08:57:25 -05002796 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002797 private = state->private;
2798 ret = 0;
2799 } else {
2800 ret = get_state_private(io_tree, start, &private);
2801 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002802 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002803 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002804 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002805
Chris Masonff79f812007-10-15 16:22:25 -04002806 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2807 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002808 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002809 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002810
Cong Wang7ac687d2011-11-25 23:14:28 +08002811 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002812good:
Chris Mason07157aa2007-08-30 08:50:51 -04002813 return 0;
2814
2815zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002816 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002817 "private %llu\n",
2818 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002819 (unsigned long long)start, csum,
2820 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002821 memset(kaddr + offset, 1, end - start + 1);
2822 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002823 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002824 if (private == 0)
2825 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002826 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002827}
Chris Masonb888db22007-08-27 16:49:44 -04002828
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002829struct delayed_iput {
2830 struct list_head list;
2831 struct inode *inode;
2832};
2833
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002834/* JDM: If this is fs-wide, why can't we add a pointer to
2835 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002836void btrfs_add_delayed_iput(struct inode *inode)
2837{
2838 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2839 struct delayed_iput *delayed;
2840
2841 if (atomic_add_unless(&inode->i_count, -1, 1))
2842 return;
2843
2844 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2845 delayed->inode = inode;
2846
2847 spin_lock(&fs_info->delayed_iput_lock);
2848 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2849 spin_unlock(&fs_info->delayed_iput_lock);
2850}
2851
2852void btrfs_run_delayed_iputs(struct btrfs_root *root)
2853{
2854 LIST_HEAD(list);
2855 struct btrfs_fs_info *fs_info = root->fs_info;
2856 struct delayed_iput *delayed;
2857 int empty;
2858
2859 spin_lock(&fs_info->delayed_iput_lock);
2860 empty = list_empty(&fs_info->delayed_iputs);
2861 spin_unlock(&fs_info->delayed_iput_lock);
2862 if (empty)
2863 return;
2864
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002865 spin_lock(&fs_info->delayed_iput_lock);
2866 list_splice_init(&fs_info->delayed_iputs, &list);
2867 spin_unlock(&fs_info->delayed_iput_lock);
2868
2869 while (!list_empty(&list)) {
2870 delayed = list_entry(list.next, struct delayed_iput, list);
2871 list_del(&delayed->list);
2872 iput(delayed->inode);
2873 kfree(delayed);
2874 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002875}
2876
Yan, Zhengd68fc572010-05-16 10:49:58 -04002877/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002878 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002879 * files in the subvolume, it removes orphan item and frees block_rsv
2880 * structure.
2881 */
2882void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2883 struct btrfs_root *root)
2884{
Josef Bacik90290e12011-12-02 15:44:12 -05002885 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002886 int ret;
2887
Josef Bacik8a35d952012-05-23 14:26:42 -04002888 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002889 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2890 return;
2891
Josef Bacik90290e12011-12-02 15:44:12 -05002892 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002893 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002894 spin_unlock(&root->orphan_lock);
2895 return;
2896 }
2897
2898 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2899 spin_unlock(&root->orphan_lock);
2900 return;
2901 }
2902
2903 block_rsv = root->orphan_block_rsv;
2904 root->orphan_block_rsv = NULL;
2905 spin_unlock(&root->orphan_lock);
2906
Yan, Zhengd68fc572010-05-16 10:49:58 -04002907 if (root->orphan_item_inserted &&
2908 btrfs_root_refs(&root->root_item) > 0) {
2909 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2910 root->root_key.objectid);
2911 BUG_ON(ret);
2912 root->orphan_item_inserted = 0;
2913 }
2914
Josef Bacik90290e12011-12-02 15:44:12 -05002915 if (block_rsv) {
2916 WARN_ON(block_rsv->size > 0);
2917 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002918 }
2919}
2920
2921/*
Josef Bacik7b128762008-07-24 12:17:14 -04002922 * This creates an orphan entry for the given inode in case something goes
2923 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002924 *
2925 * NOTE: caller of this function should reserve 5 units of metadata for
2926 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002927 */
2928int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2929{
2930 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002931 struct btrfs_block_rsv *block_rsv = NULL;
2932 int reserve = 0;
2933 int insert = 0;
2934 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002935
Yan, Zhengd68fc572010-05-16 10:49:58 -04002936 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002937 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002938 if (!block_rsv)
2939 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002940 }
2941
Yan, Zhengd68fc572010-05-16 10:49:58 -04002942 spin_lock(&root->orphan_lock);
2943 if (!root->orphan_block_rsv) {
2944 root->orphan_block_rsv = block_rsv;
2945 } else if (block_rsv) {
2946 btrfs_free_block_rsv(root, block_rsv);
2947 block_rsv = NULL;
2948 }
Josef Bacik7b128762008-07-24 12:17:14 -04002949
Josef Bacik8a35d952012-05-23 14:26:42 -04002950 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2951 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002952#if 0
2953 /*
2954 * For proper ENOSPC handling, we should do orphan
2955 * cleanup when mounting. But this introduces backward
2956 * compatibility issue.
2957 */
2958 if (!xchg(&root->orphan_item_inserted, 1))
2959 insert = 2;
2960 else
2961 insert = 1;
2962#endif
2963 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002964 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002965 }
Josef Bacik7b128762008-07-24 12:17:14 -04002966
Josef Bacik72ac3c02012-05-23 14:13:11 -04002967 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2968 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002969 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002970 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002971
Yan, Zhengd68fc572010-05-16 10:49:58 -04002972 /* grab metadata reservation from transaction handle */
2973 if (reserve) {
2974 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002975 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002976 }
2977
2978 /* insert an orphan item to track this unlinked/truncated file */
2979 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002980 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002981 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002982 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2983 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002984 btrfs_abort_transaction(trans, root, ret);
2985 return ret;
2986 }
2987 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002988 }
2989
2990 /* insert an orphan item to track subvolume contains orphan files */
2991 if (insert >= 2) {
2992 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2993 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002994 if (ret && ret != -EEXIST) {
2995 btrfs_abort_transaction(trans, root, ret);
2996 return ret;
2997 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002998 }
2999 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003000}
3001
3002/*
3003 * We have done the truncate/delete so we can go ahead and remove the orphan
3004 * item for this particular inode.
3005 */
3006int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
3007{
3008 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003009 int delete_item = 0;
3010 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003011 int ret = 0;
3012
Yan, Zhengd68fc572010-05-16 10:49:58 -04003013 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003014 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3015 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003016 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003017
Josef Bacik72ac3c02012-05-23 14:13:11 -04003018 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3019 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003020 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003021 spin_unlock(&root->orphan_lock);
3022
3023 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003024 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003025 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003026 }
3027
Josef Bacik8a35d952012-05-23 14:26:42 -04003028 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003029 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003030 atomic_dec(&root->orphan_inodes);
3031 }
Josef Bacik7b128762008-07-24 12:17:14 -04003032
Yan, Zhengd68fc572010-05-16 10:49:58 -04003033 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003034}
3035
3036/*
3037 * this cleans up any orphans that may be left on the list from the last use
3038 * of this root.
3039 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003040int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003041{
3042 struct btrfs_path *path;
3043 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003044 struct btrfs_key key, found_key;
3045 struct btrfs_trans_handle *trans;
3046 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003047 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003048 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3049
Yan, Zhengd68fc572010-05-16 10:49:58 -04003050 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003051 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003052
3053 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003054 if (!path) {
3055 ret = -ENOMEM;
3056 goto out;
3057 }
Josef Bacik7b128762008-07-24 12:17:14 -04003058 path->reada = -1;
3059
3060 key.objectid = BTRFS_ORPHAN_OBJECTID;
3061 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3062 key.offset = (u64)-1;
3063
Josef Bacik7b128762008-07-24 12:17:14 -04003064 while (1) {
3065 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003066 if (ret < 0)
3067 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003068
3069 /*
3070 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003071 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003072 * find the key and see if we have stuff that matches
3073 */
3074 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003075 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003076 if (path->slots[0] == 0)
3077 break;
3078 path->slots[0]--;
3079 }
3080
3081 /* pull out the item */
3082 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003083 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3084
3085 /* make sure the item matches what we want */
3086 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3087 break;
3088 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3089 break;
3090
3091 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003092 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003093
3094 /*
3095 * this is where we are basically btrfs_lookup, without the
3096 * crossing root thing. we store the inode number in the
3097 * offset of the orphan item.
3098 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003099
3100 if (found_key.offset == last_objectid) {
3101 printk(KERN_ERR "btrfs: Error removing orphan entry, "
3102 "stopping orphan cleanup\n");
3103 ret = -EINVAL;
3104 goto out;
3105 }
3106
3107 last_objectid = found_key.offset;
3108
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003109 found_key.objectid = found_key.offset;
3110 found_key.type = BTRFS_INODE_ITEM_KEY;
3111 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003112 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003113 ret = PTR_RET(inode);
3114 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003115 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003116
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003117 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3118 struct btrfs_root *dead_root;
3119 struct btrfs_fs_info *fs_info = root->fs_info;
3120 int is_dead_root = 0;
3121
3122 /*
3123 * this is an orphan in the tree root. Currently these
3124 * could come from 2 sources:
3125 * a) a snapshot deletion in progress
3126 * b) a free space cache inode
3127 * We need to distinguish those two, as the snapshot
3128 * orphan must not get deleted.
3129 * find_dead_roots already ran before us, so if this
3130 * is a snapshot deletion, we should find the root
3131 * in the dead_roots list
3132 */
3133 spin_lock(&fs_info->trans_lock);
3134 list_for_each_entry(dead_root, &fs_info->dead_roots,
3135 root_list) {
3136 if (dead_root->root_key.objectid ==
3137 found_key.objectid) {
3138 is_dead_root = 1;
3139 break;
3140 }
3141 }
3142 spin_unlock(&fs_info->trans_lock);
3143 if (is_dead_root) {
3144 /* prevent this orphan from being found again */
3145 key.offset = found_key.objectid - 1;
3146 continue;
3147 }
3148 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003149 /*
3150 * Inode is already gone but the orphan item is still there,
3151 * kill the orphan item.
3152 */
3153 if (ret == -ESTALE) {
3154 trans = btrfs_start_transaction(root, 1);
3155 if (IS_ERR(trans)) {
3156 ret = PTR_ERR(trans);
3157 goto out;
3158 }
Josef Bacik8a35d952012-05-23 14:26:42 -04003159 printk(KERN_ERR "auto deleting %Lu\n",
3160 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003161 ret = btrfs_del_orphan_item(trans, root,
3162 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003163 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003164 btrfs_end_transaction(trans, root);
3165 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003166 }
Josef Bacik7b128762008-07-24 12:17:14 -04003167
Josef Bacik7b128762008-07-24 12:17:14 -04003168 /*
3169 * add this inode to the orphan list so btrfs_orphan_del does
3170 * the proper thing when we hit it
3171 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003172 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3173 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003174 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003175
Josef Bacik7b128762008-07-24 12:17:14 -04003176 /* if we have links, this was a truncate, lets do that */
3177 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003178 if (!S_ISREG(inode->i_mode)) {
3179 WARN_ON(1);
3180 iput(inode);
3181 continue;
3182 }
Josef Bacik7b128762008-07-24 12:17:14 -04003183 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003184
3185 /* 1 for the orphan item deletion. */
3186 trans = btrfs_start_transaction(root, 1);
3187 if (IS_ERR(trans)) {
3188 ret = PTR_ERR(trans);
3189 goto out;
3190 }
3191 ret = btrfs_orphan_add(trans, inode);
3192 btrfs_end_transaction(trans, root);
3193 if (ret)
3194 goto out;
3195
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003196 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003197 if (ret)
3198 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003199 } else {
3200 nr_unlink++;
3201 }
3202
3203 /* this will do delete_inode and everything for us */
3204 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003205 if (ret)
3206 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003207 }
Miao Xie3254c872011-11-10 20:45:05 -05003208 /* release the path since we're done with it */
3209 btrfs_release_path(path);
3210
Yan, Zhengd68fc572010-05-16 10:49:58 -04003211 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3212
3213 if (root->orphan_block_rsv)
3214 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3215 (u64)-1);
3216
3217 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003218 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003219 if (!IS_ERR(trans))
3220 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003221 }
Josef Bacik7b128762008-07-24 12:17:14 -04003222
3223 if (nr_unlink)
3224 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
3225 if (nr_truncate)
3226 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003227
3228out:
3229 if (ret)
3230 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
3231 btrfs_free_path(path);
3232 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003233}
3234
Chris Masond352ac62008-09-29 15:18:18 -04003235/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003236 * very simple check to peek ahead in the leaf looking for xattrs. If we
3237 * don't find any xattrs, we know there can't be any acls.
3238 *
3239 * slot is the slot the inode is in, objectid is the objectid of the inode
3240 */
3241static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3242 int slot, u64 objectid)
3243{
3244 u32 nritems = btrfs_header_nritems(leaf);
3245 struct btrfs_key found_key;
3246 int scanned = 0;
3247
3248 slot++;
3249 while (slot < nritems) {
3250 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3251
3252 /* we found a different objectid, there must not be acls */
3253 if (found_key.objectid != objectid)
3254 return 0;
3255
3256 /* we found an xattr, assume we've got an acl */
3257 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3258 return 1;
3259
3260 /*
3261 * we found a key greater than an xattr key, there can't
3262 * be any acls later on
3263 */
3264 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3265 return 0;
3266
3267 slot++;
3268 scanned++;
3269
3270 /*
3271 * it goes inode, inode backrefs, xattrs, extents,
3272 * so if there are a ton of hard links to an inode there can
3273 * be a lot of backrefs. Don't waste time searching too hard,
3274 * this is just an optimization
3275 */
3276 if (scanned >= 8)
3277 break;
3278 }
3279 /* we hit the end of the leaf before we found an xattr or
3280 * something larger than an xattr. We have to assume the inode
3281 * has acls
3282 */
3283 return 1;
3284}
3285
3286/*
Chris Masond352ac62008-09-29 15:18:18 -04003287 * read an inode from the btree into the in-memory inode
3288 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003289static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003290{
3291 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003292 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003293 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003294 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003295 struct btrfs_root *root = BTRFS_I(inode)->root;
3296 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003297 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003298 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003299 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003300 bool filled = false;
3301
3302 ret = btrfs_fill_inode(inode, &rdev);
3303 if (!ret)
3304 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003305
3306 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003307 if (!path)
3308 goto make_bad;
3309
Josef Bacikd90c7322011-05-17 09:50:54 -04003310 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003311 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003312
Chris Mason39279cc2007-06-12 06:35:45 -04003313 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003314 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003315 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003316
Chris Mason5f39d392007-10-15 16:14:19 -04003317 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003318
3319 if (filled)
3320 goto cache_acl;
3321
Chris Mason5f39d392007-10-15 16:14:19 -04003322 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3323 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003324 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003325 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003326 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3327 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003328 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003329
3330 tspec = btrfs_inode_atime(inode_item);
3331 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3332 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3333
3334 tspec = btrfs_inode_mtime(inode_item);
3335 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3336 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3337
3338 tspec = btrfs_inode_ctime(inode_item);
3339 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3340 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3341
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003342 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003343 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003344 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3345
3346 /*
3347 * If we were modified in the current generation and evicted from memory
3348 * and then re-read we need to do a full sync since we don't have any
3349 * idea about which extents were modified before we were evicted from
3350 * cache.
3351 */
3352 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3353 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3354 &BTRFS_I(inode)->runtime_flags);
3355
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003356 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003357 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003358 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003359 rdev = btrfs_inode_rdev(leaf, inode_item);
3360
Josef Bacikaec74772008-07-24 12:12:38 -04003361 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003362 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003363cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003364 /*
3365 * try to precache a NULL acl entry for files that don't have
3366 * any xattrs or acls
3367 */
Li Zefan33345d012011-04-20 10:31:50 +08003368 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3369 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003370 if (!maybe_acls)
3371 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003372
Chris Mason39279cc2007-06-12 06:35:45 -04003373 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003374
Chris Mason39279cc2007-06-12 06:35:45 -04003375 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003376 case S_IFREG:
3377 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003378 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003379 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003380 inode->i_fop = &btrfs_file_operations;
3381 inode->i_op = &btrfs_file_inode_operations;
3382 break;
3383 case S_IFDIR:
3384 inode->i_fop = &btrfs_dir_file_operations;
3385 if (root == root->fs_info->tree_root)
3386 inode->i_op = &btrfs_dir_ro_inode_operations;
3387 else
3388 inode->i_op = &btrfs_dir_inode_operations;
3389 break;
3390 case S_IFLNK:
3391 inode->i_op = &btrfs_symlink_inode_operations;
3392 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003393 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003394 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003395 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003396 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003397 init_special_inode(inode, inode->i_mode, rdev);
3398 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003399 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003400
3401 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003402 return;
3403
3404make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003405 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003406 make_bad_inode(inode);
3407}
3408
Chris Masond352ac62008-09-29 15:18:18 -04003409/*
3410 * given a leaf and an inode, copy the inode fields into the leaf
3411 */
Chris Masone02119d2008-09-05 16:13:11 -04003412static void fill_inode_item(struct btrfs_trans_handle *trans,
3413 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003414 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003415 struct inode *inode)
3416{
Liu Bo51fab692012-12-27 09:01:21 +00003417 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003418
Liu Bo51fab692012-12-27 09:01:21 +00003419 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003420
Liu Bo51fab692012-12-27 09:01:21 +00003421 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3422 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3423 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3424 &token);
3425 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3426 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003427
Liu Bo51fab692012-12-27 09:01:21 +00003428 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3429 inode->i_atime.tv_sec, &token);
3430 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3431 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003432
Liu Bo51fab692012-12-27 09:01:21 +00003433 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3434 inode->i_mtime.tv_sec, &token);
3435 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3436 inode->i_mtime.tv_nsec, &token);
3437
3438 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3439 inode->i_ctime.tv_sec, &token);
3440 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3441 inode->i_ctime.tv_nsec, &token);
3442
3443 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3444 &token);
3445 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3446 &token);
3447 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3448 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3449 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3450 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3451 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003452}
3453
Chris Masond352ac62008-09-29 15:18:18 -04003454/*
3455 * copy everything in the in-memory inode into the btree.
3456 */
Chris Mason21151332011-11-10 20:39:08 -05003457static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003458 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003459{
3460 struct btrfs_inode_item *inode_item;
3461 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003462 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003463 int ret;
3464
3465 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003466 if (!path)
3467 return -ENOMEM;
3468
Chris Masonb9473432009-03-13 11:00:37 -04003469 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003470 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3471 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003472 if (ret) {
3473 if (ret > 0)
3474 ret = -ENOENT;
3475 goto failed;
3476 }
3477
Chris Masonb4ce94d2009-02-04 09:25:08 -05003478 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003479 leaf = path->nodes[0];
3480 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003481 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003482
Chris Masone02119d2008-09-05 16:13:11 -04003483 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003484 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003485 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003486 ret = 0;
3487failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003488 btrfs_free_path(path);
3489 return ret;
3490}
3491
Chris Masond352ac62008-09-29 15:18:18 -04003492/*
Chris Mason21151332011-11-10 20:39:08 -05003493 * copy everything in the in-memory inode into the btree.
3494 */
3495noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3496 struct btrfs_root *root, struct inode *inode)
3497{
3498 int ret;
3499
3500 /*
3501 * If the inode is a free space inode, we can deadlock during commit
3502 * if we put it into the delayed code.
3503 *
3504 * The data relocation inode should also be directly updated
3505 * without delay
3506 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003507 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003508 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003509 btrfs_update_root_times(trans, root);
3510
Chris Mason21151332011-11-10 20:39:08 -05003511 ret = btrfs_delayed_update_inode(trans, root, inode);
3512 if (!ret)
3513 btrfs_set_inode_last_trans(trans, inode);
3514 return ret;
3515 }
3516
3517 return btrfs_update_inode_item(trans, root, inode);
3518}
3519
Josef Bacikbe6aef62012-10-22 15:43:12 -04003520noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3521 struct btrfs_root *root,
3522 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003523{
3524 int ret;
3525
3526 ret = btrfs_update_inode(trans, root, inode);
3527 if (ret == -ENOSPC)
3528 return btrfs_update_inode_item(trans, root, inode);
3529 return ret;
3530}
3531
3532/*
Chris Masond352ac62008-09-29 15:18:18 -04003533 * unlink helper that gets used here in inode.c and in the tree logging
3534 * recovery code. It remove a link in a directory with a given name, and
3535 * also drops the back refs in the inode to the directory
3536 */
Al Viro92986792011-03-04 17:14:37 +00003537static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3538 struct btrfs_root *root,
3539 struct inode *dir, struct inode *inode,
3540 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003541{
3542 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003543 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003544 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003545 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003546 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003547 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003548 u64 ino = btrfs_ino(inode);
3549 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003550
3551 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003552 if (!path) {
3553 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003554 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003555 }
3556
Chris Masonb9473432009-03-13 11:00:37 -04003557 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003558 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003559 name, name_len, -1);
3560 if (IS_ERR(di)) {
3561 ret = PTR_ERR(di);
3562 goto err;
3563 }
3564 if (!di) {
3565 ret = -ENOENT;
3566 goto err;
3567 }
Chris Mason5f39d392007-10-15 16:14:19 -04003568 leaf = path->nodes[0];
3569 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003570 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003571 if (ret)
3572 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003573 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003574
Li Zefan33345d012011-04-20 10:31:50 +08003575 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3576 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003577 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05003578 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08003579 "inode %llu parent %llu\n", name_len, name,
3580 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003581 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003582 goto err;
3583 }
3584
Miao Xie16cdcec2011-04-22 18:12:22 +08003585 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003586 if (ret) {
3587 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003588 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003589 }
Chris Mason39279cc2007-06-12 06:35:45 -04003590
Chris Masone02119d2008-09-05 16:13:11 -04003591 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003592 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003593 if (ret != 0 && ret != -ENOENT) {
3594 btrfs_abort_transaction(trans, root, ret);
3595 goto err;
3596 }
Chris Masone02119d2008-09-05 16:13:11 -04003597
3598 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3599 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003600 if (ret == -ENOENT)
3601 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003602err:
3603 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003604 if (ret)
3605 goto out;
3606
3607 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003608 inode_inc_iversion(inode);
3609 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003610 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003611 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003612out:
Chris Mason39279cc2007-06-12 06:35:45 -04003613 return ret;
3614}
3615
Al Viro92986792011-03-04 17:14:37 +00003616int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3617 struct btrfs_root *root,
3618 struct inode *dir, struct inode *inode,
3619 const char *name, int name_len)
3620{
3621 int ret;
3622 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3623 if (!ret) {
3624 btrfs_drop_nlink(inode);
3625 ret = btrfs_update_inode(trans, root, inode);
3626 }
3627 return ret;
3628}
3629
3630
Yan, Zhenga22285a2010-05-16 10:48:46 -04003631/* helper to check if there is any shared block in the path */
3632static int check_path_shared(struct btrfs_root *root,
3633 struct btrfs_path *path)
3634{
3635 struct extent_buffer *eb;
3636 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00003637 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003638
3639 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003640 int ret;
3641
Yan, Zhenga22285a2010-05-16 10:48:46 -04003642 if (!path->nodes[level])
3643 break;
3644 eb = path->nodes[level];
3645 if (!btrfs_block_can_be_shared(root, eb))
3646 continue;
3647 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
3648 &refs, NULL);
3649 if (refs > 1)
3650 return 1;
3651 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003652 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003653}
3654
3655/*
3656 * helper to start transaction for unlink and rmdir.
3657 *
3658 * unlink and rmdir are special in btrfs, they do not always free space.
3659 * so in enospc case, we should make sure they will free space before
3660 * allowing them to use the global metadata reservation.
3661 */
3662static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3663 struct dentry *dentry)
3664{
3665 struct btrfs_trans_handle *trans;
3666 struct btrfs_root *root = BTRFS_I(dir)->root;
3667 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668 struct btrfs_dir_item *di;
3669 struct inode *inode = dentry->d_inode;
3670 u64 index;
3671 int check_link = 1;
3672 int err = -ENOSPC;
3673 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003674 u64 ino = btrfs_ino(inode);
3675 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003676
Josef Bacike70bea52011-10-11 14:18:24 -04003677 /*
3678 * 1 for the possible orphan item
3679 * 1 for the dir item
3680 * 1 for the dir index
3681 * 1 for the inode ref
3682 * 1 for the inode ref in the tree log
3683 * 2 for the dir entries in the log
3684 * 1 for the inode
3685 */
3686 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003687 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3688 return trans;
3689
Li Zefan33345d012011-04-20 10:31:50 +08003690 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003691 return ERR_PTR(-ENOSPC);
3692
3693 /* check if there is someone else holds reference */
3694 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3695 return ERR_PTR(-ENOSPC);
3696
3697 if (atomic_read(&inode->i_count) > 2)
3698 return ERR_PTR(-ENOSPC);
3699
3700 if (xchg(&root->fs_info->enospc_unlink, 1))
3701 return ERR_PTR(-ENOSPC);
3702
3703 path = btrfs_alloc_path();
3704 if (!path) {
3705 root->fs_info->enospc_unlink = 0;
3706 return ERR_PTR(-ENOMEM);
3707 }
3708
Josef Bacik3880a1b2011-10-14 14:46:51 -04003709 /* 1 for the orphan item */
3710 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003711 if (IS_ERR(trans)) {
3712 btrfs_free_path(path);
3713 root->fs_info->enospc_unlink = 0;
3714 return trans;
3715 }
3716
3717 path->skip_locking = 1;
3718 path->search_commit_root = 1;
3719
3720 ret = btrfs_lookup_inode(trans, root, path,
3721 &BTRFS_I(dir)->location, 0);
3722 if (ret < 0) {
3723 err = ret;
3724 goto out;
3725 }
3726 if (ret == 0) {
3727 if (check_path_shared(root, path))
3728 goto out;
3729 } else {
3730 check_link = 0;
3731 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003732 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003733
3734 ret = btrfs_lookup_inode(trans, root, path,
3735 &BTRFS_I(inode)->location, 0);
3736 if (ret < 0) {
3737 err = ret;
3738 goto out;
3739 }
3740 if (ret == 0) {
3741 if (check_path_shared(root, path))
3742 goto out;
3743 } else {
3744 check_link = 0;
3745 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003746 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003747
3748 if (ret == 0 && S_ISREG(inode->i_mode)) {
3749 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003750 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003751 if (ret < 0) {
3752 err = ret;
3753 goto out;
3754 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003755 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003756 if (check_path_shared(root, path))
3757 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003758 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003759 }
3760
3761 if (!check_link) {
3762 err = 0;
3763 goto out;
3764 }
3765
Li Zefan33345d012011-04-20 10:31:50 +08003766 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003767 dentry->d_name.name, dentry->d_name.len, 0);
3768 if (IS_ERR(di)) {
3769 err = PTR_ERR(di);
3770 goto out;
3771 }
3772 if (di) {
3773 if (check_path_shared(root, path))
3774 goto out;
3775 } else {
3776 err = 0;
3777 goto out;
3778 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003779 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003780
Mark Fashehf1863732012-08-08 11:32:27 -07003781 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3782 dentry->d_name.len, ino, dir_ino, 0,
3783 &index);
3784 if (ret) {
3785 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003786 goto out;
3787 }
Mark Fashehf1863732012-08-08 11:32:27 -07003788
Yan, Zhenga22285a2010-05-16 10:48:46 -04003789 if (check_path_shared(root, path))
3790 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003791
David Sterbab3b4aa72011-04-21 01:20:15 +02003792 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003793
Miao Xie16cdcec2011-04-22 18:12:22 +08003794 /*
3795 * This is a commit root search, if we can lookup inode item and other
3796 * relative items in the commit root, it means the transaction of
3797 * dir/file creation has been committed, and the dir index item that we
3798 * delay to insert has also been inserted into the commit root. So
3799 * we needn't worry about the delayed insertion of the dir index item
3800 * here.
3801 */
Li Zefan33345d012011-04-20 10:31:50 +08003802 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003803 dentry->d_name.name, dentry->d_name.len, 0);
3804 if (IS_ERR(di)) {
3805 err = PTR_ERR(di);
3806 goto out;
3807 }
3808 BUG_ON(ret == -ENOENT);
3809 if (check_path_shared(root, path))
3810 goto out;
3811
3812 err = 0;
3813out:
3814 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003815 /* Migrate the orphan reservation over */
3816 if (!err)
3817 err = btrfs_block_rsv_migrate(trans->block_rsv,
3818 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003819 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003820
Yan, Zhenga22285a2010-05-16 10:48:46 -04003821 if (err) {
3822 btrfs_end_transaction(trans, root);
3823 root->fs_info->enospc_unlink = 0;
3824 return ERR_PTR(err);
3825 }
3826
3827 trans->block_rsv = &root->fs_info->global_block_rsv;
3828 return trans;
3829}
3830
3831static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3832 struct btrfs_root *root)
3833{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003834 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003835 btrfs_block_rsv_release(root, trans->block_rsv,
3836 trans->bytes_reserved);
3837 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003838 BUG_ON(!root->fs_info->enospc_unlink);
3839 root->fs_info->enospc_unlink = 0;
3840 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003841 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003842}
3843
Chris Mason39279cc2007-06-12 06:35:45 -04003844static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3845{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003846 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003847 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003848 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003849 int ret;
3850
Yan, Zhenga22285a2010-05-16 10:48:46 -04003851 trans = __unlink_start_trans(dir, dentry);
3852 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003853 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003854
Chris Mason12fcfd22009-03-24 10:24:20 -04003855 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3856
Chris Masone02119d2008-09-05 16:13:11 -04003857 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3858 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003859 if (ret)
3860 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003861
Yan, Zhenga22285a2010-05-16 10:48:46 -04003862 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003863 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003864 if (ret)
3865 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003866 }
Josef Bacik7b128762008-07-24 12:17:14 -04003867
Tsutomu Itohb5324022011-07-19 07:27:20 +00003868out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003869 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003870 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003871 return ret;
3872}
3873
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003874int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3875 struct btrfs_root *root,
3876 struct inode *dir, u64 objectid,
3877 const char *name, int name_len)
3878{
3879 struct btrfs_path *path;
3880 struct extent_buffer *leaf;
3881 struct btrfs_dir_item *di;
3882 struct btrfs_key key;
3883 u64 index;
3884 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003885 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003886
3887 path = btrfs_alloc_path();
3888 if (!path)
3889 return -ENOMEM;
3890
Li Zefan33345d012011-04-20 10:31:50 +08003891 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003892 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003893 if (IS_ERR_OR_NULL(di)) {
3894 if (!di)
3895 ret = -ENOENT;
3896 else
3897 ret = PTR_ERR(di);
3898 goto out;
3899 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003900
3901 leaf = path->nodes[0];
3902 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3903 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3904 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003905 if (ret) {
3906 btrfs_abort_transaction(trans, root, ret);
3907 goto out;
3908 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003909 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003910
3911 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3912 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003913 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003914 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003915 if (ret != -ENOENT) {
3916 btrfs_abort_transaction(trans, root, ret);
3917 goto out;
3918 }
Li Zefan33345d012011-04-20 10:31:50 +08003919 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003920 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003921 if (IS_ERR_OR_NULL(di)) {
3922 if (!di)
3923 ret = -ENOENT;
3924 else
3925 ret = PTR_ERR(di);
3926 btrfs_abort_transaction(trans, root, ret);
3927 goto out;
3928 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003929
3930 leaf = path->nodes[0];
3931 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003932 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003933 index = key.offset;
3934 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003935 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003936
Miao Xie16cdcec2011-04-22 18:12:22 +08003937 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003938 if (ret) {
3939 btrfs_abort_transaction(trans, root, ret);
3940 goto out;
3941 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003942
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003943 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003944 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003945 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003946 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003947 if (ret)
3948 btrfs_abort_transaction(trans, root, ret);
3949out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003950 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003951 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003952}
3953
Chris Mason39279cc2007-06-12 06:35:45 -04003954static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3955{
3956 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003957 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003958 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003959 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003960
David Sterbab3ae2442012-09-13 16:04:34 -06003961 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003962 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003963 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3964 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003965
Yan, Zhenga22285a2010-05-16 10:48:46 -04003966 trans = __unlink_start_trans(dir, dentry);
3967 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003968 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003969
Li Zefan33345d012011-04-20 10:31:50 +08003970 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003971 err = btrfs_unlink_subvol(trans, root, dir,
3972 BTRFS_I(inode)->location.objectid,
3973 dentry->d_name.name,
3974 dentry->d_name.len);
3975 goto out;
3976 }
3977
Josef Bacik7b128762008-07-24 12:17:14 -04003978 err = btrfs_orphan_add(trans, inode);
3979 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003980 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003981
Chris Mason39279cc2007-06-12 06:35:45 -04003982 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003983 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3984 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003985 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003986 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003987out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003988 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003989 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003990
Chris Mason39279cc2007-06-12 06:35:45 -04003991 return err;
3992}
3993
Chris Mason323ac952008-10-01 19:05:46 -04003994/*
Chris Mason39279cc2007-06-12 06:35:45 -04003995 * this can truncate away extent items, csum items and directory items.
3996 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003997 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003998 *
3999 * csum items that cross the new i_size are truncated to the new size
4000 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004001 *
4002 * min_type is the minimum key type to truncate down to. If set to 0, this
4003 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004004 */
Yan, Zheng80825102009-11-12 09:35:36 +00004005int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4006 struct btrfs_root *root,
4007 struct inode *inode,
4008 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004009{
Chris Mason39279cc2007-06-12 06:35:45 -04004010 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004011 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004012 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004013 struct btrfs_key key;
4014 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004015 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004016 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004017 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004018 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004019 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004020 int found_extent;
4021 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004022 int pending_del_nr = 0;
4023 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004024 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004025 int ret;
4026 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004027 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004028
4029 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004030
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004031 path = btrfs_alloc_path();
4032 if (!path)
4033 return -ENOMEM;
4034 path->reada = -1;
4035
Josef Bacik5dc562c2012-08-17 13:14:17 -04004036 /*
4037 * We want to drop from the next block forward in case this new size is
4038 * not block aligned since we will be keeping the last block of the
4039 * extent just the way it is.
4040 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004041 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004042 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4043 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004044
Miao Xie16cdcec2011-04-22 18:12:22 +08004045 /*
4046 * This function is also used to drop the items in the log tree before
4047 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4048 * it is used to drop the loged items. So we shouldn't kill the delayed
4049 * items.
4050 */
4051 if (min_type == 0 && root == BTRFS_I(inode)->root)
4052 btrfs_kill_delayed_inode_items(inode);
4053
Li Zefan33345d012011-04-20 10:31:50 +08004054 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004055 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004056 key.type = (u8)-1;
4057
Chris Mason85e21ba2008-01-29 15:11:36 -05004058search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004059 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004060 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004061 if (ret < 0) {
4062 err = ret;
4063 goto out;
4064 }
Chris Masond3977122009-01-05 21:25:51 -05004065
Chris Mason85e21ba2008-01-29 15:11:36 -05004066 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004067 /* there are no items in the tree for us to truncate, we're
4068 * done
4069 */
Yan, Zheng80825102009-11-12 09:35:36 +00004070 if (path->slots[0] == 0)
4071 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004072 path->slots[0]--;
4073 }
4074
Chris Masond3977122009-01-05 21:25:51 -05004075 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004076 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004077 leaf = path->nodes[0];
4078 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4079 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004080
Li Zefan33345d012011-04-20 10:31:50 +08004081 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004082 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004083
Chris Mason85e21ba2008-01-29 15:11:36 -05004084 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004085 break;
4086
Chris Mason5f39d392007-10-15 16:14:19 -04004087 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004088 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004089 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004090 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004091 extent_type = btrfs_file_extent_type(leaf, fi);
4092 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004093 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004094 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004095 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004096 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004097 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004098 }
Yan008630c2007-11-07 13:31:09 -05004099 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004100 }
Yan, Zheng80825102009-11-12 09:35:36 +00004101 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004102 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004103 } else {
4104 if (item_end < new_size)
4105 break;
4106 if (found_key.offset >= new_size)
4107 del_item = 1;
4108 else
4109 del_item = 0;
4110 }
Chris Mason39279cc2007-06-12 06:35:45 -04004111 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004112 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004113 if (found_type != BTRFS_EXTENT_DATA_KEY)
4114 goto delete;
4115
4116 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004117 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004118 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004119 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004120 u64 orig_num_bytes =
4121 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004122 extent_num_bytes = ALIGN(new_size -
4123 found_key.offset,
4124 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004125 btrfs_set_file_extent_num_bytes(leaf, fi,
4126 extent_num_bytes);
4127 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004128 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004129 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004130 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004131 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004132 } else {
Chris Masondb945352007-10-15 16:15:53 -04004133 extent_num_bytes =
4134 btrfs_file_extent_disk_num_bytes(leaf,
4135 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004136 extent_offset = found_key.offset -
4137 btrfs_file_extent_offset(leaf, fi);
4138
Chris Mason39279cc2007-06-12 06:35:45 -04004139 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004140 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004141 if (extent_start != 0) {
4142 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004143 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004144 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004145 }
4146 }
Chris Mason90692182008-02-08 13:49:28 -05004147 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004148 /*
4149 * we can't truncate inline items that have had
4150 * special encodings
4151 */
4152 if (!del_item &&
4153 btrfs_file_extent_compression(leaf, fi) == 0 &&
4154 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4155 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004156 u32 size = new_size - found_key.offset;
4157
4158 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004159 inode_sub_bytes(inode, item_end + 1 -
4160 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004161 }
4162 size =
4163 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004164 btrfs_truncate_item(trans, root, path,
4165 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004166 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004167 inode_sub_bytes(inode, item_end + 1 -
4168 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004169 }
Chris Mason39279cc2007-06-12 06:35:45 -04004170 }
Chris Mason179e29e2007-11-01 11:28:41 -04004171delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004172 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004173 if (!pending_del_nr) {
4174 /* no pending yet, add ourselves */
4175 pending_del_slot = path->slots[0];
4176 pending_del_nr = 1;
4177 } else if (pending_del_nr &&
4178 path->slots[0] + 1 == pending_del_slot) {
4179 /* hop on the pending chunk */
4180 pending_del_nr++;
4181 pending_del_slot = path->slots[0];
4182 } else {
Chris Masond3977122009-01-05 21:25:51 -05004183 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004184 }
Chris Mason39279cc2007-06-12 06:35:45 -04004185 } else {
4186 break;
4187 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004188 if (found_extent && (root->ref_cows ||
4189 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004190 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004191 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004192 extent_num_bytes, 0,
4193 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004194 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004195 BUG_ON(ret);
4196 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004197
Yan, Zheng80825102009-11-12 09:35:36 +00004198 if (found_type == BTRFS_INODE_ITEM_KEY)
4199 break;
4200
4201 if (path->slots[0] == 0 ||
4202 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004203 if (pending_del_nr) {
4204 ret = btrfs_del_items(trans, root, path,
4205 pending_del_slot,
4206 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004207 if (ret) {
4208 btrfs_abort_transaction(trans,
4209 root, ret);
4210 goto error;
4211 }
Yan, Zheng80825102009-11-12 09:35:36 +00004212 pending_del_nr = 0;
4213 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004214 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004215 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004216 } else {
4217 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004218 }
Chris Mason39279cc2007-06-12 06:35:45 -04004219 }
Yan, Zheng80825102009-11-12 09:35:36 +00004220out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004221 if (pending_del_nr) {
4222 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4223 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004224 if (ret)
4225 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004226 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004227error:
Chris Mason39279cc2007-06-12 06:35:45 -04004228 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004229 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004230}
4231
Chris Masona52d9a82007-08-27 16:49:44 -04004232/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004233 * btrfs_truncate_page - read, zero a chunk and write a page
4234 * @inode - inode that we're zeroing
4235 * @from - the offset to start zeroing
4236 * @len - the length to zero, 0 to zero the entire range respective to the
4237 * offset
4238 * @front - zero up to the offset instead of from the offset on
4239 *
4240 * This will find the page for the "from" offset and cow the page and zero the
4241 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004242 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004243int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4244 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004245{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004246 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004247 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004248 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4249 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004250 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004251 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004252 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004253 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4254 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4255 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004256 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004257 int ret = 0;
4258 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004259 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004260
Josef Bacik2aaa6652012-08-29 14:27:18 -04004261 if ((offset & (blocksize - 1)) == 0 &&
4262 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004263 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004264 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004265 if (ret)
4266 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004267
Chris Mason211c17f2008-05-15 09:13:45 -04004268again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004269 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004270 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004271 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004272 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004273 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004274 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004275
4276 page_start = page_offset(page);
4277 page_end = page_start + PAGE_CACHE_SIZE - 1;
4278
Chris Masona52d9a82007-08-27 16:49:44 -04004279 if (!PageUptodate(page)) {
4280 ret = btrfs_readpage(NULL, page);
4281 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004282 if (page->mapping != mapping) {
4283 unlock_page(page);
4284 page_cache_release(page);
4285 goto again;
4286 }
Chris Masona52d9a82007-08-27 16:49:44 -04004287 if (!PageUptodate(page)) {
4288 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004289 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004290 }
4291 }
Chris Mason211c17f2008-05-15 09:13:45 -04004292 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004293
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004294 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004295 set_page_extent_mapped(page);
4296
4297 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4298 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004299 unlock_extent_cached(io_tree, page_start, page_end,
4300 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004301 unlock_page(page);
4302 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004303 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004304 btrfs_put_ordered_extent(ordered);
4305 goto again;
4306 }
4307
Josef Bacik2ac55d42010-02-03 19:33:23 +00004308 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004309 EXTENT_DIRTY | EXTENT_DELALLOC |
4310 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004311 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004312
Josef Bacik2ac55d42010-02-03 19:33:23 +00004313 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4314 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004315 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004316 unlock_extent_cached(io_tree, page_start, page_end,
4317 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004318 goto out_unlock;
4319 }
4320
Chris Masone6dcd2d2008-07-17 12:53:50 -04004321 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004322 if (!len)
4323 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004324 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004325 if (front)
4326 memset(kaddr, 0, offset);
4327 else
4328 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004329 flush_dcache_page(page);
4330 kunmap(page);
4331 }
Chris Mason247e7432008-07-17 12:53:51 -04004332 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004333 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004334 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4335 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004336
Chris Mason89642222008-07-24 09:41:53 -04004337out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004338 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004339 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004340 unlock_page(page);
4341 page_cache_release(page);
4342out:
4343 return ret;
4344}
4345
Josef Bacik695a0d02011-03-04 15:46:53 -05004346/*
4347 * This function puts in dummy file extents for the area we're creating a hole
4348 * for. So if we are truncating this file to a larger size we need to insert
4349 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4350 * the range between oldsize and size
4351 */
Josef Bacika41ad392011-01-31 15:30:16 -05004352int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004353{
4354 struct btrfs_trans_handle *trans;
4355 struct btrfs_root *root = BTRFS_I(inode)->root;
4356 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004357 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004358 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004359 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004360 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4361 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004362 u64 last_byte;
4363 u64 cur_offset;
4364 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004365 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004366
4367 if (size <= hole_start)
4368 return 0;
4369
Yan Zheng9036c102008-10-30 14:19:41 -04004370 while (1) {
4371 struct btrfs_ordered_extent *ordered;
4372 btrfs_wait_ordered_range(inode, hole_start,
4373 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004374 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004375 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004376 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4377 if (!ordered)
4378 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004379 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4380 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004381 btrfs_put_ordered_extent(ordered);
4382 }
4383
Yan Zheng9036c102008-10-30 14:19:41 -04004384 cur_offset = hole_start;
4385 while (1) {
4386 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4387 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004388 if (IS_ERR(em)) {
4389 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004390 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004391 break;
4392 }
Yan Zheng9036c102008-10-30 14:19:41 -04004393 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004394 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004395 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004396 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004397 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004398
Miao Xie36423202011-12-14 20:12:02 -05004399 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004400 if (IS_ERR(trans)) {
4401 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004402 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004403 }
Yan, Zheng80825102009-11-12 09:35:36 +00004404
Josef Bacik5dc562c2012-08-17 13:14:17 -04004405 err = btrfs_drop_extents(trans, root, inode,
4406 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004407 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004408 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004409 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004410 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004411 break;
Miao Xie5b397372011-09-11 10:52:24 -04004412 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004413
Yan Zheng9036c102008-10-30 14:19:41 -04004414 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004415 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004416 0, hole_size, 0, hole_size,
4417 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004418 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004419 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004420 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004421 break;
Miao Xie5b397372011-09-11 10:52:24 -04004422 }
Yan, Zheng80825102009-11-12 09:35:36 +00004423
Josef Bacik5dc562c2012-08-17 13:14:17 -04004424 btrfs_drop_extent_cache(inode, cur_offset,
4425 cur_offset + hole_size - 1, 0);
4426 hole_em = alloc_extent_map();
4427 if (!hole_em) {
4428 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4429 &BTRFS_I(inode)->runtime_flags);
4430 goto next;
4431 }
4432 hole_em->start = cur_offset;
4433 hole_em->len = hole_size;
4434 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004435
Josef Bacik5dc562c2012-08-17 13:14:17 -04004436 hole_em->block_start = EXTENT_MAP_HOLE;
4437 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004438 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004439 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4440 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4441 hole_em->generation = trans->transid;
4442
4443 while (1) {
4444 write_lock(&em_tree->lock);
4445 err = add_extent_mapping(em_tree, hole_em);
4446 if (!err)
4447 list_move(&hole_em->list,
4448 &em_tree->modified_extents);
4449 write_unlock(&em_tree->lock);
4450 if (err != -EEXIST)
4451 break;
4452 btrfs_drop_extent_cache(inode, cur_offset,
4453 cur_offset +
4454 hole_size - 1, 0);
4455 }
4456 free_extent_map(hole_em);
4457next:
Miao Xie36423202011-12-14 20:12:02 -05004458 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004459 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004460 }
4461 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004462 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004463 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004464 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004465 break;
4466 }
4467
Yan, Zhenga22285a2010-05-16 10:48:46 -04004468 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004469 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4470 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004471 return err;
4472}
4473
Eric Sandeen3972f262013-01-12 02:57:22 +00004474static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004475{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004476 struct btrfs_root *root = BTRFS_I(inode)->root;
4477 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004478 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004479 loff_t newsize = attr->ia_size;
4480 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004481 int ret;
4482
Josef Bacika41ad392011-01-31 15:30:16 -05004483 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004484 return 0;
4485
Eric Sandeen3972f262013-01-12 02:57:22 +00004486 /*
4487 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4488 * special case where we need to update the times despite not having
4489 * these flags set. For all other operations the VFS set these flags
4490 * explicitly if it wants a timestamp update.
4491 */
4492 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4493 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4494
Josef Bacika41ad392011-01-31 15:30:16 -05004495 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004496 truncate_pagecache(inode, oldsize, newsize);
4497 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004498 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004499 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004500
Miao Xief4a2f4c2011-12-14 20:12:01 -05004501 trans = btrfs_start_transaction(root, 1);
4502 if (IS_ERR(trans))
4503 return PTR_ERR(trans);
4504
4505 i_size_write(inode, newsize);
4506 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4507 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004508 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004509 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004510
Josef Bacika41ad392011-01-31 15:30:16 -05004511 /*
4512 * We're truncating a file that used to have good data down to
4513 * zero. Make sure it gets into the ordered flush list so that
4514 * any new writes get down to disk quickly.
4515 */
4516 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004517 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4518 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004519
Josef Bacikf3fe8202013-01-07 17:03:21 -05004520 /*
4521 * 1 for the orphan item we're going to add
4522 * 1 for the orphan item deletion.
4523 */
4524 trans = btrfs_start_transaction(root, 2);
4525 if (IS_ERR(trans))
4526 return PTR_ERR(trans);
4527
4528 /*
4529 * We need to do this in case we fail at _any_ point during the
4530 * actual truncate. Once we do the truncate_setsize we could
4531 * invalidate pages which forces any outstanding ordered io to
4532 * be instantly completed which will give us extents that need
4533 * to be truncated. If we fail to get an orphan inode down we
4534 * could have left over extents that were never meant to live,
4535 * so we need to garuntee from this point on that everything
4536 * will be consistent.
4537 */
4538 ret = btrfs_orphan_add(trans, inode);
4539 btrfs_end_transaction(trans, root);
4540 if (ret)
4541 return ret;
4542
Josef Bacika41ad392011-01-31 15:30:16 -05004543 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4544 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004545
4546 /* Disable nonlocked read DIO to avoid the end less truncate */
4547 btrfs_inode_block_unlocked_dio(inode);
4548 inode_dio_wait(inode);
4549 btrfs_inode_resume_unlocked_dio(inode);
4550
Josef Bacika41ad392011-01-31 15:30:16 -05004551 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004552 if (ret && inode->i_nlink)
4553 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004554 }
4555
Josef Bacika41ad392011-01-31 15:30:16 -05004556 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004557}
4558
Chris Mason39279cc2007-06-12 06:35:45 -04004559static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4560{
4561 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004562 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004563 int err;
4564
Li Zefanb83cc962010-12-20 16:04:08 +08004565 if (btrfs_root_readonly(root))
4566 return -EROFS;
4567
Chris Mason39279cc2007-06-12 06:35:45 -04004568 err = inode_change_ok(inode, attr);
4569 if (err)
4570 return err;
4571
Chris Mason5a3f23d2009-03-31 13:27:11 -04004572 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004573 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004574 if (err)
4575 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004576 }
Yan Zheng9036c102008-10-30 14:19:41 -04004577
Christoph Hellwig10257742010-06-04 11:30:02 +02004578 if (attr->ia_valid) {
4579 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004580 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004581 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004582
Josef Bacik22c44fe2011-11-30 10:45:38 -05004583 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004584 err = btrfs_acl_chmod(inode);
4585 }
4586
Chris Mason39279cc2007-06-12 06:35:45 -04004587 return err;
4588}
Chris Mason61295eb2008-01-14 16:24:38 -05004589
Al Virobd555972010-06-07 11:35:40 -04004590void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004591{
4592 struct btrfs_trans_handle *trans;
4593 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004594 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004595 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004596 int ret;
4597
liubo1abe9b82011-03-24 11:18:59 +00004598 trace_btrfs_inode_evict(inode);
4599
Chris Mason39279cc2007-06-12 06:35:45 -04004600 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004601 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004602 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004603 goto no_delete;
4604
Chris Mason39279cc2007-06-12 06:35:45 -04004605 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004606 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004607 goto no_delete;
4608 }
Al Virobd555972010-06-07 11:35:40 -04004609 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004610 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004611
Yan, Zhengc71bf092009-11-12 09:34:40 +00004612 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004613 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004614 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004615 goto no_delete;
4616 }
4617
Yan, Zheng76dda932009-09-21 16:00:26 -04004618 if (inode->i_nlink > 0) {
4619 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4620 goto no_delete;
4621 }
4622
Miao Xie0e8c36a2012-12-19 06:59:51 +00004623 ret = btrfs_commit_inode_delayed_inode(inode);
4624 if (ret) {
4625 btrfs_orphan_del(NULL, inode);
4626 goto no_delete;
4627 }
4628
Miao Xie66d8f3d2012-09-06 04:02:28 -06004629 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004630 if (!rsv) {
4631 btrfs_orphan_del(NULL, inode);
4632 goto no_delete;
4633 }
Josef Bacik4a338542011-08-29 11:01:31 -04004634 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004635 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004636 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004637
Chris Masondbe674a2008-07-17 12:54:05 -04004638 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004639
Josef Bacik4289a662011-08-05 13:22:24 -04004640 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004641 * This is a bit simpler than btrfs_truncate since we've already
4642 * reserved our space for our orphan item in the unlink, so we just
4643 * need to reserve some slack space in case we add bytes and update
4644 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004645 */
Yan, Zheng80825102009-11-12 09:35:36 +00004646 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004647 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4648 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004649
Josef Bacik726c35f2011-09-26 15:46:06 -04004650 /*
4651 * Try and steal from the global reserve since we will
4652 * likely not use this space anyway, we want to try as
4653 * hard as possible to get this to work.
4654 */
4655 if (ret)
4656 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4657
Yan, Zhengd68fc572010-05-16 10:49:58 -04004658 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04004659 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04004660 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004661 btrfs_orphan_del(NULL, inode);
4662 btrfs_free_block_rsv(root, rsv);
4663 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004664 }
4665
Miao Xie0e8c36a2012-12-19 06:59:51 +00004666 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004667 if (IS_ERR(trans)) {
4668 btrfs_orphan_del(NULL, inode);
4669 btrfs_free_block_rsv(root, rsv);
4670 goto no_delete;
4671 }
4672
4673 trans->block_rsv = rsv;
4674
Yan, Zhengd68fc572010-05-16 10:49:58 -04004675 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004676 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004677 break;
4678
Miao Xie8407aa42012-09-07 01:43:32 -06004679 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004680 btrfs_end_transaction(trans, root);
4681 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004682 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004683 }
4684
Josef Bacik4289a662011-08-05 13:22:24 -04004685 btrfs_free_block_rsv(root, rsv);
4686
Yan, Zheng80825102009-11-12 09:35:36 +00004687 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004688 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004689 ret = btrfs_orphan_del(trans, inode);
4690 BUG_ON(ret);
4691 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004692
Josef Bacik4289a662011-08-05 13:22:24 -04004693 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004694 if (!(root == root->fs_info->tree_root ||
4695 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004696 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004697
Chris Mason54aa1f42007-06-22 14:16:25 -04004698 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004699 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004700no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004701 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004702 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004703}
4704
4705/*
4706 * this returns the key found in the dir entry in the location pointer.
4707 * If no dir entries were found, location->objectid is 0.
4708 */
4709static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4710 struct btrfs_key *location)
4711{
4712 const char *name = dentry->d_name.name;
4713 int namelen = dentry->d_name.len;
4714 struct btrfs_dir_item *di;
4715 struct btrfs_path *path;
4716 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004717 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004718
4719 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004720 if (!path)
4721 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004722
Li Zefan33345d012011-04-20 10:31:50 +08004723 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004724 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004725 if (IS_ERR(di))
4726 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004727
David Sterbac7040052011-04-19 18:00:01 +02004728 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004729 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004730
Chris Mason5f39d392007-10-15 16:14:19 -04004731 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004732out:
Chris Mason39279cc2007-06-12 06:35:45 -04004733 btrfs_free_path(path);
4734 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004735out_err:
4736 location->objectid = 0;
4737 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004738}
4739
4740/*
4741 * when we hit a tree root in a directory, the btrfs part of the inode
4742 * needs to be changed to reflect the root directory of the tree root. This
4743 * is kind of like crossing a mount point.
4744 */
4745static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004746 struct inode *dir,
4747 struct dentry *dentry,
4748 struct btrfs_key *location,
4749 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004750{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004751 struct btrfs_path *path;
4752 struct btrfs_root *new_root;
4753 struct btrfs_root_ref *ref;
4754 struct extent_buffer *leaf;
4755 int ret;
4756 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004757
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004758 path = btrfs_alloc_path();
4759 if (!path) {
4760 err = -ENOMEM;
4761 goto out;
4762 }
Chris Mason39279cc2007-06-12 06:35:45 -04004763
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004764 err = -ENOENT;
4765 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4766 BTRFS_I(dir)->root->root_key.objectid,
4767 location->objectid);
4768 if (ret) {
4769 if (ret < 0)
4770 err = ret;
4771 goto out;
4772 }
Chris Mason39279cc2007-06-12 06:35:45 -04004773
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004774 leaf = path->nodes[0];
4775 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004776 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004777 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4778 goto out;
4779
4780 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4781 (unsigned long)(ref + 1),
4782 dentry->d_name.len);
4783 if (ret)
4784 goto out;
4785
David Sterbab3b4aa72011-04-21 01:20:15 +02004786 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004787
4788 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4789 if (IS_ERR(new_root)) {
4790 err = PTR_ERR(new_root);
4791 goto out;
4792 }
4793
4794 if (btrfs_root_refs(&new_root->root_item) == 0) {
4795 err = -ENOENT;
4796 goto out;
4797 }
4798
4799 *sub_root = new_root;
4800 location->objectid = btrfs_root_dirid(&new_root->root_item);
4801 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004802 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004803 err = 0;
4804out:
4805 btrfs_free_path(path);
4806 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004807}
4808
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004809static void inode_tree_add(struct inode *inode)
4810{
4811 struct btrfs_root *root = BTRFS_I(inode)->root;
4812 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004813 struct rb_node **p;
4814 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004815 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004816again:
4817 p = &root->inode_tree.rb_node;
4818 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004819
Al Viro1d3382cb2010-10-23 15:19:20 -04004820 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004821 return;
4822
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004823 spin_lock(&root->inode_lock);
4824 while (*p) {
4825 parent = *p;
4826 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4827
Li Zefan33345d012011-04-20 10:31:50 +08004828 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004829 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004830 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004831 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004832 else {
4833 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004834 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004835 rb_erase(parent, &root->inode_tree);
4836 RB_CLEAR_NODE(parent);
4837 spin_unlock(&root->inode_lock);
4838 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004839 }
4840 }
4841 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4842 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4843 spin_unlock(&root->inode_lock);
4844}
4845
4846static void inode_tree_del(struct inode *inode)
4847{
4848 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004849 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004850
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004851 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004852 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004853 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004854 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004855 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004856 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004857 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004858
Josef Bacik0af3d002010-06-21 14:48:16 -04004859 /*
4860 * Free space cache has inodes in the tree root, but the tree root has a
4861 * root_refs of 0, so this could end up dropping the tree root as a
4862 * snapshot, so we need the extra !root->fs_info->tree_root check to
4863 * make sure we don't drop it.
4864 */
4865 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4866 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004867 synchronize_srcu(&root->fs_info->subvol_srcu);
4868 spin_lock(&root->inode_lock);
4869 empty = RB_EMPTY_ROOT(&root->inode_tree);
4870 spin_unlock(&root->inode_lock);
4871 if (empty)
4872 btrfs_add_dead_root(root);
4873 }
4874}
4875
Jeff Mahoney143bede2012-03-01 14:56:26 +01004876void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004877{
4878 struct rb_node *node;
4879 struct rb_node *prev;
4880 struct btrfs_inode *entry;
4881 struct inode *inode;
4882 u64 objectid = 0;
4883
4884 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4885
4886 spin_lock(&root->inode_lock);
4887again:
4888 node = root->inode_tree.rb_node;
4889 prev = NULL;
4890 while (node) {
4891 prev = node;
4892 entry = rb_entry(node, struct btrfs_inode, rb_node);
4893
Li Zefan33345d012011-04-20 10:31:50 +08004894 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004895 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004896 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004897 node = node->rb_right;
4898 else
4899 break;
4900 }
4901 if (!node) {
4902 while (prev) {
4903 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004904 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004905 node = prev;
4906 break;
4907 }
4908 prev = rb_next(prev);
4909 }
4910 }
4911 while (node) {
4912 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004913 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004914 inode = igrab(&entry->vfs_inode);
4915 if (inode) {
4916 spin_unlock(&root->inode_lock);
4917 if (atomic_read(&inode->i_count) > 1)
4918 d_prune_aliases(inode);
4919 /*
Al Viro45321ac2010-06-07 13:43:19 -04004920 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004921 * the inode cache when its usage count
4922 * hits zero.
4923 */
4924 iput(inode);
4925 cond_resched();
4926 spin_lock(&root->inode_lock);
4927 goto again;
4928 }
4929
4930 if (cond_resched_lock(&root->inode_lock))
4931 goto again;
4932
4933 node = rb_next(node);
4934 }
4935 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004936}
4937
Chris Masone02119d2008-09-05 16:13:11 -04004938static int btrfs_init_locked_inode(struct inode *inode, void *p)
4939{
4940 struct btrfs_iget_args *args = p;
4941 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004942 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004943 return 0;
4944}
4945
4946static int btrfs_find_actor(struct inode *inode, void *opaque)
4947{
4948 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004949 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004950 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004951}
4952
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004953static struct inode *btrfs_iget_locked(struct super_block *s,
4954 u64 objectid,
4955 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004956{
4957 struct inode *inode;
4958 struct btrfs_iget_args args;
4959 args.ino = objectid;
4960 args.root = root;
4961
4962 inode = iget5_locked(s, objectid, btrfs_find_actor,
4963 btrfs_init_locked_inode,
4964 (void *)&args);
4965 return inode;
4966}
4967
Balaji Rao1a54ef82008-07-21 02:01:04 +05304968/* Get an inode object given its location and corresponding root.
4969 * Returns in *is_new if the inode was read from disk
4970 */
4971struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004972 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304973{
4974 struct inode *inode;
4975
4976 inode = btrfs_iget_locked(s, location->objectid, root);
4977 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004978 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304979
4980 if (inode->i_state & I_NEW) {
4981 BTRFS_I(inode)->root = root;
4982 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4983 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004984 if (!is_bad_inode(inode)) {
4985 inode_tree_add(inode);
4986 unlock_new_inode(inode);
4987 if (new)
4988 *new = 1;
4989 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004990 unlock_new_inode(inode);
4991 iput(inode);
4992 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004993 }
4994 }
4995
Balaji Rao1a54ef82008-07-21 02:01:04 +05304996 return inode;
4997}
4998
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004999static struct inode *new_simple_dir(struct super_block *s,
5000 struct btrfs_key *key,
5001 struct btrfs_root *root)
5002{
5003 struct inode *inode = new_inode(s);
5004
5005 if (!inode)
5006 return ERR_PTR(-ENOMEM);
5007
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005008 BTRFS_I(inode)->root = root;
5009 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005010 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005011
5012 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005013 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005014 inode->i_fop = &simple_dir_operations;
5015 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5016 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5017
5018 return inode;
5019}
5020
Chris Mason3de45862008-11-17 21:02:50 -05005021struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005022{
Chris Masond3977122009-01-05 21:25:51 -05005023 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005024 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005025 struct btrfs_root *sub_root = root;
5026 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005027 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005028 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005029
5030 if (dentry->d_name.len > BTRFS_NAME_LEN)
5031 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005032
Jeff Layton39e3c952012-11-28 11:30:53 -05005033 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005034 if (ret < 0)
5035 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005036
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005037 if (location.objectid == 0)
5038 return NULL;
5039
5040 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005041 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005042 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005043 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005044
5045 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5046
Yan, Zheng76dda932009-09-21 16:00:26 -04005047 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005048 ret = fixup_tree_root_location(root, dir, dentry,
5049 &location, &sub_root);
5050 if (ret < 0) {
5051 if (ret != -ENOENT)
5052 inode = ERR_PTR(ret);
5053 else
5054 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5055 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005056 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005057 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005058 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5059
Julia Lawall34d19ba2011-01-24 19:55:19 +00005060 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005061 down_read(&root->fs_info->cleanup_work_sem);
5062 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005063 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005064 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005065 if (ret)
5066 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005067 }
5068
Chris Mason3de45862008-11-17 21:02:50 -05005069 return inode;
5070}
5071
Nick Pigginfe15ce42011-01-07 17:49:23 +11005072static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005073{
5074 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005075 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005076
Li Zefan848cce02012-02-21 17:04:28 +08005077 if (!inode && !IS_ROOT(dentry))
5078 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005079
Li Zefan848cce02012-02-21 17:04:28 +08005080 if (inode) {
5081 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005082 if (btrfs_root_refs(&root->root_item) == 0)
5083 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005084
5085 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5086 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005087 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005088 return 0;
5089}
5090
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005091static void btrfs_dentry_release(struct dentry *dentry)
5092{
5093 if (dentry->d_fsdata)
5094 kfree(dentry->d_fsdata);
5095}
5096
Chris Mason3de45862008-11-17 21:02:50 -05005097static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005098 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005099{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005100 struct dentry *ret;
5101
5102 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005103 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005104}
5105
Miao Xie16cdcec2011-04-22 18:12:22 +08005106unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005107 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5108};
5109
David Woodhousecbdf5a22008-08-06 19:42:33 +01005110static int btrfs_real_readdir(struct file *filp, void *dirent,
5111 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005112{
Al Viro496ad9a2013-01-23 17:07:38 -05005113 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005114 struct btrfs_root *root = BTRFS_I(inode)->root;
5115 struct btrfs_item *item;
5116 struct btrfs_dir_item *di;
5117 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005118 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005119 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005120 struct list_head ins_list;
5121 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005122 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005123 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005124 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005125 unsigned char d_type;
5126 int over = 0;
5127 u32 di_cur;
5128 u32 di_total;
5129 u32 di_len;
5130 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005131 char tmp_name[32];
5132 char *name_ptr;
5133 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005134 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005135
5136 /* FIXME, use a real flag for deciding about the key type */
5137 if (root->fs_info->tree_root == root)
5138 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005139
Chris Mason39544012007-12-12 14:38:19 -05005140 /* special case for "." */
5141 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005142 over = filldir(dirent, ".", 1,
5143 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005144 if (over)
5145 return 0;
5146 filp->f_pos = 1;
5147 }
Chris Mason39544012007-12-12 14:38:19 -05005148 /* special case for .., just use the back ref */
5149 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005150 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005151 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005152 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005153 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005154 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005155 filp->f_pos = 2;
5156 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005157 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005158 if (!path)
5159 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005160
Josef Bacik026fd312011-05-13 10:32:11 -04005161 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005162
Miao Xie16cdcec2011-04-22 18:12:22 +08005163 if (key_type == BTRFS_DIR_INDEX_KEY) {
5164 INIT_LIST_HEAD(&ins_list);
5165 INIT_LIST_HEAD(&del_list);
5166 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5167 }
5168
Chris Mason39279cc2007-06-12 06:35:45 -04005169 btrfs_set_key_type(&key, key_type);
5170 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005171 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005172
Chris Mason39279cc2007-06-12 06:35:45 -04005173 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5174 if (ret < 0)
5175 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005176
5177 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005178 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005179 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005180 if (slot >= btrfs_header_nritems(leaf)) {
5181 ret = btrfs_next_leaf(root, path);
5182 if (ret < 0)
5183 goto err;
5184 else if (ret > 0)
5185 break;
5186 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005187 }
Chris Mason3de45862008-11-17 21:02:50 -05005188
Chris Mason5f39d392007-10-15 16:14:19 -04005189 item = btrfs_item_nr(leaf, slot);
5190 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5191
5192 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005193 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005194 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005195 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005196 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005197 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005198 if (key_type == BTRFS_DIR_INDEX_KEY &&
5199 btrfs_should_delete_dir_index(&del_list,
5200 found_key.offset))
5201 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005202
5203 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005204 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005205
Chris Mason39279cc2007-06-12 06:35:45 -04005206 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5207 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005208 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005209
5210 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005211 struct btrfs_key location;
5212
Josef Bacik22a94d42011-03-16 16:47:17 -04005213 if (verify_dir_item(root, leaf, di))
5214 break;
5215
Chris Mason5f39d392007-10-15 16:14:19 -04005216 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005217 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005218 name_ptr = tmp_name;
5219 } else {
5220 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005221 if (!name_ptr) {
5222 ret = -ENOMEM;
5223 goto err;
5224 }
Chris Mason5f39d392007-10-15 16:14:19 -04005225 }
5226 read_extent_buffer(leaf, name_ptr,
5227 (unsigned long)(di + 1), name_len);
5228
5229 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5230 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005231
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005232
Chris Mason3de45862008-11-17 21:02:50 -05005233 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005234 * skip it.
5235 *
5236 * In contrast to old kernels, we insert the snapshot's
5237 * dir item and dir index after it has been created, so
5238 * we won't find a reference to our own snapshot. We
5239 * still keep the following code for backward
5240 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005241 */
5242 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5243 location.objectid == root->root_key.objectid) {
5244 over = 0;
5245 goto skip;
5246 }
Chris Mason5f39d392007-10-15 16:14:19 -04005247 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005248 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005249 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005250
Chris Mason3de45862008-11-17 21:02:50 -05005251skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005252 if (name_ptr != tmp_name)
5253 kfree(name_ptr);
5254
Chris Mason39279cc2007-06-12 06:35:45 -04005255 if (over)
5256 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005257 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005258 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005259 di_cur += di_len;
5260 di = (struct btrfs_dir_item *)((char *)di + di_len);
5261 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005262next:
5263 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005264 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005265
Miao Xie16cdcec2011-04-22 18:12:22 +08005266 if (key_type == BTRFS_DIR_INDEX_KEY) {
5267 if (is_curr)
5268 filp->f_pos++;
5269 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5270 &ins_list);
5271 if (ret)
5272 goto nopos;
5273 }
5274
David Woodhouse49593bf2008-08-17 17:08:36 +01005275 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005276 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005277 /*
5278 * 32-bit glibc will use getdents64, but then strtol -
5279 * so the last number we can serve is this.
5280 */
5281 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005282 else
5283 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005284nopos:
5285 ret = 0;
5286err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005287 if (key_type == BTRFS_DIR_INDEX_KEY)
5288 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005289 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005290 return ret;
5291}
5292
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005293int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005294{
5295 struct btrfs_root *root = BTRFS_I(inode)->root;
5296 struct btrfs_trans_handle *trans;
5297 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005298 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005299
Josef Bacik72ac3c02012-05-23 14:13:11 -04005300 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005301 return 0;
5302
Liu Bo83eea1f2012-07-10 05:28:39 -06005303 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005304 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005305
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005306 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005307 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005308 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005309 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005310 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005311 if (IS_ERR(trans))
5312 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005313 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005314 }
5315 return ret;
5316}
5317
5318/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005319 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005320 * inode changes. But, it is most likely to find the inode in cache.
5321 * FIXME, needs more benchmarking...there are no reasons other than performance
5322 * to keep or drop this code.
5323 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05005324int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005325{
5326 struct btrfs_root *root = BTRFS_I(inode)->root;
5327 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005328 int ret;
5329
Josef Bacik72ac3c02012-05-23 14:13:11 -04005330 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005331 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005332
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005333 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005334 if (IS_ERR(trans))
5335 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005336
5337 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005338 if (ret && ret == -ENOSPC) {
5339 /* whoops, lets try again with the full transaction */
5340 btrfs_end_transaction(trans, root);
5341 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005342 if (IS_ERR(trans))
5343 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005344
Chris Mason94b60442010-05-26 11:02:00 -04005345 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005346 }
Chris Mason39279cc2007-06-12 06:35:45 -04005347 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005348 if (BTRFS_I(inode)->delayed_node)
5349 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005350
5351 return ret;
5352}
5353
5354/*
5355 * This is a copy of file_update_time. We need this so we can return error on
5356 * ENOSPC for updating the inode in the case of file write and mmap writes.
5357 */
Josef Bacike41f9412012-03-26 09:46:47 -04005358static int btrfs_update_time(struct inode *inode, struct timespec *now,
5359 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005360{
Alexander Block2bc5565282012-06-15 09:49:33 +02005361 struct btrfs_root *root = BTRFS_I(inode)->root;
5362
5363 if (btrfs_root_readonly(root))
5364 return -EROFS;
5365
Josef Bacike41f9412012-03-26 09:46:47 -04005366 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005367 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005368 if (flags & S_CTIME)
5369 inode->i_ctime = *now;
5370 if (flags & S_MTIME)
5371 inode->i_mtime = *now;
5372 if (flags & S_ATIME)
5373 inode->i_atime = *now;
5374 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005375}
5376
Chris Masond352ac62008-09-29 15:18:18 -04005377/*
5378 * find the highest existing sequence number in a directory
5379 * and then set the in-memory index_cnt variable to reflect
5380 * free sequence numbers
5381 */
Josef Bacikaec74772008-07-24 12:12:38 -04005382static int btrfs_set_inode_index_count(struct inode *inode)
5383{
5384 struct btrfs_root *root = BTRFS_I(inode)->root;
5385 struct btrfs_key key, found_key;
5386 struct btrfs_path *path;
5387 struct extent_buffer *leaf;
5388 int ret;
5389
Li Zefan33345d012011-04-20 10:31:50 +08005390 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005391 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5392 key.offset = (u64)-1;
5393
5394 path = btrfs_alloc_path();
5395 if (!path)
5396 return -ENOMEM;
5397
5398 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5399 if (ret < 0)
5400 goto out;
5401 /* FIXME: we should be able to handle this */
5402 if (ret == 0)
5403 goto out;
5404 ret = 0;
5405
5406 /*
5407 * MAGIC NUMBER EXPLANATION:
5408 * since we search a directory based on f_pos we have to start at 2
5409 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5410 * else has to start at 2
5411 */
5412 if (path->slots[0] == 0) {
5413 BTRFS_I(inode)->index_cnt = 2;
5414 goto out;
5415 }
5416
5417 path->slots[0]--;
5418
5419 leaf = path->nodes[0];
5420 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5421
Li Zefan33345d012011-04-20 10:31:50 +08005422 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005423 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5424 BTRFS_I(inode)->index_cnt = 2;
5425 goto out;
5426 }
5427
5428 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5429out:
5430 btrfs_free_path(path);
5431 return ret;
5432}
5433
Chris Masond352ac62008-09-29 15:18:18 -04005434/*
5435 * helper to find a free sequence number in a given directory. This current
5436 * code is very simple, later versions will do smarter things in the btree
5437 */
Chris Mason3de45862008-11-17 21:02:50 -05005438int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005439{
5440 int ret = 0;
5441
5442 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005443 ret = btrfs_inode_delayed_dir_index_count(dir);
5444 if (ret) {
5445 ret = btrfs_set_inode_index_count(dir);
5446 if (ret)
5447 return ret;
5448 }
Josef Bacikaec74772008-07-24 12:12:38 -04005449 }
5450
Chris Mason00e4e6b2008-08-05 11:18:09 -04005451 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005452 BTRFS_I(dir)->index_cnt++;
5453
5454 return ret;
5455}
5456
Chris Mason39279cc2007-06-12 06:35:45 -04005457static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5458 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005459 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005460 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005461 u64 ref_objectid, u64 objectid,
5462 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005463{
5464 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005465 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005466 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005467 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005468 struct btrfs_inode_ref *ref;
5469 struct btrfs_key key[2];
5470 u32 sizes[2];
5471 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005472 int ret;
5473 int owner;
5474
Chris Mason5f39d392007-10-15 16:14:19 -04005475 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005476 if (!path)
5477 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005478
Chris Mason39279cc2007-06-12 06:35:45 -04005479 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005480 if (!inode) {
5481 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005482 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005483 }
Chris Mason39279cc2007-06-12 06:35:45 -04005484
Li Zefan581bb052011-04-20 10:06:11 +08005485 /*
5486 * we have to initialize this early, so we can reclaim the inode
5487 * number if we fail afterwards in this function.
5488 */
5489 inode->i_ino = objectid;
5490
Josef Bacikaec74772008-07-24 12:12:38 -04005491 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005492 trace_btrfs_inode_request(dir);
5493
Chris Mason3de45862008-11-17 21:02:50 -05005494 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005495 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005496 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005497 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005498 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005499 }
Josef Bacikaec74772008-07-24 12:12:38 -04005500 }
5501 /*
5502 * index_cnt is ignored for everything but a dir,
5503 * btrfs_get_inode_index_count has an explanation for the magic
5504 * number
5505 */
5506 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005507 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005508 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005509 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005510
Josef Bacik5dc562c2012-08-17 13:14:17 -04005511 /*
5512 * We could have gotten an inode number from somebody who was fsynced
5513 * and then removed in this same transaction, so let's just set full
5514 * sync since it will be a full sync anyway and this will blow away the
5515 * old info in the log.
5516 */
5517 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5518
Al Viro569254b2011-07-24 17:08:40 -04005519 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005520 owner = 0;
5521 else
5522 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005523
5524 key[0].objectid = objectid;
5525 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5526 key[0].offset = 0;
5527
Mark Fashehf1863732012-08-08 11:32:27 -07005528 /*
5529 * Start new inodes with an inode_ref. This is slightly more
5530 * efficient for small numbers of hard links since they will
5531 * be packed into one item. Extended refs will kick in if we
5532 * add more hard links than can fit in the ref item.
5533 */
Chris Mason9c583092008-01-29 15:15:18 -05005534 key[1].objectid = objectid;
5535 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5536 key[1].offset = ref_objectid;
5537
5538 sizes[0] = sizeof(struct btrfs_inode_item);
5539 sizes[1] = name_len + sizeof(*ref);
5540
Chris Masonb9473432009-03-13 11:00:37 -04005541 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005542 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5543 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005544 goto fail;
5545
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005546 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005547 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005548 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005549 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5550 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005551 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5552 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005553 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005554
5555 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5556 struct btrfs_inode_ref);
5557 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005558 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005559 ptr = (unsigned long)(ref + 1);
5560 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5561
Chris Mason5f39d392007-10-15 16:14:19 -04005562 btrfs_mark_buffer_dirty(path->nodes[0]);
5563 btrfs_free_path(path);
5564
Chris Mason39279cc2007-06-12 06:35:45 -04005565 location = &BTRFS_I(inode)->location;
5566 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005567 location->offset = 0;
5568 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5569
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005570 btrfs_inherit_iflags(inode, dir);
5571
Al Viro569254b2011-07-24 17:08:40 -04005572 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005573 if (btrfs_test_opt(root, NODATASUM))
5574 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005575 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005576 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5577 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005578 }
5579
Chris Mason39279cc2007-06-12 06:35:45 -04005580 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005581 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005582
5583 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005584 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005585
Alexander Block8ea05e32012-07-25 17:35:53 +02005586 btrfs_update_root_times(trans, root);
5587
Chris Mason39279cc2007-06-12 06:35:45 -04005588 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005589fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005590 if (dir)
5591 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005592 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005593 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005594 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005595}
5596
5597static inline u8 btrfs_inode_type(struct inode *inode)
5598{
5599 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5600}
5601
Chris Masond352ac62008-09-29 15:18:18 -04005602/*
5603 * utility function to add 'inode' into 'parent_inode' with
5604 * a give name and a given sequence number.
5605 * if 'add_backref' is true, also insert a backref from the
5606 * inode to the parent directory.
5607 */
Chris Masone02119d2008-09-05 16:13:11 -04005608int btrfs_add_link(struct btrfs_trans_handle *trans,
5609 struct inode *parent_inode, struct inode *inode,
5610 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005611{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005612 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005613 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005614 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005615 u64 ino = btrfs_ino(inode);
5616 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005617
Li Zefan33345d012011-04-20 10:31:50 +08005618 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005619 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5620 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005621 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005622 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5623 key.offset = 0;
5624 }
Chris Mason39279cc2007-06-12 06:35:45 -04005625
Li Zefan33345d012011-04-20 10:31:50 +08005626 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005627 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5628 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005629 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005630 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005631 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5632 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005633 }
5634
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005635 /* Nothing to clean up yet */
5636 if (ret)
5637 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005639 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5640 parent_inode, &key,
5641 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005642 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005643 goto fail_dir_item;
5644 else if (ret) {
5645 btrfs_abort_transaction(trans, root, ret);
5646 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005647 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005648
5649 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5650 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005651 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005652 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5653 ret = btrfs_update_inode(trans, root, parent_inode);
5654 if (ret)
5655 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005656 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005657
5658fail_dir_item:
5659 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5660 u64 local_index;
5661 int err;
5662 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5663 key.objectid, root->root_key.objectid,
5664 parent_ino, &local_index, name, name_len);
5665
5666 } else if (add_backref) {
5667 u64 local_index;
5668 int err;
5669
5670 err = btrfs_del_inode_ref(trans, root, name, name_len,
5671 ino, parent_ino, &local_index);
5672 }
5673 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005674}
5675
5676static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005677 struct inode *dir, struct dentry *dentry,
5678 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005679{
Josef Bacika1b075d2010-11-19 20:36:11 +00005680 int err = btrfs_add_link(trans, dir, inode,
5681 dentry->d_name.name, dentry->d_name.len,
5682 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005683 if (err > 0)
5684 err = -EEXIST;
5685 return err;
5686}
5687
Josef Bacik618e21d2007-07-11 10:18:17 -04005688static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005689 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005690{
5691 struct btrfs_trans_handle *trans;
5692 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005693 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005694 int err;
5695 int drop_inode = 0;
5696 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005697 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005698
5699 if (!new_valid_dev(rdev))
5700 return -EINVAL;
5701
Josef Bacik9ed74f22009-09-11 16:12:44 -04005702 /*
5703 * 2 for inode item and ref
5704 * 2 for dir items
5705 * 1 for xattr if selinux is on
5706 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005707 trans = btrfs_start_transaction(root, 5);
5708 if (IS_ERR(trans))
5709 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005710
Li Zefan581bb052011-04-20 10:06:11 +08005711 err = btrfs_find_free_ino(root, &objectid);
5712 if (err)
5713 goto out_unlock;
5714
Josef Bacikaec74772008-07-24 12:12:38 -04005715 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005716 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005717 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005718 if (IS_ERR(inode)) {
5719 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005720 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005721 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005722
Eric Paris2a7dba32011-02-01 11:05:39 -05005723 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005724 if (err) {
5725 drop_inode = 1;
5726 goto out_unlock;
5727 }
5728
Casey Schauflerad19db72011-12-15 10:09:07 -05005729 /*
5730 * If the active LSM wants to access the inode during
5731 * d_instantiate it needs these. Smack checks to see
5732 * if the filesystem supports xattrs by looking at the
5733 * ops vector.
5734 */
5735
5736 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005737 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005738 if (err)
5739 drop_inode = 1;
5740 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005741 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005742 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005743 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005744 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005745out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005746 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005747 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005748 if (drop_inode) {
5749 inode_dec_link_count(inode);
5750 iput(inode);
5751 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005752 return err;
5753}
5754
Chris Mason39279cc2007-06-12 06:35:45 -04005755static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005756 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005757{
5758 struct btrfs_trans_handle *trans;
5759 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005760 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005761 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005762 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005763 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005764 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005765
Josef Bacik9ed74f22009-09-11 16:12:44 -04005766 /*
5767 * 2 for inode item and ref
5768 * 2 for dir items
5769 * 1 for xattr if selinux is on
5770 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005771 trans = btrfs_start_transaction(root, 5);
5772 if (IS_ERR(trans))
5773 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005774
Li Zefan581bb052011-04-20 10:06:11 +08005775 err = btrfs_find_free_ino(root, &objectid);
5776 if (err)
5777 goto out_unlock;
5778
Josef Bacikaec74772008-07-24 12:12:38 -04005779 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005780 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005781 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005782 if (IS_ERR(inode)) {
5783 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005784 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005785 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005786 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005787
Eric Paris2a7dba32011-02-01 11:05:39 -05005788 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005789 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005790 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005791
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005792 err = btrfs_update_inode(trans, root, inode);
5793 if (err)
5794 goto out_unlock;
5795
Casey Schauflerad19db72011-12-15 10:09:07 -05005796 /*
5797 * If the active LSM wants to access the inode during
5798 * d_instantiate it needs these. Smack checks to see
5799 * if the filesystem supports xattrs by looking at the
5800 * ops vector.
5801 */
5802 inode->i_fop = &btrfs_file_operations;
5803 inode->i_op = &btrfs_file_inode_operations;
5804
Josef Bacika1b075d2010-11-19 20:36:11 +00005805 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005806 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005807 goto out_unlock;
5808
5809 inode->i_mapping->a_ops = &btrfs_aops;
5810 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5811 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5812 d_instantiate(dentry, inode);
5813
Chris Mason39279cc2007-06-12 06:35:45 -04005814out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005815 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005816 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005817 inode_dec_link_count(inode);
5818 iput(inode);
5819 }
Liu Bob53d3f52012-11-14 14:34:34 +00005820 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005821 return err;
5822}
5823
5824static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5825 struct dentry *dentry)
5826{
5827 struct btrfs_trans_handle *trans;
5828 struct btrfs_root *root = BTRFS_I(dir)->root;
5829 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005830 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005831 int err;
5832 int drop_inode = 0;
5833
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005834 /* do not allow sys_link's with other subvols of the same device */
5835 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005836 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005837
Mark Fashehf1863732012-08-08 11:32:27 -07005838 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005839 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005840
Chris Mason3de45862008-11-17 21:02:50 -05005841 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005842 if (err)
5843 goto fail;
5844
Yan, Zhenga22285a2010-05-16 10:48:46 -04005845 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005846 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005847 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005848 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005849 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005850 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005851 if (IS_ERR(trans)) {
5852 err = PTR_ERR(trans);
5853 goto fail;
5854 }
Chris Mason5f39d392007-10-15 16:14:19 -04005855
Miao Xie31534952011-04-13 13:19:21 +08005856 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005857 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005858 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005859 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005860 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005861
Josef Bacika1b075d2010-11-19 20:36:11 +00005862 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005863
Yan, Zhenga5719522009-09-24 09:17:31 -04005864 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005865 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005866 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005867 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005868 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005869 if (err)
5870 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005871 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005872 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005873 }
Chris Mason39279cc2007-06-12 06:35:45 -04005874
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005875 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005876fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005877 if (drop_inode) {
5878 inode_dec_link_count(inode);
5879 iput(inode);
5880 }
Liu Bob53d3f52012-11-14 14:34:34 +00005881 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005882 return err;
5883}
5884
Al Viro18bb1db2011-07-26 01:41:39 -04005885static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005886{
Chris Masonb9d86662008-05-02 16:13:49 -04005887 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005888 struct btrfs_trans_handle *trans;
5889 struct btrfs_root *root = BTRFS_I(dir)->root;
5890 int err = 0;
5891 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005892 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005893 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005894
Josef Bacik9ed74f22009-09-11 16:12:44 -04005895 /*
5896 * 2 items for inode and ref
5897 * 2 items for dir items
5898 * 1 for xattr if selinux is on
5899 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005900 trans = btrfs_start_transaction(root, 5);
5901 if (IS_ERR(trans))
5902 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005903
Li Zefan581bb052011-04-20 10:06:11 +08005904 err = btrfs_find_free_ino(root, &objectid);
5905 if (err)
5906 goto out_fail;
5907
Josef Bacikaec74772008-07-24 12:12:38 -04005908 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005909 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005910 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005911 if (IS_ERR(inode)) {
5912 err = PTR_ERR(inode);
5913 goto out_fail;
5914 }
Chris Mason5f39d392007-10-15 16:14:19 -04005915
Chris Mason39279cc2007-06-12 06:35:45 -04005916 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005917
Eric Paris2a7dba32011-02-01 11:05:39 -05005918 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005919 if (err)
5920 goto out_fail;
5921
Chris Mason39279cc2007-06-12 06:35:45 -04005922 inode->i_op = &btrfs_dir_inode_operations;
5923 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005924
Chris Masondbe674a2008-07-17 12:54:05 -04005925 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005926 err = btrfs_update_inode(trans, root, inode);
5927 if (err)
5928 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005929
Josef Bacika1b075d2010-11-19 20:36:11 +00005930 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5931 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005932 if (err)
5933 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005934
Chris Mason39279cc2007-06-12 06:35:45 -04005935 d_instantiate(dentry, inode);
5936 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005937
5938out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005939 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005940 if (drop_on_err)
5941 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005942 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005943 return err;
5944}
5945
Chris Masond352ac62008-09-29 15:18:18 -04005946/* helper for btfs_get_extent. Given an existing extent in the tree,
5947 * and an extent that you want to insert, deal with overlap and insert
5948 * the new extent into the tree.
5949 */
Chris Mason3b951512008-04-17 11:29:12 -04005950static int merge_extent_mapping(struct extent_map_tree *em_tree,
5951 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005952 struct extent_map *em,
5953 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005954{
5955 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005956
Chris Masone6dcd2d2008-07-17 12:53:50 -04005957 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5958 start_diff = map_start - em->start;
5959 em->start = map_start;
5960 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005961 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5962 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005963 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005964 em->block_len -= start_diff;
5965 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005966 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005967}
5968
Chris Masonc8b97812008-10-29 14:49:59 -04005969static noinline int uncompress_inline(struct btrfs_path *path,
5970 struct inode *inode, struct page *page,
5971 size_t pg_offset, u64 extent_offset,
5972 struct btrfs_file_extent_item *item)
5973{
5974 int ret;
5975 struct extent_buffer *leaf = path->nodes[0];
5976 char *tmp;
5977 size_t max_size;
5978 unsigned long inline_size;
5979 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005980 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005981
5982 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005983 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005984 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5985 inline_size = btrfs_file_extent_inline_item_len(leaf,
5986 btrfs_item_nr(leaf, path->slots[0]));
5987 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005988 if (!tmp)
5989 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005990 ptr = btrfs_file_extent_inline_start(item);
5991
5992 read_extent_buffer(leaf, tmp, ptr, inline_size);
5993
Chris Mason5b050f02008-11-11 09:34:41 -05005994 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005995 ret = btrfs_decompress(compress_type, tmp, page,
5996 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005997 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005998 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005999 unsigned long copy_size = min_t(u64,
6000 PAGE_CACHE_SIZE - pg_offset,
6001 max_size - extent_offset);
6002 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006003 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006004 }
6005 kfree(tmp);
6006 return 0;
6007}
6008
Chris Masond352ac62008-09-29 15:18:18 -04006009/*
6010 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006011 * the ugly parts come from merging extents from the disk with the in-ram
6012 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006013 * where the in-ram extents might be locked pending data=ordered completion.
6014 *
6015 * This also copies inline extents directly into the page.
6016 */
Chris Masond3977122009-01-05 21:25:51 -05006017
Chris Masona52d9a82007-08-27 16:49:44 -04006018struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006019 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006020 int create)
6021{
6022 int ret;
6023 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006024 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006025 u64 extent_start = 0;
6026 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006027 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006028 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006029 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006030 struct btrfs_root *root = BTRFS_I(inode)->root;
6031 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006032 struct extent_buffer *leaf;
6033 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006034 struct extent_map *em = NULL;
6035 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006036 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006037 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006038 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006039
Chris Masona52d9a82007-08-27 16:49:44 -04006040again:
Chris Mason890871b2009-09-02 16:24:52 -04006041 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006042 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006043 if (em)
6044 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006045 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006046
Chris Masona52d9a82007-08-27 16:49:44 -04006047 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006048 if (em->start > start || em->start + em->len <= start)
6049 free_extent_map(em);
6050 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006051 free_extent_map(em);
6052 else
6053 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006054 }
David Sterba172ddd62011-04-21 00:48:27 +02006055 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006056 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006057 err = -ENOMEM;
6058 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006059 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006060 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006061 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006062 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006063 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006064 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006065
6066 if (!path) {
6067 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006068 if (!path) {
6069 err = -ENOMEM;
6070 goto out;
6071 }
6072 /*
6073 * Chances are we'll be called again, so go ahead and do
6074 * readahead
6075 */
6076 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006077 }
6078
Chris Mason179e29e2007-11-01 11:28:41 -04006079 ret = btrfs_lookup_file_extent(trans, root, path,
6080 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006081 if (ret < 0) {
6082 err = ret;
6083 goto out;
6084 }
6085
6086 if (ret != 0) {
6087 if (path->slots[0] == 0)
6088 goto not_found;
6089 path->slots[0]--;
6090 }
6091
Chris Mason5f39d392007-10-15 16:14:19 -04006092 leaf = path->nodes[0];
6093 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006094 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006095 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006096 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6097 found_type = btrfs_key_type(&found_key);
6098 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006099 found_type != BTRFS_EXTENT_DATA_KEY) {
6100 goto not_found;
6101 }
6102
Chris Mason5f39d392007-10-15 16:14:19 -04006103 found_type = btrfs_file_extent_type(leaf, item);
6104 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006105 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006106 if (found_type == BTRFS_FILE_EXTENT_REG ||
6107 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006108 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006109 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006110 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6111 size_t size;
6112 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006113 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006114 }
6115
6116 if (start >= extent_end) {
6117 path->slots[0]++;
6118 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6119 ret = btrfs_next_leaf(root, path);
6120 if (ret < 0) {
6121 err = ret;
6122 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006123 }
Yan Zheng9036c102008-10-30 14:19:41 -04006124 if (ret > 0)
6125 goto not_found;
6126 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006127 }
Yan Zheng9036c102008-10-30 14:19:41 -04006128 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6129 if (found_key.objectid != objectid ||
6130 found_key.type != BTRFS_EXTENT_DATA_KEY)
6131 goto not_found;
6132 if (start + len <= found_key.offset)
6133 goto not_found;
6134 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006135 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006136 em->len = found_key.offset - start;
6137 goto not_found_em;
6138 }
6139
Yan Zhengd899e052008-10-30 14:25:28 -04006140 if (found_type == BTRFS_FILE_EXTENT_REG ||
6141 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006142 em->start = extent_start;
6143 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006144 em->orig_start = extent_start -
6145 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006146 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6147 item);
Chris Masondb945352007-10-15 16:15:53 -04006148 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6149 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006150 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006151 goto insert;
6152 }
Li Zefan261507a02010-12-17 14:21:50 +08006153 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006154 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006155 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006156 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006157 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006158 } else {
6159 bytenr += btrfs_file_extent_offset(leaf, item);
6160 em->block_start = bytenr;
6161 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006162 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6163 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006164 }
Chris Masona52d9a82007-08-27 16:49:44 -04006165 goto insert;
6166 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006167 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006168 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006169 size_t size;
6170 size_t extent_offset;
6171 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006172
Chris Masona52d9a82007-08-27 16:49:44 -04006173 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006174 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006175 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006176 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006177 goto out;
6178 }
6179
Yan Zheng9036c102008-10-30 14:19:41 -04006180 size = btrfs_file_extent_inline_len(leaf, item);
6181 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006182 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006183 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006184 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006185 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006186 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006187 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006188 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006189 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006190 em->compress_type = compress_type;
6191 }
Yan689f9342007-10-29 11:41:07 -04006192 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006193 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006194 if (btrfs_file_extent_compression(leaf, item) !=
6195 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006196 ret = uncompress_inline(path, inode, page,
6197 pg_offset,
6198 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006199 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006200 } else {
6201 map = kmap(page);
6202 read_extent_buffer(leaf, map + pg_offset, ptr,
6203 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006204 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6205 memset(map + pg_offset + copy_size, 0,
6206 PAGE_CACHE_SIZE - pg_offset -
6207 copy_size);
6208 }
Chris Masonc8b97812008-10-29 14:49:59 -04006209 kunmap(page);
6210 }
Chris Mason179e29e2007-11-01 11:28:41 -04006211 flush_dcache_page(page);
6212 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006213 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006214 if (!trans) {
6215 kunmap(page);
6216 free_extent_map(em);
6217 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006218
David Sterbab3b4aa72011-04-21 01:20:15 +02006219 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006220 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006221
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006222 if (IS_ERR(trans))
6223 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006224 goto again;
6225 }
Chris Masonc8b97812008-10-29 14:49:59 -04006226 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006227 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006228 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006229 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006230 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006231 }
Chris Masond1310b22008-01-24 16:13:08 -05006232 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006233 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006234 goto insert;
6235 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006236 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006237 }
6238not_found:
6239 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006240 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006241 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006242not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006243 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006244 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006245insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006246 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006247 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05006248 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
6249 "[%llu %llu]\n", (unsigned long long)em->start,
6250 (unsigned long long)em->len,
6251 (unsigned long long)start,
6252 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006253 err = -EIO;
6254 goto out;
6255 }
Chris Masond1310b22008-01-24 16:13:08 -05006256
6257 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006258 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006259 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04006260 /* it is possible that someone inserted the extent into the tree
6261 * while we had the lock dropped. It is also possible that
6262 * an overlapping map exists in the tree
6263 */
Chris Masona52d9a82007-08-27 16:49:44 -04006264 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006265 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006266
6267 ret = 0;
6268
Chris Mason3b951512008-04-17 11:29:12 -04006269 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006270 if (existing && (existing->start > start ||
6271 existing->start + existing->len <= start)) {
6272 free_extent_map(existing);
6273 existing = NULL;
6274 }
Chris Mason3b951512008-04-17 11:29:12 -04006275 if (!existing) {
6276 existing = lookup_extent_mapping(em_tree, em->start,
6277 em->len);
6278 if (existing) {
6279 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006280 em, start,
6281 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006282 free_extent_map(existing);
6283 if (err) {
6284 free_extent_map(em);
6285 em = NULL;
6286 }
6287 } else {
6288 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006289 free_extent_map(em);
6290 em = NULL;
6291 }
6292 } else {
6293 free_extent_map(em);
6294 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006295 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006296 }
Chris Masona52d9a82007-08-27 16:49:44 -04006297 }
Chris Mason890871b2009-09-02 16:24:52 -04006298 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006299out:
liubo1abe9b82011-03-24 11:18:59 +00006300
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006301 if (em)
6302 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006303
Chris Masonf4219502008-07-22 11:18:09 -04006304 if (path)
6305 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006306 if (trans) {
6307 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006308 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006309 err = ret;
6310 }
Chris Masona52d9a82007-08-27 16:49:44 -04006311 if (err) {
6312 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006313 return ERR_PTR(err);
6314 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006315 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006316 return em;
6317}
6318
Chris Masonec29ed52011-02-23 16:23:20 -05006319struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6320 size_t pg_offset, u64 start, u64 len,
6321 int create)
6322{
6323 struct extent_map *em;
6324 struct extent_map *hole_em = NULL;
6325 u64 range_start = start;
6326 u64 end;
6327 u64 found;
6328 u64 found_end;
6329 int err = 0;
6330
6331 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6332 if (IS_ERR(em))
6333 return em;
6334 if (em) {
6335 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006336 * if our em maps to
6337 * - a hole or
6338 * - a pre-alloc extent,
6339 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006340 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006341 if (em->block_start != EXTENT_MAP_HOLE &&
6342 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006343 return em;
6344 else
6345 hole_em = em;
6346 }
6347
6348 /* check to see if we've wrapped (len == -1 or similar) */
6349 end = start + len;
6350 if (end < start)
6351 end = (u64)-1;
6352 else
6353 end -= 1;
6354
6355 em = NULL;
6356
6357 /* ok, we didn't find anything, lets look for delalloc */
6358 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6359 end, len, EXTENT_DELALLOC, 1);
6360 found_end = range_start + found;
6361 if (found_end < range_start)
6362 found_end = (u64)-1;
6363
6364 /*
6365 * we didn't find anything useful, return
6366 * the original results from get_extent()
6367 */
6368 if (range_start > end || found_end <= start) {
6369 em = hole_em;
6370 hole_em = NULL;
6371 goto out;
6372 }
6373
6374 /* adjust the range_start to make sure it doesn't
6375 * go backwards from the start they passed in
6376 */
6377 range_start = max(start,range_start);
6378 found = found_end - range_start;
6379
6380 if (found > 0) {
6381 u64 hole_start = start;
6382 u64 hole_len = len;
6383
David Sterba172ddd62011-04-21 00:48:27 +02006384 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006385 if (!em) {
6386 err = -ENOMEM;
6387 goto out;
6388 }
6389 /*
6390 * when btrfs_get_extent can't find anything it
6391 * returns one huge hole
6392 *
6393 * make sure what it found really fits our range, and
6394 * adjust to make sure it is based on the start from
6395 * the caller
6396 */
6397 if (hole_em) {
6398 u64 calc_end = extent_map_end(hole_em);
6399
6400 if (calc_end <= start || (hole_em->start > end)) {
6401 free_extent_map(hole_em);
6402 hole_em = NULL;
6403 } else {
6404 hole_start = max(hole_em->start, start);
6405 hole_len = calc_end - hole_start;
6406 }
6407 }
6408 em->bdev = NULL;
6409 if (hole_em && range_start > hole_start) {
6410 /* our hole starts before our delalloc, so we
6411 * have to return just the parts of the hole
6412 * that go until the delalloc starts
6413 */
6414 em->len = min(hole_len,
6415 range_start - hole_start);
6416 em->start = hole_start;
6417 em->orig_start = hole_start;
6418 /*
6419 * don't adjust block start at all,
6420 * it is fixed at EXTENT_MAP_HOLE
6421 */
6422 em->block_start = hole_em->block_start;
6423 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006424 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6425 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006426 } else {
6427 em->start = range_start;
6428 em->len = found;
6429 em->orig_start = range_start;
6430 em->block_start = EXTENT_MAP_DELALLOC;
6431 em->block_len = found;
6432 }
6433 } else if (hole_em) {
6434 return hole_em;
6435 }
6436out:
6437
6438 free_extent_map(hole_em);
6439 if (err) {
6440 free_extent_map(em);
6441 return ERR_PTR(err);
6442 }
6443 return em;
6444}
6445
Josef Bacik4b46fce2010-05-23 11:00:55 -04006446static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6447 u64 start, u64 len)
6448{
6449 struct btrfs_root *root = BTRFS_I(inode)->root;
6450 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006451 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006452 struct btrfs_key ins;
6453 u64 alloc_hint;
6454 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006455
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006456 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006457 if (IS_ERR(trans))
6458 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006459
6460 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6461
6462 alloc_hint = get_extent_allocation_hint(inode, start, len);
6463 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006464 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006465 if (ret) {
6466 em = ERR_PTR(ret);
6467 goto out;
6468 }
6469
Josef Bacik70c8a912012-10-11 16:54:30 -04006470 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
6471 ins.offset, ins.offset, 0);
6472 if (IS_ERR(em))
6473 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006474
6475 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6476 ins.offset, ins.offset, 0);
6477 if (ret) {
6478 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6479 em = ERR_PTR(ret);
6480 }
6481out:
6482 btrfs_end_transaction(trans, root);
6483 return em;
6484}
6485
Chris Mason46bfbb52010-05-26 11:04:10 -04006486/*
6487 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6488 * block must be cow'd
6489 */
6490static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
6491 struct inode *inode, u64 offset, u64 len)
6492{
6493 struct btrfs_path *path;
6494 int ret;
6495 struct extent_buffer *leaf;
6496 struct btrfs_root *root = BTRFS_I(inode)->root;
6497 struct btrfs_file_extent_item *fi;
6498 struct btrfs_key key;
6499 u64 disk_bytenr;
6500 u64 backref_offset;
6501 u64 extent_end;
6502 u64 num_bytes;
6503 int slot;
6504 int found_type;
6505
6506 path = btrfs_alloc_path();
6507 if (!path)
6508 return -ENOMEM;
6509
Li Zefan33345d012011-04-20 10:31:50 +08006510 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006511 offset, 0);
6512 if (ret < 0)
6513 goto out;
6514
6515 slot = path->slots[0];
6516 if (ret == 1) {
6517 if (slot == 0) {
6518 /* can't find the item, must cow */
6519 ret = 0;
6520 goto out;
6521 }
6522 slot--;
6523 }
6524 ret = 0;
6525 leaf = path->nodes[0];
6526 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006527 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006528 key.type != BTRFS_EXTENT_DATA_KEY) {
6529 /* not our file or wrong item type, must cow */
6530 goto out;
6531 }
6532
6533 if (key.offset > offset) {
6534 /* Wrong offset, must cow */
6535 goto out;
6536 }
6537
6538 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6539 found_type = btrfs_file_extent_type(leaf, fi);
6540 if (found_type != BTRFS_FILE_EXTENT_REG &&
6541 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6542 /* not a regular extent, must cow */
6543 goto out;
6544 }
6545 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6546 backref_offset = btrfs_file_extent_offset(leaf, fi);
6547
6548 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6549 if (extent_end < offset + len) {
6550 /* extent doesn't include our full range, must cow */
6551 goto out;
6552 }
6553
6554 if (btrfs_extent_readonly(root, disk_bytenr))
6555 goto out;
6556
6557 /*
6558 * look for other files referencing this extent, if we
6559 * find any we must cow
6560 */
Li Zefan33345d012011-04-20 10:31:50 +08006561 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006562 key.offset - backref_offset, disk_bytenr))
6563 goto out;
6564
6565 /*
6566 * adjust disk_bytenr and num_bytes to cover just the bytes
6567 * in this extent we are about to write. If there
6568 * are any csums in that range we have to cow in order
6569 * to keep the csums correct
6570 */
6571 disk_bytenr += backref_offset;
6572 disk_bytenr += offset - key.offset;
6573 num_bytes = min(offset + len, extent_end) - offset;
6574 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6575 goto out;
6576 /*
6577 * all of the above have passed, it is safe to overwrite this extent
6578 * without cow
6579 */
6580 ret = 1;
6581out:
6582 btrfs_free_path(path);
6583 return ret;
6584}
6585
Josef Bacikeb838e72012-07-31 16:28:48 -04006586static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6587 struct extent_state **cached_state, int writing)
6588{
6589 struct btrfs_ordered_extent *ordered;
6590 int ret = 0;
6591
6592 while (1) {
6593 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6594 0, cached_state);
6595 /*
6596 * We're concerned with the entire range that we're going to be
6597 * doing DIO to, so we need to make sure theres no ordered
6598 * extents in this range.
6599 */
6600 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6601 lockend - lockstart + 1);
6602
6603 /*
6604 * We need to make sure there are no buffered pages in this
6605 * range either, we could have raced between the invalidate in
6606 * generic_file_direct_write and locking the extent. The
6607 * invalidate needs to happen so that reads after a write do not
6608 * get stale data.
6609 */
6610 if (!ordered && (!writing ||
6611 !test_range_bit(&BTRFS_I(inode)->io_tree,
6612 lockstart, lockend, EXTENT_UPTODATE, 0,
6613 *cached_state)))
6614 break;
6615
6616 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6617 cached_state, GFP_NOFS);
6618
6619 if (ordered) {
6620 btrfs_start_ordered_extent(inode, ordered, 1);
6621 btrfs_put_ordered_extent(ordered);
6622 } else {
6623 /* Screw you mmap */
6624 ret = filemap_write_and_wait_range(inode->i_mapping,
6625 lockstart,
6626 lockend);
6627 if (ret)
6628 break;
6629
6630 /*
6631 * If we found a page that couldn't be invalidated just
6632 * fall back to buffered.
6633 */
6634 ret = invalidate_inode_pages2_range(inode->i_mapping,
6635 lockstart >> PAGE_CACHE_SHIFT,
6636 lockend >> PAGE_CACHE_SHIFT);
6637 if (ret)
6638 break;
6639 }
6640
6641 cond_resched();
6642 }
6643
6644 return ret;
6645}
6646
Josef Bacik69ffb542012-09-11 15:40:07 -04006647static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6648 u64 len, u64 orig_start,
6649 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05006650 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006651{
6652 struct extent_map_tree *em_tree;
6653 struct extent_map *em;
6654 struct btrfs_root *root = BTRFS_I(inode)->root;
6655 int ret;
6656
6657 em_tree = &BTRFS_I(inode)->extent_tree;
6658 em = alloc_extent_map();
6659 if (!em)
6660 return ERR_PTR(-ENOMEM);
6661
6662 em->start = start;
6663 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006664 em->mod_start = start;
6665 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006666 em->len = len;
6667 em->block_len = block_len;
6668 em->block_start = block_start;
6669 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006670 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006671 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006672 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6673 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006674 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006675
6676 do {
6677 btrfs_drop_extent_cache(inode, em->start,
6678 em->start + em->len - 1, 0);
6679 write_lock(&em_tree->lock);
6680 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04006681 if (!ret)
6682 list_move(&em->list,
6683 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006684 write_unlock(&em_tree->lock);
6685 } while (ret == -EEXIST);
6686
6687 if (ret) {
6688 free_extent_map(em);
6689 return ERR_PTR(ret);
6690 }
6691
6692 return em;
6693}
6694
6695
Josef Bacik4b46fce2010-05-23 11:00:55 -04006696static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6697 struct buffer_head *bh_result, int create)
6698{
6699 struct extent_map *em;
6700 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006701 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006702 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006703 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006704 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006705 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006706 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006707 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006708
Miao Xie172a5042013-02-21 02:48:22 -07006709 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006710 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006711 else
Josef Bacikc3298612012-08-03 16:49:19 -04006712 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006713
Josef Bacikc3298612012-08-03 16:49:19 -04006714 lockstart = start;
6715 lockend = start + len - 1;
6716
Josef Bacikeb838e72012-07-31 16:28:48 -04006717 /*
6718 * If this errors out it's because we couldn't invalidate pagecache for
6719 * this range and we need to fallback to buffered.
6720 */
6721 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6722 return -ENOTBLK;
6723
Josef Bacik4b46fce2010-05-23 11:00:55 -04006724 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006725 if (IS_ERR(em)) {
6726 ret = PTR_ERR(em);
6727 goto unlock_err;
6728 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006729
6730 /*
6731 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6732 * io. INLINE is special, and we could probably kludge it in here, but
6733 * it's still buffered so for safety lets just fall back to the generic
6734 * buffered path.
6735 *
6736 * For COMPRESSED we _have_ to read the entire extent in so we can
6737 * decompress it, so there will be buffering required no matter what we
6738 * do, so go ahead and fallback to buffered.
6739 *
6740 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6741 * to buffered IO. Don't blame me, this is the price we pay for using
6742 * the generic code.
6743 */
6744 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6745 em->block_start == EXTENT_MAP_INLINE) {
6746 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006747 ret = -ENOTBLK;
6748 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006749 }
6750
6751 /* Just a good old fashioned hole, return */
6752 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6753 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6754 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006755 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006756 }
6757
6758 /*
6759 * We don't allocate a new extent in the following cases
6760 *
6761 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6762 * existing extent.
6763 * 2) The extent is marked as PREALLOC. We're good to go here and can
6764 * just use the extent.
6765 *
6766 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006767 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006768 len = min(len, em->len - (start - em->start));
6769 lockstart = start + len;
6770 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006771 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006772
6773 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6774 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6775 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006776 int type;
6777 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006778 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006779
6780 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6781 type = BTRFS_ORDERED_PREALLOC;
6782 else
6783 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006784 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006785 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006786
6787 /*
6788 * we're not going to log anything, but we do need
6789 * to make sure the current transaction stays open
6790 * while we look for nocow cross refs
6791 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006792 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006793 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006794 goto must_cow;
6795
6796 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006797 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006798 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006799
6800 if (type == BTRFS_ORDERED_PREALLOC) {
6801 free_extent_map(em);
6802 em = create_pinned_em(inode, start, len,
6803 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006804 block_start, len,
6805 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006806 if (IS_ERR(em)) {
6807 btrfs_end_transaction(trans, root);
6808 goto unlock_err;
6809 }
6810 }
6811
Chris Mason46bfbb52010-05-26 11:04:10 -04006812 ret = btrfs_add_ordered_extent_dio(inode, start,
6813 block_start, len, len, type);
6814 btrfs_end_transaction(trans, root);
6815 if (ret) {
6816 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006817 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006818 }
6819 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006820 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006821 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006822 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006823must_cow:
6824 /*
6825 * this will cow the extent, reset the len in case we changed
6826 * it above
6827 */
6828 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006829 free_extent_map(em);
6830 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006831 if (IS_ERR(em)) {
6832 ret = PTR_ERR(em);
6833 goto unlock_err;
6834 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006835 len = min(len, em->len - (start - em->start));
6836unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006837 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6838 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006839 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006840 bh_result->b_bdev = em->bdev;
6841 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006842 if (create) {
6843 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6844 set_buffer_new(bh_result);
6845
6846 /*
6847 * Need to update the i_size under the extent lock so buffered
6848 * readers will get the updated i_size when we unlock.
6849 */
6850 if (start + len > i_size_read(inode))
6851 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006852
Miao Xie172a5042013-02-21 02:48:22 -07006853 spin_lock(&BTRFS_I(inode)->lock);
6854 BTRFS_I(inode)->outstanding_extents++;
6855 spin_unlock(&BTRFS_I(inode)->lock);
6856
Miao Xie09348562013-02-07 10:12:07 +00006857 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6858 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6859 &cached_state, GFP_NOFS);
6860 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006861 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006862
Josef Bacikeb838e72012-07-31 16:28:48 -04006863 /*
6864 * In the case of write we need to clear and unlock the entire range,
6865 * in the case of read we need to unlock only the end area that we
6866 * aren't using if there is any left over space.
6867 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006868 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006869 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6870 lockend, unlock_bits, 1, 0,
6871 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006872 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006873 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006874 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006875
Josef Bacik4b46fce2010-05-23 11:00:55 -04006876 free_extent_map(em);
6877
6878 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006879
6880unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006881 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6882 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6883 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006884}
6885
6886struct btrfs_dio_private {
6887 struct inode *inode;
6888 u64 logical_offset;
6889 u64 disk_bytenr;
6890 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006891 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006892
6893 /* number of bios pending for this dio */
6894 atomic_t pending_bios;
6895
6896 /* IO errors */
6897 int errors;
6898
6899 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006900};
6901
6902static void btrfs_endio_direct_read(struct bio *bio, int err)
6903{
Miao Xiee65e1532010-11-22 03:04:43 +00006904 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006905 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6906 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006907 struct inode *inode = dip->inode;
6908 struct btrfs_root *root = BTRFS_I(inode)->root;
6909 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006910
6911 start = dip->logical_offset;
6912 do {
6913 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6914 struct page *page = bvec->bv_page;
6915 char *kaddr;
6916 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006917 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006918 unsigned long flags;
6919
Josef Bacikc3298612012-08-03 16:49:19 -04006920 if (get_state_private(&BTRFS_I(inode)->io_tree,
6921 start, &private))
6922 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006923 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006924 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006925 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6926 csum, bvec->bv_len);
6927 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006928 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006929 local_irq_restore(flags);
6930
6931 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006932 if (csum != private) {
6933failed:
Li Zefan33345d012011-04-20 10:31:50 +08006934 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006935 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006936 (unsigned long long)btrfs_ino(inode),
6937 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006938 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006939 err = -EIO;
6940 }
6941 }
6942
6943 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006944 bvec++;
6945 } while (bvec <= bvec_end);
6946
6947 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006948 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006949 bio->bi_private = dip->private;
6950
Josef Bacik4b46fce2010-05-23 11:00:55 -04006951 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006952
6953 /* If we had a csum failure make sure to clear the uptodate flag */
6954 if (err)
6955 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006956 dio_end_io(bio, err);
6957}
6958
6959static void btrfs_endio_direct_write(struct bio *bio, int err)
6960{
6961 struct btrfs_dio_private *dip = bio->bi_private;
6962 struct inode *inode = dip->inode;
6963 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006964 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006965 u64 ordered_offset = dip->logical_offset;
6966 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006967 int ret;
6968
6969 if (err)
6970 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006971again:
6972 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6973 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006974 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006975 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006976 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006977
Josef Bacik5fd02042012-05-02 14:00:54 -04006978 ordered->work.func = finish_ordered_fn;
6979 ordered->work.flags = 0;
6980 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6981 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006982out_test:
6983 /*
6984 * our bio might span multiple ordered extents. If we haven't
6985 * completed the accounting for the whole dio, go back and try again
6986 */
6987 if (ordered_offset < dip->logical_offset + dip->bytes) {
6988 ordered_bytes = dip->logical_offset + dip->bytes -
6989 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006990 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006991 goto again;
6992 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006993out_done:
6994 bio->bi_private = dip->private;
6995
Josef Bacik4b46fce2010-05-23 11:00:55 -04006996 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006997
6998 /* If we had an error make sure to clear the uptodate flag */
6999 if (err)
7000 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007001 dio_end_io(bio, err);
7002}
7003
Chris Masoneaf25d92010-05-25 09:48:28 -04007004static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7005 struct bio *bio, int mirror_num,
7006 unsigned long bio_flags, u64 offset)
7007{
7008 int ret;
7009 struct btrfs_root *root = BTRFS_I(inode)->root;
7010 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007011 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007012 return 0;
7013}
7014
Miao Xiee65e1532010-11-22 03:04:43 +00007015static void btrfs_end_dio_bio(struct bio *bio, int err)
7016{
7017 struct btrfs_dio_private *dip = bio->bi_private;
7018
7019 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007020 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007021 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007022 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007023 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007024 dip->errors = 1;
7025
7026 /*
7027 * before atomic variable goto zero, we must make sure
7028 * dip->errors is perceived to be set.
7029 */
7030 smp_mb__before_atomic_dec();
7031 }
7032
7033 /* if there are more bios still pending for this dio, just exit */
7034 if (!atomic_dec_and_test(&dip->pending_bios))
7035 goto out;
7036
7037 if (dip->errors)
7038 bio_io_error(dip->orig_bio);
7039 else {
7040 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
7041 bio_endio(dip->orig_bio, 0);
7042 }
7043out:
7044 bio_put(bio);
7045}
7046
7047static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7048 u64 first_sector, gfp_t gfp_flags)
7049{
7050 int nr_vecs = bio_get_nr_vecs(bdev);
7051 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7052}
7053
7054static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7055 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007056 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007057{
7058 int write = rw & REQ_WRITE;
7059 struct btrfs_root *root = BTRFS_I(inode)->root;
7060 int ret;
7061
Josef Bacikb812ce22012-11-16 13:56:32 -05007062 if (async_submit)
7063 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7064
Miao Xiee65e1532010-11-22 03:04:43 +00007065 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007066
7067 if (!write) {
7068 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7069 if (ret)
7070 goto err;
7071 }
Miao Xiee65e1532010-11-22 03:04:43 +00007072
Josef Bacik1ae39932011-04-06 14:41:34 -04007073 if (skip_sum)
7074 goto map;
7075
7076 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007077 ret = btrfs_wq_submit_bio(root->fs_info,
7078 inode, rw, bio, 0, 0,
7079 file_offset,
7080 __btrfs_submit_bio_start_direct_io,
7081 __btrfs_submit_bio_done);
7082 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007083 } else if (write) {
7084 /*
7085 * If we aren't doing async submit, calculate the csum of the
7086 * bio now.
7087 */
7088 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7089 if (ret)
7090 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007091 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007092 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007093 if (ret)
7094 goto err;
7095 }
Miao Xiee65e1532010-11-22 03:04:43 +00007096
Josef Bacik1ae39932011-04-06 14:41:34 -04007097map:
7098 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007099err:
7100 bio_put(bio);
7101 return ret;
7102}
7103
7104static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7105 int skip_sum)
7106{
7107 struct inode *inode = dip->inode;
7108 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007109 struct bio *bio;
7110 struct bio *orig_bio = dip->orig_bio;
7111 struct bio_vec *bvec = orig_bio->bi_io_vec;
7112 u64 start_sector = orig_bio->bi_sector;
7113 u64 file_offset = dip->logical_offset;
7114 u64 submit_len = 0;
7115 u64 map_length;
7116 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007117 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007118 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007119
Miao Xiee65e1532010-11-22 03:04:43 +00007120 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007121 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007122 &map_length, NULL, 0);
7123 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007124 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007125 return -EIO;
7126 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007127 if (map_length >= orig_bio->bi_size) {
7128 bio = orig_bio;
7129 goto submit;
7130 }
7131
David Woodhouse53b381b2013-01-29 18:40:14 -05007132 /* async crcs make it difficult to collect full stripe writes. */
7133 if (btrfs_get_alloc_profile(root, 1) &
7134 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7135 async_submit = 0;
7136 else
7137 async_submit = 1;
7138
Josef Bacik02f57c72011-04-06 14:25:44 -04007139 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7140 if (!bio)
7141 return -ENOMEM;
7142 bio->bi_private = dip;
7143 bio->bi_end_io = btrfs_end_dio_bio;
7144 atomic_inc(&dip->pending_bios);
7145
Miao Xiee65e1532010-11-22 03:04:43 +00007146 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7147 if (unlikely(map_length < submit_len + bvec->bv_len ||
7148 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7149 bvec->bv_offset) < bvec->bv_len)) {
7150 /*
7151 * inc the count before we submit the bio so
7152 * we know the end IO handler won't happen before
7153 * we inc the count. Otherwise, the dip might get freed
7154 * before we're done setting it up
7155 */
7156 atomic_inc(&dip->pending_bios);
7157 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7158 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007159 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007160 if (ret) {
7161 bio_put(bio);
7162 atomic_dec(&dip->pending_bios);
7163 goto out_err;
7164 }
7165
Miao Xiee65e1532010-11-22 03:04:43 +00007166 start_sector += submit_len >> 9;
7167 file_offset += submit_len;
7168
7169 submit_len = 0;
7170 nr_pages = 0;
7171
7172 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7173 start_sector, GFP_NOFS);
7174 if (!bio)
7175 goto out_err;
7176 bio->bi_private = dip;
7177 bio->bi_end_io = btrfs_end_dio_bio;
7178
7179 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007180 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007181 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007182 &map_length, NULL, 0);
7183 if (ret) {
7184 bio_put(bio);
7185 goto out_err;
7186 }
7187 } else {
7188 submit_len += bvec->bv_len;
7189 nr_pages ++;
7190 bvec++;
7191 }
7192 }
7193
Josef Bacik02f57c72011-04-06 14:25:44 -04007194submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007195 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007196 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007197 if (!ret)
7198 return 0;
7199
7200 bio_put(bio);
7201out_err:
7202 dip->errors = 1;
7203 /*
7204 * before atomic variable goto zero, we must
7205 * make sure dip->errors is perceived to be set.
7206 */
7207 smp_mb__before_atomic_dec();
7208 if (atomic_dec_and_test(&dip->pending_bios))
7209 bio_io_error(dip->orig_bio);
7210
7211 /* bio_end_io() will handle error, so we needn't return it */
7212 return 0;
7213}
7214
Josef Bacik4b46fce2010-05-23 11:00:55 -04007215static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
7216 loff_t file_offset)
7217{
7218 struct btrfs_root *root = BTRFS_I(inode)->root;
7219 struct btrfs_dio_private *dip;
7220 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007221 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007222 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007223 int ret = 0;
7224
7225 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7226
7227 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7228 if (!dip) {
7229 ret = -ENOMEM;
7230 goto free_ordered;
7231 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007232
7233 dip->private = bio->bi_private;
7234 dip->inode = inode;
7235 dip->logical_offset = file_offset;
7236
Josef Bacik4b46fce2010-05-23 11:00:55 -04007237 dip->bytes = 0;
7238 do {
7239 dip->bytes += bvec->bv_len;
7240 bvec++;
7241 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
7242
Chris Mason46bfbb52010-05-26 11:04:10 -04007243 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007244 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007245 dip->errors = 0;
7246 dip->orig_bio = bio;
7247 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007248
7249 if (write)
7250 bio->bi_end_io = btrfs_endio_direct_write;
7251 else
7252 bio->bi_end_io = btrfs_endio_direct_read;
7253
Miao Xiee65e1532010-11-22 03:04:43 +00007254 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7255 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007256 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007257free_ordered:
7258 /*
7259 * If this is a write, we need to clean up the reserved space and kill
7260 * the ordered extent.
7261 */
7262 if (write) {
7263 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007264 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007265 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7266 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7267 btrfs_free_reserved_extent(root, ordered->start,
7268 ordered->disk_len);
7269 btrfs_put_ordered_extent(ordered);
7270 btrfs_put_ordered_extent(ordered);
7271 }
7272 bio_endio(bio, ret);
7273}
7274
Chris Mason5a5f79b2010-05-26 21:33:37 -04007275static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7276 const struct iovec *iov, loff_t offset,
7277 unsigned long nr_segs)
7278{
7279 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007280 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007281 size_t size;
7282 unsigned long addr;
7283 unsigned blocksize_mask = root->sectorsize - 1;
7284 ssize_t retval = -EINVAL;
7285 loff_t end = offset;
7286
7287 if (offset & blocksize_mask)
7288 goto out;
7289
7290 /* Check the memory alignment. Blocks cannot straddle pages */
7291 for (seg = 0; seg < nr_segs; seg++) {
7292 addr = (unsigned long)iov[seg].iov_base;
7293 size = iov[seg].iov_len;
7294 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007295 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007296 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007297
7298 /* If this is a write we don't need to check anymore */
7299 if (rw & WRITE)
7300 continue;
7301
7302 /*
7303 * Check to make sure we don't have duplicate iov_base's in this
7304 * iovec, if so return EINVAL, otherwise we'll get csum errors
7305 * when reading back.
7306 */
7307 for (i = seg + 1; i < nr_segs; i++) {
7308 if (iov[seg].iov_base == iov[i].iov_base)
7309 goto out;
7310 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007311 }
7312 retval = 0;
7313out:
7314 return retval;
7315}
Josef Bacikeb838e72012-07-31 16:28:48 -04007316
Chris Mason16432982008-04-10 10:23:21 -04007317static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7318 const struct iovec *iov, loff_t offset,
7319 unsigned long nr_segs)
7320{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007321 struct file *file = iocb->ki_filp;
7322 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007323 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007324 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007325 bool wakeup = true;
7326 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007327 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007328
Chris Mason5a5f79b2010-05-26 21:33:37 -04007329 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007330 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007331 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007332
Miao Xie38851cc2013-02-08 07:04:11 +00007333 atomic_inc(&inode->i_dio_count);
7334 smp_mb__after_atomic_inc();
7335
Miao Xie09348562013-02-07 10:12:07 +00007336 if (rw & WRITE) {
7337 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007338 /*
7339 * If the write DIO is beyond the EOF, we need update
7340 * the isize, but it is protected by i_mutex. So we can
7341 * not unlock the i_mutex at this case.
7342 */
7343 if (offset + count <= inode->i_size) {
7344 mutex_unlock(&inode->i_mutex);
7345 relock = true;
7346 }
Miao Xie09348562013-02-07 10:12:07 +00007347 ret = btrfs_delalloc_reserve_space(inode, count);
7348 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007349 goto out;
7350 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7351 &BTRFS_I(inode)->runtime_flags))) {
7352 inode_dio_done(inode);
7353 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7354 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007355 }
7356
7357 ret = __blockdev_direct_IO(rw, iocb, inode,
7358 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7359 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007360 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007361 if (rw & WRITE) {
7362 if (ret < 0 && ret != -EIOCBQUEUED)
7363 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007364 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007365 btrfs_delalloc_release_space(inode,
7366 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007367 else
7368 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007369 }
Miao Xie38851cc2013-02-08 07:04:11 +00007370out:
Miao Xie2e60a512013-02-08 07:01:08 +00007371 if (wakeup)
7372 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007373 if (relock)
7374 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007375
7376 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007377}
7378
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007379#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7380
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007381static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7382 __u64 start, __u64 len)
7383{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007384 int ret;
7385
7386 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7387 if (ret)
7388 return ret;
7389
Chris Masonec29ed52011-02-23 16:23:20 -05007390 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007391}
7392
Chris Mason9ebefb182007-06-15 13:50:00 -04007393int btrfs_readpage(struct file *file, struct page *page)
7394{
Chris Masond1310b22008-01-24 16:13:08 -05007395 struct extent_io_tree *tree;
7396 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007397 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007398}
Chris Mason1832a6d2007-12-21 16:27:21 -05007399
Chris Mason39279cc2007-06-12 06:35:45 -04007400static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7401{
Chris Masond1310b22008-01-24 16:13:08 -05007402 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007403
7404
7405 if (current->flags & PF_MEMALLOC) {
7406 redirty_page_for_writepage(wbc, page);
7407 unlock_page(page);
7408 return 0;
7409 }
Chris Masond1310b22008-01-24 16:13:08 -05007410 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007411 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7412}
Chris Mason39279cc2007-06-12 06:35:45 -04007413
Chris Masonf4219502008-07-22 11:18:09 -04007414int btrfs_writepages(struct address_space *mapping,
7415 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007416{
Chris Masond1310b22008-01-24 16:13:08 -05007417 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007418
Chris Masond1310b22008-01-24 16:13:08 -05007419 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007420 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7421}
7422
Chris Mason3ab2fb52007-11-08 10:59:22 -05007423static int
7424btrfs_readpages(struct file *file, struct address_space *mapping,
7425 struct list_head *pages, unsigned nr_pages)
7426{
Chris Masond1310b22008-01-24 16:13:08 -05007427 struct extent_io_tree *tree;
7428 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007429 return extent_readpages(tree, mapping, pages, nr_pages,
7430 btrfs_get_extent);
7431}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007432static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007433{
Chris Masond1310b22008-01-24 16:13:08 -05007434 struct extent_io_tree *tree;
7435 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007436 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007437
Chris Masond1310b22008-01-24 16:13:08 -05007438 tree = &BTRFS_I(page->mapping->host)->io_tree;
7439 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007440 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007441 if (ret == 1) {
7442 ClearPagePrivate(page);
7443 set_page_private(page, 0);
7444 page_cache_release(page);
7445 }
7446 return ret;
7447}
Chris Mason39279cc2007-06-12 06:35:45 -04007448
Chris Masone6dcd2d2008-07-17 12:53:50 -04007449static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7450{
Chris Mason98509cf2008-09-11 15:51:43 -04007451 if (PageWriteback(page) || PageDirty(page))
7452 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007453 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007454}
7455
Chris Masona52d9a82007-08-27 16:49:44 -04007456static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7457{
Josef Bacik5fd02042012-05-02 14:00:54 -04007458 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007459 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007460 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007461 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007462 u64 page_start = page_offset(page);
7463 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007464
Chris Mason8b62b722009-09-02 16:53:46 -04007465 /*
7466 * we have the page locked, so new writeback can't start,
7467 * and the dirty bit won't be cleared while we are here.
7468 *
7469 * Wait for IO on this page so that we can safely clear
7470 * the PagePrivate2 bit and do ordered accounting
7471 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007472 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007473
Josef Bacik5fd02042012-05-02 14:00:54 -04007474 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007475 if (offset) {
7476 btrfs_releasepage(page, GFP_NOFS);
7477 return;
7478 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007479 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007480 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007481 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007482 /*
7483 * IO on this page will never be started, so we need
7484 * to account for any ordered extents now
7485 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007486 clear_extent_bit(tree, page_start, page_end,
7487 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007488 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7489 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007490 /*
7491 * whoever cleared the private bit is responsible
7492 * for the finish_ordered_io
7493 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007494 if (TestClearPagePrivate2(page) &&
7495 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7496 PAGE_CACHE_SIZE, 1)) {
7497 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007498 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007499 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007500 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007501 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007502 }
7503 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007504 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007505 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7506 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007507 __btrfs_releasepage(page, GFP_NOFS);
7508
Chris Mason4a096752008-07-21 10:29:44 -04007509 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007510 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007511 ClearPagePrivate(page);
7512 set_page_private(page, 0);
7513 page_cache_release(page);
7514 }
Chris Mason39279cc2007-06-12 06:35:45 -04007515}
7516
Chris Mason9ebefb182007-06-15 13:50:00 -04007517/*
7518 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7519 * called from a page fault handler when a page is first dirtied. Hence we must
7520 * be careful to check for EOF conditions here. We set the page up correctly
7521 * for a written page which means we get ENOSPC checking when writing into
7522 * holes and correct delalloc and unwritten extent mapping on filesystems that
7523 * support these features.
7524 *
7525 * We are not allowed to take the i_mutex here so we have to play games to
7526 * protect against truncate races as the page could now be beyond EOF. Because
7527 * vmtruncate() writes the inode size before removing pages, once we have the
7528 * page lock we can determine safely if the page is beyond EOF. If it is not
7529 * beyond EOF, then the page is guaranteed safe against truncation until we
7530 * unlock the page.
7531 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007532int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007533{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007534 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007535 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007536 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007537 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7538 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007539 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007540 char *kaddr;
7541 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007542 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007543 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007544 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007545 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007546 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007547
Jan Karab2b5ef52012-06-12 16:20:45 +02007548 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007549 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007550 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007551 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007552 reserved = 1;
7553 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007554 if (ret) {
7555 if (ret == -ENOMEM)
7556 ret = VM_FAULT_OOM;
7557 else /* -ENOSPC, -EIO, etc */
7558 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007559 if (reserved)
7560 goto out;
7561 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007562 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007563
Nick Piggin56a76f82009-03-31 15:23:23 -07007564 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007565again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007566 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007567 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007568 page_start = page_offset(page);
7569 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007570
Chris Mason9ebefb182007-06-15 13:50:00 -04007571 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007572 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007573 /* page got truncated out from underneath us */
7574 goto out_unlock;
7575 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007576 wait_on_page_writeback(page);
7577
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007578 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007579 set_page_extent_mapped(page);
7580
Chris Masoneb84ae02008-07-17 13:53:27 -04007581 /*
7582 * we can't set the delalloc bits if there are pending ordered
7583 * extents. Drop our locks and wait for them to finish
7584 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007585 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7586 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007587 unlock_extent_cached(io_tree, page_start, page_end,
7588 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007589 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007590 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007591 btrfs_put_ordered_extent(ordered);
7592 goto again;
7593 }
7594
Josef Bacikfbf19082009-10-01 17:10:23 -04007595 /*
7596 * XXX - page_mkwrite gets called every time the page is dirtied, even
7597 * if it was already dirty, so for space accounting reasons we need to
7598 * clear any delalloc bits for the range we are fixing to save. There
7599 * is probably a better way to do this, but for now keep consistent with
7600 * prepare_pages in the normal write path.
7601 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007602 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007603 EXTENT_DIRTY | EXTENT_DELALLOC |
7604 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007605 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007606
Josef Bacik2ac55d42010-02-03 19:33:23 +00007607 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7608 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007609 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007610 unlock_extent_cached(io_tree, page_start, page_end,
7611 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007612 ret = VM_FAULT_SIGBUS;
7613 goto out_unlock;
7614 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007615 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007616
7617 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007618 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007619 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007620 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007621 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007622
Chris Masone6dcd2d2008-07-17 12:53:50 -04007623 if (zero_start != PAGE_CACHE_SIZE) {
7624 kaddr = kmap(page);
7625 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7626 flush_dcache_page(page);
7627 kunmap(page);
7628 }
Chris Mason247e7432008-07-17 12:53:51 -04007629 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007630 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007631 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007632
Chris Mason257c62e2009-10-13 13:21:08 -04007633 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7634 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007635 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007636
Josef Bacik2ac55d42010-02-03 19:33:23 +00007637 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007638
7639out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007640 if (!ret) {
7641 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007642 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007643 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007644 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007645out:
Josef Bacikec39e182012-01-12 19:10:12 -05007646 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007647out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007648 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007649 return ret;
7650}
7651
Josef Bacika41ad392011-01-31 15:30:16 -05007652static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007653{
7654 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007655 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007656 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007657 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007658 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007659 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007660 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007661
Josef Bacik2aaa6652012-08-29 14:27:18 -04007662 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007663 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007664 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007665
Chris Mason4a096752008-07-21 10:29:44 -04007666 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007667 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007668
Josef Bacikfcb80c22011-05-03 10:40:22 -04007669 /*
7670 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7671 * 3 things going on here
7672 *
7673 * 1) We need to reserve space for our orphan item and the space to
7674 * delete our orphan item. Lord knows we don't want to have a dangling
7675 * orphan item because we didn't reserve space to remove it.
7676 *
7677 * 2) We need to reserve space to update our inode.
7678 *
7679 * 3) We need to have something to cache all the space that is going to
7680 * be free'd up by the truncate operation, but also have some slack
7681 * space reserved in case it uses space during the truncate (thank you
7682 * very much snapshotting).
7683 *
7684 * And we need these to all be seperate. The fact is we can use alot of
7685 * space doing the truncate, and we have no earthly idea how much space
7686 * we will use, so we need the truncate reservation to be seperate so it
7687 * doesn't end up using space reserved for updating the inode or
7688 * removing the orphan item. We also need to be able to stop the
7689 * transaction and start a new one, which means we need to be able to
7690 * update the inode several times, and we have no idea of knowing how
7691 * many times that will be, so we can't just reserve 1 item for the
7692 * entirety of the opration, so that has to be done seperately as well.
7693 * Then there is the orphan item, which does indeed need to be held on
7694 * to for the whole operation, and we need nobody to touch this reserved
7695 * space except the orphan code.
7696 *
7697 * So that leaves us with
7698 *
7699 * 1) root->orphan_block_rsv - for the orphan deletion.
7700 * 2) rsv - for the truncate reservation, which we will steal from the
7701 * transaction reservation.
7702 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7703 * updating the inode.
7704 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007705 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007706 if (!rsv)
7707 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007708 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007709 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007710
Josef Bacik907cbce2011-08-08 13:46:15 -04007711 /*
Josef Bacik07127182011-08-19 10:29:59 -04007712 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007713 * 1 for updating the inode.
7714 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007715 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007716 if (IS_ERR(trans)) {
7717 err = PTR_ERR(trans);
7718 goto out;
7719 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007720
Josef Bacik907cbce2011-08-08 13:46:15 -04007721 /* Migrate the slack space for the truncate to our reserve */
7722 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7723 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007724 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007725
Chris Mason5a3f23d2009-03-31 13:27:11 -04007726 /*
7727 * setattr is responsible for setting the ordered_data_close flag,
7728 * but that is only tested during the last file release. That
7729 * could happen well after the next commit, leaving a great big
7730 * window where new writes may get lost if someone chooses to write
7731 * to this file after truncating to zero
7732 *
7733 * The inode doesn't have any dirty data here, and so if we commit
7734 * this is a noop. If someone immediately starts writing to the inode
7735 * it is very likely we'll catch some of their writes in this
7736 * transaction, and the commit will find this file on the ordered
7737 * data list with good things to send down.
7738 *
7739 * This is a best effort solution, there is still a window where
7740 * using truncate to replace the contents of the file will
7741 * end up with a zero length file after a crash.
7742 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007743 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7744 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007745 btrfs_add_ordered_operation(trans, root, inode);
7746
Josef Bacik5dc562c2012-08-17 13:14:17 -04007747 /*
7748 * So if we truncate and then write and fsync we normally would just
7749 * write the extents that changed, which is a problem if we need to
7750 * first truncate that entire inode. So set this flag so we write out
7751 * all of the extents in the inode to the sync log so we're completely
7752 * safe.
7753 */
7754 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007755 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007756
Yan, Zheng80825102009-11-12 09:35:36 +00007757 while (1) {
7758 ret = btrfs_truncate_inode_items(trans, root, inode,
7759 inode->i_size,
7760 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007761 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007762 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007763 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007764 }
Chris Mason39279cc2007-06-12 06:35:45 -04007765
Josef Bacikfcb80c22011-05-03 10:40:22 -04007766 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007767 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007768 if (ret) {
7769 err = ret;
7770 break;
7771 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007772
Yan, Zheng80825102009-11-12 09:35:36 +00007773 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007774 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007775
7776 trans = btrfs_start_transaction(root, 2);
7777 if (IS_ERR(trans)) {
7778 ret = err = PTR_ERR(trans);
7779 trans = NULL;
7780 break;
7781 }
7782
7783 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7784 rsv, min_size);
7785 BUG_ON(ret); /* shouldn't happen */
7786 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007787 }
7788
7789 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007790 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007791 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007792 if (ret)
7793 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007794 }
7795
Chris Mason917c16b2011-11-08 14:49:59 -05007796 if (trans) {
7797 trans->block_rsv = &root->fs_info->trans_block_rsv;
7798 ret = btrfs_update_inode(trans, root, inode);
7799 if (ret && !err)
7800 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007801
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007802 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007803 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007804 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007805
7806out:
7807 btrfs_free_block_rsv(root, rsv);
7808
Josef Bacik3893e332011-01-31 16:03:11 -05007809 if (ret && !err)
7810 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007811
Josef Bacik3893e332011-01-31 16:03:11 -05007812 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007813}
7814
Sven Wegener3b963622008-06-09 21:57:42 -04007815/*
Chris Masond352ac62008-09-29 15:18:18 -04007816 * create a new subvolume directory/inode (helper for the ioctl).
7817 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007818int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007819 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007820{
Chris Mason39279cc2007-06-12 06:35:45 -04007821 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007822 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007823 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007824
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007825 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7826 new_dirid, new_dirid,
7827 S_IFDIR | (~current_umask() & S_IRWXUGO),
7828 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007829 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007830 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007831 inode->i_op = &btrfs_dir_inode_operations;
7832 inode->i_fop = &btrfs_dir_file_operations;
7833
Miklos Szeredibfe86842011-10-28 14:13:29 +02007834 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007835 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007836
Yan, Zheng76dda932009-09-21 16:00:26 -04007837 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007838
Yan, Zheng76dda932009-09-21 16:00:26 -04007839 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007840 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007841}
7842
Chris Mason39279cc2007-06-12 06:35:45 -04007843struct inode *btrfs_alloc_inode(struct super_block *sb)
7844{
7845 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007846 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007847
7848 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7849 if (!ei)
7850 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007851
7852 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007853 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007854 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007855 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007856 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007857 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007858 ei->disk_i_size = 0;
7859 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007860 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007861 ei->index_cnt = (u64)-1;
7862 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007863 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007864
Josef Bacik9e0baf62011-07-15 15:16:44 +00007865 spin_lock_init(&ei->lock);
7866 ei->outstanding_extents = 0;
7867 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007868
Josef Bacik72ac3c02012-05-23 14:13:11 -04007869 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007870 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007871
Miao Xie16cdcec2011-04-22 18:12:22 +08007872 ei->delayed_node = NULL;
7873
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007874 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007875 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007876 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7877 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007878 ei->io_tree.track_uptodate = 1;
7879 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007880 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007881 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007882 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007883 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007884 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007885 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007886 RB_CLEAR_NODE(&ei->rb_node);
7887
7888 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007889}
7890
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007891static void btrfs_i_callback(struct rcu_head *head)
7892{
7893 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007894 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7895}
7896
Chris Mason39279cc2007-06-12 06:35:45 -04007897void btrfs_destroy_inode(struct inode *inode)
7898{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007899 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007900 struct btrfs_root *root = BTRFS_I(inode)->root;
7901
Al Virob3d9b7a2012-06-09 13:51:19 -04007902 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007903 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007904 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7905 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007906 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7907 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007908
Chris Mason5a3f23d2009-03-31 13:27:11 -04007909 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007910 * This can happen where we create an inode, but somebody else also
7911 * created the same inode and we need to destroy the one we already
7912 * created.
7913 */
7914 if (!root)
7915 goto free;
7916
7917 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007918 * Make sure we're properly removed from the ordered operation
7919 * lists.
7920 */
7921 smp_mb();
7922 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7923 spin_lock(&root->fs_info->ordered_extent_lock);
7924 list_del_init(&BTRFS_I(inode)->ordered_operations);
7925 spin_unlock(&root->fs_info->ordered_extent_lock);
7926 }
7927
Josef Bacik8a35d952012-05-23 14:26:42 -04007928 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7929 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007930 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7931 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007932 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007933 }
Josef Bacik7b128762008-07-24 12:17:14 -04007934
Chris Masond3977122009-01-05 21:25:51 -05007935 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007936 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7937 if (!ordered)
7938 break;
7939 else {
Chris Masond3977122009-01-05 21:25:51 -05007940 printk(KERN_ERR "btrfs found ordered "
7941 "extent %llu %llu on inode cleanup\n",
7942 (unsigned long long)ordered->file_offset,
7943 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007944 btrfs_remove_ordered_extent(inode, ordered);
7945 btrfs_put_ordered_extent(ordered);
7946 btrfs_put_ordered_extent(ordered);
7947 }
7948 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007949 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007950 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007951free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007952 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007953 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007954}
7955
Al Viro45321ac2010-06-07 13:43:19 -04007956int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007957{
7958 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007959
Liu Bofa6ac872013-02-20 14:10:23 +00007960 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007961 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007962 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007963 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007964 else
Al Viro45321ac2010-06-07 13:43:19 -04007965 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007966}
7967
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007968static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007969{
7970 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7971
7972 inode_init_once(&ei->vfs_inode);
7973}
7974
7975void btrfs_destroy_cachep(void)
7976{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007977 /*
7978 * Make sure all delayed rcu free inodes are flushed before we
7979 * destroy cache.
7980 */
7981 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007982 if (btrfs_inode_cachep)
7983 kmem_cache_destroy(btrfs_inode_cachep);
7984 if (btrfs_trans_handle_cachep)
7985 kmem_cache_destroy(btrfs_trans_handle_cachep);
7986 if (btrfs_transaction_cachep)
7987 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007988 if (btrfs_path_cachep)
7989 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007990 if (btrfs_free_space_cachep)
7991 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007992 if (btrfs_delalloc_work_cachep)
7993 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007994}
7995
7996int btrfs_init_cachep(void)
7997{
David Sterba837e1972012-09-07 03:00:48 -06007998 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007999 sizeof(struct btrfs_inode), 0,
8000 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008001 if (!btrfs_inode_cachep)
8002 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008003
David Sterba837e1972012-09-07 03:00:48 -06008004 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008005 sizeof(struct btrfs_trans_handle), 0,
8006 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008007 if (!btrfs_trans_handle_cachep)
8008 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008009
David Sterba837e1972012-09-07 03:00:48 -06008010 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008011 sizeof(struct btrfs_transaction), 0,
8012 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008013 if (!btrfs_transaction_cachep)
8014 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008015
David Sterba837e1972012-09-07 03:00:48 -06008016 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008017 sizeof(struct btrfs_path), 0,
8018 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008019 if (!btrfs_path_cachep)
8020 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008021
David Sterba837e1972012-09-07 03:00:48 -06008022 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008023 sizeof(struct btrfs_free_space), 0,
8024 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8025 if (!btrfs_free_space_cachep)
8026 goto fail;
8027
Miao Xie8ccf6f192012-10-25 09:28:04 +00008028 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8029 sizeof(struct btrfs_delalloc_work), 0,
8030 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8031 NULL);
8032 if (!btrfs_delalloc_work_cachep)
8033 goto fail;
8034
Chris Mason39279cc2007-06-12 06:35:45 -04008035 return 0;
8036fail:
8037 btrfs_destroy_cachep();
8038 return -ENOMEM;
8039}
8040
8041static int btrfs_getattr(struct vfsmount *mnt,
8042 struct dentry *dentry, struct kstat *stat)
8043{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008044 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008045 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008046 u32 blocksize = inode->i_sb->s_blocksize;
8047
Chris Mason39279cc2007-06-12 06:35:45 -04008048 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008049 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008050 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008051
8052 spin_lock(&BTRFS_I(inode)->lock);
8053 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8054 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008055 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008056 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008057 return 0;
8058}
8059
Chris Masond3977122009-01-05 21:25:51 -05008060static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8061 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008062{
8063 struct btrfs_trans_handle *trans;
8064 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008065 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008066 struct inode *new_inode = new_dentry->d_inode;
8067 struct inode *old_inode = old_dentry->d_inode;
8068 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008069 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008070 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008071 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008072 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008073
Li Zefan33345d012011-04-20 10:31:50 +08008074 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008075 return -EPERM;
8076
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008077 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008078 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008079 return -EXDEV;
8080
Li Zefan33345d012011-04-20 10:31:50 +08008081 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8082 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008083 return -ENOTEMPTY;
8084
Chris Mason39279cc2007-06-12 06:35:45 -04008085 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008086 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008087 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008088
8089
8090 /* check for collisions, even if the name isn't there */
8091 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8092 new_dentry->d_name.name,
8093 new_dentry->d_name.len);
8094
8095 if (ret) {
8096 if (ret == -EEXIST) {
8097 /* we shouldn't get
8098 * eexist without a new_inode */
8099 if (!new_inode) {
8100 WARN_ON(1);
8101 return ret;
8102 }
8103 } else {
8104 /* maybe -EOVERFLOW */
8105 return ret;
8106 }
8107 }
8108 ret = 0;
8109
Chris Mason5a3f23d2009-03-31 13:27:11 -04008110 /*
8111 * we're using rename to replace one file with another.
8112 * and the replacement file is large. Start IO on it now so
8113 * we don't add too much work to the end of the transaction
8114 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008115 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008116 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8117 filemap_flush(old_inode->i_mapping);
8118
Yan, Zheng76dda932009-09-21 16:00:26 -04008119 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008120 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008121 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008122 /*
8123 * We want to reserve the absolute worst case amount of items. So if
8124 * both inodes are subvols and we need to unlink them then that would
8125 * require 4 item modifications, but if they are both normal inodes it
8126 * would require 5 item modifications, so we'll assume their normal
8127 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8128 * should cover the worst case number of items we'll modify.
8129 */
8130 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008131 if (IS_ERR(trans)) {
8132 ret = PTR_ERR(trans);
8133 goto out_notrans;
8134 }
Chris Mason5f39d392007-10-15 16:14:19 -04008135
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008136 if (dest != root)
8137 btrfs_record_root_in_trans(trans, dest);
8138
Yan, Zhenga5719522009-09-24 09:17:31 -04008139 ret = btrfs_set_inode_index(new_dir, &index);
8140 if (ret)
8141 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008142
Li Zefan33345d012011-04-20 10:31:50 +08008143 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008144 /* force full log commit if subvolume involved. */
8145 root->fs_info->last_trans_log_full_commit = trans->transid;
8146 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008147 ret = btrfs_insert_inode_ref(trans, dest,
8148 new_dentry->d_name.name,
8149 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008150 old_ino,
8151 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008152 if (ret)
8153 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008154 /*
8155 * this is an ugly little race, but the rename is required
8156 * to make sure that if we crash, the inode is either at the
8157 * old name or the new one. pinning the log transaction lets
8158 * us make sure we don't allow a log commit to come in after
8159 * we unlink the name but before we add the new name back in.
8160 */
8161 btrfs_pin_log_trans(root);
8162 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008163 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008164 * make sure the inode gets flushed if it is replacing
8165 * something.
8166 */
Li Zefan33345d012011-04-20 10:31:50 +08008167 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008168 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008169
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008170 inode_inc_iversion(old_dir);
8171 inode_inc_iversion(new_dir);
8172 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008173 old_dir->i_ctime = old_dir->i_mtime = ctime;
8174 new_dir->i_ctime = new_dir->i_mtime = ctime;
8175 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008176
Chris Mason12fcfd22009-03-24 10:24:20 -04008177 if (old_dentry->d_parent != new_dentry->d_parent)
8178 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8179
Li Zefan33345d012011-04-20 10:31:50 +08008180 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008181 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8182 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8183 old_dentry->d_name.name,
8184 old_dentry->d_name.len);
8185 } else {
Al Viro92986792011-03-04 17:14:37 +00008186 ret = __btrfs_unlink_inode(trans, root, old_dir,
8187 old_dentry->d_inode,
8188 old_dentry->d_name.name,
8189 old_dentry->d_name.len);
8190 if (!ret)
8191 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008192 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008193 if (ret) {
8194 btrfs_abort_transaction(trans, root, ret);
8195 goto out_fail;
8196 }
Chris Mason39279cc2007-06-12 06:35:45 -04008197
8198 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008199 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008200 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008201 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008202 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8203 root_objectid = BTRFS_I(new_inode)->location.objectid;
8204 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8205 root_objectid,
8206 new_dentry->d_name.name,
8207 new_dentry->d_name.len);
8208 BUG_ON(new_inode->i_nlink == 0);
8209 } else {
8210 ret = btrfs_unlink_inode(trans, dest, new_dir,
8211 new_dentry->d_inode,
8212 new_dentry->d_name.name,
8213 new_dentry->d_name.len);
8214 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008215 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008216 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008217 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008218 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008219 if (ret) {
8220 btrfs_abort_transaction(trans, root, ret);
8221 goto out_fail;
8222 }
Chris Mason39279cc2007-06-12 06:35:45 -04008223 }
Josef Bacikaec74772008-07-24 12:12:38 -04008224
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008225 ret = btrfs_add_link(trans, new_dir, old_inode,
8226 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008227 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008228 if (ret) {
8229 btrfs_abort_transaction(trans, root, ret);
8230 goto out_fail;
8231 }
Chris Mason39279cc2007-06-12 06:35:45 -04008232
Li Zefan33345d012011-04-20 10:31:50 +08008233 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008234 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008235 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008236 btrfs_end_log_trans(root);
8237 }
Chris Mason39279cc2007-06-12 06:35:45 -04008238out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008239 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008240out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008241 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008242 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008243
Chris Mason39279cc2007-06-12 06:35:45 -04008244 return ret;
8245}
8246
Miao Xie8ccf6f192012-10-25 09:28:04 +00008247static void btrfs_run_delalloc_work(struct btrfs_work *work)
8248{
8249 struct btrfs_delalloc_work *delalloc_work;
8250
8251 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8252 work);
8253 if (delalloc_work->wait)
8254 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8255 else
8256 filemap_flush(delalloc_work->inode->i_mapping);
8257
8258 if (delalloc_work->delay_iput)
8259 btrfs_add_delayed_iput(delalloc_work->inode);
8260 else
8261 iput(delalloc_work->inode);
8262 complete(&delalloc_work->completion);
8263}
8264
8265struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8266 int wait, int delay_iput)
8267{
8268 struct btrfs_delalloc_work *work;
8269
8270 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8271 if (!work)
8272 return NULL;
8273
8274 init_completion(&work->completion);
8275 INIT_LIST_HEAD(&work->list);
8276 work->inode = inode;
8277 work->wait = wait;
8278 work->delay_iput = delay_iput;
8279 work->work.func = btrfs_run_delalloc_work;
8280
8281 return work;
8282}
8283
8284void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8285{
8286 wait_for_completion(&work->completion);
8287 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8288}
8289
Chris Masond352ac62008-09-29 15:18:18 -04008290/*
8291 * some fairly slow code that needs optimization. This walks the list
8292 * of all the inodes with pending delalloc and forces them to disk.
8293 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008294int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008295{
Chris Masonea8c2812008-08-04 23:17:27 -04008296 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008297 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008298 struct btrfs_delalloc_work *work, *next;
8299 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008300 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008301 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008302
Yan Zhengc146afa2008-11-12 14:34:12 -05008303 if (root->fs_info->sb->s_flags & MS_RDONLY)
8304 return -EROFS;
8305
Miao Xie8ccf6f192012-10-25 09:28:04 +00008306 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008307 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008308
Chris Mason75eff682008-12-15 15:54:40 -05008309 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008310 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8311 while (!list_empty(&splice)) {
8312 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008313 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008314
8315 list_del_init(&binode->delalloc_inodes);
8316
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008317 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008318 if (!inode) {
8319 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8320 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008321 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008322 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008323
8324 list_add_tail(&binode->delalloc_inodes,
8325 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008326 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008327
8328 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8329 if (unlikely(!work)) {
8330 ret = -ENOMEM;
8331 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008332 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008333 list_add_tail(&work->list, &works);
8334 btrfs_queue_worker(&root->fs_info->flush_workers,
8335 &work->work);
8336
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008337 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008338 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008339 }
Chris Mason75eff682008-12-15 15:54:40 -05008340 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008341
Miao Xie1eafa6c2013-01-22 10:49:00 +00008342 list_for_each_entry_safe(work, next, &works, list) {
8343 list_del_init(&work->list);
8344 btrfs_wait_and_free_delalloc_work(work);
8345 }
8346
Chris Mason8c8bee12008-09-29 11:19:10 -04008347 /* the filemap_flush will queue IO into the worker threads, but
8348 * we have to make sure the IO is actually started and that
8349 * ordered extents get created before we return
8350 */
8351 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008352 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008353 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008354 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008355 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8356 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008357 }
8358 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008359 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008360out:
8361 list_for_each_entry_safe(work, next, &works, list) {
8362 list_del_init(&work->list);
8363 btrfs_wait_and_free_delalloc_work(work);
8364 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008365
8366 if (!list_empty_careful(&splice)) {
8367 spin_lock(&root->fs_info->delalloc_lock);
8368 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8369 spin_unlock(&root->fs_info->delalloc_lock);
8370 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008371 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008372}
8373
Chris Mason39279cc2007-06-12 06:35:45 -04008374static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8375 const char *symname)
8376{
8377 struct btrfs_trans_handle *trans;
8378 struct btrfs_root *root = BTRFS_I(dir)->root;
8379 struct btrfs_path *path;
8380 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008381 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008382 int err;
8383 int drop_inode = 0;
8384 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008385 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008386 int name_len;
8387 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008388 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008389 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008390 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008391
8392 name_len = strlen(symname) + 1;
8393 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8394 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008395
Josef Bacik9ed74f22009-09-11 16:12:44 -04008396 /*
8397 * 2 items for inode item and ref
8398 * 2 items for dir items
8399 * 1 item for xattr if selinux is on
8400 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008401 trans = btrfs_start_transaction(root, 5);
8402 if (IS_ERR(trans))
8403 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008404
Li Zefan581bb052011-04-20 10:06:11 +08008405 err = btrfs_find_free_ino(root, &objectid);
8406 if (err)
8407 goto out_unlock;
8408
Josef Bacikaec74772008-07-24 12:12:38 -04008409 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008410 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008411 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008412 if (IS_ERR(inode)) {
8413 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008414 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008415 }
Chris Mason39279cc2007-06-12 06:35:45 -04008416
Eric Paris2a7dba32011-02-01 11:05:39 -05008417 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008418 if (err) {
8419 drop_inode = 1;
8420 goto out_unlock;
8421 }
8422
Casey Schauflerad19db72011-12-15 10:09:07 -05008423 /*
8424 * If the active LSM wants to access the inode during
8425 * d_instantiate it needs these. Smack checks to see
8426 * if the filesystem supports xattrs by looking at the
8427 * ops vector.
8428 */
8429 inode->i_fop = &btrfs_file_operations;
8430 inode->i_op = &btrfs_file_inode_operations;
8431
Josef Bacika1b075d2010-11-19 20:36:11 +00008432 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008433 if (err)
8434 drop_inode = 1;
8435 else {
8436 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008437 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008438 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008439 }
Chris Mason39279cc2007-06-12 06:35:45 -04008440 if (drop_inode)
8441 goto out_unlock;
8442
8443 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008444 if (!path) {
8445 err = -ENOMEM;
8446 drop_inode = 1;
8447 goto out_unlock;
8448 }
Li Zefan33345d012011-04-20 10:31:50 +08008449 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008450 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008451 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8452 datasize = btrfs_file_extent_calc_inline_size(name_len);
8453 err = btrfs_insert_empty_item(trans, root, path, &key,
8454 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008455 if (err) {
8456 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008457 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008458 goto out_unlock;
8459 }
Chris Mason5f39d392007-10-15 16:14:19 -04008460 leaf = path->nodes[0];
8461 ei = btrfs_item_ptr(leaf, path->slots[0],
8462 struct btrfs_file_extent_item);
8463 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8464 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008465 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008466 btrfs_set_file_extent_encryption(leaf, ei, 0);
8467 btrfs_set_file_extent_compression(leaf, ei, 0);
8468 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8469 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8470
Chris Mason39279cc2007-06-12 06:35:45 -04008471 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008472 write_extent_buffer(leaf, symname, ptr, name_len);
8473 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008474 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008475
Chris Mason39279cc2007-06-12 06:35:45 -04008476 inode->i_op = &btrfs_symlink_inode_operations;
8477 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008478 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008479 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008480 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008481 err = btrfs_update_inode(trans, root, inode);
8482 if (err)
8483 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008484
8485out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008486 if (!err)
8487 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008488 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008489 if (drop_inode) {
8490 inode_dec_link_count(inode);
8491 iput(inode);
8492 }
Liu Bob53d3f52012-11-14 14:34:34 +00008493 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008494 return err;
8495}
Chris Mason16432982008-04-10 10:23:21 -04008496
Josef Bacik0af3d002010-06-21 14:48:16 -04008497static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8498 u64 start, u64 num_bytes, u64 min_size,
8499 loff_t actual_len, u64 *alloc_hint,
8500 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008501{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008502 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8503 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008504 struct btrfs_root *root = BTRFS_I(inode)->root;
8505 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008506 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008507 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008508 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008509 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008510 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008511
Josef Bacik0af3d002010-06-21 14:48:16 -04008512 if (trans)
8513 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008514 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008515 if (own_trans) {
8516 trans = btrfs_start_transaction(root, 3);
8517 if (IS_ERR(trans)) {
8518 ret = PTR_ERR(trans);
8519 break;
8520 }
Yan Zhengd899e052008-10-30 14:25:28 -04008521 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008522
Chris Mason154ea282013-03-05 11:11:26 -05008523 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8524 cur_bytes = max(cur_bytes, min_size);
8525 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008526 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008527 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008528 if (own_trans)
8529 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008530 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008531 }
8532
Yan Zhengd899e052008-10-30 14:25:28 -04008533 ret = insert_reserved_file_extent(trans, inode,
8534 cur_offset, ins.objectid,
8535 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008536 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008537 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008538 if (ret) {
8539 btrfs_abort_transaction(trans, root, ret);
8540 if (own_trans)
8541 btrfs_end_transaction(trans, root);
8542 break;
8543 }
Chris Masona1ed8352009-09-11 12:27:37 -04008544 btrfs_drop_extent_cache(inode, cur_offset,
8545 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008546
Josef Bacik5dc562c2012-08-17 13:14:17 -04008547 em = alloc_extent_map();
8548 if (!em) {
8549 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8550 &BTRFS_I(inode)->runtime_flags);
8551 goto next;
8552 }
8553
8554 em->start = cur_offset;
8555 em->orig_start = cur_offset;
8556 em->len = ins.offset;
8557 em->block_start = ins.objectid;
8558 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008559 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008560 em->bdev = root->fs_info->fs_devices->latest_bdev;
8561 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8562 em->generation = trans->transid;
8563
8564 while (1) {
8565 write_lock(&em_tree->lock);
8566 ret = add_extent_mapping(em_tree, em);
8567 if (!ret)
8568 list_move(&em->list,
8569 &em_tree->modified_extents);
8570 write_unlock(&em_tree->lock);
8571 if (ret != -EEXIST)
8572 break;
8573 btrfs_drop_extent_cache(inode, cur_offset,
8574 cur_offset + ins.offset - 1,
8575 0);
8576 }
8577 free_extent_map(em);
8578next:
Yan Zhengd899e052008-10-30 14:25:28 -04008579 num_bytes -= ins.offset;
8580 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008581 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008582
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008583 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008584 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008585 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008586 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008587 (actual_len > inode->i_size) &&
8588 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008589 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008590 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008591 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008592 i_size = cur_offset;
8593 i_size_write(inode, i_size);
8594 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008595 }
8596
Yan Zhengd899e052008-10-30 14:25:28 -04008597 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008598
8599 if (ret) {
8600 btrfs_abort_transaction(trans, root, ret);
8601 if (own_trans)
8602 btrfs_end_transaction(trans, root);
8603 break;
8604 }
Yan Zhengd899e052008-10-30 14:25:28 -04008605
Josef Bacik0af3d002010-06-21 14:48:16 -04008606 if (own_trans)
8607 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008608 }
Yan Zhengd899e052008-10-30 14:25:28 -04008609 return ret;
8610}
8611
Josef Bacik0af3d002010-06-21 14:48:16 -04008612int btrfs_prealloc_file_range(struct inode *inode, int mode,
8613 u64 start, u64 num_bytes, u64 min_size,
8614 loff_t actual_len, u64 *alloc_hint)
8615{
8616 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8617 min_size, actual_len, alloc_hint,
8618 NULL);
8619}
8620
8621int btrfs_prealloc_file_range_trans(struct inode *inode,
8622 struct btrfs_trans_handle *trans, int mode,
8623 u64 start, u64 num_bytes, u64 min_size,
8624 loff_t actual_len, u64 *alloc_hint)
8625{
8626 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8627 min_size, actual_len, alloc_hint, trans);
8628}
8629
Chris Masone6dcd2d2008-07-17 12:53:50 -04008630static int btrfs_set_page_dirty(struct page *page)
8631{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008632 return __set_page_dirty_nobuffers(page);
8633}
8634
Al Viro10556cb2011-06-20 19:28:19 -04008635static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008636{
Li Zefanb83cc962010-12-20 16:04:08 +08008637 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008638 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008639
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008640 if (mask & MAY_WRITE &&
8641 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8642 if (btrfs_root_readonly(root))
8643 return -EROFS;
8644 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8645 return -EACCES;
8646 }
Al Viro2830ba72011-06-20 19:16:29 -04008647 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008648}
Chris Mason39279cc2007-06-12 06:35:45 -04008649
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008650static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008651 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008652 .lookup = btrfs_lookup,
8653 .create = btrfs_create,
8654 .unlink = btrfs_unlink,
8655 .link = btrfs_link,
8656 .mkdir = btrfs_mkdir,
8657 .rmdir = btrfs_rmdir,
8658 .rename = btrfs_rename,
8659 .symlink = btrfs_symlink,
8660 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008661 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008662 .setxattr = btrfs_setxattr,
8663 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008664 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008665 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008666 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008667 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008668};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008669static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008670 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008671 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008672 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008673};
Yan, Zheng76dda932009-09-21 16:00:26 -04008674
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008675static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008676 .llseek = generic_file_llseek,
8677 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008678 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008679 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008680#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008681 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008682#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008683 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008684 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008685};
8686
Chris Masond1310b22008-01-24 16:13:08 -05008687static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008688 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008689 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008690 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008691 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008692 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008693 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008694 .set_bit_hook = btrfs_set_bit_hook,
8695 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008696 .merge_extent_hook = btrfs_merge_extent_hook,
8697 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008698};
8699
Chris Mason35054392009-01-21 13:11:13 -05008700/*
8701 * btrfs doesn't support the bmap operation because swapfiles
8702 * use bmap to make a mapping of extents in the file. They assume
8703 * these extents won't change over the life of the file and they
8704 * use the bmap result to do IO directly to the drive.
8705 *
8706 * the btrfs bmap call would return logical addresses that aren't
8707 * suitable for IO and they also will change frequently as COW
8708 * operations happen. So, swapfile + btrfs == corruption.
8709 *
8710 * For now we're avoiding this by dropping bmap.
8711 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008712static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008713 .readpage = btrfs_readpage,
8714 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008715 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008716 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008717 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008718 .invalidatepage = btrfs_invalidatepage,
8719 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008720 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008721 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008722};
8723
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008724static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008725 .readpage = btrfs_readpage,
8726 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008727 .invalidatepage = btrfs_invalidatepage,
8728 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008729};
8730
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008731static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008732 .getattr = btrfs_getattr,
8733 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008734 .setxattr = btrfs_setxattr,
8735 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008736 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008737 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008738 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008739 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008740 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008741 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008742};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008743static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008744 .getattr = btrfs_getattr,
8745 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008746 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008747 .setxattr = btrfs_setxattr,
8748 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008749 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008750 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008751 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008752 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008753};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008754static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008755 .readlink = generic_readlink,
8756 .follow_link = page_follow_link_light,
8757 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008758 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008759 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008760 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008761 .setxattr = btrfs_setxattr,
8762 .getxattr = btrfs_getxattr,
8763 .listxattr = btrfs_listxattr,
8764 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008765 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008766 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008767};
Yan, Zheng76dda932009-09-21 16:00:26 -04008768
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008769const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008770 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008771 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008772};