blob: 25e6a8e1014e4b8728fef089990944ef2935acf9 [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050045#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Chris Mason39279cc2007-06-12 06:35:45 -040060
61struct btrfs_iget_args {
62 u64 ino;
63 struct btrfs_root *root;
64};
65
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070066static const struct inode_operations btrfs_dir_inode_operations;
67static const struct inode_operations btrfs_symlink_inode_operations;
68static const struct inode_operations btrfs_dir_ro_inode_operations;
69static const struct inode_operations btrfs_special_inode_operations;
70static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070071static const struct address_space_operations btrfs_aops;
72static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070073static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050074static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040075
76static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000077static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078struct kmem_cache *btrfs_trans_handle_cachep;
79struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050081struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082
83#define S_SHIFT 12
84static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
85 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
86 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
87 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
88 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
89 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
90 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
91 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
92};
93
Eric Sandeen3972f262013-01-12 02:57:22 +000094static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050095static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040096static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050097static noinline int cow_file_range(struct inode *inode,
98 struct page *locked_page,
99 u64 start, u64 end, int *page_started,
100 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400101static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
102 u64 len, u64 orig_start,
103 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400104 u64 orig_block_len, u64 ram_bytes,
105 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400106
Eric Sandeen48a3b632013-04-25 20:41:01 +0000107static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400108
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000109static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500110 struct inode *inode, struct inode *dir,
111 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500112{
113 int err;
114
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000115 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500116 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500117 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500118 return err;
119}
120
Chris Masond352ac62008-09-29 15:18:18 -0400121/*
Chris Masonc8b97812008-10-29 14:49:59 -0400122 * this does all the hard work for inserting an inline extent into
123 * the btree. The caller should have done a btrfs_drop_extents so that
124 * no overlapping inline items exist in the btree
125 */
Chris Masond3977122009-01-05 21:25:51 -0500126static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400127 struct btrfs_root *root, struct inode *inode,
128 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000129 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400130 struct page **compressed_pages)
131{
132 struct btrfs_key key;
133 struct btrfs_path *path;
134 struct extent_buffer *leaf;
135 struct page *page = NULL;
136 char *kaddr;
137 unsigned long ptr;
138 struct btrfs_file_extent_item *ei;
139 int err = 0;
140 int ret;
141 size_t cur_size = size;
142 size_t datasize;
143 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefanfe3f5662011-03-28 08:30:38 +0000145 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400146 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Chris Masond3977122009-01-05 21:25:51 -0500148 path = btrfs_alloc_path();
149 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400150 return -ENOMEM;
151
Chris Masonb9473432009-03-13 11:00:37 -0400152 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400153
Li Zefan33345d012011-04-20 10:31:50 +0800154 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400155 key.offset = start;
156 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400157 datasize = btrfs_file_extent_calc_inline_size(cur_size);
158
159 inode_add_bytes(inode, size);
160 ret = btrfs_insert_empty_item(trans, root, path, &key,
161 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400162 if (ret) {
163 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400164 goto fail;
165 }
166 leaf = path->nodes[0];
167 ei = btrfs_item_ptr(leaf, path->slots[0],
168 struct btrfs_file_extent_item);
169 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
170 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
171 btrfs_set_file_extent_encryption(leaf, ei, 0);
172 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
173 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
174 ptr = btrfs_file_extent_inline_start(ei);
175
Li Zefan261507a02010-12-17 14:21:50 +0800176 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400177 struct page *cpage;
178 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500179 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400180 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500181 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400182 PAGE_CACHE_SIZE);
183
Cong Wang7ac687d2011-11-25 23:14:28 +0800184 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400185 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800186 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400187
188 i++;
189 ptr += cur_size;
190 compressed_size -= cur_size;
191 }
192 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800193 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400194 } else {
195 page = find_get_page(inode->i_mapping,
196 start >> PAGE_CACHE_SHIFT);
197 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800198 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400199 offset = start & (PAGE_CACHE_SIZE - 1);
200 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 page_cache_release(page);
203 }
204 btrfs_mark_buffer_dirty(leaf);
205 btrfs_free_path(path);
206
Yan, Zhengc2167752009-11-12 09:34:21 +0000207 /*
208 * we're an inline extent, so nobody can
209 * extend the file past i_size without locking
210 * a page we already have locked.
211 *
212 * We must do any isize and inode updates
213 * before we unlock the pages. Otherwise we
214 * could end up racing with unlink.
215 */
Chris Masonc8b97812008-10-29 14:49:59 -0400216 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100217 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000218
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100219 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400220fail:
221 btrfs_free_path(path);
222 return err;
223}
224
225
226/*
227 * conditionally insert an inline extent into the file. This
228 * does the checks required to make sure the data is small enough
229 * to fit as an inline extent.
230 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400231static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400232 struct btrfs_root *root,
233 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000234 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400235 struct page **compressed_pages)
236{
237 u64 isize = i_size_read(inode);
238 u64 actual_end = min(end + 1, isize);
239 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000240 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400241 u64 data_len = inline_len;
242 int ret;
243
244 if (compressed_size)
245 data_len = compressed_size;
246
247 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400248 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400249 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
250 (!compressed_size &&
251 (actual_end & (root->sectorsize - 1)) == 0) ||
252 end + 1 < isize ||
253 data_len > root->fs_info->max_inline) {
254 return 1;
255 }
256
Josef Bacik26714852012-08-29 12:24:27 -0400257 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100258 if (ret)
259 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400260
261 if (isize > actual_end)
262 inline_len = min_t(u64, isize, actual_end);
263 ret = insert_inline_extent(trans, root, inode, start,
264 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000265 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400266 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 btrfs_abort_transaction(trans, root, ret);
268 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400269 } else if (ret == -ENOSPC) {
270 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100271 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400272
Josef Bacikbdc20e62013-02-28 13:23:38 -0500273 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400274 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400275 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400276 return 0;
277}
278
Chris Mason771ed682008-11-06 22:02:51 -0500279struct async_extent {
280 u64 start;
281 u64 ram_size;
282 u64 compressed_size;
283 struct page **pages;
284 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800285 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500286 struct list_head list;
287};
288
289struct async_cow {
290 struct inode *inode;
291 struct btrfs_root *root;
292 struct page *locked_page;
293 u64 start;
294 u64 end;
295 struct list_head extents;
296 struct btrfs_work work;
297};
298
299static noinline int add_async_extent(struct async_cow *cow,
300 u64 start, u64 ram_size,
301 u64 compressed_size,
302 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800303 unsigned long nr_pages,
304 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500305{
306 struct async_extent *async_extent;
307
308 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100309 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500310 async_extent->start = start;
311 async_extent->ram_size = ram_size;
312 async_extent->compressed_size = compressed_size;
313 async_extent->pages = pages;
314 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800315 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500316 list_add_tail(&async_extent->list, &cow->extents);
317 return 0;
318}
319
Chris Masonc8b97812008-10-29 14:49:59 -0400320/*
Chris Mason771ed682008-11-06 22:02:51 -0500321 * we create compressed extents in two phases. The first
322 * phase compresses a range of pages that have already been
323 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400324 *
Chris Mason771ed682008-11-06 22:02:51 -0500325 * This is done inside an ordered work queue, and the compression
326 * is spread across many cpus. The actual IO submission is step
327 * two, and the ordered work queue takes care of making sure that
328 * happens in the same order things were put onto the queue by
329 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400330 *
Chris Mason771ed682008-11-06 22:02:51 -0500331 * If this code finds it can't get good compression, it puts an
332 * entry onto the work queue to write the uncompressed bytes. This
333 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300334 * are written in the same order that the flusher thread sent them
335 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400336 */
Chris Mason771ed682008-11-06 22:02:51 -0500337static noinline int compress_file_range(struct inode *inode,
338 struct page *locked_page,
339 u64 start, u64 end,
340 struct async_cow *async_cow,
341 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400342{
343 struct btrfs_root *root = BTRFS_I(inode)->root;
344 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400345 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400346 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400347 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500348 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400349 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400350 struct page **pages = NULL;
351 unsigned long nr_pages;
352 unsigned long nr_pages_ret = 0;
353 unsigned long total_compressed = 0;
354 unsigned long total_in = 0;
355 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500356 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400357 int i;
358 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800359 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400360 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400361
Liu Bo4cb13e52012-03-29 09:57:45 -0400362 /* if this is a small write inside eof, kick off a defrag */
363 if ((end - start + 1) < 16 * 1024 &&
364 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400365 btrfs_add_inode_defrag(NULL, inode);
366
Chris Mason42dc7ba2008-12-15 11:44:56 -0500367 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400368again:
369 will_compress = 0;
370 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
371 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
372
Chris Masonf03d9301f2009-02-04 09:31:06 -0500373 /*
374 * we don't want to send crud past the end of i_size through
375 * compression, that's just a waste of CPU time. So, if the
376 * end of the file is before the start of our current
377 * requested range of bytes, we bail out to the uncompressed
378 * cleanup code that can deal with all of this.
379 *
380 * It isn't really the fastest way to fix things, but this is a
381 * very uncommon corner.
382 */
383 if (actual_end <= start)
384 goto cleanup_and_bail_uncompressed;
385
Chris Masonc8b97812008-10-29 14:49:59 -0400386 total_compressed = actual_end - start;
387
388 /* we want to make sure that amount of ram required to uncompress
389 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500390 * of a compressed extent to 128k. This is a crucial number
391 * because it also controls how easily we can spread reads across
392 * cpus for decompression.
393 *
394 * We also want to make sure the amount of IO required to do
395 * a random read is reasonably small, so we limit the size of
396 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400397 */
398 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000399 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500400 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400401 total_in = 0;
402 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400403
Chris Mason771ed682008-11-06 22:02:51 -0500404 /*
405 * we do compression for mount -o compress and when the
406 * inode has not been flagged as nocompress. This flag can
407 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400408 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200409 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500410 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000411 (BTRFS_I(inode)->force_compress) ||
412 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400413 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400414 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800415 if (!pages) {
416 /* just bail out to the uncompressed code */
417 goto cont;
418 }
Chris Mason179e29e2007-11-01 11:28:41 -0400419
Li Zefan261507a02010-12-17 14:21:50 +0800420 if (BTRFS_I(inode)->force_compress)
421 compress_type = BTRFS_I(inode)->force_compress;
422
Chris Mason4adaa612013-03-26 13:07:00 -0400423 /*
424 * we need to call clear_page_dirty_for_io on each
425 * page in the range. Otherwise applications with the file
426 * mmap'd can wander in and change the page contents while
427 * we are compressing them.
428 *
429 * If the compression fails for any reason, we set the pages
430 * dirty again later on.
431 */
432 extent_range_clear_dirty_for_io(inode, start, end);
433 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800434 ret = btrfs_compress_pages(compress_type,
435 inode->i_mapping, start,
436 total_compressed, pages,
437 nr_pages, &nr_pages_ret,
438 &total_in,
439 &total_compressed,
440 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400441
442 if (!ret) {
443 unsigned long offset = total_compressed &
444 (PAGE_CACHE_SIZE - 1);
445 struct page *page = pages[nr_pages_ret - 1];
446 char *kaddr;
447
448 /* zero the tail end of the last page, we might be
449 * sending it down to disk
450 */
451 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800452 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400453 memset(kaddr + offset, 0,
454 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800455 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400456 }
457 will_compress = 1;
458 }
459 }
Li Zefan560f7d72011-09-08 10:22:01 +0800460cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400461 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400462 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100463 if (IS_ERR(trans)) {
464 ret = PTR_ERR(trans);
465 trans = NULL;
466 goto cleanup_and_out;
467 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400468 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500469
Chris Masonc8b97812008-10-29 14:49:59 -0400470 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500471 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400472 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500473 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400474 */
475 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000476 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400477 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500478 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400479 ret = cow_file_range_inline(trans, root, inode,
480 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000481 total_compressed,
482 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400483 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100484 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500485 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100486 * inline extent creation worked or returned error,
487 * we don't need to create any more async work items.
488 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500489 */
Chris Masonc8b97812008-10-29 14:49:59 -0400490 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400491 &BTRFS_I(inode)->io_tree,
492 start, end, NULL,
493 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400494 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400495 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000496
497 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400498 goto free_pages_out;
499 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000500 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400501 }
502
503 if (will_compress) {
504 /*
505 * we aren't doing an inline extent round the compressed size
506 * up to a block size boundary so the allocator does sane
507 * things
508 */
Qu Wenruofda28322013-02-26 08:10:22 +0000509 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400510
511 /*
512 * one last check to make sure the compression is really a
513 * win, compare the page count read with the blocks on disk
514 */
Qu Wenruofda28322013-02-26 08:10:22 +0000515 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400516 if (total_compressed >= total_in) {
517 will_compress = 0;
518 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400519 num_bytes = total_in;
520 }
521 }
522 if (!will_compress && pages) {
523 /*
524 * the compression code ran but failed to make things smaller,
525 * free any pages it allocated and our page pointer array
526 */
527 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400528 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400529 page_cache_release(pages[i]);
530 }
531 kfree(pages);
532 pages = NULL;
533 total_compressed = 0;
534 nr_pages_ret = 0;
535
536 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500537 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
538 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500539 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500540 }
Chris Masonc8b97812008-10-29 14:49:59 -0400541 }
Chris Mason771ed682008-11-06 22:02:51 -0500542 if (will_compress) {
543 *num_added += 1;
544
545 /* the async work queues will take care of doing actual
546 * allocation on disk for these compressed pages,
547 * and will submit them to the elevator.
548 */
549 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800550 total_compressed, pages, nr_pages_ret,
551 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500552
Yan, Zheng24ae6362010-12-06 07:02:36 +0000553 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500554 start += num_bytes;
555 pages = NULL;
556 cond_resched();
557 goto again;
558 }
559 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500560cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500561 /*
562 * No compression, but we still need to write the pages in
563 * the file we've been given so far. redirty the locked
564 * page if it corresponds to our extent and set things up
565 * for the async work queue to run cow_file_range to do
566 * the normal delalloc dance
567 */
568 if (page_offset(locked_page) >= start &&
569 page_offset(locked_page) <= end) {
570 __set_page_dirty_nobuffers(locked_page);
571 /* unlocked later on in the async handlers */
572 }
Chris Mason4adaa612013-03-26 13:07:00 -0400573 if (redirty)
574 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800575 add_async_extent(async_cow, start, end - start + 1,
576 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500577 *num_added += 1;
578 }
579
580out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100581 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500582
583free_pages_out:
584 for (i = 0; i < nr_pages_ret; i++) {
585 WARN_ON(pages[i]->mapping);
586 page_cache_release(pages[i]);
587 }
Chris Masond3977122009-01-05 21:25:51 -0500588 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500589
590 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100591
592cleanup_and_out:
593 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
594 start, end, NULL,
595 EXTENT_CLEAR_UNLOCK_PAGE |
596 EXTENT_CLEAR_DIRTY |
597 EXTENT_CLEAR_DELALLOC |
598 EXTENT_SET_WRITEBACK |
599 EXTENT_END_WRITEBACK);
600 if (!trans || IS_ERR(trans))
601 btrfs_error(root->fs_info, ret, "Failed to join transaction");
602 else
603 btrfs_abort_transaction(trans, root, ret);
604 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500605}
606
607/*
608 * phase two of compressed writeback. This is the ordered portion
609 * of the code, which only gets called in the order the work was
610 * queued. We walk all the async extents created by compress_file_range
611 * and send them down to the disk.
612 */
613static noinline int submit_compressed_extents(struct inode *inode,
614 struct async_cow *async_cow)
615{
616 struct async_extent *async_extent;
617 u64 alloc_hint = 0;
618 struct btrfs_trans_handle *trans;
619 struct btrfs_key ins;
620 struct extent_map *em;
621 struct btrfs_root *root = BTRFS_I(inode)->root;
622 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
623 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500624 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500625
626 if (list_empty(&async_cow->extents))
627 return 0;
628
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500629again:
Chris Masond3977122009-01-05 21:25:51 -0500630 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500631 async_extent = list_entry(async_cow->extents.next,
632 struct async_extent, list);
633 list_del(&async_extent->list);
634
635 io_tree = &BTRFS_I(inode)->io_tree;
636
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500637retry:
Chris Mason771ed682008-11-06 22:02:51 -0500638 /* did the compression code fall back to uncompressed IO? */
639 if (!async_extent->pages) {
640 int page_started = 0;
641 unsigned long nr_written = 0;
642
643 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000644 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100645 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500646
647 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500648 ret = cow_file_range(inode, async_cow->locked_page,
649 async_extent->start,
650 async_extent->start +
651 async_extent->ram_size - 1,
652 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500653
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100654 /* JDM XXX */
655
Chris Mason771ed682008-11-06 22:02:51 -0500656 /*
657 * if page_started, cow_file_range inserted an
658 * inline extent and took care of all the unlocking
659 * and IO for us. Otherwise, we need to submit
660 * all those pages down to the drive.
661 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500662 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500663 extent_write_locked_range(io_tree,
664 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500665 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500666 async_extent->ram_size - 1,
667 btrfs_get_extent,
668 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500669 else if (ret)
670 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500671 kfree(async_extent);
672 cond_resched();
673 continue;
674 }
675
676 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100677 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500678
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400679 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100680 if (IS_ERR(trans)) {
681 ret = PTR_ERR(trans);
682 } else {
683 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
684 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500685 async_extent->compressed_size,
686 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500687 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600688 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100689 btrfs_abort_transaction(trans, root, ret);
690 btrfs_end_transaction(trans, root);
691 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000692
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500693 if (ret) {
694 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500695
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500696 for (i = 0; i < async_extent->nr_pages; i++) {
697 WARN_ON(async_extent->pages[i]->mapping);
698 page_cache_release(async_extent->pages[i]);
699 }
700 kfree(async_extent->pages);
701 async_extent->nr_pages = 0;
702 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500703
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100704 if (ret == -ENOSPC)
705 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500706 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500707 }
708
Yan, Zhengc2167752009-11-12 09:34:21 +0000709 /*
710 * here we're doing allocation and writeback of the
711 * compressed pages
712 */
713 btrfs_drop_extent_cache(inode, async_extent->start,
714 async_extent->start +
715 async_extent->ram_size - 1, 0);
716
David Sterba172ddd62011-04-21 00:48:27 +0200717 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000718 if (!em) {
719 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500720 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000721 }
Chris Mason771ed682008-11-06 22:02:51 -0500722 em->start = async_extent->start;
723 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500724 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400725 em->mod_start = em->start;
726 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500727
728 em->block_start = ins.objectid;
729 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500730 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400731 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500732 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800733 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500734 set_bit(EXTENT_FLAG_PINNED, &em->flags);
735 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400736 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500737
Chris Masond3977122009-01-05 21:25:51 -0500738 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400739 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400740 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400741 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500742 if (ret != -EEXIST) {
743 free_extent_map(em);
744 break;
745 }
746 btrfs_drop_extent_cache(inode, async_extent->start,
747 async_extent->start +
748 async_extent->ram_size - 1, 0);
749 }
750
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500751 if (ret)
752 goto out_free_reserve;
753
Li Zefan261507a02010-12-17 14:21:50 +0800754 ret = btrfs_add_ordered_extent_compress(inode,
755 async_extent->start,
756 ins.objectid,
757 async_extent->ram_size,
758 ins.offset,
759 BTRFS_ORDERED_COMPRESSED,
760 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500761 if (ret)
762 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500763
Chris Mason771ed682008-11-06 22:02:51 -0500764 /*
765 * clear dirty, set writeback and unlock the pages.
766 */
767 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400768 &BTRFS_I(inode)->io_tree,
769 async_extent->start,
770 async_extent->start +
771 async_extent->ram_size - 1,
772 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
773 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400774 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400775 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500776
777 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500778 async_extent->start,
779 async_extent->ram_size,
780 ins.objectid,
781 ins.offset, async_extent->pages,
782 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500783 alloc_hint = ins.objectid + ins.offset;
784 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500785 if (ret)
786 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500787 cond_resched();
788 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100789 ret = 0;
790out:
791 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500792out_free_reserve:
793 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100794out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500795 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
796 async_extent->start,
797 async_extent->start +
798 async_extent->ram_size - 1,
799 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
800 EXTENT_CLEAR_UNLOCK |
801 EXTENT_CLEAR_DELALLOC |
802 EXTENT_CLEAR_DIRTY |
803 EXTENT_SET_WRITEBACK |
804 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100805 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500806 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500807}
808
Josef Bacik4b46fce2010-05-23 11:00:55 -0400809static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
810 u64 num_bytes)
811{
812 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
813 struct extent_map *em;
814 u64 alloc_hint = 0;
815
816 read_lock(&em_tree->lock);
817 em = search_extent_mapping(em_tree, start, num_bytes);
818 if (em) {
819 /*
820 * if block start isn't an actual block number then find the
821 * first block in this inode and use that as a hint. If that
822 * block is also bogus then just don't worry about it.
823 */
824 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
825 free_extent_map(em);
826 em = search_extent_mapping(em_tree, 0, 0);
827 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
828 alloc_hint = em->block_start;
829 if (em)
830 free_extent_map(em);
831 } else {
832 alloc_hint = em->block_start;
833 free_extent_map(em);
834 }
835 }
836 read_unlock(&em_tree->lock);
837
838 return alloc_hint;
839}
840
Chris Mason771ed682008-11-06 22:02:51 -0500841/*
842 * when extent_io.c finds a delayed allocation range in the file,
843 * the call backs end up in this code. The basic idea is to
844 * allocate extents on disk for the range, and create ordered data structs
845 * in ram to track those extents.
846 *
847 * locked_page is the page that writepage had locked already. We use
848 * it to make sure we don't do extra locks or unlocks.
849 *
850 * *page_started is set to one if we unlock locked_page and do everything
851 * required to start IO on it. It may be clean and already done with
852 * IO when we return.
853 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000854static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
855 struct inode *inode,
856 struct btrfs_root *root,
857 struct page *locked_page,
858 u64 start, u64 end, int *page_started,
859 unsigned long *nr_written,
860 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500861{
Chris Mason771ed682008-11-06 22:02:51 -0500862 u64 alloc_hint = 0;
863 u64 num_bytes;
864 unsigned long ram_size;
865 u64 disk_num_bytes;
866 u64 cur_alloc_size;
867 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500868 struct btrfs_key ins;
869 struct extent_map *em;
870 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
871 int ret = 0;
872
Liu Bo83eea1f2012-07-10 05:28:39 -0600873 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500874
Qu Wenruofda28322013-02-26 08:10:22 +0000875 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500876 num_bytes = max(blocksize, num_bytes);
877 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500878
Chris Mason4cb53002011-05-24 15:35:30 -0400879 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400880 if (num_bytes < 64 * 1024 &&
881 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400882 btrfs_add_inode_defrag(trans, inode);
883
Chris Mason771ed682008-11-06 22:02:51 -0500884 if (start == 0) {
885 /* lets try to make an inline extent */
886 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000887 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500888 if (ret == 0) {
889 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400890 &BTRFS_I(inode)->io_tree,
891 start, end, NULL,
892 EXTENT_CLEAR_UNLOCK_PAGE |
893 EXTENT_CLEAR_UNLOCK |
894 EXTENT_CLEAR_DELALLOC |
895 EXTENT_CLEAR_DIRTY |
896 EXTENT_SET_WRITEBACK |
897 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000898
Chris Mason771ed682008-11-06 22:02:51 -0500899 *nr_written = *nr_written +
900 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
901 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500902 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100903 } else if (ret < 0) {
904 btrfs_abort_transaction(trans, root, ret);
905 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500906 }
907 }
Chris Masonc8b97812008-10-29 14:49:59 -0400908
909 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200910 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400911
Josef Bacik4b46fce2010-05-23 11:00:55 -0400912 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400913 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400914
Chris Masond3977122009-01-05 21:25:51 -0500915 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400916 unsigned long op;
917
Josef Bacik287a0ab2010-03-19 18:07:23 +0000918 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400919 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500920 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500921 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100922 if (ret < 0) {
923 btrfs_abort_transaction(trans, root, ret);
924 goto out_unlock;
925 }
Chris Masond3977122009-01-05 21:25:51 -0500926
David Sterba172ddd62011-04-21 00:48:27 +0200927 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000928 if (!em) {
929 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000930 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000931 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400932 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500933 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500934 ram_size = ins.offset;
935 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400936 em->mod_start = em->start;
937 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400938
Chris Masone6dcd2d2008-07-17 12:53:50 -0400939 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400940 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500941 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400942 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400943 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400944 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400945 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400946
Chris Masond3977122009-01-05 21:25:51 -0500947 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400948 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400949 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400950 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400951 if (ret != -EEXIST) {
952 free_extent_map(em);
953 break;
954 }
955 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400956 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400957 }
Liu Boace68ba2013-04-22 10:53:47 +0000958 if (ret)
959 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400960
Chris Mason98d20f62008-04-14 09:46:10 -0400961 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400962 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500963 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000964 if (ret)
965 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400966
Yan Zheng17d217f2008-12-12 10:03:38 -0500967 if (root->root_key.objectid ==
968 BTRFS_DATA_RELOC_TREE_OBJECTID) {
969 ret = btrfs_reloc_clone_csums(inode, start,
970 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100971 if (ret) {
972 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000973 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100974 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500975 }
976
Chris Masond3977122009-01-05 21:25:51 -0500977 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400978 break;
Chris Masond3977122009-01-05 21:25:51 -0500979
Chris Masonc8b97812008-10-29 14:49:59 -0400980 /* we're not doing compressed IO, don't unlock the first
981 * page (which the caller expects to stay locked), don't
982 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400983 *
984 * Do set the Private2 bit so we know this page was properly
985 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400986 */
Chris Masona791e352009-10-08 11:27:10 -0400987 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
988 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
989 EXTENT_SET_PRIVATE2;
990
Chris Masonc8b97812008-10-29 14:49:59 -0400991 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
992 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400993 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400994 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500995 num_bytes -= cur_alloc_size;
996 alloc_hint = ins.objectid + ins.offset;
997 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400998 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100999out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001000 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001001
Liu Boace68ba2013-04-22 10:53:47 +00001002out_reserve:
1003 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004out_unlock:
1005 extent_clear_unlock_delalloc(inode,
1006 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -04001007 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001008 EXTENT_CLEAR_UNLOCK_PAGE |
1009 EXTENT_CLEAR_UNLOCK |
1010 EXTENT_CLEAR_DELALLOC |
1011 EXTENT_CLEAR_DIRTY |
1012 EXTENT_SET_WRITEBACK |
1013 EXTENT_END_WRITEBACK);
1014
1015 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001016}
Chris Masonc8b97812008-10-29 14:49:59 -04001017
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001018static noinline int cow_file_range(struct inode *inode,
1019 struct page *locked_page,
1020 u64 start, u64 end, int *page_started,
1021 unsigned long *nr_written,
1022 int unlock)
1023{
1024 struct btrfs_trans_handle *trans;
1025 struct btrfs_root *root = BTRFS_I(inode)->root;
1026 int ret;
1027
1028 trans = btrfs_join_transaction(root);
1029 if (IS_ERR(trans)) {
1030 extent_clear_unlock_delalloc(inode,
1031 &BTRFS_I(inode)->io_tree,
1032 start, end, locked_page,
1033 EXTENT_CLEAR_UNLOCK_PAGE |
1034 EXTENT_CLEAR_UNLOCK |
1035 EXTENT_CLEAR_DELALLOC |
1036 EXTENT_CLEAR_DIRTY |
1037 EXTENT_SET_WRITEBACK |
1038 EXTENT_END_WRITEBACK);
1039 return PTR_ERR(trans);
1040 }
1041 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1042
1043 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1044 page_started, nr_written, unlock);
1045
1046 btrfs_end_transaction(trans, root);
1047
1048 return ret;
1049}
1050
Chris Mason771ed682008-11-06 22:02:51 -05001051/*
1052 * work queue call back to started compression on a file and pages
1053 */
1054static noinline void async_cow_start(struct btrfs_work *work)
1055{
1056 struct async_cow *async_cow;
1057 int num_added = 0;
1058 async_cow = container_of(work, struct async_cow, work);
1059
1060 compress_file_range(async_cow->inode, async_cow->locked_page,
1061 async_cow->start, async_cow->end, async_cow,
1062 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001063 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001064 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001065 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001066 }
Chris Mason771ed682008-11-06 22:02:51 -05001067}
1068
1069/*
1070 * work queue call back to submit previously compressed pages
1071 */
1072static noinline void async_cow_submit(struct btrfs_work *work)
1073{
1074 struct async_cow *async_cow;
1075 struct btrfs_root *root;
1076 unsigned long nr_pages;
1077
1078 async_cow = container_of(work, struct async_cow, work);
1079
1080 root = async_cow->root;
1081 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1082 PAGE_CACHE_SHIFT;
1083
Josef Bacik66657b32012-08-01 15:36:24 -04001084 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001085 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001086 waitqueue_active(&root->fs_info->async_submit_wait))
1087 wake_up(&root->fs_info->async_submit_wait);
1088
Chris Masond3977122009-01-05 21:25:51 -05001089 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001090 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001091}
Chris Masonc8b97812008-10-29 14:49:59 -04001092
Chris Mason771ed682008-11-06 22:02:51 -05001093static noinline void async_cow_free(struct btrfs_work *work)
1094{
1095 struct async_cow *async_cow;
1096 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001097 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001098 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001099 kfree(async_cow);
1100}
1101
1102static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1103 u64 start, u64 end, int *page_started,
1104 unsigned long *nr_written)
1105{
1106 struct async_cow *async_cow;
1107 struct btrfs_root *root = BTRFS_I(inode)->root;
1108 unsigned long nr_pages;
1109 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001110 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001111
Chris Masona3429ab2009-10-08 12:30:20 -04001112 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1113 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001114 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001115 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001116 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001117 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001118 async_cow->root = root;
1119 async_cow->locked_page = locked_page;
1120 async_cow->start = start;
1121
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001122 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001123 cur_end = end;
1124 else
1125 cur_end = min(end, start + 512 * 1024 - 1);
1126
1127 async_cow->end = cur_end;
1128 INIT_LIST_HEAD(&async_cow->extents);
1129
1130 async_cow->work.func = async_cow_start;
1131 async_cow->work.ordered_func = async_cow_submit;
1132 async_cow->work.ordered_free = async_cow_free;
1133 async_cow->work.flags = 0;
1134
Chris Mason771ed682008-11-06 22:02:51 -05001135 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1136 PAGE_CACHE_SHIFT;
1137 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1138
1139 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1140 &async_cow->work);
1141
1142 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1143 wait_event(root->fs_info->async_submit_wait,
1144 (atomic_read(&root->fs_info->async_delalloc_pages) <
1145 limit));
1146 }
1147
Chris Masond3977122009-01-05 21:25:51 -05001148 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001149 atomic_read(&root->fs_info->async_delalloc_pages)) {
1150 wait_event(root->fs_info->async_submit_wait,
1151 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1152 0));
1153 }
1154
1155 *nr_written += nr_pages;
1156 start = cur_end + 1;
1157 }
1158 *page_started = 1;
1159 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001160}
1161
Chris Masond3977122009-01-05 21:25:51 -05001162static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001163 u64 bytenr, u64 num_bytes)
1164{
1165 int ret;
1166 struct btrfs_ordered_sum *sums;
1167 LIST_HEAD(list);
1168
Yan Zheng07d400a2009-01-06 11:42:00 -05001169 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001170 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001171 if (ret == 0 && list_empty(&list))
1172 return 0;
1173
1174 while (!list_empty(&list)) {
1175 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1176 list_del(&sums->list);
1177 kfree(sums);
1178 }
1179 return 1;
1180}
1181
Chris Masond352ac62008-09-29 15:18:18 -04001182/*
1183 * when nowcow writeback call back. This checks for snapshots or COW copies
1184 * of the extents that exist in the file, and COWs the file as required.
1185 *
1186 * If no cow copies or snapshots exist, we write directly to the existing
1187 * blocks on disk
1188 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001189static noinline int run_delalloc_nocow(struct inode *inode,
1190 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001191 u64 start, u64 end, int *page_started, int force,
1192 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001193{
Chris Masonbe20aa92007-12-17 20:14:01 -05001194 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001195 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001196 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001197 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001198 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001199 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 u64 cow_start;
1201 u64 cur_offset;
1202 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001203 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001204 u64 disk_bytenr;
1205 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001206 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001207 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001209 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001210 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 int nocow;
1212 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001213 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001214 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001215
1216 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001217 if (!path) {
1218 extent_clear_unlock_delalloc(inode,
1219 &BTRFS_I(inode)->io_tree,
1220 start, end, locked_page,
1221 EXTENT_CLEAR_UNLOCK_PAGE |
1222 EXTENT_CLEAR_UNLOCK |
1223 EXTENT_CLEAR_DELALLOC |
1224 EXTENT_CLEAR_DIRTY |
1225 EXTENT_SET_WRITEBACK |
1226 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001227 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001228 }
Li Zefan82d59022011-04-20 10:33:24 +08001229
Liu Bo83eea1f2012-07-10 05:28:39 -06001230 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001231
1232 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001233 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001234 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001235 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001236
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001237 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001238 extent_clear_unlock_delalloc(inode,
1239 &BTRFS_I(inode)->io_tree,
1240 start, end, locked_page,
1241 EXTENT_CLEAR_UNLOCK_PAGE |
1242 EXTENT_CLEAR_UNLOCK |
1243 EXTENT_CLEAR_DELALLOC |
1244 EXTENT_CLEAR_DIRTY |
1245 EXTENT_SET_WRITEBACK |
1246 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001247 btrfs_free_path(path);
1248 return PTR_ERR(trans);
1249 }
1250
Josef Bacik74b21072011-04-13 12:02:53 -04001251 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001252
Yan Zheng80ff3852008-10-30 14:20:02 -04001253 cow_start = (u64)-1;
1254 cur_offset = start;
1255 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001256 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001258 if (ret < 0) {
1259 btrfs_abort_transaction(trans, root, ret);
1260 goto error;
1261 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1263 leaf = path->nodes[0];
1264 btrfs_item_key_to_cpu(leaf, &found_key,
1265 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001266 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 found_key.type == BTRFS_EXTENT_DATA_KEY)
1268 path->slots[0]--;
1269 }
1270 check_prev = 0;
1271next_slot:
1272 leaf = path->nodes[0];
1273 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1274 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001275 if (ret < 0) {
1276 btrfs_abort_transaction(trans, root, ret);
1277 goto error;
1278 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 if (ret > 0)
1280 break;
1281 leaf = path->nodes[0];
1282 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001283
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 nocow = 0;
1285 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001286 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001288
Li Zefan33345d012011-04-20 10:31:50 +08001289 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1291 found_key.offset > end)
1292 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001293
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 if (found_key.offset > cur_offset) {
1295 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001296 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 goto out_check;
1298 }
Chris Masonc31f8832008-01-08 15:46:31 -05001299
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 fi = btrfs_item_ptr(leaf, path->slots[0],
1301 struct btrfs_file_extent_item);
1302 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001303
Josef Bacikcc95bef2013-04-04 14:31:27 -04001304 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001305 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1306 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001308 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 extent_end = found_key.offset +
1310 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001311 disk_num_bytes =
1312 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 if (extent_end <= start) {
1314 path->slots[0]++;
1315 goto next_slot;
1316 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001317 if (disk_bytenr == 0)
1318 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 if (btrfs_file_extent_compression(leaf, fi) ||
1320 btrfs_file_extent_encryption(leaf, fi) ||
1321 btrfs_file_extent_other_encoding(leaf, fi))
1322 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001323 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1324 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001325 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001327 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001328 found_key.offset -
1329 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001330 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001331 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001332 disk_bytenr += cur_offset - found_key.offset;
1333 num_bytes = min(end + 1, extent_end) - cur_offset;
1334 /*
1335 * force cow if csum exists in the range.
1336 * this ensure that csum for a given extent are
1337 * either valid or do not exist.
1338 */
1339 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1340 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 nocow = 1;
1342 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1343 extent_end = found_key.offset +
1344 btrfs_file_extent_inline_len(leaf, fi);
1345 extent_end = ALIGN(extent_end, root->sectorsize);
1346 } else {
1347 BUG_ON(1);
1348 }
1349out_check:
1350 if (extent_end <= start) {
1351 path->slots[0]++;
1352 goto next_slot;
1353 }
1354 if (!nocow) {
1355 if (cow_start == (u64)-1)
1356 cow_start = cur_offset;
1357 cur_offset = extent_end;
1358 if (cur_offset > end)
1359 break;
1360 path->slots[0]++;
1361 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001362 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001363
David Sterbab3b4aa72011-04-21 01:20:15 +02001364 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001366 ret = __cow_file_range(trans, inode, root, locked_page,
1367 cow_start, found_key.offset - 1,
1368 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001369 if (ret) {
1370 btrfs_abort_transaction(trans, root, ret);
1371 goto error;
1372 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001374 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001375
Yan Zhengd899e052008-10-30 14:25:28 -04001376 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1377 struct extent_map *em;
1378 struct extent_map_tree *em_tree;
1379 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001380 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001381 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001382 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001383 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001384 em->len = num_bytes;
1385 em->block_len = num_bytes;
1386 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001387 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001388 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001389 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001390 em->mod_start = em->start;
1391 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001392 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001393 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001394 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001395 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001396 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001397 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001398 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001399 if (ret != -EEXIST) {
1400 free_extent_map(em);
1401 break;
1402 }
1403 btrfs_drop_extent_cache(inode, em->start,
1404 em->start + em->len - 1, 0);
1405 }
1406 type = BTRFS_ORDERED_PREALLOC;
1407 } else {
1408 type = BTRFS_ORDERED_NOCOW;
1409 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001410
1411 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001412 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001413 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001414
Yan, Zhengefa56462010-05-16 10:49:59 -04001415 if (root->root_key.objectid ==
1416 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1417 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1418 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001419 if (ret) {
1420 btrfs_abort_transaction(trans, root, ret);
1421 goto error;
1422 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001423 }
1424
Yan Zhengd899e052008-10-30 14:25:28 -04001425 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001426 cur_offset, cur_offset + num_bytes - 1,
1427 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1428 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1429 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001430 cur_offset = extent_end;
1431 if (cur_offset > end)
1432 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001433 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001434 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001435
Josef Bacik17ca04a2012-05-31 15:58:55 -04001436 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001438 cur_offset = end;
1439 }
1440
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001442 ret = __cow_file_range(trans, inode, root, locked_page,
1443 cow_start, end,
1444 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001445 if (ret) {
1446 btrfs_abort_transaction(trans, root, ret);
1447 goto error;
1448 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001449 }
1450
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001451error:
Miao Xiea698d0752012-09-20 01:51:59 -06001452 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001453 if (!ret)
1454 ret = err;
1455
Josef Bacik17ca04a2012-05-31 15:58:55 -04001456 if (ret && cur_offset < end)
1457 extent_clear_unlock_delalloc(inode,
1458 &BTRFS_I(inode)->io_tree,
1459 cur_offset, end, locked_page,
1460 EXTENT_CLEAR_UNLOCK_PAGE |
1461 EXTENT_CLEAR_UNLOCK |
1462 EXTENT_CLEAR_DELALLOC |
1463 EXTENT_CLEAR_DIRTY |
1464 EXTENT_SET_WRITEBACK |
1465 EXTENT_END_WRITEBACK);
1466
Yan Zheng7ea394f2008-08-05 13:05:02 -04001467 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001468 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001469}
1470
Chris Masond352ac62008-09-29 15:18:18 -04001471/*
1472 * extent_io.c call back to do delayed allocation processing
1473 */
Chris Masonc8b97812008-10-29 14:49:59 -04001474static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001475 u64 start, u64 end, int *page_started,
1476 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001477{
Chris Masonbe20aa92007-12-17 20:14:01 -05001478 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001479 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001480
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001481 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001482 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001483 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001484 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001485 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001486 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001487 } else if (!btrfs_test_opt(root, COMPRESS) &&
1488 !(BTRFS_I(inode)->force_compress) &&
1489 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001490 ret = cow_file_range(inode, locked_page, start, end,
1491 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001492 } else {
1493 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1494 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001495 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001496 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001497 }
Chris Masonb888db22007-08-27 16:49:44 -04001498 return ret;
1499}
1500
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001501static void btrfs_split_extent_hook(struct inode *inode,
1502 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001503{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001504 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001505 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001506 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001507
Josef Bacik9e0baf62011-07-15 15:16:44 +00001508 spin_lock(&BTRFS_I(inode)->lock);
1509 BTRFS_I(inode)->outstanding_extents++;
1510 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001511}
1512
1513/*
1514 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1515 * extents so we can keep track of new extents that are just merged onto old
1516 * extents, such as when we are doing sequential writes, so we can properly
1517 * account for the metadata space we'll need.
1518 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001519static void btrfs_merge_extent_hook(struct inode *inode,
1520 struct extent_state *new,
1521 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001522{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001523 /* not delalloc, ignore it */
1524 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001525 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001526
Josef Bacik9e0baf62011-07-15 15:16:44 +00001527 spin_lock(&BTRFS_I(inode)->lock);
1528 BTRFS_I(inode)->outstanding_extents--;
1529 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001530}
1531
Chris Masond352ac62008-09-29 15:18:18 -04001532/*
1533 * extent_io.c set_bit_hook, used to track delayed allocation
1534 * bytes in this file, and to maintain the list of inodes that
1535 * have pending delalloc work to be done.
1536 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001537static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001538 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001539{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001540
Chris Mason75eff682008-12-15 15:54:40 -05001541 /*
1542 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001543 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001544 * bit, which is only set or cleared with irqs on
1545 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001546 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001547 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001548 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001549 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001550
Josef Bacik9e0baf62011-07-15 15:16:44 +00001551 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001552 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001553 } else {
1554 spin_lock(&BTRFS_I(inode)->lock);
1555 BTRFS_I(inode)->outstanding_extents++;
1556 spin_unlock(&BTRFS_I(inode)->lock);
1557 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001558
Miao Xie963d6782013-01-29 10:10:51 +00001559 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1560 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001561 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001562 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001563 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1564 &BTRFS_I(inode)->runtime_flags)) {
1565 spin_lock(&root->fs_info->delalloc_lock);
1566 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1567 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1568 &root->fs_info->delalloc_inodes);
1569 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1570 &BTRFS_I(inode)->runtime_flags);
1571 }
1572 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001573 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001574 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001575 }
Chris Mason291d6732008-01-29 15:55:23 -05001576}
1577
Chris Masond352ac62008-09-29 15:18:18 -04001578/*
1579 * extent_io.c clear_bit_hook, see set_bit_hook for why
1580 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001581static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001582 struct extent_state *state,
1583 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001584{
Chris Mason75eff682008-12-15 15:54:40 -05001585 /*
1586 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001587 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001588 * bit, which is only set or cleared with irqs on
1589 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001590 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001591 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001592 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001593 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001594
Josef Bacik9e0baf62011-07-15 15:16:44 +00001595 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001596 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001597 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1598 spin_lock(&BTRFS_I(inode)->lock);
1599 BTRFS_I(inode)->outstanding_extents--;
1600 spin_unlock(&BTRFS_I(inode)->lock);
1601 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001602
1603 if (*bits & EXTENT_DO_ACCOUNTING)
1604 btrfs_delalloc_release_metadata(inode, len);
1605
Josef Bacik0cb59c92010-07-02 12:14:14 -04001606 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1607 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001608 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001609
Miao Xie963d6782013-01-29 10:10:51 +00001610 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1611 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001612 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001613 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001614 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001615 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1616 &BTRFS_I(inode)->runtime_flags)) {
1617 spin_lock(&root->fs_info->delalloc_lock);
1618 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1619 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1620 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1621 &BTRFS_I(inode)->runtime_flags);
1622 }
1623 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001624 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001625 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001626 }
Chris Mason291d6732008-01-29 15:55:23 -05001627}
1628
Chris Masond352ac62008-09-29 15:18:18 -04001629/*
1630 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1631 * we don't create bios that span stripes or chunks
1632 */
David Woodhouse64a16702009-07-15 23:29:37 +01001633int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001634 size_t size, struct bio *bio,
1635 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001636{
1637 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001638 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001639 u64 length = 0;
1640 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001641 int ret;
1642
Chris Mason771ed682008-11-06 22:02:51 -05001643 if (bio_flags & EXTENT_BIO_COMPRESSED)
1644 return 0;
1645
Chris Masonf2d8d742008-04-21 10:03:05 -04001646 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001647 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001648 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001649 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001650 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001651 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001652 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001653 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001654 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001655}
1656
Chris Masond352ac62008-09-29 15:18:18 -04001657/*
1658 * in order to insert checksums into the metadata in large chunks,
1659 * we wait until bio submission time. All the pages in the bio are
1660 * checksummed and sums are attached onto the ordered extent record.
1661 *
1662 * At IO completion time the cums attached on the ordered extent record
1663 * are inserted into the btree
1664 */
Chris Masond3977122009-01-05 21:25:51 -05001665static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1666 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001667 unsigned long bio_flags,
1668 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001669{
Chris Mason065631f2008-02-20 12:07:25 -05001670 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001671 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001672
Chris Masond20f7042008-12-08 16:58:54 -05001673 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001674 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001675 return 0;
1676}
Chris Masone0156402008-04-16 11:15:20 -04001677
Chris Mason4a69a412008-11-06 22:03:00 -05001678/*
1679 * in order to insert checksums into the metadata in large chunks,
1680 * we wait until bio submission time. All the pages in the bio are
1681 * checksummed and sums are attached onto the ordered extent record.
1682 *
1683 * At IO completion time the cums attached on the ordered extent record
1684 * are inserted into the btree
1685 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001686static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001687 int mirror_num, unsigned long bio_flags,
1688 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001689{
1690 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001691 int ret;
1692
1693 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1694 if (ret)
1695 bio_endio(bio, ret);
1696 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001697}
1698
Chris Masond352ac62008-09-29 15:18:18 -04001699/*
Chris Masoncad321a2008-12-17 14:51:42 -05001700 * extent_io.c submission hook. This does the right thing for csum calculation
1701 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001702 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001703static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001704 int mirror_num, unsigned long bio_flags,
1705 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001706{
1707 struct btrfs_root *root = BTRFS_I(inode)->root;
1708 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001709 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001710 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001711 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001712
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001713 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001714
Liu Bo83eea1f2012-07-10 05:28:39 -06001715 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001716 metadata = 2;
1717
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001718 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001719 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1720 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001721 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001722
Chris Masond20f7042008-12-08 16:58:54 -05001723 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001724 ret = btrfs_submit_compressed_read(inode, bio,
1725 mirror_num,
1726 bio_flags);
1727 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001728 } else if (!skip_sum) {
1729 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1730 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001731 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001732 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001733 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001734 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001735 /* csum items have already been cloned */
1736 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1737 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001738 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001739 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001740 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001741 bio_flags, bio_offset,
1742 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001743 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001744 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001745 } else if (!skip_sum) {
1746 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1747 if (ret)
1748 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001749 }
1750
Chris Mason0b86a832008-03-24 15:01:56 -04001751mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001752 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1753
1754out:
1755 if (ret < 0)
1756 bio_endio(bio, ret);
1757 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001758}
Chris Mason6885f302008-02-20 16:11:05 -05001759
Chris Masond352ac62008-09-29 15:18:18 -04001760/*
1761 * given a list of ordered sums record them in the inode. This happens
1762 * at IO completion time based on sums calculated at bio submission time.
1763 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001764static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001765 struct inode *inode, u64 file_offset,
1766 struct list_head *list)
1767{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001768 struct btrfs_ordered_sum *sum;
1769
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001770 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001771 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001772 btrfs_csum_file_blocks(trans,
1773 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001774 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001775 }
1776 return 0;
1777}
1778
Josef Bacik2ac55d42010-02-03 19:33:23 +00001779int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1780 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001781{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001782 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001783 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001784 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001785}
1786
Chris Masond352ac62008-09-29 15:18:18 -04001787/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001788struct btrfs_writepage_fixup {
1789 struct page *page;
1790 struct btrfs_work work;
1791};
1792
Christoph Hellwigb2950862008-12-02 09:54:17 -05001793static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001794{
1795 struct btrfs_writepage_fixup *fixup;
1796 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001797 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001798 struct page *page;
1799 struct inode *inode;
1800 u64 page_start;
1801 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001802 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001803
1804 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1805 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001806again:
Chris Mason247e7432008-07-17 12:53:51 -04001807 lock_page(page);
1808 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1809 ClearPageChecked(page);
1810 goto out_page;
1811 }
1812
1813 inode = page->mapping->host;
1814 page_start = page_offset(page);
1815 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1816
Josef Bacik2ac55d42010-02-03 19:33:23 +00001817 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001818 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001819
1820 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001821 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001822 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001823
1824 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1825 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001826 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1827 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001828 unlock_page(page);
1829 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001830 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001831 goto again;
1832 }
Chris Mason247e7432008-07-17 12:53:51 -04001833
Jeff Mahoney87826df2012-02-15 16:23:57 +01001834 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1835 if (ret) {
1836 mapping_set_error(page->mapping, ret);
1837 end_extent_writepage(page, ret, page_start, page_end);
1838 ClearPageChecked(page);
1839 goto out;
1840 }
1841
Josef Bacik2ac55d42010-02-03 19:33:23 +00001842 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001843 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001844 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001845out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001846 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1847 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001848out_page:
1849 unlock_page(page);
1850 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001851 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001852}
1853
1854/*
1855 * There are a few paths in the higher layers of the kernel that directly
1856 * set the page dirty bit without asking the filesystem if it is a
1857 * good idea. This causes problems because we want to make sure COW
1858 * properly happens and the data=ordered rules are followed.
1859 *
Chris Masonc8b97812008-10-29 14:49:59 -04001860 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001861 * hasn't been properly setup for IO. We kick off an async process
1862 * to fix it up. The async helper will wait for ordered extents, set
1863 * the delalloc bit and make it safe to write the page.
1864 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001865static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001866{
1867 struct inode *inode = page->mapping->host;
1868 struct btrfs_writepage_fixup *fixup;
1869 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001870
Chris Mason8b62b722009-09-02 16:53:46 -04001871 /* this page is properly in the ordered list */
1872 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001873 return 0;
1874
1875 if (PageChecked(page))
1876 return -EAGAIN;
1877
1878 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1879 if (!fixup)
1880 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001881
Chris Mason247e7432008-07-17 12:53:51 -04001882 SetPageChecked(page);
1883 page_cache_get(page);
1884 fixup->work.func = btrfs_writepage_fixup_worker;
1885 fixup->page = page;
1886 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001887 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001888}
1889
Yan Zhengd899e052008-10-30 14:25:28 -04001890static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1891 struct inode *inode, u64 file_pos,
1892 u64 disk_bytenr, u64 disk_num_bytes,
1893 u64 num_bytes, u64 ram_bytes,
1894 u8 compression, u8 encryption,
1895 u16 other_encoding, int extent_type)
1896{
1897 struct btrfs_root *root = BTRFS_I(inode)->root;
1898 struct btrfs_file_extent_item *fi;
1899 struct btrfs_path *path;
1900 struct extent_buffer *leaf;
1901 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001902 int ret;
1903
1904 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001905 if (!path)
1906 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001907
Chris Masonb9473432009-03-13 11:00:37 -04001908 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001909
1910 /*
1911 * we may be replacing one extent in the tree with another.
1912 * The new extent is pinned in the extent map, and we don't want
1913 * to drop it from the cache until it is completely in the btree.
1914 *
1915 * So, tell btrfs_drop_extents to leave this extent in the cache.
1916 * the caller is expected to unpin it and allow it to be merged
1917 * with the others.
1918 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001919 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001920 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001921 if (ret)
1922 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001923
Li Zefan33345d012011-04-20 10:31:50 +08001924 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001925 ins.offset = file_pos;
1926 ins.type = BTRFS_EXTENT_DATA_KEY;
1927 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928 if (ret)
1929 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001930 leaf = path->nodes[0];
1931 fi = btrfs_item_ptr(leaf, path->slots[0],
1932 struct btrfs_file_extent_item);
1933 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1934 btrfs_set_file_extent_type(leaf, fi, extent_type);
1935 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1936 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1937 btrfs_set_file_extent_offset(leaf, fi, 0);
1938 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1939 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1940 btrfs_set_file_extent_compression(leaf, fi, compression);
1941 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1942 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001943
Yan Zhengd899e052008-10-30 14:25:28 -04001944 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001945 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001946
1947 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001948
1949 ins.objectid = disk_bytenr;
1950 ins.offset = disk_num_bytes;
1951 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001952 ret = btrfs_alloc_reserved_file_extent(trans, root,
1953 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001954 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001955out:
Yan Zhengd899e052008-10-30 14:25:28 -04001956 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001957
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001958 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001959}
1960
Liu Bo38c227d2013-01-29 03:18:40 +00001961/* snapshot-aware defrag */
1962struct sa_defrag_extent_backref {
1963 struct rb_node node;
1964 struct old_sa_defrag_extent *old;
1965 u64 root_id;
1966 u64 inum;
1967 u64 file_pos;
1968 u64 extent_offset;
1969 u64 num_bytes;
1970 u64 generation;
1971};
1972
1973struct old_sa_defrag_extent {
1974 struct list_head list;
1975 struct new_sa_defrag_extent *new;
1976
1977 u64 extent_offset;
1978 u64 bytenr;
1979 u64 offset;
1980 u64 len;
1981 int count;
1982};
1983
1984struct new_sa_defrag_extent {
1985 struct rb_root root;
1986 struct list_head head;
1987 struct btrfs_path *path;
1988 struct inode *inode;
1989 u64 file_pos;
1990 u64 len;
1991 u64 bytenr;
1992 u64 disk_len;
1993 u8 compress_type;
1994};
1995
1996static int backref_comp(struct sa_defrag_extent_backref *b1,
1997 struct sa_defrag_extent_backref *b2)
1998{
1999 if (b1->root_id < b2->root_id)
2000 return -1;
2001 else if (b1->root_id > b2->root_id)
2002 return 1;
2003
2004 if (b1->inum < b2->inum)
2005 return -1;
2006 else if (b1->inum > b2->inum)
2007 return 1;
2008
2009 if (b1->file_pos < b2->file_pos)
2010 return -1;
2011 else if (b1->file_pos > b2->file_pos)
2012 return 1;
2013
2014 /*
2015 * [------------------------------] ===> (a range of space)
2016 * |<--->| |<---->| =============> (fs/file tree A)
2017 * |<---------------------------->| ===> (fs/file tree B)
2018 *
2019 * A range of space can refer to two file extents in one tree while
2020 * refer to only one file extent in another tree.
2021 *
2022 * So we may process a disk offset more than one time(two extents in A)
2023 * and locate at the same extent(one extent in B), then insert two same
2024 * backrefs(both refer to the extent in B).
2025 */
2026 return 0;
2027}
2028
2029static void backref_insert(struct rb_root *root,
2030 struct sa_defrag_extent_backref *backref)
2031{
2032 struct rb_node **p = &root->rb_node;
2033 struct rb_node *parent = NULL;
2034 struct sa_defrag_extent_backref *entry;
2035 int ret;
2036
2037 while (*p) {
2038 parent = *p;
2039 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2040
2041 ret = backref_comp(backref, entry);
2042 if (ret < 0)
2043 p = &(*p)->rb_left;
2044 else
2045 p = &(*p)->rb_right;
2046 }
2047
2048 rb_link_node(&backref->node, parent, p);
2049 rb_insert_color(&backref->node, root);
2050}
2051
2052/*
2053 * Note the backref might has changed, and in this case we just return 0.
2054 */
2055static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2056 void *ctx)
2057{
2058 struct btrfs_file_extent_item *extent;
2059 struct btrfs_fs_info *fs_info;
2060 struct old_sa_defrag_extent *old = ctx;
2061 struct new_sa_defrag_extent *new = old->new;
2062 struct btrfs_path *path = new->path;
2063 struct btrfs_key key;
2064 struct btrfs_root *root;
2065 struct sa_defrag_extent_backref *backref;
2066 struct extent_buffer *leaf;
2067 struct inode *inode = new->inode;
2068 int slot;
2069 int ret;
2070 u64 extent_offset;
2071 u64 num_bytes;
2072
2073 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2074 inum == btrfs_ino(inode))
2075 return 0;
2076
2077 key.objectid = root_id;
2078 key.type = BTRFS_ROOT_ITEM_KEY;
2079 key.offset = (u64)-1;
2080
2081 fs_info = BTRFS_I(inode)->root->fs_info;
2082 root = btrfs_read_fs_root_no_name(fs_info, &key);
2083 if (IS_ERR(root)) {
2084 if (PTR_ERR(root) == -ENOENT)
2085 return 0;
2086 WARN_ON(1);
2087 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2088 inum, offset, root_id);
2089 return PTR_ERR(root);
2090 }
2091
2092 key.objectid = inum;
2093 key.type = BTRFS_EXTENT_DATA_KEY;
2094 if (offset > (u64)-1 << 32)
2095 key.offset = 0;
2096 else
2097 key.offset = offset;
2098
2099 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2100 if (ret < 0) {
2101 WARN_ON(1);
2102 return ret;
2103 }
2104
2105 while (1) {
2106 cond_resched();
2107
2108 leaf = path->nodes[0];
2109 slot = path->slots[0];
2110
2111 if (slot >= btrfs_header_nritems(leaf)) {
2112 ret = btrfs_next_leaf(root, path);
2113 if (ret < 0) {
2114 goto out;
2115 } else if (ret > 0) {
2116 ret = 0;
2117 goto out;
2118 }
2119 continue;
2120 }
2121
2122 path->slots[0]++;
2123
2124 btrfs_item_key_to_cpu(leaf, &key, slot);
2125
2126 if (key.objectid > inum)
2127 goto out;
2128
2129 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2130 continue;
2131
2132 extent = btrfs_item_ptr(leaf, slot,
2133 struct btrfs_file_extent_item);
2134
2135 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2136 continue;
2137
2138 extent_offset = btrfs_file_extent_offset(leaf, extent);
2139 if (key.offset - extent_offset != offset)
2140 continue;
2141
2142 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2143 if (extent_offset >= old->extent_offset + old->offset +
2144 old->len || extent_offset + num_bytes <=
2145 old->extent_offset + old->offset)
2146 continue;
2147
2148 break;
2149 }
2150
2151 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2152 if (!backref) {
2153 ret = -ENOENT;
2154 goto out;
2155 }
2156
2157 backref->root_id = root_id;
2158 backref->inum = inum;
2159 backref->file_pos = offset + extent_offset;
2160 backref->num_bytes = num_bytes;
2161 backref->extent_offset = extent_offset;
2162 backref->generation = btrfs_file_extent_generation(leaf, extent);
2163 backref->old = old;
2164 backref_insert(&new->root, backref);
2165 old->count++;
2166out:
2167 btrfs_release_path(path);
2168 WARN_ON(ret);
2169 return ret;
2170}
2171
2172static noinline bool record_extent_backrefs(struct btrfs_path *path,
2173 struct new_sa_defrag_extent *new)
2174{
2175 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2176 struct old_sa_defrag_extent *old, *tmp;
2177 int ret;
2178
2179 new->path = path;
2180
2181 list_for_each_entry_safe(old, tmp, &new->head, list) {
2182 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2183 path, record_one_backref,
2184 old);
2185 BUG_ON(ret < 0 && ret != -ENOENT);
2186
2187 /* no backref to be processed for this extent */
2188 if (!old->count) {
2189 list_del(&old->list);
2190 kfree(old);
2191 }
2192 }
2193
2194 if (list_empty(&new->head))
2195 return false;
2196
2197 return true;
2198}
2199
2200static int relink_is_mergable(struct extent_buffer *leaf,
2201 struct btrfs_file_extent_item *fi,
2202 u64 disk_bytenr)
2203{
2204 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2205 return 0;
2206
2207 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2208 return 0;
2209
2210 if (btrfs_file_extent_compression(leaf, fi) ||
2211 btrfs_file_extent_encryption(leaf, fi) ||
2212 btrfs_file_extent_other_encoding(leaf, fi))
2213 return 0;
2214
2215 return 1;
2216}
2217
2218/*
2219 * Note the backref might has changed, and in this case we just return 0.
2220 */
2221static noinline int relink_extent_backref(struct btrfs_path *path,
2222 struct sa_defrag_extent_backref *prev,
2223 struct sa_defrag_extent_backref *backref)
2224{
2225 struct btrfs_file_extent_item *extent;
2226 struct btrfs_file_extent_item *item;
2227 struct btrfs_ordered_extent *ordered;
2228 struct btrfs_trans_handle *trans;
2229 struct btrfs_fs_info *fs_info;
2230 struct btrfs_root *root;
2231 struct btrfs_key key;
2232 struct extent_buffer *leaf;
2233 struct old_sa_defrag_extent *old = backref->old;
2234 struct new_sa_defrag_extent *new = old->new;
2235 struct inode *src_inode = new->inode;
2236 struct inode *inode;
2237 struct extent_state *cached = NULL;
2238 int ret = 0;
2239 u64 start;
2240 u64 len;
2241 u64 lock_start;
2242 u64 lock_end;
2243 bool merge = false;
2244 int index;
2245
2246 if (prev && prev->root_id == backref->root_id &&
2247 prev->inum == backref->inum &&
2248 prev->file_pos + prev->num_bytes == backref->file_pos)
2249 merge = true;
2250
2251 /* step 1: get root */
2252 key.objectid = backref->root_id;
2253 key.type = BTRFS_ROOT_ITEM_KEY;
2254 key.offset = (u64)-1;
2255
2256 fs_info = BTRFS_I(src_inode)->root->fs_info;
2257 index = srcu_read_lock(&fs_info->subvol_srcu);
2258
2259 root = btrfs_read_fs_root_no_name(fs_info, &key);
2260 if (IS_ERR(root)) {
2261 srcu_read_unlock(&fs_info->subvol_srcu, index);
2262 if (PTR_ERR(root) == -ENOENT)
2263 return 0;
2264 return PTR_ERR(root);
2265 }
2266 if (btrfs_root_refs(&root->root_item) == 0) {
2267 srcu_read_unlock(&fs_info->subvol_srcu, index);
2268 /* parse ENOENT to 0 */
2269 return 0;
2270 }
2271
2272 /* step 2: get inode */
2273 key.objectid = backref->inum;
2274 key.type = BTRFS_INODE_ITEM_KEY;
2275 key.offset = 0;
2276
2277 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2278 if (IS_ERR(inode)) {
2279 srcu_read_unlock(&fs_info->subvol_srcu, index);
2280 return 0;
2281 }
2282
2283 srcu_read_unlock(&fs_info->subvol_srcu, index);
2284
2285 /* step 3: relink backref */
2286 lock_start = backref->file_pos;
2287 lock_end = backref->file_pos + backref->num_bytes - 1;
2288 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2289 0, &cached);
2290
2291 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2292 if (ordered) {
2293 btrfs_put_ordered_extent(ordered);
2294 goto out_unlock;
2295 }
2296
2297 trans = btrfs_join_transaction(root);
2298 if (IS_ERR(trans)) {
2299 ret = PTR_ERR(trans);
2300 goto out_unlock;
2301 }
2302
2303 key.objectid = backref->inum;
2304 key.type = BTRFS_EXTENT_DATA_KEY;
2305 key.offset = backref->file_pos;
2306
2307 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2308 if (ret < 0) {
2309 goto out_free_path;
2310 } else if (ret > 0) {
2311 ret = 0;
2312 goto out_free_path;
2313 }
2314
2315 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2316 struct btrfs_file_extent_item);
2317
2318 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2319 backref->generation)
2320 goto out_free_path;
2321
2322 btrfs_release_path(path);
2323
2324 start = backref->file_pos;
2325 if (backref->extent_offset < old->extent_offset + old->offset)
2326 start += old->extent_offset + old->offset -
2327 backref->extent_offset;
2328
2329 len = min(backref->extent_offset + backref->num_bytes,
2330 old->extent_offset + old->offset + old->len);
2331 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2332
2333 ret = btrfs_drop_extents(trans, root, inode, start,
2334 start + len, 1);
2335 if (ret)
2336 goto out_free_path;
2337again:
2338 key.objectid = btrfs_ino(inode);
2339 key.type = BTRFS_EXTENT_DATA_KEY;
2340 key.offset = start;
2341
Liu Boa09a0a72013-03-11 09:20:58 +00002342 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002343 if (merge) {
2344 struct btrfs_file_extent_item *fi;
2345 u64 extent_len;
2346 struct btrfs_key found_key;
2347
2348 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2349 if (ret < 0)
2350 goto out_free_path;
2351
2352 path->slots[0]--;
2353 leaf = path->nodes[0];
2354 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2355
2356 fi = btrfs_item_ptr(leaf, path->slots[0],
2357 struct btrfs_file_extent_item);
2358 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2359
2360 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2361 extent_len + found_key.offset == start) {
2362 btrfs_set_file_extent_num_bytes(leaf, fi,
2363 extent_len + len);
2364 btrfs_mark_buffer_dirty(leaf);
2365 inode_add_bytes(inode, len);
2366
2367 ret = 1;
2368 goto out_free_path;
2369 } else {
2370 merge = false;
2371 btrfs_release_path(path);
2372 goto again;
2373 }
2374 }
2375
2376 ret = btrfs_insert_empty_item(trans, root, path, &key,
2377 sizeof(*extent));
2378 if (ret) {
2379 btrfs_abort_transaction(trans, root, ret);
2380 goto out_free_path;
2381 }
2382
2383 leaf = path->nodes[0];
2384 item = btrfs_item_ptr(leaf, path->slots[0],
2385 struct btrfs_file_extent_item);
2386 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2387 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2388 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2389 btrfs_set_file_extent_num_bytes(leaf, item, len);
2390 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2391 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2392 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2393 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2394 btrfs_set_file_extent_encryption(leaf, item, 0);
2395 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2396
2397 btrfs_mark_buffer_dirty(leaf);
2398 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002399 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002400
2401 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2402 new->disk_len, 0,
2403 backref->root_id, backref->inum,
2404 new->file_pos, 0); /* start - extent_offset */
2405 if (ret) {
2406 btrfs_abort_transaction(trans, root, ret);
2407 goto out_free_path;
2408 }
2409
2410 ret = 1;
2411out_free_path:
2412 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002413 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002414 btrfs_end_transaction(trans, root);
2415out_unlock:
2416 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2417 &cached, GFP_NOFS);
2418 iput(inode);
2419 return ret;
2420}
2421
Liu Bof5749e32013-10-29 10:45:05 +08002422static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2423{
2424 struct old_sa_defrag_extent *old, *tmp;
2425
2426 if (!new)
2427 return;
2428
2429 list_for_each_entry_safe(old, tmp, &new->head, list) {
2430 list_del(&old->list);
2431 kfree(old);
2432 }
2433 kfree(new);
2434}
2435
Liu Bo38c227d2013-01-29 03:18:40 +00002436static void relink_file_extents(struct new_sa_defrag_extent *new)
2437{
2438 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002439 struct sa_defrag_extent_backref *backref;
2440 struct sa_defrag_extent_backref *prev = NULL;
2441 struct inode *inode;
2442 struct btrfs_root *root;
2443 struct rb_node *node;
2444 int ret;
2445
2446 inode = new->inode;
2447 root = BTRFS_I(inode)->root;
2448
2449 path = btrfs_alloc_path();
2450 if (!path)
2451 return;
2452
2453 if (!record_extent_backrefs(path, new)) {
2454 btrfs_free_path(path);
2455 goto out;
2456 }
2457 btrfs_release_path(path);
2458
2459 while (1) {
2460 node = rb_first(&new->root);
2461 if (!node)
2462 break;
2463 rb_erase(node, &new->root);
2464
2465 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2466
2467 ret = relink_extent_backref(path, prev, backref);
2468 WARN_ON(ret < 0);
2469
2470 kfree(prev);
2471
2472 if (ret == 1)
2473 prev = backref;
2474 else
2475 prev = NULL;
2476 cond_resched();
2477 }
2478 kfree(prev);
2479
2480 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002481out:
Liu Bof5749e32013-10-29 10:45:05 +08002482 free_sa_defrag_extent(new);
2483
Liu Bo38c227d2013-01-29 03:18:40 +00002484 atomic_dec(&root->fs_info->defrag_running);
2485 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002486}
2487
2488static struct new_sa_defrag_extent *
2489record_old_file_extents(struct inode *inode,
2490 struct btrfs_ordered_extent *ordered)
2491{
2492 struct btrfs_root *root = BTRFS_I(inode)->root;
2493 struct btrfs_path *path;
2494 struct btrfs_key key;
Liu Bof5749e32013-10-29 10:45:05 +08002495 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002496 struct new_sa_defrag_extent *new;
2497 int ret;
2498
2499 new = kmalloc(sizeof(*new), GFP_NOFS);
2500 if (!new)
2501 return NULL;
2502
2503 new->inode = inode;
2504 new->file_pos = ordered->file_offset;
2505 new->len = ordered->len;
2506 new->bytenr = ordered->start;
2507 new->disk_len = ordered->disk_len;
2508 new->compress_type = ordered->compress_type;
2509 new->root = RB_ROOT;
2510 INIT_LIST_HEAD(&new->head);
2511
2512 path = btrfs_alloc_path();
2513 if (!path)
2514 goto out_kfree;
2515
2516 key.objectid = btrfs_ino(inode);
2517 key.type = BTRFS_EXTENT_DATA_KEY;
2518 key.offset = new->file_pos;
2519
2520 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2521 if (ret < 0)
2522 goto out_free_path;
2523 if (ret > 0 && path->slots[0] > 0)
2524 path->slots[0]--;
2525
2526 /* find out all the old extents for the file range */
2527 while (1) {
2528 struct btrfs_file_extent_item *extent;
2529 struct extent_buffer *l;
2530 int slot;
2531 u64 num_bytes;
2532 u64 offset;
2533 u64 end;
2534 u64 disk_bytenr;
2535 u64 extent_offset;
2536
2537 l = path->nodes[0];
2538 slot = path->slots[0];
2539
2540 if (slot >= btrfs_header_nritems(l)) {
2541 ret = btrfs_next_leaf(root, path);
2542 if (ret < 0)
Liu Bof5749e32013-10-29 10:45:05 +08002543 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002544 else if (ret > 0)
2545 break;
2546 continue;
2547 }
2548
2549 btrfs_item_key_to_cpu(l, &key, slot);
2550
2551 if (key.objectid != btrfs_ino(inode))
2552 break;
2553 if (key.type != BTRFS_EXTENT_DATA_KEY)
2554 break;
2555 if (key.offset >= new->file_pos + new->len)
2556 break;
2557
2558 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2559
2560 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2561 if (key.offset + num_bytes < new->file_pos)
2562 goto next;
2563
2564 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2565 if (!disk_bytenr)
2566 goto next;
2567
2568 extent_offset = btrfs_file_extent_offset(l, extent);
2569
2570 old = kmalloc(sizeof(*old), GFP_NOFS);
2571 if (!old)
Liu Bof5749e32013-10-29 10:45:05 +08002572 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002573
2574 offset = max(new->file_pos, key.offset);
2575 end = min(new->file_pos + new->len, key.offset + num_bytes);
2576
2577 old->bytenr = disk_bytenr;
2578 old->extent_offset = extent_offset;
2579 old->offset = offset - key.offset;
2580 old->len = end - offset;
2581 old->new = new;
2582 old->count = 0;
2583 list_add_tail(&old->list, &new->head);
2584next:
2585 path->slots[0]++;
2586 cond_resched();
2587 }
2588
2589 btrfs_free_path(path);
2590 atomic_inc(&root->fs_info->defrag_running);
2591
2592 return new;
2593
Liu Bo38c227d2013-01-29 03:18:40 +00002594out_free_path:
2595 btrfs_free_path(path);
2596out_kfree:
Liu Bof5749e32013-10-29 10:45:05 +08002597 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002598 return NULL;
2599}
2600
Chris Mason5d13a982009-03-13 11:41:46 -04002601/*
2602 * helper function for btrfs_finish_ordered_io, this
2603 * just reads in some of the csum leaves to prime them into ram
2604 * before we start the transaction. It limits the amount of btree
2605 * reads required while inside the transaction.
2606 */
Chris Masond352ac62008-09-29 15:18:18 -04002607/* as ordered data IO finishes, this gets called so we can finish
2608 * an ordered extent if the range of bytes in the file it covers are
2609 * fully written.
2610 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002611static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002612{
Josef Bacik5fd02042012-05-02 14:00:54 -04002613 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002614 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002615 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002616 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002617 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002618 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002619 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002620 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002621 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002622
Liu Bo83eea1f2012-07-10 05:28:39 -06002623 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002624
Josef Bacik5fd02042012-05-02 14:00:54 -04002625 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2626 ret = -EIO;
2627 goto out;
2628 }
2629
Yan, Zhengc2167752009-11-12 09:34:21 +00002630 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002631 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002632 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2633 if (nolock)
2634 trans = btrfs_join_transaction_nolock(root);
2635 else
2636 trans = btrfs_join_transaction(root);
2637 if (IS_ERR(trans)) {
2638 ret = PTR_ERR(trans);
2639 trans = NULL;
2640 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002641 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002642 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2643 ret = btrfs_update_inode_fallback(trans, root, inode);
2644 if (ret) /* -ENOMEM or corruption */
2645 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002646 goto out;
2647 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002648
Josef Bacik2ac55d42010-02-03 19:33:23 +00002649 lock_extent_bits(io_tree, ordered_extent->file_offset,
2650 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002651 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002652
Liu Bo38c227d2013-01-29 03:18:40 +00002653 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2654 ordered_extent->file_offset + ordered_extent->len - 1,
2655 EXTENT_DEFRAG, 1, cached_state);
2656 if (ret) {
2657 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacika082f872014-01-29 16:05:30 -05002658 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002659 /* the inode is shared */
2660 new = record_old_file_extents(inode, ordered_extent);
2661
2662 clear_extent_bit(io_tree, ordered_extent->file_offset,
2663 ordered_extent->file_offset + ordered_extent->len - 1,
2664 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2665 }
2666
Josef Bacik0cb59c92010-07-02 12:14:14 -04002667 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002668 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002669 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002670 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002671 if (IS_ERR(trans)) {
2672 ret = PTR_ERR(trans);
2673 trans = NULL;
2674 goto out_unlock;
2675 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002676 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002677
Chris Masonc8b97812008-10-29 14:49:59 -04002678 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002679 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002680 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002681 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002682 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002683 ordered_extent->file_offset,
2684 ordered_extent->file_offset +
2685 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002686 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002687 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002688 ret = insert_reserved_file_extent(trans, inode,
2689 ordered_extent->file_offset,
2690 ordered_extent->start,
2691 ordered_extent->disk_len,
2692 ordered_extent->len,
2693 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002694 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002695 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002696 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002697 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2698 ordered_extent->file_offset, ordered_extent->len,
2699 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002700 if (ret < 0) {
2701 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002702 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002703 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002704
Chris Masone6dcd2d2008-07-17 12:53:50 -04002705 add_pending_csums(trans, inode, ordered_extent->file_offset,
2706 &ordered_extent->list);
2707
Josef Bacik6c760c02012-11-09 10:53:21 -05002708 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2709 ret = btrfs_update_inode_fallback(trans, root, inode);
2710 if (ret) { /* -ENOMEM or corruption */
2711 btrfs_abort_transaction(trans, root, ret);
2712 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002713 }
2714 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002715out_unlock:
2716 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2717 ordered_extent->file_offset +
2718 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002719out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002720 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002721 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002722 if (trans)
2723 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002724
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002725 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002726 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2727 ordered_extent->file_offset +
2728 ordered_extent->len - 1, NULL, GFP_NOFS);
2729
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002730 /*
2731 * If the ordered extent had an IOERR or something else went
2732 * wrong we need to return the space for this ordered extent
2733 * back to the allocator.
2734 */
2735 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2736 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2737 btrfs_free_reserved_extent(root, ordered_extent->start,
2738 ordered_extent->disk_len);
2739 }
2740
2741
Josef Bacik5fd02042012-05-02 14:00:54 -04002742 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002743 * This needs to be done to make sure anybody waiting knows we are done
2744 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002745 */
2746 btrfs_remove_ordered_extent(inode, ordered_extent);
2747
Liu Bo38c227d2013-01-29 03:18:40 +00002748 /* for snapshot-aware defrag */
Liu Bof5749e32013-10-29 10:45:05 +08002749 if (new) {
2750 if (ret) {
2751 free_sa_defrag_extent(new);
2752 atomic_dec(&root->fs_info->defrag_running);
2753 } else {
2754 relink_file_extents(new);
2755 }
2756 }
Liu Bo38c227d2013-01-29 03:18:40 +00002757
Chris Masone6dcd2d2008-07-17 12:53:50 -04002758 /* once for us */
2759 btrfs_put_ordered_extent(ordered_extent);
2760 /* once for the tree */
2761 btrfs_put_ordered_extent(ordered_extent);
2762
Josef Bacik5fd02042012-05-02 14:00:54 -04002763 return ret;
2764}
2765
2766static void finish_ordered_fn(struct btrfs_work *work)
2767{
2768 struct btrfs_ordered_extent *ordered_extent;
2769 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2770 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002771}
2772
Christoph Hellwigb2950862008-12-02 09:54:17 -05002773static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002774 struct extent_state *state, int uptodate)
2775{
Josef Bacik5fd02042012-05-02 14:00:54 -04002776 struct inode *inode = page->mapping->host;
2777 struct btrfs_root *root = BTRFS_I(inode)->root;
2778 struct btrfs_ordered_extent *ordered_extent = NULL;
2779 struct btrfs_workers *workers;
2780
liubo1abe9b82011-03-24 11:18:59 +00002781 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2782
Chris Mason8b62b722009-09-02 16:53:46 -04002783 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002784 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2785 end - start + 1, uptodate))
2786 return 0;
2787
2788 ordered_extent->work.func = finish_ordered_fn;
2789 ordered_extent->work.flags = 0;
2790
Liu Bo83eea1f2012-07-10 05:28:39 -06002791 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002792 workers = &root->fs_info->endio_freespace_worker;
2793 else
2794 workers = &root->fs_info->endio_write_workers;
2795 btrfs_queue_worker(workers, &ordered_extent->work);
2796
2797 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002798}
2799
Chris Masond352ac62008-09-29 15:18:18 -04002800/*
Chris Masond352ac62008-09-29 15:18:18 -04002801 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002802 * if there's a match, we allow the bio to finish. If not, the code in
2803 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002804 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002805static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002806 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002807{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002808 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002809 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002810 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002811 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002812 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002813 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002814 struct btrfs_root *root = BTRFS_I(inode)->root;
2815 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002816 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2817 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002818
Chris Masond20f7042008-12-08 16:58:54 -05002819 if (PageChecked(page)) {
2820 ClearPageChecked(page);
2821 goto good;
2822 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002823
2824 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002825 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002826
Yan Zheng17d217f2008-12-12 10:03:38 -05002827 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002828 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002829 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2830 GFP_NOFS);
2831 return 0;
2832 }
2833
Yanc2e639f2008-02-04 08:57:25 -05002834 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002835 private = state->private;
2836 ret = 0;
2837 } else {
2838 ret = get_state_private(io_tree, start, &private);
2839 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002840 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002841 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002842 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002843
Liu Bob0496682013-03-14 14:57:45 +00002844 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002845 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002846 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002847 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002848
Cong Wang7ac687d2011-11-25 23:14:28 +08002849 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002850good:
Chris Mason07157aa2007-08-30 08:50:51 -04002851 return 0;
2852
2853zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002854 if (__ratelimit(&_rs))
2855 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2856 (unsigned long long)btrfs_ino(page->mapping->host),
2857 (unsigned long long)start, csum,
2858 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002859 memset(kaddr + offset, 1, end - start + 1);
2860 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002861 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002862 if (private == 0)
2863 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002864 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002865}
Chris Masonb888db22007-08-27 16:49:44 -04002866
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002867struct delayed_iput {
2868 struct list_head list;
2869 struct inode *inode;
2870};
2871
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002872/* JDM: If this is fs-wide, why can't we add a pointer to
2873 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002874void btrfs_add_delayed_iput(struct inode *inode)
2875{
2876 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2877 struct delayed_iput *delayed;
2878
2879 if (atomic_add_unless(&inode->i_count, -1, 1))
2880 return;
2881
2882 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2883 delayed->inode = inode;
2884
2885 spin_lock(&fs_info->delayed_iput_lock);
2886 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2887 spin_unlock(&fs_info->delayed_iput_lock);
2888}
2889
2890void btrfs_run_delayed_iputs(struct btrfs_root *root)
2891{
2892 LIST_HEAD(list);
2893 struct btrfs_fs_info *fs_info = root->fs_info;
2894 struct delayed_iput *delayed;
2895 int empty;
2896
2897 spin_lock(&fs_info->delayed_iput_lock);
2898 empty = list_empty(&fs_info->delayed_iputs);
2899 spin_unlock(&fs_info->delayed_iput_lock);
2900 if (empty)
2901 return;
2902
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002903 spin_lock(&fs_info->delayed_iput_lock);
2904 list_splice_init(&fs_info->delayed_iputs, &list);
2905 spin_unlock(&fs_info->delayed_iput_lock);
2906
2907 while (!list_empty(&list)) {
2908 delayed = list_entry(list.next, struct delayed_iput, list);
2909 list_del(&delayed->list);
2910 iput(delayed->inode);
2911 kfree(delayed);
2912 }
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002913}
2914
Yan, Zhengd68fc572010-05-16 10:49:58 -04002915/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002916 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002917 * files in the subvolume, it removes orphan item and frees block_rsv
2918 * structure.
2919 */
2920void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2921 struct btrfs_root *root)
2922{
Josef Bacik90290e12011-12-02 15:44:12 -05002923 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002924 int ret;
2925
Josef Bacik8a35d952012-05-23 14:26:42 -04002926 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002927 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2928 return;
2929
Josef Bacik90290e12011-12-02 15:44:12 -05002930 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002931 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002932 spin_unlock(&root->orphan_lock);
2933 return;
2934 }
2935
2936 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2937 spin_unlock(&root->orphan_lock);
2938 return;
2939 }
2940
2941 block_rsv = root->orphan_block_rsv;
2942 root->orphan_block_rsv = NULL;
2943 spin_unlock(&root->orphan_lock);
2944
Yan, Zhengd68fc572010-05-16 10:49:58 -04002945 if (root->orphan_item_inserted &&
2946 btrfs_root_refs(&root->root_item) > 0) {
2947 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2948 root->root_key.objectid);
2949 BUG_ON(ret);
2950 root->orphan_item_inserted = 0;
2951 }
2952
Josef Bacik90290e12011-12-02 15:44:12 -05002953 if (block_rsv) {
2954 WARN_ON(block_rsv->size > 0);
2955 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002956 }
2957}
2958
2959/*
Josef Bacik7b128762008-07-24 12:17:14 -04002960 * This creates an orphan entry for the given inode in case something goes
2961 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002962 *
2963 * NOTE: caller of this function should reserve 5 units of metadata for
2964 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002965 */
2966int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2967{
2968 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002969 struct btrfs_block_rsv *block_rsv = NULL;
2970 int reserve = 0;
2971 int insert = 0;
2972 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002973
Yan, Zhengd68fc572010-05-16 10:49:58 -04002974 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002975 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002976 if (!block_rsv)
2977 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002978 }
2979
Yan, Zhengd68fc572010-05-16 10:49:58 -04002980 spin_lock(&root->orphan_lock);
2981 if (!root->orphan_block_rsv) {
2982 root->orphan_block_rsv = block_rsv;
2983 } else if (block_rsv) {
2984 btrfs_free_block_rsv(root, block_rsv);
2985 block_rsv = NULL;
2986 }
Josef Bacik7b128762008-07-24 12:17:14 -04002987
Josef Bacik8a35d952012-05-23 14:26:42 -04002988 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2989 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002990#if 0
2991 /*
2992 * For proper ENOSPC handling, we should do orphan
2993 * cleanup when mounting. But this introduces backward
2994 * compatibility issue.
2995 */
2996 if (!xchg(&root->orphan_item_inserted, 1))
2997 insert = 2;
2998 else
2999 insert = 1;
3000#endif
3001 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003002 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003003 }
Josef Bacik7b128762008-07-24 12:17:14 -04003004
Josef Bacik72ac3c02012-05-23 14:13:11 -04003005 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3006 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003007 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003008 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003009
Yan, Zhengd68fc572010-05-16 10:49:58 -04003010 /* grab metadata reservation from transaction handle */
3011 if (reserve) {
3012 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003013 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003014 }
3015
3016 /* insert an orphan item to track this unlinked/truncated file */
3017 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003018 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003019 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003020 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3021 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003022 btrfs_abort_transaction(trans, root, ret);
3023 return ret;
3024 }
3025 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003026 }
3027
3028 /* insert an orphan item to track subvolume contains orphan files */
3029 if (insert >= 2) {
3030 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3031 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003032 if (ret && ret != -EEXIST) {
3033 btrfs_abort_transaction(trans, root, ret);
3034 return ret;
3035 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003036 }
3037 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003038}
3039
3040/*
3041 * We have done the truncate/delete so we can go ahead and remove the orphan
3042 * item for this particular inode.
3043 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003044static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3045 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003046{
3047 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003048 int delete_item = 0;
3049 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003050 int ret = 0;
3051
Yan, Zhengd68fc572010-05-16 10:49:58 -04003052 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003053 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3054 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003055 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003056
Josef Bacik72ac3c02012-05-23 14:13:11 -04003057 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3058 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003059 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003060 spin_unlock(&root->orphan_lock);
3061
3062 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003063 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003064 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003065 }
3066
Josef Bacik8a35d952012-05-23 14:26:42 -04003067 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003068 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003069 atomic_dec(&root->orphan_inodes);
3070 }
Josef Bacik7b128762008-07-24 12:17:14 -04003071
Yan, Zhengd68fc572010-05-16 10:49:58 -04003072 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003073}
3074
3075/*
3076 * this cleans up any orphans that may be left on the list from the last use
3077 * of this root.
3078 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003079int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003080{
3081 struct btrfs_path *path;
3082 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003083 struct btrfs_key key, found_key;
3084 struct btrfs_trans_handle *trans;
3085 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003086 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003087 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3088
Yan, Zhengd68fc572010-05-16 10:49:58 -04003089 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003090 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003091
3092 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003093 if (!path) {
3094 ret = -ENOMEM;
3095 goto out;
3096 }
Josef Bacik7b128762008-07-24 12:17:14 -04003097 path->reada = -1;
3098
3099 key.objectid = BTRFS_ORPHAN_OBJECTID;
3100 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3101 key.offset = (u64)-1;
3102
Josef Bacik7b128762008-07-24 12:17:14 -04003103 while (1) {
3104 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003105 if (ret < 0)
3106 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003107
3108 /*
3109 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003110 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003111 * find the key and see if we have stuff that matches
3112 */
3113 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003114 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003115 if (path->slots[0] == 0)
3116 break;
3117 path->slots[0]--;
3118 }
3119
3120 /* pull out the item */
3121 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003122 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3123
3124 /* make sure the item matches what we want */
3125 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3126 break;
3127 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3128 break;
3129
3130 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003131 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003132
3133 /*
3134 * this is where we are basically btrfs_lookup, without the
3135 * crossing root thing. we store the inode number in the
3136 * offset of the orphan item.
3137 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003138
3139 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003140 btrfs_err(root->fs_info,
3141 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003142 ret = -EINVAL;
3143 goto out;
3144 }
3145
3146 last_objectid = found_key.offset;
3147
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003148 found_key.objectid = found_key.offset;
3149 found_key.type = BTRFS_INODE_ITEM_KEY;
3150 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003151 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003152 ret = PTR_RET(inode);
3153 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003154 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003155
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003156 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3157 struct btrfs_root *dead_root;
3158 struct btrfs_fs_info *fs_info = root->fs_info;
3159 int is_dead_root = 0;
3160
3161 /*
3162 * this is an orphan in the tree root. Currently these
3163 * could come from 2 sources:
3164 * a) a snapshot deletion in progress
3165 * b) a free space cache inode
3166 * We need to distinguish those two, as the snapshot
3167 * orphan must not get deleted.
3168 * find_dead_roots already ran before us, so if this
3169 * is a snapshot deletion, we should find the root
3170 * in the dead_roots list
3171 */
3172 spin_lock(&fs_info->trans_lock);
3173 list_for_each_entry(dead_root, &fs_info->dead_roots,
3174 root_list) {
3175 if (dead_root->root_key.objectid ==
3176 found_key.objectid) {
3177 is_dead_root = 1;
3178 break;
3179 }
3180 }
3181 spin_unlock(&fs_info->trans_lock);
3182 if (is_dead_root) {
3183 /* prevent this orphan from being found again */
3184 key.offset = found_key.objectid - 1;
3185 continue;
3186 }
3187 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003188 /*
3189 * Inode is already gone but the orphan item is still there,
3190 * kill the orphan item.
3191 */
3192 if (ret == -ESTALE) {
3193 trans = btrfs_start_transaction(root, 1);
3194 if (IS_ERR(trans)) {
3195 ret = PTR_ERR(trans);
3196 goto out;
3197 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003198 btrfs_debug(root->fs_info, "auto deleting %Lu",
3199 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003200 ret = btrfs_del_orphan_item(trans, root,
3201 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003202 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003203 btrfs_end_transaction(trans, root);
3204 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003205 }
Josef Bacik7b128762008-07-24 12:17:14 -04003206
Josef Bacik7b128762008-07-24 12:17:14 -04003207 /*
3208 * add this inode to the orphan list so btrfs_orphan_del does
3209 * the proper thing when we hit it
3210 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003211 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3212 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003213 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003214
Josef Bacik7b128762008-07-24 12:17:14 -04003215 /* if we have links, this was a truncate, lets do that */
3216 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003217 if (!S_ISREG(inode->i_mode)) {
3218 WARN_ON(1);
3219 iput(inode);
3220 continue;
3221 }
Josef Bacik7b128762008-07-24 12:17:14 -04003222 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003223
3224 /* 1 for the orphan item deletion. */
3225 trans = btrfs_start_transaction(root, 1);
3226 if (IS_ERR(trans)) {
3227 ret = PTR_ERR(trans);
3228 goto out;
3229 }
3230 ret = btrfs_orphan_add(trans, inode);
3231 btrfs_end_transaction(trans, root);
3232 if (ret)
3233 goto out;
3234
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003235 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003236 if (ret)
3237 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003238 } else {
3239 nr_unlink++;
3240 }
3241
3242 /* this will do delete_inode and everything for us */
3243 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003244 if (ret)
3245 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003246 }
Miao Xie3254c872011-11-10 20:45:05 -05003247 /* release the path since we're done with it */
3248 btrfs_release_path(path);
3249
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3251
3252 if (root->orphan_block_rsv)
3253 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3254 (u64)-1);
3255
3256 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003257 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003258 if (!IS_ERR(trans))
3259 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003260 }
Josef Bacik7b128762008-07-24 12:17:14 -04003261
3262 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003263 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003264 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003265 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003266
3267out:
3268 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003269 btrfs_crit(root->fs_info,
3270 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003271 btrfs_free_path(path);
3272 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003273}
3274
Chris Masond352ac62008-09-29 15:18:18 -04003275/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003276 * very simple check to peek ahead in the leaf looking for xattrs. If we
3277 * don't find any xattrs, we know there can't be any acls.
3278 *
3279 * slot is the slot the inode is in, objectid is the objectid of the inode
3280 */
3281static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3282 int slot, u64 objectid)
3283{
3284 u32 nritems = btrfs_header_nritems(leaf);
3285 struct btrfs_key found_key;
3286 int scanned = 0;
3287
3288 slot++;
3289 while (slot < nritems) {
3290 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3291
3292 /* we found a different objectid, there must not be acls */
3293 if (found_key.objectid != objectid)
3294 return 0;
3295
3296 /* we found an xattr, assume we've got an acl */
3297 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3298 return 1;
3299
3300 /*
3301 * we found a key greater than an xattr key, there can't
3302 * be any acls later on
3303 */
3304 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3305 return 0;
3306
3307 slot++;
3308 scanned++;
3309
3310 /*
3311 * it goes inode, inode backrefs, xattrs, extents,
3312 * so if there are a ton of hard links to an inode there can
3313 * be a lot of backrefs. Don't waste time searching too hard,
3314 * this is just an optimization
3315 */
3316 if (scanned >= 8)
3317 break;
3318 }
3319 /* we hit the end of the leaf before we found an xattr or
3320 * something larger than an xattr. We have to assume the inode
3321 * has acls
3322 */
3323 return 1;
3324}
3325
3326/*
Chris Masond352ac62008-09-29 15:18:18 -04003327 * read an inode from the btree into the in-memory inode
3328 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003329static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003330{
3331 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003332 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003333 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003334 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003335 struct btrfs_root *root = BTRFS_I(inode)->root;
3336 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003337 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003338 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003339 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003340 bool filled = false;
3341
3342 ret = btrfs_fill_inode(inode, &rdev);
3343 if (!ret)
3344 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003345
3346 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003347 if (!path)
3348 goto make_bad;
3349
Josef Bacikd90c7322011-05-17 09:50:54 -04003350 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003351 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003352
Chris Mason39279cc2007-06-12 06:35:45 -04003353 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003354 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003355 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003356
Chris Mason5f39d392007-10-15 16:14:19 -04003357 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003358
3359 if (filled)
3360 goto cache_acl;
3361
Chris Mason5f39d392007-10-15 16:14:19 -04003362 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3363 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003364 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003365 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003366 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3367 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003368 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003369
3370 tspec = btrfs_inode_atime(inode_item);
3371 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3372 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3373
3374 tspec = btrfs_inode_mtime(inode_item);
3375 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3376 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3377
3378 tspec = btrfs_inode_ctime(inode_item);
3379 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3380 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3381
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003382 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003383 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003384 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3385
3386 /*
3387 * If we were modified in the current generation and evicted from memory
3388 * and then re-read we need to do a full sync since we don't have any
3389 * idea about which extents were modified before we were evicted from
3390 * cache.
3391 */
3392 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3393 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3394 &BTRFS_I(inode)->runtime_flags);
3395
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003396 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003397 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003398 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003399 rdev = btrfs_inode_rdev(leaf, inode_item);
3400
Josef Bacikaec74772008-07-24 12:12:38 -04003401 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003402 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003403cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003404 /*
3405 * try to precache a NULL acl entry for files that don't have
3406 * any xattrs or acls
3407 */
Li Zefan33345d012011-04-20 10:31:50 +08003408 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3409 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003410 if (!maybe_acls)
3411 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003412
Chris Mason39279cc2007-06-12 06:35:45 -04003413 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003414
Chris Mason39279cc2007-06-12 06:35:45 -04003415 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003416 case S_IFREG:
3417 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003418 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003419 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003420 inode->i_fop = &btrfs_file_operations;
3421 inode->i_op = &btrfs_file_inode_operations;
3422 break;
3423 case S_IFDIR:
3424 inode->i_fop = &btrfs_dir_file_operations;
3425 if (root == root->fs_info->tree_root)
3426 inode->i_op = &btrfs_dir_ro_inode_operations;
3427 else
3428 inode->i_op = &btrfs_dir_inode_operations;
3429 break;
3430 case S_IFLNK:
3431 inode->i_op = &btrfs_symlink_inode_operations;
3432 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003433 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003434 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003435 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003436 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003437 init_special_inode(inode, inode->i_mode, rdev);
3438 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003439 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003440
3441 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003442 return;
3443
3444make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003445 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003446 make_bad_inode(inode);
3447}
3448
Chris Masond352ac62008-09-29 15:18:18 -04003449/*
3450 * given a leaf and an inode, copy the inode fields into the leaf
3451 */
Chris Masone02119d2008-09-05 16:13:11 -04003452static void fill_inode_item(struct btrfs_trans_handle *trans,
3453 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003454 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003455 struct inode *inode)
3456{
Liu Bo51fab692012-12-27 09:01:21 +00003457 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003458
Liu Bo51fab692012-12-27 09:01:21 +00003459 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003460
Liu Bo51fab692012-12-27 09:01:21 +00003461 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3462 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3463 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3464 &token);
3465 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3466 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003467
Liu Bo51fab692012-12-27 09:01:21 +00003468 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3469 inode->i_atime.tv_sec, &token);
3470 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3471 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003472
Liu Bo51fab692012-12-27 09:01:21 +00003473 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3474 inode->i_mtime.tv_sec, &token);
3475 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3476 inode->i_mtime.tv_nsec, &token);
3477
3478 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3479 inode->i_ctime.tv_sec, &token);
3480 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3481 inode->i_ctime.tv_nsec, &token);
3482
3483 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3484 &token);
3485 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3486 &token);
3487 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3488 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3489 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3490 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3491 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003492}
3493
Chris Masond352ac62008-09-29 15:18:18 -04003494/*
3495 * copy everything in the in-memory inode into the btree.
3496 */
Chris Mason21151332011-11-10 20:39:08 -05003497static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003498 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003499{
3500 struct btrfs_inode_item *inode_item;
3501 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003502 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003503 int ret;
3504
3505 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003506 if (!path)
3507 return -ENOMEM;
3508
Chris Masonb9473432009-03-13 11:00:37 -04003509 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003510 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3511 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003512 if (ret) {
3513 if (ret > 0)
3514 ret = -ENOENT;
3515 goto failed;
3516 }
3517
Chris Masonb4ce94d2009-02-04 09:25:08 -05003518 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003519 leaf = path->nodes[0];
3520 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003521 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003522
Chris Masone02119d2008-09-05 16:13:11 -04003523 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003524 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003525 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003526 ret = 0;
3527failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003528 btrfs_free_path(path);
3529 return ret;
3530}
3531
Chris Masond352ac62008-09-29 15:18:18 -04003532/*
Chris Mason21151332011-11-10 20:39:08 -05003533 * copy everything in the in-memory inode into the btree.
3534 */
3535noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3536 struct btrfs_root *root, struct inode *inode)
3537{
3538 int ret;
3539
3540 /*
3541 * If the inode is a free space inode, we can deadlock during commit
3542 * if we put it into the delayed code.
3543 *
3544 * The data relocation inode should also be directly updated
3545 * without delay
3546 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003547 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003548 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003549 btrfs_update_root_times(trans, root);
3550
Chris Mason21151332011-11-10 20:39:08 -05003551 ret = btrfs_delayed_update_inode(trans, root, inode);
3552 if (!ret)
3553 btrfs_set_inode_last_trans(trans, inode);
3554 return ret;
3555 }
3556
3557 return btrfs_update_inode_item(trans, root, inode);
3558}
3559
Josef Bacikbe6aef62012-10-22 15:43:12 -04003560noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3561 struct btrfs_root *root,
3562 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003563{
3564 int ret;
3565
3566 ret = btrfs_update_inode(trans, root, inode);
3567 if (ret == -ENOSPC)
3568 return btrfs_update_inode_item(trans, root, inode);
3569 return ret;
3570}
3571
3572/*
Chris Masond352ac62008-09-29 15:18:18 -04003573 * unlink helper that gets used here in inode.c and in the tree logging
3574 * recovery code. It remove a link in a directory with a given name, and
3575 * also drops the back refs in the inode to the directory
3576 */
Al Viro92986792011-03-04 17:14:37 +00003577static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3578 struct btrfs_root *root,
3579 struct inode *dir, struct inode *inode,
3580 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003581{
3582 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003583 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003584 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003585 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003586 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003587 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003588 u64 ino = btrfs_ino(inode);
3589 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003590
3591 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003592 if (!path) {
3593 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003594 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003595 }
3596
Chris Masonb9473432009-03-13 11:00:37 -04003597 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003598 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003599 name, name_len, -1);
3600 if (IS_ERR(di)) {
3601 ret = PTR_ERR(di);
3602 goto err;
3603 }
3604 if (!di) {
3605 ret = -ENOENT;
3606 goto err;
3607 }
Chris Mason5f39d392007-10-15 16:14:19 -04003608 leaf = path->nodes[0];
3609 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003610 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003611 if (ret)
3612 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003613 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003614
Li Zefan33345d012011-04-20 10:31:50 +08003615 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3616 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003617 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003618 btrfs_info(root->fs_info,
3619 "failed to delete reference to %.*s, inode %llu parent %llu",
3620 name_len, name,
3621 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003622 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003623 goto err;
3624 }
3625
Miao Xie16cdcec2011-04-22 18:12:22 +08003626 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003627 if (ret) {
3628 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003629 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003630 }
Chris Mason39279cc2007-06-12 06:35:45 -04003631
Chris Masone02119d2008-09-05 16:13:11 -04003632 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003633 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003634 if (ret != 0 && ret != -ENOENT) {
3635 btrfs_abort_transaction(trans, root, ret);
3636 goto err;
3637 }
Chris Masone02119d2008-09-05 16:13:11 -04003638
3639 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3640 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003641 if (ret == -ENOENT)
3642 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003643 else if (ret)
3644 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003645err:
3646 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003647 if (ret)
3648 goto out;
3649
3650 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003651 inode_inc_iversion(inode);
3652 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003653 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003654 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003655out:
Chris Mason39279cc2007-06-12 06:35:45 -04003656 return ret;
3657}
3658
Al Viro92986792011-03-04 17:14:37 +00003659int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3660 struct btrfs_root *root,
3661 struct inode *dir, struct inode *inode,
3662 const char *name, int name_len)
3663{
3664 int ret;
3665 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3666 if (!ret) {
3667 btrfs_drop_nlink(inode);
3668 ret = btrfs_update_inode(trans, root, inode);
3669 }
3670 return ret;
3671}
3672
3673
Yan, Zhenga22285a2010-05-16 10:48:46 -04003674/* helper to check if there is any shared block in the path */
3675static int check_path_shared(struct btrfs_root *root,
3676 struct btrfs_path *path)
3677{
3678 struct extent_buffer *eb;
3679 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00003680 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003681
3682 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003683 int ret;
3684
Yan, Zhenga22285a2010-05-16 10:48:46 -04003685 if (!path->nodes[level])
3686 break;
3687 eb = path->nodes[level];
3688 if (!btrfs_block_can_be_shared(root, eb))
3689 continue;
Josef Bacik3173a182013-03-07 14:22:04 -05003690 ret = btrfs_lookup_extent_info(NULL, root, eb->start, level, 1,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003691 &refs, NULL);
3692 if (refs > 1)
3693 return 1;
3694 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003695 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003696}
3697
3698/*
3699 * helper to start transaction for unlink and rmdir.
3700 *
3701 * unlink and rmdir are special in btrfs, they do not always free space.
3702 * so in enospc case, we should make sure they will free space before
3703 * allowing them to use the global metadata reservation.
3704 */
3705static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3706 struct dentry *dentry)
3707{
3708 struct btrfs_trans_handle *trans;
3709 struct btrfs_root *root = BTRFS_I(dir)->root;
3710 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003711 struct btrfs_dir_item *di;
3712 struct inode *inode = dentry->d_inode;
3713 u64 index;
3714 int check_link = 1;
3715 int err = -ENOSPC;
3716 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003717 u64 ino = btrfs_ino(inode);
3718 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003719
Josef Bacike70bea52011-10-11 14:18:24 -04003720 /*
3721 * 1 for the possible orphan item
3722 * 1 for the dir item
3723 * 1 for the dir index
3724 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003725 * 1 for the inode
3726 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003727 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003728 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3729 return trans;
3730
Li Zefan33345d012011-04-20 10:31:50 +08003731 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003732 return ERR_PTR(-ENOSPC);
3733
3734 /* check if there is someone else holds reference */
3735 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3736 return ERR_PTR(-ENOSPC);
3737
3738 if (atomic_read(&inode->i_count) > 2)
3739 return ERR_PTR(-ENOSPC);
3740
3741 if (xchg(&root->fs_info->enospc_unlink, 1))
3742 return ERR_PTR(-ENOSPC);
3743
3744 path = btrfs_alloc_path();
3745 if (!path) {
3746 root->fs_info->enospc_unlink = 0;
3747 return ERR_PTR(-ENOMEM);
3748 }
3749
Josef Bacik3880a1b2011-10-14 14:46:51 -04003750 /* 1 for the orphan item */
3751 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003752 if (IS_ERR(trans)) {
3753 btrfs_free_path(path);
3754 root->fs_info->enospc_unlink = 0;
3755 return trans;
3756 }
3757
3758 path->skip_locking = 1;
3759 path->search_commit_root = 1;
3760
3761 ret = btrfs_lookup_inode(trans, root, path,
3762 &BTRFS_I(dir)->location, 0);
3763 if (ret < 0) {
3764 err = ret;
3765 goto out;
3766 }
3767 if (ret == 0) {
3768 if (check_path_shared(root, path))
3769 goto out;
3770 } else {
3771 check_link = 0;
3772 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003773 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003774
3775 ret = btrfs_lookup_inode(trans, root, path,
3776 &BTRFS_I(inode)->location, 0);
3777 if (ret < 0) {
3778 err = ret;
3779 goto out;
3780 }
3781 if (ret == 0) {
3782 if (check_path_shared(root, path))
3783 goto out;
3784 } else {
3785 check_link = 0;
3786 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003787 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003788
3789 if (ret == 0 && S_ISREG(inode->i_mode)) {
3790 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003791 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003792 if (ret < 0) {
3793 err = ret;
3794 goto out;
3795 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003796 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003797 if (check_path_shared(root, path))
3798 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003799 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003800 }
3801
3802 if (!check_link) {
3803 err = 0;
3804 goto out;
3805 }
3806
Li Zefan33345d012011-04-20 10:31:50 +08003807 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003808 dentry->d_name.name, dentry->d_name.len, 0);
3809 if (IS_ERR(di)) {
3810 err = PTR_ERR(di);
3811 goto out;
3812 }
3813 if (di) {
3814 if (check_path_shared(root, path))
3815 goto out;
3816 } else {
3817 err = 0;
3818 goto out;
3819 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003820 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003821
Mark Fashehf1863732012-08-08 11:32:27 -07003822 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3823 dentry->d_name.len, ino, dir_ino, 0,
3824 &index);
3825 if (ret) {
3826 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003827 goto out;
3828 }
Mark Fashehf1863732012-08-08 11:32:27 -07003829
Yan, Zhenga22285a2010-05-16 10:48:46 -04003830 if (check_path_shared(root, path))
3831 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003832
David Sterbab3b4aa72011-04-21 01:20:15 +02003833 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003834
Miao Xie16cdcec2011-04-22 18:12:22 +08003835 /*
3836 * This is a commit root search, if we can lookup inode item and other
3837 * relative items in the commit root, it means the transaction of
3838 * dir/file creation has been committed, and the dir index item that we
3839 * delay to insert has also been inserted into the commit root. So
3840 * we needn't worry about the delayed insertion of the dir index item
3841 * here.
3842 */
Li Zefan33345d012011-04-20 10:31:50 +08003843 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003844 dentry->d_name.name, dentry->d_name.len, 0);
3845 if (IS_ERR(di)) {
3846 err = PTR_ERR(di);
3847 goto out;
3848 }
3849 BUG_ON(ret == -ENOENT);
3850 if (check_path_shared(root, path))
3851 goto out;
3852
3853 err = 0;
3854out:
3855 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003856 /* Migrate the orphan reservation over */
3857 if (!err)
3858 err = btrfs_block_rsv_migrate(trans->block_rsv,
3859 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003860 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003861
Yan, Zhenga22285a2010-05-16 10:48:46 -04003862 if (err) {
3863 btrfs_end_transaction(trans, root);
3864 root->fs_info->enospc_unlink = 0;
3865 return ERR_PTR(err);
3866 }
3867
3868 trans->block_rsv = &root->fs_info->global_block_rsv;
3869 return trans;
3870}
3871
3872static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3873 struct btrfs_root *root)
3874{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003875 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003876 btrfs_block_rsv_release(root, trans->block_rsv,
3877 trans->bytes_reserved);
3878 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003879 BUG_ON(!root->fs_info->enospc_unlink);
3880 root->fs_info->enospc_unlink = 0;
3881 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003882 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003883}
3884
Chris Mason39279cc2007-06-12 06:35:45 -04003885static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3886{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003887 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003888 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003889 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003890 int ret;
3891
Yan, Zhenga22285a2010-05-16 10:48:46 -04003892 trans = __unlink_start_trans(dir, dentry);
3893 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003894 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003895
Chris Mason12fcfd22009-03-24 10:24:20 -04003896 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3897
Chris Masone02119d2008-09-05 16:13:11 -04003898 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3899 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003900 if (ret)
3901 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003902
Yan, Zhenga22285a2010-05-16 10:48:46 -04003903 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003904 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003905 if (ret)
3906 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003907 }
Josef Bacik7b128762008-07-24 12:17:14 -04003908
Tsutomu Itohb5324022011-07-19 07:27:20 +00003909out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003910 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003911 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003912 return ret;
3913}
3914
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003915int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3916 struct btrfs_root *root,
3917 struct inode *dir, u64 objectid,
3918 const char *name, int name_len)
3919{
3920 struct btrfs_path *path;
3921 struct extent_buffer *leaf;
3922 struct btrfs_dir_item *di;
3923 struct btrfs_key key;
3924 u64 index;
3925 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003926 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003927
3928 path = btrfs_alloc_path();
3929 if (!path)
3930 return -ENOMEM;
3931
Li Zefan33345d012011-04-20 10:31:50 +08003932 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003933 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003934 if (IS_ERR_OR_NULL(di)) {
3935 if (!di)
3936 ret = -ENOENT;
3937 else
3938 ret = PTR_ERR(di);
3939 goto out;
3940 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003941
3942 leaf = path->nodes[0];
3943 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3944 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3945 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003946 if (ret) {
3947 btrfs_abort_transaction(trans, root, ret);
3948 goto out;
3949 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003950 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003951
3952 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3953 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003954 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003955 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003956 if (ret != -ENOENT) {
3957 btrfs_abort_transaction(trans, root, ret);
3958 goto out;
3959 }
Li Zefan33345d012011-04-20 10:31:50 +08003960 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003961 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003962 if (IS_ERR_OR_NULL(di)) {
3963 if (!di)
3964 ret = -ENOENT;
3965 else
3966 ret = PTR_ERR(di);
3967 btrfs_abort_transaction(trans, root, ret);
3968 goto out;
3969 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003970
3971 leaf = path->nodes[0];
3972 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003973 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003974 index = key.offset;
3975 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003976 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003977
Miao Xie16cdcec2011-04-22 18:12:22 +08003978 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003979 if (ret) {
3980 btrfs_abort_transaction(trans, root, ret);
3981 goto out;
3982 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003983
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003984 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003985 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003986 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003987 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003988 if (ret)
3989 btrfs_abort_transaction(trans, root, ret);
3990out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003991 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003992 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003993}
3994
Chris Mason39279cc2007-06-12 06:35:45 -04003995static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3996{
3997 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003998 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003999 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004000 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004001
David Sterbab3ae2442012-09-13 16:04:34 -06004002 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004003 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004004 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4005 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004006
Yan, Zhenga22285a2010-05-16 10:48:46 -04004007 trans = __unlink_start_trans(dir, dentry);
4008 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004009 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004010
Li Zefan33345d012011-04-20 10:31:50 +08004011 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004012 err = btrfs_unlink_subvol(trans, root, dir,
4013 BTRFS_I(inode)->location.objectid,
4014 dentry->d_name.name,
4015 dentry->d_name.len);
4016 goto out;
4017 }
4018
Josef Bacik7b128762008-07-24 12:17:14 -04004019 err = btrfs_orphan_add(trans, inode);
4020 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004021 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004022
Chris Mason39279cc2007-06-12 06:35:45 -04004023 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004024 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4025 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004026 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004027 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004028out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04004029 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004030 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004031
Chris Mason39279cc2007-06-12 06:35:45 -04004032 return err;
4033}
4034
Chris Mason323ac952008-10-01 19:05:46 -04004035/*
Chris Mason39279cc2007-06-12 06:35:45 -04004036 * this can truncate away extent items, csum items and directory items.
4037 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004038 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004039 *
4040 * csum items that cross the new i_size are truncated to the new size
4041 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004042 *
4043 * min_type is the minimum key type to truncate down to. If set to 0, this
4044 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004045 */
Yan, Zheng80825102009-11-12 09:35:36 +00004046int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4047 struct btrfs_root *root,
4048 struct inode *inode,
4049 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004050{
Chris Mason39279cc2007-06-12 06:35:45 -04004051 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004052 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004053 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004054 struct btrfs_key key;
4055 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004056 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004057 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004058 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004059 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004060 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004061 int found_extent;
4062 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004063 int pending_del_nr = 0;
4064 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004065 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004066 int ret;
4067 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004068 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004069
4070 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004071
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004072 path = btrfs_alloc_path();
4073 if (!path)
4074 return -ENOMEM;
4075 path->reada = -1;
4076
Josef Bacik5dc562c2012-08-17 13:14:17 -04004077 /*
4078 * We want to drop from the next block forward in case this new size is
4079 * not block aligned since we will be keeping the last block of the
4080 * extent just the way it is.
4081 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004082 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004083 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4084 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004085
Miao Xie16cdcec2011-04-22 18:12:22 +08004086 /*
4087 * This function is also used to drop the items in the log tree before
4088 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4089 * it is used to drop the loged items. So we shouldn't kill the delayed
4090 * items.
4091 */
4092 if (min_type == 0 && root == BTRFS_I(inode)->root)
4093 btrfs_kill_delayed_inode_items(inode);
4094
Li Zefan33345d012011-04-20 10:31:50 +08004095 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004096 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004097 key.type = (u8)-1;
4098
Chris Mason85e21ba2008-01-29 15:11:36 -05004099search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004100 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004101 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004102 if (ret < 0) {
4103 err = ret;
4104 goto out;
4105 }
Chris Masond3977122009-01-05 21:25:51 -05004106
Chris Mason85e21ba2008-01-29 15:11:36 -05004107 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004108 /* there are no items in the tree for us to truncate, we're
4109 * done
4110 */
Yan, Zheng80825102009-11-12 09:35:36 +00004111 if (path->slots[0] == 0)
4112 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004113 path->slots[0]--;
4114 }
4115
Chris Masond3977122009-01-05 21:25:51 -05004116 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004117 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004118 leaf = path->nodes[0];
4119 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4120 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004121
Li Zefan33345d012011-04-20 10:31:50 +08004122 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004123 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004124
Chris Mason85e21ba2008-01-29 15:11:36 -05004125 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004126 break;
4127
Chris Mason5f39d392007-10-15 16:14:19 -04004128 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004129 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004130 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004131 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004132 extent_type = btrfs_file_extent_type(leaf, fi);
4133 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004134 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004135 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004136 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004137 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004138 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004139 }
Yan008630c2007-11-07 13:31:09 -05004140 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004141 }
Yan, Zheng80825102009-11-12 09:35:36 +00004142 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004143 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004144 } else {
4145 if (item_end < new_size)
4146 break;
4147 if (found_key.offset >= new_size)
4148 del_item = 1;
4149 else
4150 del_item = 0;
4151 }
Chris Mason39279cc2007-06-12 06:35:45 -04004152 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004153 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004154 if (found_type != BTRFS_EXTENT_DATA_KEY)
4155 goto delete;
4156
4157 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004158 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004159 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004160 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004161 u64 orig_num_bytes =
4162 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004163 extent_num_bytes = ALIGN(new_size -
4164 found_key.offset,
4165 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004166 btrfs_set_file_extent_num_bytes(leaf, fi,
4167 extent_num_bytes);
4168 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004169 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004170 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004171 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004172 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004173 } else {
Chris Masondb945352007-10-15 16:15:53 -04004174 extent_num_bytes =
4175 btrfs_file_extent_disk_num_bytes(leaf,
4176 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004177 extent_offset = found_key.offset -
4178 btrfs_file_extent_offset(leaf, fi);
4179
Chris Mason39279cc2007-06-12 06:35:45 -04004180 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004181 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004182 if (extent_start != 0) {
4183 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004184 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004185 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004186 }
4187 }
Chris Mason90692182008-02-08 13:49:28 -05004188 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004189 /*
4190 * we can't truncate inline items that have had
4191 * special encodings
4192 */
4193 if (!del_item &&
4194 btrfs_file_extent_compression(leaf, fi) == 0 &&
4195 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4196 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004197 u32 size = new_size - found_key.offset;
4198
4199 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004200 inode_sub_bytes(inode, item_end + 1 -
4201 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004202 }
4203 size =
4204 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004205 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004206 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004207 inode_sub_bytes(inode, item_end + 1 -
4208 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004209 }
Chris Mason39279cc2007-06-12 06:35:45 -04004210 }
Chris Mason179e29e2007-11-01 11:28:41 -04004211delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004212 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004213 if (!pending_del_nr) {
4214 /* no pending yet, add ourselves */
4215 pending_del_slot = path->slots[0];
4216 pending_del_nr = 1;
4217 } else if (pending_del_nr &&
4218 path->slots[0] + 1 == pending_del_slot) {
4219 /* hop on the pending chunk */
4220 pending_del_nr++;
4221 pending_del_slot = path->slots[0];
4222 } else {
Chris Masond3977122009-01-05 21:25:51 -05004223 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004224 }
Chris Mason39279cc2007-06-12 06:35:45 -04004225 } else {
4226 break;
4227 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004228 if (found_extent && (root->ref_cows ||
4229 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004230 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004231 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004232 extent_num_bytes, 0,
4233 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004234 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004235 BUG_ON(ret);
4236 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004237
Yan, Zheng80825102009-11-12 09:35:36 +00004238 if (found_type == BTRFS_INODE_ITEM_KEY)
4239 break;
4240
4241 if (path->slots[0] == 0 ||
4242 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004243 if (pending_del_nr) {
4244 ret = btrfs_del_items(trans, root, path,
4245 pending_del_slot,
4246 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004247 if (ret) {
4248 btrfs_abort_transaction(trans,
4249 root, ret);
4250 goto error;
4251 }
Yan, Zheng80825102009-11-12 09:35:36 +00004252 pending_del_nr = 0;
4253 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004254 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004255 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004256 } else {
4257 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004258 }
Chris Mason39279cc2007-06-12 06:35:45 -04004259 }
Yan, Zheng80825102009-11-12 09:35:36 +00004260out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004261 if (pending_del_nr) {
4262 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4263 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004264 if (ret)
4265 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004266 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004267error:
Chris Mason39279cc2007-06-12 06:35:45 -04004268 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004269 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004270}
4271
Chris Masona52d9a82007-08-27 16:49:44 -04004272/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004273 * btrfs_truncate_page - read, zero a chunk and write a page
4274 * @inode - inode that we're zeroing
4275 * @from - the offset to start zeroing
4276 * @len - the length to zero, 0 to zero the entire range respective to the
4277 * offset
4278 * @front - zero up to the offset instead of from the offset on
4279 *
4280 * This will find the page for the "from" offset and cow the page and zero the
4281 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004282 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004283int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4284 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004285{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004286 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004287 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004288 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4289 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004290 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004291 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004292 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004293 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4294 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4295 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004296 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004297 int ret = 0;
4298 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004299 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004300
Josef Bacik2aaa6652012-08-29 14:27:18 -04004301 if ((offset & (blocksize - 1)) == 0 &&
4302 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004303 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004304 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004305 if (ret)
4306 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004307
Chris Mason211c17f2008-05-15 09:13:45 -04004308again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004309 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004310 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004311 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004312 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004313 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004314 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004315
4316 page_start = page_offset(page);
4317 page_end = page_start + PAGE_CACHE_SIZE - 1;
4318
Chris Masona52d9a82007-08-27 16:49:44 -04004319 if (!PageUptodate(page)) {
4320 ret = btrfs_readpage(NULL, page);
4321 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004322 if (page->mapping != mapping) {
4323 unlock_page(page);
4324 page_cache_release(page);
4325 goto again;
4326 }
Chris Masona52d9a82007-08-27 16:49:44 -04004327 if (!PageUptodate(page)) {
4328 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004329 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004330 }
4331 }
Chris Mason211c17f2008-05-15 09:13:45 -04004332 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004333
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004334 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004335 set_page_extent_mapped(page);
4336
4337 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4338 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004339 unlock_extent_cached(io_tree, page_start, page_end,
4340 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004341 unlock_page(page);
4342 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004343 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004344 btrfs_put_ordered_extent(ordered);
4345 goto again;
4346 }
4347
Josef Bacik2ac55d42010-02-03 19:33:23 +00004348 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004349 EXTENT_DIRTY | EXTENT_DELALLOC |
4350 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004351 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004352
Josef Bacik2ac55d42010-02-03 19:33:23 +00004353 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4354 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004355 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004356 unlock_extent_cached(io_tree, page_start, page_end,
4357 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004358 goto out_unlock;
4359 }
4360
Chris Masone6dcd2d2008-07-17 12:53:50 -04004361 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004362 if (!len)
4363 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004364 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004365 if (front)
4366 memset(kaddr, 0, offset);
4367 else
4368 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004369 flush_dcache_page(page);
4370 kunmap(page);
4371 }
Chris Mason247e7432008-07-17 12:53:51 -04004372 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004373 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004374 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4375 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004376
Chris Mason89642222008-07-24 09:41:53 -04004377out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004378 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004379 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004380 unlock_page(page);
4381 page_cache_release(page);
4382out:
4383 return ret;
4384}
4385
Josef Bacik695a0d02011-03-04 15:46:53 -05004386/*
4387 * This function puts in dummy file extents for the area we're creating a hole
4388 * for. So if we are truncating this file to a larger size we need to insert
4389 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4390 * the range between oldsize and size
4391 */
Josef Bacika41ad392011-01-31 15:30:16 -05004392int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004393{
4394 struct btrfs_trans_handle *trans;
4395 struct btrfs_root *root = BTRFS_I(inode)->root;
4396 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004397 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004398 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004399 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004400 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4401 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004402 u64 last_byte;
4403 u64 cur_offset;
4404 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004405 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004406
4407 if (size <= hole_start)
4408 return 0;
4409
Yan Zheng9036c102008-10-30 14:19:41 -04004410 while (1) {
4411 struct btrfs_ordered_extent *ordered;
4412 btrfs_wait_ordered_range(inode, hole_start,
4413 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004414 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004415 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004416 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4417 if (!ordered)
4418 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004419 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4420 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004421 btrfs_put_ordered_extent(ordered);
4422 }
4423
Yan Zheng9036c102008-10-30 14:19:41 -04004424 cur_offset = hole_start;
4425 while (1) {
4426 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4427 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004428 if (IS_ERR(em)) {
4429 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004430 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004431 break;
4432 }
Yan Zheng9036c102008-10-30 14:19:41 -04004433 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004434 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004435 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004436 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004437 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004438
Miao Xie36423202011-12-14 20:12:02 -05004439 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004440 if (IS_ERR(trans)) {
4441 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004442 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004443 }
Yan, Zheng80825102009-11-12 09:35:36 +00004444
Josef Bacik5dc562c2012-08-17 13:14:17 -04004445 err = btrfs_drop_extents(trans, root, inode,
4446 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004447 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004448 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004449 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004450 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004451 break;
Miao Xie5b397372011-09-11 10:52:24 -04004452 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004453
Yan Zheng9036c102008-10-30 14:19:41 -04004454 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004455 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004456 0, hole_size, 0, hole_size,
4457 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004458 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004459 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004460 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004461 break;
Miao Xie5b397372011-09-11 10:52:24 -04004462 }
Yan, Zheng80825102009-11-12 09:35:36 +00004463
Josef Bacik5dc562c2012-08-17 13:14:17 -04004464 btrfs_drop_extent_cache(inode, cur_offset,
4465 cur_offset + hole_size - 1, 0);
4466 hole_em = alloc_extent_map();
4467 if (!hole_em) {
4468 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4469 &BTRFS_I(inode)->runtime_flags);
4470 goto next;
4471 }
4472 hole_em->start = cur_offset;
4473 hole_em->len = hole_size;
4474 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004475
Josef Bacik5dc562c2012-08-17 13:14:17 -04004476 hole_em->block_start = EXTENT_MAP_HOLE;
4477 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004478 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004479 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004480 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4481 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4482 hole_em->generation = trans->transid;
4483
4484 while (1) {
4485 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004486 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004487 write_unlock(&em_tree->lock);
4488 if (err != -EEXIST)
4489 break;
4490 btrfs_drop_extent_cache(inode, cur_offset,
4491 cur_offset +
4492 hole_size - 1, 0);
4493 }
4494 free_extent_map(hole_em);
4495next:
Miao Xie36423202011-12-14 20:12:02 -05004496 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004497 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004498 }
4499 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004500 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004501 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004502 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004503 break;
4504 }
4505
Yan, Zhenga22285a2010-05-16 10:48:46 -04004506 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004507 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4508 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004509 return err;
4510}
4511
Eric Sandeen3972f262013-01-12 02:57:22 +00004512static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004513{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004514 struct btrfs_root *root = BTRFS_I(inode)->root;
4515 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004516 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004517 loff_t newsize = attr->ia_size;
4518 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004519 int ret;
4520
Josef Bacika41ad392011-01-31 15:30:16 -05004521 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004522 return 0;
4523
Eric Sandeen3972f262013-01-12 02:57:22 +00004524 /*
4525 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4526 * special case where we need to update the times despite not having
4527 * these flags set. For all other operations the VFS set these flags
4528 * explicitly if it wants a timestamp update.
4529 */
4530 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4531 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4532
Josef Bacika41ad392011-01-31 15:30:16 -05004533 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004534 truncate_pagecache(inode, oldsize, newsize);
4535 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004536 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004537 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004538
Miao Xief4a2f4c2011-12-14 20:12:01 -05004539 trans = btrfs_start_transaction(root, 1);
4540 if (IS_ERR(trans))
4541 return PTR_ERR(trans);
4542
4543 i_size_write(inode, newsize);
4544 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4545 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004546 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004547 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004548
Josef Bacika41ad392011-01-31 15:30:16 -05004549 /*
4550 * We're truncating a file that used to have good data down to
4551 * zero. Make sure it gets into the ordered flush list so that
4552 * any new writes get down to disk quickly.
4553 */
4554 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004555 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4556 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004557
Josef Bacikf3fe8202013-01-07 17:03:21 -05004558 /*
4559 * 1 for the orphan item we're going to add
4560 * 1 for the orphan item deletion.
4561 */
4562 trans = btrfs_start_transaction(root, 2);
4563 if (IS_ERR(trans))
4564 return PTR_ERR(trans);
4565
4566 /*
4567 * We need to do this in case we fail at _any_ point during the
4568 * actual truncate. Once we do the truncate_setsize we could
4569 * invalidate pages which forces any outstanding ordered io to
4570 * be instantly completed which will give us extents that need
4571 * to be truncated. If we fail to get an orphan inode down we
4572 * could have left over extents that were never meant to live,
4573 * so we need to garuntee from this point on that everything
4574 * will be consistent.
4575 */
4576 ret = btrfs_orphan_add(trans, inode);
4577 btrfs_end_transaction(trans, root);
4578 if (ret)
4579 return ret;
4580
Josef Bacika41ad392011-01-31 15:30:16 -05004581 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4582 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004583
4584 /* Disable nonlocked read DIO to avoid the end less truncate */
4585 btrfs_inode_block_unlocked_dio(inode);
4586 inode_dio_wait(inode);
4587 btrfs_inode_resume_unlocked_dio(inode);
4588
Josef Bacika41ad392011-01-31 15:30:16 -05004589 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004590 if (ret && inode->i_nlink)
4591 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004592 }
4593
Josef Bacika41ad392011-01-31 15:30:16 -05004594 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004595}
4596
Chris Mason39279cc2007-06-12 06:35:45 -04004597static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4598{
4599 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004600 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004601 int err;
4602
Li Zefanb83cc962010-12-20 16:04:08 +08004603 if (btrfs_root_readonly(root))
4604 return -EROFS;
4605
Chris Mason39279cc2007-06-12 06:35:45 -04004606 err = inode_change_ok(inode, attr);
4607 if (err)
4608 return err;
4609
Chris Mason5a3f23d2009-03-31 13:27:11 -04004610 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004611 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004612 if (err)
4613 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004614 }
Yan Zheng9036c102008-10-30 14:19:41 -04004615
Christoph Hellwig10257742010-06-04 11:30:02 +02004616 if (attr->ia_valid) {
4617 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004618 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004619 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004620
Josef Bacik22c44fe2011-11-30 10:45:38 -05004621 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004622 err = btrfs_acl_chmod(inode);
4623 }
4624
Chris Mason39279cc2007-06-12 06:35:45 -04004625 return err;
4626}
Chris Mason61295eb2008-01-14 16:24:38 -05004627
Al Virobd555972010-06-07 11:35:40 -04004628void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004629{
4630 struct btrfs_trans_handle *trans;
4631 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004632 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004633 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004634 int ret;
4635
liubo1abe9b82011-03-24 11:18:59 +00004636 trace_btrfs_inode_evict(inode);
4637
Chris Mason39279cc2007-06-12 06:35:45 -04004638 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004639 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004640 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004641 goto no_delete;
4642
Chris Mason39279cc2007-06-12 06:35:45 -04004643 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004644 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004645 goto no_delete;
4646 }
Al Virobd555972010-06-07 11:35:40 -04004647 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004648 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004649
Yan, Zhengc71bf092009-11-12 09:34:40 +00004650 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004651 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004652 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004653 goto no_delete;
4654 }
4655
Yan, Zheng76dda932009-09-21 16:00:26 -04004656 if (inode->i_nlink > 0) {
4657 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4658 goto no_delete;
4659 }
4660
Miao Xie0e8c36a2012-12-19 06:59:51 +00004661 ret = btrfs_commit_inode_delayed_inode(inode);
4662 if (ret) {
4663 btrfs_orphan_del(NULL, inode);
4664 goto no_delete;
4665 }
4666
Miao Xie66d8f3d2012-09-06 04:02:28 -06004667 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004668 if (!rsv) {
4669 btrfs_orphan_del(NULL, inode);
4670 goto no_delete;
4671 }
Josef Bacik4a338542011-08-29 11:01:31 -04004672 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004673 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004674 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004675
Chris Masondbe674a2008-07-17 12:54:05 -04004676 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004677
Josef Bacik4289a662011-08-05 13:22:24 -04004678 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004679 * This is a bit simpler than btrfs_truncate since we've already
4680 * reserved our space for our orphan item in the unlink, so we just
4681 * need to reserve some slack space in case we add bytes and update
4682 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004683 */
Yan, Zheng80825102009-11-12 09:35:36 +00004684 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004685 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4686 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004687
Josef Bacik726c35f2011-09-26 15:46:06 -04004688 /*
4689 * Try and steal from the global reserve since we will
4690 * likely not use this space anyway, we want to try as
4691 * hard as possible to get this to work.
4692 */
4693 if (ret)
4694 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4695
Yan, Zhengd68fc572010-05-16 10:49:58 -04004696 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004697 btrfs_warn(root->fs_info,
4698 "Could not get space for a delete, will truncate on mount %d",
4699 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004700 btrfs_orphan_del(NULL, inode);
4701 btrfs_free_block_rsv(root, rsv);
4702 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004703 }
4704
Miao Xie0e8c36a2012-12-19 06:59:51 +00004705 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004706 if (IS_ERR(trans)) {
4707 btrfs_orphan_del(NULL, inode);
4708 btrfs_free_block_rsv(root, rsv);
4709 goto no_delete;
4710 }
4711
4712 trans->block_rsv = rsv;
4713
Yan, Zhengd68fc572010-05-16 10:49:58 -04004714 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004715 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004716 break;
4717
Miao Xie8407aa42012-09-07 01:43:32 -06004718 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004719 btrfs_end_transaction(trans, root);
4720 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004721 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004722 }
4723
Josef Bacik4289a662011-08-05 13:22:24 -04004724 btrfs_free_block_rsv(root, rsv);
4725
Yan, Zheng80825102009-11-12 09:35:36 +00004726 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004727 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004728 ret = btrfs_orphan_del(trans, inode);
4729 BUG_ON(ret);
4730 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004731
Josef Bacik4289a662011-08-05 13:22:24 -04004732 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004733 if (!(root == root->fs_info->tree_root ||
4734 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004735 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004736
Chris Mason54aa1f42007-06-22 14:16:25 -04004737 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004738 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004739no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004740 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004741 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004742 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004743}
4744
4745/*
4746 * this returns the key found in the dir entry in the location pointer.
4747 * If no dir entries were found, location->objectid is 0.
4748 */
4749static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4750 struct btrfs_key *location)
4751{
4752 const char *name = dentry->d_name.name;
4753 int namelen = dentry->d_name.len;
4754 struct btrfs_dir_item *di;
4755 struct btrfs_path *path;
4756 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004757 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004758
4759 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004760 if (!path)
4761 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004762
Li Zefan33345d012011-04-20 10:31:50 +08004763 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004764 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004765 if (IS_ERR(di))
4766 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004767
David Sterbac7040052011-04-19 18:00:01 +02004768 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004769 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004770
Chris Mason5f39d392007-10-15 16:14:19 -04004771 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004772out:
Chris Mason39279cc2007-06-12 06:35:45 -04004773 btrfs_free_path(path);
4774 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004775out_err:
4776 location->objectid = 0;
4777 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004778}
4779
4780/*
4781 * when we hit a tree root in a directory, the btrfs part of the inode
4782 * needs to be changed to reflect the root directory of the tree root. This
4783 * is kind of like crossing a mount point.
4784 */
4785static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004786 struct inode *dir,
4787 struct dentry *dentry,
4788 struct btrfs_key *location,
4789 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004790{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004791 struct btrfs_path *path;
4792 struct btrfs_root *new_root;
4793 struct btrfs_root_ref *ref;
4794 struct extent_buffer *leaf;
4795 int ret;
4796 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004797
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004798 path = btrfs_alloc_path();
4799 if (!path) {
4800 err = -ENOMEM;
4801 goto out;
4802 }
Chris Mason39279cc2007-06-12 06:35:45 -04004803
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004804 err = -ENOENT;
4805 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4806 BTRFS_I(dir)->root->root_key.objectid,
4807 location->objectid);
4808 if (ret) {
4809 if (ret < 0)
4810 err = ret;
4811 goto out;
4812 }
Chris Mason39279cc2007-06-12 06:35:45 -04004813
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004814 leaf = path->nodes[0];
4815 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004816 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004817 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4818 goto out;
4819
4820 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4821 (unsigned long)(ref + 1),
4822 dentry->d_name.len);
4823 if (ret)
4824 goto out;
4825
David Sterbab3b4aa72011-04-21 01:20:15 +02004826 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004827
4828 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4829 if (IS_ERR(new_root)) {
4830 err = PTR_ERR(new_root);
4831 goto out;
4832 }
4833
4834 if (btrfs_root_refs(&new_root->root_item) == 0) {
4835 err = -ENOENT;
4836 goto out;
4837 }
4838
4839 *sub_root = new_root;
4840 location->objectid = btrfs_root_dirid(&new_root->root_item);
4841 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004842 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004843 err = 0;
4844out:
4845 btrfs_free_path(path);
4846 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004847}
4848
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004849static void inode_tree_add(struct inode *inode)
4850{
4851 struct btrfs_root *root = BTRFS_I(inode)->root;
4852 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004853 struct rb_node **p;
4854 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004855 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004856
Al Viro1d3382cb2010-10-23 15:19:20 -04004857 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004858 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004859again:
4860 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004861 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004862 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004863 while (*p) {
4864 parent = *p;
4865 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4866
Li Zefan33345d012011-04-20 10:31:50 +08004867 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004868 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004869 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004870 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004871 else {
4872 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004873 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004874 rb_erase(parent, &root->inode_tree);
4875 RB_CLEAR_NODE(parent);
4876 spin_unlock(&root->inode_lock);
4877 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004878 }
4879 }
4880 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4881 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4882 spin_unlock(&root->inode_lock);
4883}
4884
4885static void inode_tree_del(struct inode *inode)
4886{
4887 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004888 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004889
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004890 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004891 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004892 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004893 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004894 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004895 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004896 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004897
Josef Bacik0af3d002010-06-21 14:48:16 -04004898 /*
4899 * Free space cache has inodes in the tree root, but the tree root has a
4900 * root_refs of 0, so this could end up dropping the tree root as a
4901 * snapshot, so we need the extra !root->fs_info->tree_root check to
4902 * make sure we don't drop it.
4903 */
4904 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4905 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004906 synchronize_srcu(&root->fs_info->subvol_srcu);
4907 spin_lock(&root->inode_lock);
4908 empty = RB_EMPTY_ROOT(&root->inode_tree);
4909 spin_unlock(&root->inode_lock);
4910 if (empty)
4911 btrfs_add_dead_root(root);
4912 }
4913}
4914
Jeff Mahoney143bede2012-03-01 14:56:26 +01004915void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004916{
4917 struct rb_node *node;
4918 struct rb_node *prev;
4919 struct btrfs_inode *entry;
4920 struct inode *inode;
4921 u64 objectid = 0;
4922
4923 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4924
4925 spin_lock(&root->inode_lock);
4926again:
4927 node = root->inode_tree.rb_node;
4928 prev = NULL;
4929 while (node) {
4930 prev = node;
4931 entry = rb_entry(node, struct btrfs_inode, rb_node);
4932
Li Zefan33345d012011-04-20 10:31:50 +08004933 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004934 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004935 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004936 node = node->rb_right;
4937 else
4938 break;
4939 }
4940 if (!node) {
4941 while (prev) {
4942 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004943 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004944 node = prev;
4945 break;
4946 }
4947 prev = rb_next(prev);
4948 }
4949 }
4950 while (node) {
4951 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004952 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004953 inode = igrab(&entry->vfs_inode);
4954 if (inode) {
4955 spin_unlock(&root->inode_lock);
4956 if (atomic_read(&inode->i_count) > 1)
4957 d_prune_aliases(inode);
4958 /*
Al Viro45321ac2010-06-07 13:43:19 -04004959 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004960 * the inode cache when its usage count
4961 * hits zero.
4962 */
4963 iput(inode);
4964 cond_resched();
4965 spin_lock(&root->inode_lock);
4966 goto again;
4967 }
4968
4969 if (cond_resched_lock(&root->inode_lock))
4970 goto again;
4971
4972 node = rb_next(node);
4973 }
4974 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004975}
4976
Chris Masone02119d2008-09-05 16:13:11 -04004977static int btrfs_init_locked_inode(struct inode *inode, void *p)
4978{
4979 struct btrfs_iget_args *args = p;
4980 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004981 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004982 return 0;
4983}
4984
4985static int btrfs_find_actor(struct inode *inode, void *opaque)
4986{
4987 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004988 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004989 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004990}
4991
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004992static struct inode *btrfs_iget_locked(struct super_block *s,
4993 u64 objectid,
4994 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004995{
4996 struct inode *inode;
4997 struct btrfs_iget_args args;
4998 args.ino = objectid;
4999 args.root = root;
5000
5001 inode = iget5_locked(s, objectid, btrfs_find_actor,
5002 btrfs_init_locked_inode,
5003 (void *)&args);
5004 return inode;
5005}
5006
Balaji Rao1a54ef82008-07-21 02:01:04 +05305007/* Get an inode object given its location and corresponding root.
5008 * Returns in *is_new if the inode was read from disk
5009 */
5010struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005011 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305012{
5013 struct inode *inode;
5014
5015 inode = btrfs_iget_locked(s, location->objectid, root);
5016 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005017 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305018
5019 if (inode->i_state & I_NEW) {
5020 BTRFS_I(inode)->root = root;
5021 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
5022 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005023 if (!is_bad_inode(inode)) {
5024 inode_tree_add(inode);
5025 unlock_new_inode(inode);
5026 if (new)
5027 *new = 1;
5028 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005029 unlock_new_inode(inode);
5030 iput(inode);
5031 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005032 }
5033 }
5034
Balaji Rao1a54ef82008-07-21 02:01:04 +05305035 return inode;
5036}
5037
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005038static struct inode *new_simple_dir(struct super_block *s,
5039 struct btrfs_key *key,
5040 struct btrfs_root *root)
5041{
5042 struct inode *inode = new_inode(s);
5043
5044 if (!inode)
5045 return ERR_PTR(-ENOMEM);
5046
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005047 BTRFS_I(inode)->root = root;
5048 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005049 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005050
5051 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005052 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005053 inode->i_fop = &simple_dir_operations;
5054 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5055 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5056
5057 return inode;
5058}
5059
Chris Mason3de45862008-11-17 21:02:50 -05005060struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005061{
Chris Masond3977122009-01-05 21:25:51 -05005062 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005063 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005064 struct btrfs_root *sub_root = root;
5065 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005066 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005067 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005068
5069 if (dentry->d_name.len > BTRFS_NAME_LEN)
5070 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005071
Jeff Layton39e3c952012-11-28 11:30:53 -05005072 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005073 if (ret < 0)
5074 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005075
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005076 if (location.objectid == 0)
5077 return NULL;
5078
5079 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005080 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005081 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005082 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005083
5084 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5085
Yan, Zheng76dda932009-09-21 16:00:26 -04005086 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005087 ret = fixup_tree_root_location(root, dir, dentry,
5088 &location, &sub_root);
5089 if (ret < 0) {
5090 if (ret != -ENOENT)
5091 inode = ERR_PTR(ret);
5092 else
5093 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5094 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005095 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005096 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005097 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5098
Julia Lawall34d19ba2011-01-24 19:55:19 +00005099 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005100 down_read(&root->fs_info->cleanup_work_sem);
5101 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005102 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005103 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005104 if (ret)
5105 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005106 }
5107
Chris Mason3de45862008-11-17 21:02:50 -05005108 return inode;
5109}
5110
Nick Pigginfe15ce42011-01-07 17:49:23 +11005111static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005112{
5113 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005114 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005115
Li Zefan848cce02012-02-21 17:04:28 +08005116 if (!inode && !IS_ROOT(dentry))
5117 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005118
Li Zefan848cce02012-02-21 17:04:28 +08005119 if (inode) {
5120 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005121 if (btrfs_root_refs(&root->root_item) == 0)
5122 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005123
5124 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5125 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005126 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005127 return 0;
5128}
5129
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005130static void btrfs_dentry_release(struct dentry *dentry)
5131{
5132 if (dentry->d_fsdata)
5133 kfree(dentry->d_fsdata);
5134}
5135
Chris Mason3de45862008-11-17 21:02:50 -05005136static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005137 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005138{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005139 struct dentry *ret;
5140
5141 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005142 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005143}
5144
Miao Xie16cdcec2011-04-22 18:12:22 +08005145unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005146 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5147};
5148
David Woodhousecbdf5a22008-08-06 19:42:33 +01005149static int btrfs_real_readdir(struct file *filp, void *dirent,
5150 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005151{
Al Viro496ad9a2013-01-23 17:07:38 -05005152 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005153 struct btrfs_root *root = BTRFS_I(inode)->root;
5154 struct btrfs_item *item;
5155 struct btrfs_dir_item *di;
5156 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005157 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005158 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005159 struct list_head ins_list;
5160 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005161 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005162 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005163 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005164 unsigned char d_type;
5165 int over = 0;
5166 u32 di_cur;
5167 u32 di_total;
5168 u32 di_len;
5169 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005170 char tmp_name[32];
5171 char *name_ptr;
5172 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005173 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005174
5175 /* FIXME, use a real flag for deciding about the key type */
5176 if (root->fs_info->tree_root == root)
5177 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005178
Chris Mason39544012007-12-12 14:38:19 -05005179 /* special case for "." */
5180 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005181 over = filldir(dirent, ".", 1,
5182 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005183 if (over)
5184 return 0;
5185 filp->f_pos = 1;
5186 }
Chris Mason39544012007-12-12 14:38:19 -05005187 /* special case for .., just use the back ref */
5188 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005189 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005190 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005191 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005192 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005193 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005194 filp->f_pos = 2;
5195 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005196 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005197 if (!path)
5198 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005199
Josef Bacik026fd312011-05-13 10:32:11 -04005200 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005201
Miao Xie16cdcec2011-04-22 18:12:22 +08005202 if (key_type == BTRFS_DIR_INDEX_KEY) {
5203 INIT_LIST_HEAD(&ins_list);
5204 INIT_LIST_HEAD(&del_list);
5205 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5206 }
5207
Chris Mason39279cc2007-06-12 06:35:45 -04005208 btrfs_set_key_type(&key, key_type);
5209 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005210 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005211
Chris Mason39279cc2007-06-12 06:35:45 -04005212 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5213 if (ret < 0)
5214 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005215
5216 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005217 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005218 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005219 if (slot >= btrfs_header_nritems(leaf)) {
5220 ret = btrfs_next_leaf(root, path);
5221 if (ret < 0)
5222 goto err;
5223 else if (ret > 0)
5224 break;
5225 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005226 }
Chris Mason3de45862008-11-17 21:02:50 -05005227
Chris Mason5f39d392007-10-15 16:14:19 -04005228 item = btrfs_item_nr(leaf, slot);
5229 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5230
5231 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005232 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005233 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005234 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005235 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005236 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005237 if (key_type == BTRFS_DIR_INDEX_KEY &&
5238 btrfs_should_delete_dir_index(&del_list,
5239 found_key.offset))
5240 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005241
5242 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005243 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005244
Chris Mason39279cc2007-06-12 06:35:45 -04005245 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5246 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005247 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005248
5249 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005250 struct btrfs_key location;
5251
Josef Bacik22a94d42011-03-16 16:47:17 -04005252 if (verify_dir_item(root, leaf, di))
5253 break;
5254
Chris Mason5f39d392007-10-15 16:14:19 -04005255 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005256 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005257 name_ptr = tmp_name;
5258 } else {
5259 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005260 if (!name_ptr) {
5261 ret = -ENOMEM;
5262 goto err;
5263 }
Chris Mason5f39d392007-10-15 16:14:19 -04005264 }
5265 read_extent_buffer(leaf, name_ptr,
5266 (unsigned long)(di + 1), name_len);
5267
5268 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5269 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005270
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005271
Chris Mason3de45862008-11-17 21:02:50 -05005272 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005273 * skip it.
5274 *
5275 * In contrast to old kernels, we insert the snapshot's
5276 * dir item and dir index after it has been created, so
5277 * we won't find a reference to our own snapshot. We
5278 * still keep the following code for backward
5279 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005280 */
5281 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5282 location.objectid == root->root_key.objectid) {
5283 over = 0;
5284 goto skip;
5285 }
Chris Mason5f39d392007-10-15 16:14:19 -04005286 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005287 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005288 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005289
Chris Mason3de45862008-11-17 21:02:50 -05005290skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005291 if (name_ptr != tmp_name)
5292 kfree(name_ptr);
5293
Chris Mason39279cc2007-06-12 06:35:45 -04005294 if (over)
5295 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005296 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005297 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005298 di_cur += di_len;
5299 di = (struct btrfs_dir_item *)((char *)di + di_len);
5300 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005301next:
5302 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005303 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005304
Miao Xie16cdcec2011-04-22 18:12:22 +08005305 if (key_type == BTRFS_DIR_INDEX_KEY) {
5306 if (is_curr)
5307 filp->f_pos++;
5308 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5309 &ins_list);
5310 if (ret)
5311 goto nopos;
5312 }
5313
David Woodhouse49593bf2008-08-17 17:08:36 +01005314 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005315 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005316 /*
5317 * 32-bit glibc will use getdents64, but then strtol -
5318 * so the last number we can serve is this.
5319 */
5320 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005321 else
5322 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005323nopos:
5324 ret = 0;
5325err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005326 if (key_type == BTRFS_DIR_INDEX_KEY)
5327 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005328 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005329 return ret;
5330}
5331
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005332int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005333{
5334 struct btrfs_root *root = BTRFS_I(inode)->root;
5335 struct btrfs_trans_handle *trans;
5336 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005337 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005338
Josef Bacik72ac3c02012-05-23 14:13:11 -04005339 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005340 return 0;
5341
Liu Bo83eea1f2012-07-10 05:28:39 -06005342 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005343 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005344
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005345 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005346 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005347 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005348 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005349 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005350 if (IS_ERR(trans))
5351 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005352 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005353 }
5354 return ret;
5355}
5356
5357/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005358 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005359 * inode changes. But, it is most likely to find the inode in cache.
5360 * FIXME, needs more benchmarking...there are no reasons other than performance
5361 * to keep or drop this code.
5362 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005363static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005364{
5365 struct btrfs_root *root = BTRFS_I(inode)->root;
5366 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005367 int ret;
5368
Josef Bacik72ac3c02012-05-23 14:13:11 -04005369 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005370 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005371
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005372 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005373 if (IS_ERR(trans))
5374 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005375
5376 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005377 if (ret && ret == -ENOSPC) {
5378 /* whoops, lets try again with the full transaction */
5379 btrfs_end_transaction(trans, root);
5380 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005381 if (IS_ERR(trans))
5382 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005383
Chris Mason94b60442010-05-26 11:02:00 -04005384 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005385 }
Chris Mason39279cc2007-06-12 06:35:45 -04005386 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005387 if (BTRFS_I(inode)->delayed_node)
5388 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005389
5390 return ret;
5391}
5392
5393/*
5394 * This is a copy of file_update_time. We need this so we can return error on
5395 * ENOSPC for updating the inode in the case of file write and mmap writes.
5396 */
Josef Bacike41f9412012-03-26 09:46:47 -04005397static int btrfs_update_time(struct inode *inode, struct timespec *now,
5398 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005399{
Alexander Block2bc5565282012-06-15 09:49:33 +02005400 struct btrfs_root *root = BTRFS_I(inode)->root;
5401
5402 if (btrfs_root_readonly(root))
5403 return -EROFS;
5404
Josef Bacike41f9412012-03-26 09:46:47 -04005405 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005406 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005407 if (flags & S_CTIME)
5408 inode->i_ctime = *now;
5409 if (flags & S_MTIME)
5410 inode->i_mtime = *now;
5411 if (flags & S_ATIME)
5412 inode->i_atime = *now;
5413 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005414}
5415
Chris Masond352ac62008-09-29 15:18:18 -04005416/*
5417 * find the highest existing sequence number in a directory
5418 * and then set the in-memory index_cnt variable to reflect
5419 * free sequence numbers
5420 */
Josef Bacikaec74772008-07-24 12:12:38 -04005421static int btrfs_set_inode_index_count(struct inode *inode)
5422{
5423 struct btrfs_root *root = BTRFS_I(inode)->root;
5424 struct btrfs_key key, found_key;
5425 struct btrfs_path *path;
5426 struct extent_buffer *leaf;
5427 int ret;
5428
Li Zefan33345d012011-04-20 10:31:50 +08005429 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005430 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5431 key.offset = (u64)-1;
5432
5433 path = btrfs_alloc_path();
5434 if (!path)
5435 return -ENOMEM;
5436
5437 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5438 if (ret < 0)
5439 goto out;
5440 /* FIXME: we should be able to handle this */
5441 if (ret == 0)
5442 goto out;
5443 ret = 0;
5444
5445 /*
5446 * MAGIC NUMBER EXPLANATION:
5447 * since we search a directory based on f_pos we have to start at 2
5448 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5449 * else has to start at 2
5450 */
5451 if (path->slots[0] == 0) {
5452 BTRFS_I(inode)->index_cnt = 2;
5453 goto out;
5454 }
5455
5456 path->slots[0]--;
5457
5458 leaf = path->nodes[0];
5459 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5460
Li Zefan33345d012011-04-20 10:31:50 +08005461 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005462 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5463 BTRFS_I(inode)->index_cnt = 2;
5464 goto out;
5465 }
5466
5467 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5468out:
5469 btrfs_free_path(path);
5470 return ret;
5471}
5472
Chris Masond352ac62008-09-29 15:18:18 -04005473/*
5474 * helper to find a free sequence number in a given directory. This current
5475 * code is very simple, later versions will do smarter things in the btree
5476 */
Chris Mason3de45862008-11-17 21:02:50 -05005477int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005478{
5479 int ret = 0;
5480
5481 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005482 ret = btrfs_inode_delayed_dir_index_count(dir);
5483 if (ret) {
5484 ret = btrfs_set_inode_index_count(dir);
5485 if (ret)
5486 return ret;
5487 }
Josef Bacikaec74772008-07-24 12:12:38 -04005488 }
5489
Chris Mason00e4e6b2008-08-05 11:18:09 -04005490 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005491 BTRFS_I(dir)->index_cnt++;
5492
5493 return ret;
5494}
5495
Chris Mason39279cc2007-06-12 06:35:45 -04005496static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5497 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005498 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005499 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005500 u64 ref_objectid, u64 objectid,
5501 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005502{
5503 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005504 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005505 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005506 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005507 struct btrfs_inode_ref *ref;
5508 struct btrfs_key key[2];
5509 u32 sizes[2];
5510 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005511 int ret;
5512 int owner;
5513
Chris Mason5f39d392007-10-15 16:14:19 -04005514 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005515 if (!path)
5516 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005517
Chris Mason39279cc2007-06-12 06:35:45 -04005518 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005519 if (!inode) {
5520 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005521 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005522 }
Chris Mason39279cc2007-06-12 06:35:45 -04005523
Li Zefan581bb052011-04-20 10:06:11 +08005524 /*
5525 * we have to initialize this early, so we can reclaim the inode
5526 * number if we fail afterwards in this function.
5527 */
5528 inode->i_ino = objectid;
5529
Josef Bacikaec74772008-07-24 12:12:38 -04005530 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005531 trace_btrfs_inode_request(dir);
5532
Chris Mason3de45862008-11-17 21:02:50 -05005533 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005534 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005535 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005536 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005537 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005538 }
Josef Bacikaec74772008-07-24 12:12:38 -04005539 }
5540 /*
5541 * index_cnt is ignored for everything but a dir,
5542 * btrfs_get_inode_index_count has an explanation for the magic
5543 * number
5544 */
5545 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005546 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005547 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005548 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005549
Josef Bacik5dc562c2012-08-17 13:14:17 -04005550 /*
5551 * We could have gotten an inode number from somebody who was fsynced
5552 * and then removed in this same transaction, so let's just set full
5553 * sync since it will be a full sync anyway and this will blow away the
5554 * old info in the log.
5555 */
5556 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5557
Al Viro569254b2011-07-24 17:08:40 -04005558 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005559 owner = 0;
5560 else
5561 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005562
5563 key[0].objectid = objectid;
5564 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5565 key[0].offset = 0;
5566
Mark Fashehf1863732012-08-08 11:32:27 -07005567 /*
5568 * Start new inodes with an inode_ref. This is slightly more
5569 * efficient for small numbers of hard links since they will
5570 * be packed into one item. Extended refs will kick in if we
5571 * add more hard links than can fit in the ref item.
5572 */
Chris Mason9c583092008-01-29 15:15:18 -05005573 key[1].objectid = objectid;
5574 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5575 key[1].offset = ref_objectid;
5576
5577 sizes[0] = sizeof(struct btrfs_inode_item);
5578 sizes[1] = name_len + sizeof(*ref);
5579
Chris Masonb9473432009-03-13 11:00:37 -04005580 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005581 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5582 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005583 goto fail;
5584
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005585 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005586 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005587 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005588 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5589 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005590 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5591 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005592 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005593
5594 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5595 struct btrfs_inode_ref);
5596 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005597 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005598 ptr = (unsigned long)(ref + 1);
5599 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5600
Chris Mason5f39d392007-10-15 16:14:19 -04005601 btrfs_mark_buffer_dirty(path->nodes[0]);
5602 btrfs_free_path(path);
5603
Chris Mason39279cc2007-06-12 06:35:45 -04005604 location = &BTRFS_I(inode)->location;
5605 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005606 location->offset = 0;
5607 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5608
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005609 btrfs_inherit_iflags(inode, dir);
5610
Al Viro569254b2011-07-24 17:08:40 -04005611 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005612 if (btrfs_test_opt(root, NODATASUM))
5613 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005614 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005615 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5616 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005617 }
5618
Chris Mason39279cc2007-06-12 06:35:45 -04005619 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005620 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005621
5622 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005623 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005624
Alexander Block8ea05e32012-07-25 17:35:53 +02005625 btrfs_update_root_times(trans, root);
5626
Chris Mason39279cc2007-06-12 06:35:45 -04005627 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005628fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005629 if (dir)
5630 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005631 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005632 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005633 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005634}
5635
5636static inline u8 btrfs_inode_type(struct inode *inode)
5637{
5638 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5639}
5640
Chris Masond352ac62008-09-29 15:18:18 -04005641/*
5642 * utility function to add 'inode' into 'parent_inode' with
5643 * a give name and a given sequence number.
5644 * if 'add_backref' is true, also insert a backref from the
5645 * inode to the parent directory.
5646 */
Chris Masone02119d2008-09-05 16:13:11 -04005647int btrfs_add_link(struct btrfs_trans_handle *trans,
5648 struct inode *parent_inode, struct inode *inode,
5649 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005650{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005651 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005652 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005653 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005654 u64 ino = btrfs_ino(inode);
5655 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005656
Li Zefan33345d012011-04-20 10:31:50 +08005657 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005658 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5659 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005660 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005661 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5662 key.offset = 0;
5663 }
Chris Mason39279cc2007-06-12 06:35:45 -04005664
Li Zefan33345d012011-04-20 10:31:50 +08005665 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005666 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5667 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005668 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005669 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005670 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5671 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005672 }
5673
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005674 /* Nothing to clean up yet */
5675 if (ret)
5676 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005677
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005678 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5679 parent_inode, &key,
5680 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005681 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005682 goto fail_dir_item;
5683 else if (ret) {
5684 btrfs_abort_transaction(trans, root, ret);
5685 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005686 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005687
5688 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5689 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005690 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005691 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5692 ret = btrfs_update_inode(trans, root, parent_inode);
5693 if (ret)
5694 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005695 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005696
5697fail_dir_item:
5698 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5699 u64 local_index;
5700 int err;
5701 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5702 key.objectid, root->root_key.objectid,
5703 parent_ino, &local_index, name, name_len);
5704
5705 } else if (add_backref) {
5706 u64 local_index;
5707 int err;
5708
5709 err = btrfs_del_inode_ref(trans, root, name, name_len,
5710 ino, parent_ino, &local_index);
5711 }
5712 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005713}
5714
5715static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005716 struct inode *dir, struct dentry *dentry,
5717 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005718{
Josef Bacika1b075d2010-11-19 20:36:11 +00005719 int err = btrfs_add_link(trans, dir, inode,
5720 dentry->d_name.name, dentry->d_name.len,
5721 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005722 if (err > 0)
5723 err = -EEXIST;
5724 return err;
5725}
5726
Josef Bacik618e21d2007-07-11 10:18:17 -04005727static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005728 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005729{
5730 struct btrfs_trans_handle *trans;
5731 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005732 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005733 int err;
5734 int drop_inode = 0;
5735 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005736 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005737
5738 if (!new_valid_dev(rdev))
5739 return -EINVAL;
5740
Josef Bacik9ed74f22009-09-11 16:12:44 -04005741 /*
5742 * 2 for inode item and ref
5743 * 2 for dir items
5744 * 1 for xattr if selinux is on
5745 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005746 trans = btrfs_start_transaction(root, 5);
5747 if (IS_ERR(trans))
5748 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005749
Li Zefan581bb052011-04-20 10:06:11 +08005750 err = btrfs_find_free_ino(root, &objectid);
5751 if (err)
5752 goto out_unlock;
5753
Josef Bacikaec74772008-07-24 12:12:38 -04005754 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005755 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005756 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005757 if (IS_ERR(inode)) {
5758 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005759 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005760 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005761
Eric Paris2a7dba32011-02-01 11:05:39 -05005762 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005763 if (err) {
5764 drop_inode = 1;
5765 goto out_unlock;
5766 }
5767
Casey Schauflerad19db72011-12-15 10:09:07 -05005768 /*
5769 * If the active LSM wants to access the inode during
5770 * d_instantiate it needs these. Smack checks to see
5771 * if the filesystem supports xattrs by looking at the
5772 * ops vector.
5773 */
5774
5775 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005776 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005777 if (err)
5778 drop_inode = 1;
5779 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005780 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005781 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005782 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005783 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005784out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005785 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005786 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005787 if (drop_inode) {
5788 inode_dec_link_count(inode);
5789 iput(inode);
5790 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005791 return err;
5792}
5793
Chris Mason39279cc2007-06-12 06:35:45 -04005794static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005795 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005796{
5797 struct btrfs_trans_handle *trans;
5798 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005799 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005800 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005801 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005802 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005803 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005804
Josef Bacik9ed74f22009-09-11 16:12:44 -04005805 /*
5806 * 2 for inode item and ref
5807 * 2 for dir items
5808 * 1 for xattr if selinux is on
5809 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005810 trans = btrfs_start_transaction(root, 5);
5811 if (IS_ERR(trans))
5812 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005813
Li Zefan581bb052011-04-20 10:06:11 +08005814 err = btrfs_find_free_ino(root, &objectid);
5815 if (err)
5816 goto out_unlock;
5817
Josef Bacikaec74772008-07-24 12:12:38 -04005818 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005819 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005820 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005821 if (IS_ERR(inode)) {
5822 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005823 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005824 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005825 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005826
Eric Paris2a7dba32011-02-01 11:05:39 -05005827 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005828 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005829 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005830
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005831 err = btrfs_update_inode(trans, root, inode);
5832 if (err)
5833 goto out_unlock;
5834
Casey Schauflerad19db72011-12-15 10:09:07 -05005835 /*
5836 * If the active LSM wants to access the inode during
5837 * d_instantiate it needs these. Smack checks to see
5838 * if the filesystem supports xattrs by looking at the
5839 * ops vector.
5840 */
5841 inode->i_fop = &btrfs_file_operations;
5842 inode->i_op = &btrfs_file_inode_operations;
5843
Josef Bacika1b075d2010-11-19 20:36:11 +00005844 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005845 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005846 goto out_unlock;
5847
5848 inode->i_mapping->a_ops = &btrfs_aops;
5849 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5850 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5851 d_instantiate(dentry, inode);
5852
Chris Mason39279cc2007-06-12 06:35:45 -04005853out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005854 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005855 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005856 inode_dec_link_count(inode);
5857 iput(inode);
5858 }
Liu Bob53d3f52012-11-14 14:34:34 +00005859 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005860 return err;
5861}
5862
5863static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5864 struct dentry *dentry)
5865{
5866 struct btrfs_trans_handle *trans;
5867 struct btrfs_root *root = BTRFS_I(dir)->root;
5868 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005869 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005870 int err;
5871 int drop_inode = 0;
5872
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005873 /* do not allow sys_link's with other subvols of the same device */
5874 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005875 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005876
Mark Fashehf1863732012-08-08 11:32:27 -07005877 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005878 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005879
Chris Mason3de45862008-11-17 21:02:50 -05005880 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005881 if (err)
5882 goto fail;
5883
Yan, Zhenga22285a2010-05-16 10:48:46 -04005884 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005885 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005886 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005887 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005888 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005889 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005890 if (IS_ERR(trans)) {
5891 err = PTR_ERR(trans);
5892 goto fail;
5893 }
Chris Mason5f39d392007-10-15 16:14:19 -04005894
Miao Xie31534952011-04-13 13:19:21 +08005895 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005896 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005897 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005898 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005899 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005900
Josef Bacika1b075d2010-11-19 20:36:11 +00005901 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005902
Yan, Zhenga5719522009-09-24 09:17:31 -04005903 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005904 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005905 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005906 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005907 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005908 if (err)
5909 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005910 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005911 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005912 }
Chris Mason39279cc2007-06-12 06:35:45 -04005913
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005914 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005915fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005916 if (drop_inode) {
5917 inode_dec_link_count(inode);
5918 iput(inode);
5919 }
Liu Bob53d3f52012-11-14 14:34:34 +00005920 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005921 return err;
5922}
5923
Al Viro18bb1db2011-07-26 01:41:39 -04005924static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005925{
Chris Masonb9d86662008-05-02 16:13:49 -04005926 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005927 struct btrfs_trans_handle *trans;
5928 struct btrfs_root *root = BTRFS_I(dir)->root;
5929 int err = 0;
5930 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005931 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005932 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005933
Josef Bacik9ed74f22009-09-11 16:12:44 -04005934 /*
5935 * 2 items for inode and ref
5936 * 2 items for dir items
5937 * 1 for xattr if selinux is on
5938 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005939 trans = btrfs_start_transaction(root, 5);
5940 if (IS_ERR(trans))
5941 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005942
Li Zefan581bb052011-04-20 10:06:11 +08005943 err = btrfs_find_free_ino(root, &objectid);
5944 if (err)
5945 goto out_fail;
5946
Josef Bacikaec74772008-07-24 12:12:38 -04005947 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005948 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005949 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005950 if (IS_ERR(inode)) {
5951 err = PTR_ERR(inode);
5952 goto out_fail;
5953 }
Chris Mason5f39d392007-10-15 16:14:19 -04005954
Chris Mason39279cc2007-06-12 06:35:45 -04005955 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005956
Eric Paris2a7dba32011-02-01 11:05:39 -05005957 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005958 if (err)
5959 goto out_fail;
5960
Chris Mason39279cc2007-06-12 06:35:45 -04005961 inode->i_op = &btrfs_dir_inode_operations;
5962 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005963
Chris Masondbe674a2008-07-17 12:54:05 -04005964 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005965 err = btrfs_update_inode(trans, root, inode);
5966 if (err)
5967 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005968
Josef Bacika1b075d2010-11-19 20:36:11 +00005969 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5970 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005971 if (err)
5972 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005973
Chris Mason39279cc2007-06-12 06:35:45 -04005974 d_instantiate(dentry, inode);
5975 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005976
5977out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005978 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005979 if (drop_on_err)
5980 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005981 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005982 return err;
5983}
5984
Chris Masond352ac62008-09-29 15:18:18 -04005985/* helper for btfs_get_extent. Given an existing extent in the tree,
5986 * and an extent that you want to insert, deal with overlap and insert
5987 * the new extent into the tree.
5988 */
Chris Mason3b951512008-04-17 11:29:12 -04005989static int merge_extent_mapping(struct extent_map_tree *em_tree,
5990 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005991 struct extent_map *em,
5992 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005993{
5994 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005995
Chris Masone6dcd2d2008-07-17 12:53:50 -04005996 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5997 start_diff = map_start - em->start;
5998 em->start = map_start;
5999 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006000 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6001 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006002 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006003 em->block_len -= start_diff;
6004 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006005 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006006}
6007
Chris Masonc8b97812008-10-29 14:49:59 -04006008static noinline int uncompress_inline(struct btrfs_path *path,
6009 struct inode *inode, struct page *page,
6010 size_t pg_offset, u64 extent_offset,
6011 struct btrfs_file_extent_item *item)
6012{
6013 int ret;
6014 struct extent_buffer *leaf = path->nodes[0];
6015 char *tmp;
6016 size_t max_size;
6017 unsigned long inline_size;
6018 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006019 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006020
6021 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006022 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006023 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6024 inline_size = btrfs_file_extent_inline_item_len(leaf,
6025 btrfs_item_nr(leaf, path->slots[0]));
6026 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006027 if (!tmp)
6028 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006029 ptr = btrfs_file_extent_inline_start(item);
6030
6031 read_extent_buffer(leaf, tmp, ptr, inline_size);
6032
Chris Mason5b050f02008-11-11 09:34:41 -05006033 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006034 ret = btrfs_decompress(compress_type, tmp, page,
6035 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006036 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006037 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006038 unsigned long copy_size = min_t(u64,
6039 PAGE_CACHE_SIZE - pg_offset,
6040 max_size - extent_offset);
6041 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006042 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006043 }
6044 kfree(tmp);
6045 return 0;
6046}
6047
Chris Masond352ac62008-09-29 15:18:18 -04006048/*
6049 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006050 * the ugly parts come from merging extents from the disk with the in-ram
6051 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006052 * where the in-ram extents might be locked pending data=ordered completion.
6053 *
6054 * This also copies inline extents directly into the page.
6055 */
Chris Masond3977122009-01-05 21:25:51 -05006056
Chris Masona52d9a82007-08-27 16:49:44 -04006057struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006058 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006059 int create)
6060{
6061 int ret;
6062 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006063 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006064 u64 extent_start = 0;
6065 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006066 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006067 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006068 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006069 struct btrfs_root *root = BTRFS_I(inode)->root;
6070 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006071 struct extent_buffer *leaf;
6072 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006073 struct extent_map *em = NULL;
6074 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006075 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006076 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006077 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006078
Chris Masona52d9a82007-08-27 16:49:44 -04006079again:
Chris Mason890871b2009-09-02 16:24:52 -04006080 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006081 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006082 if (em)
6083 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006084 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006085
Chris Masona52d9a82007-08-27 16:49:44 -04006086 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006087 if (em->start > start || em->start + em->len <= start)
6088 free_extent_map(em);
6089 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006090 free_extent_map(em);
6091 else
6092 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006093 }
David Sterba172ddd62011-04-21 00:48:27 +02006094 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006095 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006096 err = -ENOMEM;
6097 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006098 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006099 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006100 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006101 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006102 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006103 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006104
6105 if (!path) {
6106 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006107 if (!path) {
6108 err = -ENOMEM;
6109 goto out;
6110 }
6111 /*
6112 * Chances are we'll be called again, so go ahead and do
6113 * readahead
6114 */
6115 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006116 }
6117
Chris Mason179e29e2007-11-01 11:28:41 -04006118 ret = btrfs_lookup_file_extent(trans, root, path,
6119 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006120 if (ret < 0) {
6121 err = ret;
6122 goto out;
6123 }
6124
6125 if (ret != 0) {
6126 if (path->slots[0] == 0)
6127 goto not_found;
6128 path->slots[0]--;
6129 }
6130
Chris Mason5f39d392007-10-15 16:14:19 -04006131 leaf = path->nodes[0];
6132 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006133 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006134 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006135 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6136 found_type = btrfs_key_type(&found_key);
6137 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006138 found_type != BTRFS_EXTENT_DATA_KEY) {
6139 goto not_found;
6140 }
6141
Chris Mason5f39d392007-10-15 16:14:19 -04006142 found_type = btrfs_file_extent_type(leaf, item);
6143 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006144 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006145 if (found_type == BTRFS_FILE_EXTENT_REG ||
6146 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006147 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006148 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006149 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6150 size_t size;
6151 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006152 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006153 }
6154
6155 if (start >= extent_end) {
6156 path->slots[0]++;
6157 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6158 ret = btrfs_next_leaf(root, path);
6159 if (ret < 0) {
6160 err = ret;
6161 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006162 }
Yan Zheng9036c102008-10-30 14:19:41 -04006163 if (ret > 0)
6164 goto not_found;
6165 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006166 }
Yan Zheng9036c102008-10-30 14:19:41 -04006167 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6168 if (found_key.objectid != objectid ||
6169 found_key.type != BTRFS_EXTENT_DATA_KEY)
6170 goto not_found;
6171 if (start + len <= found_key.offset)
6172 goto not_found;
6173 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006174 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006175 em->len = found_key.offset - start;
6176 goto not_found_em;
6177 }
6178
Josef Bacikcc95bef2013-04-04 14:31:27 -04006179 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006180 if (found_type == BTRFS_FILE_EXTENT_REG ||
6181 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006182 em->start = extent_start;
6183 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006184 em->orig_start = extent_start -
6185 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006186 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6187 item);
Chris Masondb945352007-10-15 16:15:53 -04006188 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6189 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006190 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006191 goto insert;
6192 }
Li Zefan261507a02010-12-17 14:21:50 +08006193 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006194 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006195 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006196 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006197 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006198 } else {
6199 bytenr += btrfs_file_extent_offset(leaf, item);
6200 em->block_start = bytenr;
6201 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006202 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6203 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006204 }
Chris Masona52d9a82007-08-27 16:49:44 -04006205 goto insert;
6206 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006207 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006208 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006209 size_t size;
6210 size_t extent_offset;
6211 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006212
Chris Masona52d9a82007-08-27 16:49:44 -04006213 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006214 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006215 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006216 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006217 goto out;
6218 }
6219
Yan Zheng9036c102008-10-30 14:19:41 -04006220 size = btrfs_file_extent_inline_len(leaf, item);
6221 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006222 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006223 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006224 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006225 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006226 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006227 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006228 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006229 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006230 em->compress_type = compress_type;
6231 }
Yan689f9342007-10-29 11:41:07 -04006232 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006233 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006234 if (btrfs_file_extent_compression(leaf, item) !=
6235 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006236 ret = uncompress_inline(path, inode, page,
6237 pg_offset,
6238 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006239 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006240 } else {
6241 map = kmap(page);
6242 read_extent_buffer(leaf, map + pg_offset, ptr,
6243 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006244 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6245 memset(map + pg_offset + copy_size, 0,
6246 PAGE_CACHE_SIZE - pg_offset -
6247 copy_size);
6248 }
Chris Masonc8b97812008-10-29 14:49:59 -04006249 kunmap(page);
6250 }
Chris Mason179e29e2007-11-01 11:28:41 -04006251 flush_dcache_page(page);
6252 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006253 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006254 if (!trans) {
6255 kunmap(page);
6256 free_extent_map(em);
6257 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006258
David Sterbab3b4aa72011-04-21 01:20:15 +02006259 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006260 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006261
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006262 if (IS_ERR(trans))
6263 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006264 goto again;
6265 }
Chris Masonc8b97812008-10-29 14:49:59 -04006266 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006267 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006268 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006269 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006270 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006271 }
Chris Masond1310b22008-01-24 16:13:08 -05006272 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006273 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006274 goto insert;
6275 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006276 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006277 }
6278not_found:
6279 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006280 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006281 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006282not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006283 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006284 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006285insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006286 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006287 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006288 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6289 (unsigned long long)em->start,
6290 (unsigned long long)em->len,
6291 (unsigned long long)start,
6292 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006293 err = -EIO;
6294 goto out;
6295 }
Chris Masond1310b22008-01-24 16:13:08 -05006296
6297 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006298 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006299 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006300 /* it is possible that someone inserted the extent into the tree
6301 * while we had the lock dropped. It is also possible that
6302 * an overlapping map exists in the tree
6303 */
Chris Masona52d9a82007-08-27 16:49:44 -04006304 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006305 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006306
6307 ret = 0;
6308
Chris Mason3b951512008-04-17 11:29:12 -04006309 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006310 if (existing && (existing->start > start ||
6311 existing->start + existing->len <= start)) {
6312 free_extent_map(existing);
6313 existing = NULL;
6314 }
Chris Mason3b951512008-04-17 11:29:12 -04006315 if (!existing) {
6316 existing = lookup_extent_mapping(em_tree, em->start,
6317 em->len);
6318 if (existing) {
6319 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006320 em, start,
6321 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006322 free_extent_map(existing);
6323 if (err) {
6324 free_extent_map(em);
6325 em = NULL;
6326 }
6327 } else {
6328 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006329 free_extent_map(em);
6330 em = NULL;
6331 }
6332 } else {
6333 free_extent_map(em);
6334 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006335 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006336 }
Chris Masona52d9a82007-08-27 16:49:44 -04006337 }
Chris Mason890871b2009-09-02 16:24:52 -04006338 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006339out:
liubo1abe9b82011-03-24 11:18:59 +00006340
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006341 if (em)
6342 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006343
Chris Masonf4219502008-07-22 11:18:09 -04006344 if (path)
6345 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006346 if (trans) {
6347 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006348 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006349 err = ret;
6350 }
Chris Masona52d9a82007-08-27 16:49:44 -04006351 if (err) {
6352 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006353 return ERR_PTR(err);
6354 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006355 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006356 return em;
6357}
6358
Chris Masonec29ed52011-02-23 16:23:20 -05006359struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6360 size_t pg_offset, u64 start, u64 len,
6361 int create)
6362{
6363 struct extent_map *em;
6364 struct extent_map *hole_em = NULL;
6365 u64 range_start = start;
6366 u64 end;
6367 u64 found;
6368 u64 found_end;
6369 int err = 0;
6370
6371 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6372 if (IS_ERR(em))
6373 return em;
6374 if (em) {
6375 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006376 * if our em maps to
6377 * - a hole or
6378 * - a pre-alloc extent,
6379 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006380 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006381 if (em->block_start != EXTENT_MAP_HOLE &&
6382 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006383 return em;
6384 else
6385 hole_em = em;
6386 }
6387
6388 /* check to see if we've wrapped (len == -1 or similar) */
6389 end = start + len;
6390 if (end < start)
6391 end = (u64)-1;
6392 else
6393 end -= 1;
6394
6395 em = NULL;
6396
6397 /* ok, we didn't find anything, lets look for delalloc */
6398 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6399 end, len, EXTENT_DELALLOC, 1);
6400 found_end = range_start + found;
6401 if (found_end < range_start)
6402 found_end = (u64)-1;
6403
6404 /*
6405 * we didn't find anything useful, return
6406 * the original results from get_extent()
6407 */
6408 if (range_start > end || found_end <= start) {
6409 em = hole_em;
6410 hole_em = NULL;
6411 goto out;
6412 }
6413
6414 /* adjust the range_start to make sure it doesn't
6415 * go backwards from the start they passed in
6416 */
6417 range_start = max(start,range_start);
6418 found = found_end - range_start;
6419
6420 if (found > 0) {
6421 u64 hole_start = start;
6422 u64 hole_len = len;
6423
David Sterba172ddd62011-04-21 00:48:27 +02006424 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006425 if (!em) {
6426 err = -ENOMEM;
6427 goto out;
6428 }
6429 /*
6430 * when btrfs_get_extent can't find anything it
6431 * returns one huge hole
6432 *
6433 * make sure what it found really fits our range, and
6434 * adjust to make sure it is based on the start from
6435 * the caller
6436 */
6437 if (hole_em) {
6438 u64 calc_end = extent_map_end(hole_em);
6439
6440 if (calc_end <= start || (hole_em->start > end)) {
6441 free_extent_map(hole_em);
6442 hole_em = NULL;
6443 } else {
6444 hole_start = max(hole_em->start, start);
6445 hole_len = calc_end - hole_start;
6446 }
6447 }
6448 em->bdev = NULL;
6449 if (hole_em && range_start > hole_start) {
6450 /* our hole starts before our delalloc, so we
6451 * have to return just the parts of the hole
6452 * that go until the delalloc starts
6453 */
6454 em->len = min(hole_len,
6455 range_start - hole_start);
6456 em->start = hole_start;
6457 em->orig_start = hole_start;
6458 /*
6459 * don't adjust block start at all,
6460 * it is fixed at EXTENT_MAP_HOLE
6461 */
6462 em->block_start = hole_em->block_start;
6463 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006464 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6465 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006466 } else {
6467 em->start = range_start;
6468 em->len = found;
6469 em->orig_start = range_start;
6470 em->block_start = EXTENT_MAP_DELALLOC;
6471 em->block_len = found;
6472 }
6473 } else if (hole_em) {
6474 return hole_em;
6475 }
6476out:
6477
6478 free_extent_map(hole_em);
6479 if (err) {
6480 free_extent_map(em);
6481 return ERR_PTR(err);
6482 }
6483 return em;
6484}
6485
Josef Bacik4b46fce2010-05-23 11:00:55 -04006486static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6487 u64 start, u64 len)
6488{
6489 struct btrfs_root *root = BTRFS_I(inode)->root;
6490 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006491 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006492 struct btrfs_key ins;
6493 u64 alloc_hint;
6494 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006495
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006496 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006497 if (IS_ERR(trans))
6498 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006499
6500 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6501
6502 alloc_hint = get_extent_allocation_hint(inode, start, len);
6503 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006504 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006505 if (ret) {
6506 em = ERR_PTR(ret);
6507 goto out;
6508 }
6509
Josef Bacik70c8a912012-10-11 16:54:30 -04006510 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006511 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006512 if (IS_ERR(em))
6513 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006514
6515 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6516 ins.offset, ins.offset, 0);
6517 if (ret) {
6518 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6519 em = ERR_PTR(ret);
6520 }
6521out:
6522 btrfs_end_transaction(trans, root);
6523 return em;
6524}
6525
Chris Mason46bfbb52010-05-26 11:04:10 -04006526/*
6527 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6528 * block must be cow'd
6529 */
6530static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
Josef Bacikeb384b52013-04-24 16:32:55 -04006531 struct inode *inode, u64 offset, u64 *len,
6532 u64 *orig_start, u64 *orig_block_len,
6533 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006534{
6535 struct btrfs_path *path;
6536 int ret;
6537 struct extent_buffer *leaf;
6538 struct btrfs_root *root = BTRFS_I(inode)->root;
6539 struct btrfs_file_extent_item *fi;
6540 struct btrfs_key key;
6541 u64 disk_bytenr;
6542 u64 backref_offset;
6543 u64 extent_end;
6544 u64 num_bytes;
6545 int slot;
6546 int found_type;
6547
6548 path = btrfs_alloc_path();
6549 if (!path)
6550 return -ENOMEM;
6551
Li Zefan33345d012011-04-20 10:31:50 +08006552 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006553 offset, 0);
6554 if (ret < 0)
6555 goto out;
6556
6557 slot = path->slots[0];
6558 if (ret == 1) {
6559 if (slot == 0) {
6560 /* can't find the item, must cow */
6561 ret = 0;
6562 goto out;
6563 }
6564 slot--;
6565 }
6566 ret = 0;
6567 leaf = path->nodes[0];
6568 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006569 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006570 key.type != BTRFS_EXTENT_DATA_KEY) {
6571 /* not our file or wrong item type, must cow */
6572 goto out;
6573 }
6574
6575 if (key.offset > offset) {
6576 /* Wrong offset, must cow */
6577 goto out;
6578 }
6579
6580 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6581 found_type = btrfs_file_extent_type(leaf, fi);
6582 if (found_type != BTRFS_FILE_EXTENT_REG &&
6583 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6584 /* not a regular extent, must cow */
6585 goto out;
6586 }
6587 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6588 backref_offset = btrfs_file_extent_offset(leaf, fi);
6589
Josef Bacikeb384b52013-04-24 16:32:55 -04006590 *orig_start = key.offset - backref_offset;
6591 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6592 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6593
Chris Mason46bfbb52010-05-26 11:04:10 -04006594 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikeb384b52013-04-24 16:32:55 -04006595 if (extent_end < offset + *len) {
Chris Mason46bfbb52010-05-26 11:04:10 -04006596 /* extent doesn't include our full range, must cow */
6597 goto out;
6598 }
6599
6600 if (btrfs_extent_readonly(root, disk_bytenr))
6601 goto out;
6602
6603 /*
6604 * look for other files referencing this extent, if we
6605 * find any we must cow
6606 */
Li Zefan33345d012011-04-20 10:31:50 +08006607 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006608 key.offset - backref_offset, disk_bytenr))
6609 goto out;
6610
6611 /*
6612 * adjust disk_bytenr and num_bytes to cover just the bytes
6613 * in this extent we are about to write. If there
6614 * are any csums in that range we have to cow in order
6615 * to keep the csums correct
6616 */
6617 disk_bytenr += backref_offset;
6618 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006619 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006620 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6621 goto out;
6622 /*
6623 * all of the above have passed, it is safe to overwrite this extent
6624 * without cow
6625 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006626 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006627 ret = 1;
6628out:
6629 btrfs_free_path(path);
6630 return ret;
6631}
6632
Josef Bacikeb838e72012-07-31 16:28:48 -04006633static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6634 struct extent_state **cached_state, int writing)
6635{
6636 struct btrfs_ordered_extent *ordered;
6637 int ret = 0;
6638
6639 while (1) {
6640 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6641 0, cached_state);
6642 /*
6643 * We're concerned with the entire range that we're going to be
6644 * doing DIO to, so we need to make sure theres no ordered
6645 * extents in this range.
6646 */
6647 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6648 lockend - lockstart + 1);
6649
6650 /*
6651 * We need to make sure there are no buffered pages in this
6652 * range either, we could have raced between the invalidate in
6653 * generic_file_direct_write and locking the extent. The
6654 * invalidate needs to happen so that reads after a write do not
6655 * get stale data.
6656 */
6657 if (!ordered && (!writing ||
6658 !test_range_bit(&BTRFS_I(inode)->io_tree,
6659 lockstart, lockend, EXTENT_UPTODATE, 0,
6660 *cached_state)))
6661 break;
6662
6663 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6664 cached_state, GFP_NOFS);
6665
6666 if (ordered) {
6667 btrfs_start_ordered_extent(inode, ordered, 1);
6668 btrfs_put_ordered_extent(ordered);
6669 } else {
6670 /* Screw you mmap */
6671 ret = filemap_write_and_wait_range(inode->i_mapping,
6672 lockstart,
6673 lockend);
6674 if (ret)
6675 break;
6676
6677 /*
6678 * If we found a page that couldn't be invalidated just
6679 * fall back to buffered.
6680 */
6681 ret = invalidate_inode_pages2_range(inode->i_mapping,
6682 lockstart >> PAGE_CACHE_SHIFT,
6683 lockend >> PAGE_CACHE_SHIFT);
6684 if (ret)
6685 break;
6686 }
6687
6688 cond_resched();
6689 }
6690
6691 return ret;
6692}
6693
Josef Bacik69ffb542012-09-11 15:40:07 -04006694static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6695 u64 len, u64 orig_start,
6696 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006697 u64 orig_block_len, u64 ram_bytes,
6698 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006699{
6700 struct extent_map_tree *em_tree;
6701 struct extent_map *em;
6702 struct btrfs_root *root = BTRFS_I(inode)->root;
6703 int ret;
6704
6705 em_tree = &BTRFS_I(inode)->extent_tree;
6706 em = alloc_extent_map();
6707 if (!em)
6708 return ERR_PTR(-ENOMEM);
6709
6710 em->start = start;
6711 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006712 em->mod_start = start;
6713 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006714 em->len = len;
6715 em->block_len = block_len;
6716 em->block_start = block_start;
6717 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006718 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006719 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006720 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006721 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6722 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006723 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006724
6725 do {
6726 btrfs_drop_extent_cache(inode, em->start,
6727 em->start + em->len - 1, 0);
6728 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006729 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006730 write_unlock(&em_tree->lock);
6731 } while (ret == -EEXIST);
6732
6733 if (ret) {
6734 free_extent_map(em);
6735 return ERR_PTR(ret);
6736 }
6737
6738 return em;
6739}
6740
6741
Josef Bacik4b46fce2010-05-23 11:00:55 -04006742static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6743 struct buffer_head *bh_result, int create)
6744{
6745 struct extent_map *em;
6746 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006747 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006748 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006749 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006750 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006751 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006752 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006753 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006754
Miao Xie172a5042013-02-21 02:48:22 -07006755 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006756 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006757 else
Josef Bacikc3298612012-08-03 16:49:19 -04006758 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006759
Josef Bacikc3298612012-08-03 16:49:19 -04006760 lockstart = start;
6761 lockend = start + len - 1;
6762
Josef Bacikeb838e72012-07-31 16:28:48 -04006763 /*
6764 * If this errors out it's because we couldn't invalidate pagecache for
6765 * this range and we need to fallback to buffered.
6766 */
6767 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6768 return -ENOTBLK;
6769
Josef Bacik4b46fce2010-05-23 11:00:55 -04006770 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006771 if (IS_ERR(em)) {
6772 ret = PTR_ERR(em);
6773 goto unlock_err;
6774 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006775
6776 /*
6777 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6778 * io. INLINE is special, and we could probably kludge it in here, but
6779 * it's still buffered so for safety lets just fall back to the generic
6780 * buffered path.
6781 *
6782 * For COMPRESSED we _have_ to read the entire extent in so we can
6783 * decompress it, so there will be buffering required no matter what we
6784 * do, so go ahead and fallback to buffered.
6785 *
6786 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6787 * to buffered IO. Don't blame me, this is the price we pay for using
6788 * the generic code.
6789 */
6790 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6791 em->block_start == EXTENT_MAP_INLINE) {
6792 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006793 ret = -ENOTBLK;
6794 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006795 }
6796
6797 /* Just a good old fashioned hole, return */
6798 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6799 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6800 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006801 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006802 }
6803
6804 /*
6805 * We don't allocate a new extent in the following cases
6806 *
6807 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6808 * existing extent.
6809 * 2) The extent is marked as PREALLOC. We're good to go here and can
6810 * just use the extent.
6811 *
6812 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006813 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006814 len = min(len, em->len - (start - em->start));
6815 lockstart = start + len;
6816 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006817 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006818
6819 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6820 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6821 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006822 int type;
6823 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006824 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006825
6826 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6827 type = BTRFS_ORDERED_PREALLOC;
6828 else
6829 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006830 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006831 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006832
6833 /*
6834 * we're not going to log anything, but we do need
6835 * to make sure the current transaction stays open
6836 * while we look for nocow cross refs
6837 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006838 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006839 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006840 goto must_cow;
6841
Josef Bacikeb384b52013-04-24 16:32:55 -04006842 if (can_nocow_odirect(trans, inode, start, &len, &orig_start,
6843 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006844 if (type == BTRFS_ORDERED_PREALLOC) {
6845 free_extent_map(em);
6846 em = create_pinned_em(inode, start, len,
6847 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006848 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006849 orig_block_len,
6850 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006851 if (IS_ERR(em)) {
6852 btrfs_end_transaction(trans, root);
6853 goto unlock_err;
6854 }
6855 }
6856
Chris Mason46bfbb52010-05-26 11:04:10 -04006857 ret = btrfs_add_ordered_extent_dio(inode, start,
6858 block_start, len, len, type);
6859 btrfs_end_transaction(trans, root);
6860 if (ret) {
6861 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006862 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006863 }
6864 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006865 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006866 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006867 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006868must_cow:
6869 /*
6870 * this will cow the extent, reset the len in case we changed
6871 * it above
6872 */
6873 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006874 free_extent_map(em);
6875 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006876 if (IS_ERR(em)) {
6877 ret = PTR_ERR(em);
6878 goto unlock_err;
6879 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006880 len = min(len, em->len - (start - em->start));
6881unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006882 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6883 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006884 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006885 bh_result->b_bdev = em->bdev;
6886 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006887 if (create) {
6888 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6889 set_buffer_new(bh_result);
6890
6891 /*
6892 * Need to update the i_size under the extent lock so buffered
6893 * readers will get the updated i_size when we unlock.
6894 */
6895 if (start + len > i_size_read(inode))
6896 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006897
Miao Xie172a5042013-02-21 02:48:22 -07006898 spin_lock(&BTRFS_I(inode)->lock);
6899 BTRFS_I(inode)->outstanding_extents++;
6900 spin_unlock(&BTRFS_I(inode)->lock);
6901
Miao Xie09348562013-02-07 10:12:07 +00006902 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6903 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6904 &cached_state, GFP_NOFS);
6905 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006906 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006907
Josef Bacikeb838e72012-07-31 16:28:48 -04006908 /*
6909 * In the case of write we need to clear and unlock the entire range,
6910 * in the case of read we need to unlock only the end area that we
6911 * aren't using if there is any left over space.
6912 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006913 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006914 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6915 lockend, unlock_bits, 1, 0,
6916 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006917 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006918 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006919 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006920
Josef Bacik4b46fce2010-05-23 11:00:55 -04006921 free_extent_map(em);
6922
6923 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006924
6925unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006926 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6927 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6928 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006929}
6930
6931struct btrfs_dio_private {
6932 struct inode *inode;
6933 u64 logical_offset;
6934 u64 disk_bytenr;
6935 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006936 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006937
6938 /* number of bios pending for this dio */
6939 atomic_t pending_bios;
6940
6941 /* IO errors */
6942 int errors;
6943
Chris Mason9be33952013-05-17 18:30:14 -04006944 /* orig_bio is our btrfs_io_bio */
Miao Xiee65e1532010-11-22 03:04:43 +00006945 struct bio *orig_bio;
Chris Mason9be33952013-05-17 18:30:14 -04006946
6947 /* dio_bio came from fs/direct-io.c */
6948 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006949};
6950
6951static void btrfs_endio_direct_read(struct bio *bio, int err)
6952{
Miao Xiee65e1532010-11-22 03:04:43 +00006953 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006954 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6955 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006956 struct inode *inode = dip->inode;
6957 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006958 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006959 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006960
6961 start = dip->logical_offset;
6962 do {
6963 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6964 struct page *page = bvec->bv_page;
6965 char *kaddr;
6966 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006967 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006968 unsigned long flags;
6969
Josef Bacikc3298612012-08-03 16:49:19 -04006970 if (get_state_private(&BTRFS_I(inode)->io_tree,
6971 start, &private))
6972 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006973 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006974 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006975 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006976 csum, bvec->bv_len);
6977 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006978 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006979 local_irq_restore(flags);
6980
6981 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006982 if (csum != private) {
6983failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006984 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6985 (unsigned long long)btrfs_ino(inode),
6986 (unsigned long long)start,
6987 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006988 err = -EIO;
6989 }
6990 }
6991
6992 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006993 bvec++;
6994 } while (bvec <= bvec_end);
6995
6996 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006997 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006998 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006999
Josef Bacik4b46fce2010-05-23 11:00:55 -04007000 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007001
7002 /* If we had a csum failure make sure to clear the uptodate flag */
7003 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007004 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7005 dio_end_io(dio_bio, err);
7006 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007007}
7008
7009static void btrfs_endio_direct_write(struct bio *bio, int err)
7010{
7011 struct btrfs_dio_private *dip = bio->bi_private;
7012 struct inode *inode = dip->inode;
7013 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007014 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007015 u64 ordered_offset = dip->logical_offset;
7016 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007017 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007018 int ret;
7019
7020 if (err)
7021 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007022again:
7023 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7024 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007025 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007026 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007027 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007028
Josef Bacik5fd02042012-05-02 14:00:54 -04007029 ordered->work.func = finish_ordered_fn;
7030 ordered->work.flags = 0;
7031 btrfs_queue_worker(&root->fs_info->endio_write_workers,
7032 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007033out_test:
7034 /*
7035 * our bio might span multiple ordered extents. If we haven't
7036 * completed the accounting for the whole dio, go back and try again
7037 */
7038 if (ordered_offset < dip->logical_offset + dip->bytes) {
7039 ordered_bytes = dip->logical_offset + dip->bytes -
7040 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007041 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007042 goto again;
7043 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007044out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007045 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007046
Josef Bacik4b46fce2010-05-23 11:00:55 -04007047 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007048
7049 /* If we had an error make sure to clear the uptodate flag */
7050 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007051 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7052 dio_end_io(dio_bio, err);
7053 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007054}
7055
Chris Masoneaf25d92010-05-25 09:48:28 -04007056static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7057 struct bio *bio, int mirror_num,
7058 unsigned long bio_flags, u64 offset)
7059{
7060 int ret;
7061 struct btrfs_root *root = BTRFS_I(inode)->root;
7062 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007063 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007064 return 0;
7065}
7066
Miao Xiee65e1532010-11-22 03:04:43 +00007067static void btrfs_end_dio_bio(struct bio *bio, int err)
7068{
7069 struct btrfs_dio_private *dip = bio->bi_private;
7070
7071 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007072 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007073 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007074 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007075 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007076 dip->errors = 1;
7077
7078 /*
7079 * before atomic variable goto zero, we must make sure
7080 * dip->errors is perceived to be set.
7081 */
7082 smp_mb__before_atomic_dec();
7083 }
7084
7085 /* if there are more bios still pending for this dio, just exit */
7086 if (!atomic_dec_and_test(&dip->pending_bios))
7087 goto out;
7088
Chris Mason9be33952013-05-17 18:30:14 -04007089 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007090 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007091 } else {
7092 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007093 bio_endio(dip->orig_bio, 0);
7094 }
7095out:
7096 bio_put(bio);
7097}
7098
7099static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7100 u64 first_sector, gfp_t gfp_flags)
7101{
7102 int nr_vecs = bio_get_nr_vecs(bdev);
7103 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7104}
7105
7106static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7107 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007108 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007109{
7110 int write = rw & REQ_WRITE;
7111 struct btrfs_root *root = BTRFS_I(inode)->root;
7112 int ret;
7113
Josef Bacikb812ce22012-11-16 13:56:32 -05007114 if (async_submit)
7115 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7116
Miao Xiee65e1532010-11-22 03:04:43 +00007117 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007118
7119 if (!write) {
7120 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7121 if (ret)
7122 goto err;
7123 }
Miao Xiee65e1532010-11-22 03:04:43 +00007124
Josef Bacik1ae39932011-04-06 14:41:34 -04007125 if (skip_sum)
7126 goto map;
7127
7128 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007129 ret = btrfs_wq_submit_bio(root->fs_info,
7130 inode, rw, bio, 0, 0,
7131 file_offset,
7132 __btrfs_submit_bio_start_direct_io,
7133 __btrfs_submit_bio_done);
7134 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007135 } else if (write) {
7136 /*
7137 * If we aren't doing async submit, calculate the csum of the
7138 * bio now.
7139 */
7140 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7141 if (ret)
7142 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007143 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007144 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007145 if (ret)
7146 goto err;
7147 }
Miao Xiee65e1532010-11-22 03:04:43 +00007148
Josef Bacik1ae39932011-04-06 14:41:34 -04007149map:
7150 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007151err:
7152 bio_put(bio);
7153 return ret;
7154}
7155
7156static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7157 int skip_sum)
7158{
7159 struct inode *inode = dip->inode;
7160 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007161 struct bio *bio;
7162 struct bio *orig_bio = dip->orig_bio;
7163 struct bio_vec *bvec = orig_bio->bi_io_vec;
7164 u64 start_sector = orig_bio->bi_sector;
7165 u64 file_offset = dip->logical_offset;
7166 u64 submit_len = 0;
7167 u64 map_length;
7168 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007169 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007170 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007171
Miao Xiee65e1532010-11-22 03:04:43 +00007172 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007173 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007174 &map_length, NULL, 0);
7175 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007176 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007177 return -EIO;
7178 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007179 if (map_length >= orig_bio->bi_size) {
7180 bio = orig_bio;
7181 goto submit;
7182 }
7183
David Woodhouse53b381b2013-01-29 18:40:14 -05007184 /* async crcs make it difficult to collect full stripe writes. */
7185 if (btrfs_get_alloc_profile(root, 1) &
7186 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7187 async_submit = 0;
7188 else
7189 async_submit = 1;
7190
Josef Bacik02f57c72011-04-06 14:25:44 -04007191 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7192 if (!bio)
7193 return -ENOMEM;
7194 bio->bi_private = dip;
7195 bio->bi_end_io = btrfs_end_dio_bio;
7196 atomic_inc(&dip->pending_bios);
7197
Miao Xiee65e1532010-11-22 03:04:43 +00007198 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7199 if (unlikely(map_length < submit_len + bvec->bv_len ||
7200 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7201 bvec->bv_offset) < bvec->bv_len)) {
7202 /*
7203 * inc the count before we submit the bio so
7204 * we know the end IO handler won't happen before
7205 * we inc the count. Otherwise, the dip might get freed
7206 * before we're done setting it up
7207 */
7208 atomic_inc(&dip->pending_bios);
7209 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7210 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007211 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007212 if (ret) {
7213 bio_put(bio);
7214 atomic_dec(&dip->pending_bios);
7215 goto out_err;
7216 }
7217
Miao Xiee65e1532010-11-22 03:04:43 +00007218 start_sector += submit_len >> 9;
7219 file_offset += submit_len;
7220
7221 submit_len = 0;
7222 nr_pages = 0;
7223
7224 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7225 start_sector, GFP_NOFS);
7226 if (!bio)
7227 goto out_err;
7228 bio->bi_private = dip;
7229 bio->bi_end_io = btrfs_end_dio_bio;
7230
7231 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007232 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007233 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007234 &map_length, NULL, 0);
7235 if (ret) {
7236 bio_put(bio);
7237 goto out_err;
7238 }
7239 } else {
7240 submit_len += bvec->bv_len;
7241 nr_pages ++;
7242 bvec++;
7243 }
7244 }
7245
Josef Bacik02f57c72011-04-06 14:25:44 -04007246submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007247 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007248 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007249 if (!ret)
7250 return 0;
7251
7252 bio_put(bio);
7253out_err:
7254 dip->errors = 1;
7255 /*
7256 * before atomic variable goto zero, we must
7257 * make sure dip->errors is perceived to be set.
7258 */
7259 smp_mb__before_atomic_dec();
7260 if (atomic_dec_and_test(&dip->pending_bios))
7261 bio_io_error(dip->orig_bio);
7262
7263 /* bio_end_io() will handle error, so we needn't return it */
7264 return 0;
7265}
7266
Chris Mason9be33952013-05-17 18:30:14 -04007267static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7268 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007269{
7270 struct btrfs_root *root = BTRFS_I(inode)->root;
7271 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007272 struct bio_vec *bvec = dio_bio->bi_io_vec;
7273 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007274 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007275 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007276 int ret = 0;
7277
7278 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7279
Chris Mason9be33952013-05-17 18:30:14 -04007280 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7281
7282 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283 ret = -ENOMEM;
7284 goto free_ordered;
7285 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007286
Chris Mason9be33952013-05-17 18:30:14 -04007287 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7288 if (!dip) {
7289 ret = -ENOMEM;
7290 goto free_io_bio;
7291 }
7292
7293 dip->private = dio_bio->bi_private;
7294 io_bio->bi_private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007295 dip->inode = inode;
7296 dip->logical_offset = file_offset;
7297
Josef Bacik4b46fce2010-05-23 11:00:55 -04007298 dip->bytes = 0;
7299 do {
7300 dip->bytes += bvec->bv_len;
7301 bvec++;
Chris Mason9be33952013-05-17 18:30:14 -04007302 } while (bvec <= (dio_bio->bi_io_vec + dio_bio->bi_vcnt - 1));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007303
Chris Mason9be33952013-05-17 18:30:14 -04007304 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7305 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007306 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007307 dip->orig_bio = io_bio;
7308 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007309 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007310
7311 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007312 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007313 else
Chris Mason9be33952013-05-17 18:30:14 -04007314 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007315
Miao Xiee65e1532010-11-22 03:04:43 +00007316 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7317 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007318 return;
Chris Mason9be33952013-05-17 18:30:14 -04007319
7320free_io_bio:
7321 bio_put(io_bio);
7322
Josef Bacik4b46fce2010-05-23 11:00:55 -04007323free_ordered:
7324 /*
7325 * If this is a write, we need to clean up the reserved space and kill
7326 * the ordered extent.
7327 */
7328 if (write) {
7329 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007330 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007331 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7332 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7333 btrfs_free_reserved_extent(root, ordered->start,
7334 ordered->disk_len);
7335 btrfs_put_ordered_extent(ordered);
7336 btrfs_put_ordered_extent(ordered);
7337 }
Chris Mason9be33952013-05-17 18:30:14 -04007338 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007339}
7340
Chris Mason5a5f79b2010-05-26 21:33:37 -04007341static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7342 const struct iovec *iov, loff_t offset,
7343 unsigned long nr_segs)
7344{
7345 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007346 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007347 size_t size;
7348 unsigned long addr;
7349 unsigned blocksize_mask = root->sectorsize - 1;
7350 ssize_t retval = -EINVAL;
7351 loff_t end = offset;
7352
7353 if (offset & blocksize_mask)
7354 goto out;
7355
7356 /* Check the memory alignment. Blocks cannot straddle pages */
7357 for (seg = 0; seg < nr_segs; seg++) {
7358 addr = (unsigned long)iov[seg].iov_base;
7359 size = iov[seg].iov_len;
7360 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007361 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007362 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007363
7364 /* If this is a write we don't need to check anymore */
7365 if (rw & WRITE)
7366 continue;
7367
7368 /*
7369 * Check to make sure we don't have duplicate iov_base's in this
7370 * iovec, if so return EINVAL, otherwise we'll get csum errors
7371 * when reading back.
7372 */
7373 for (i = seg + 1; i < nr_segs; i++) {
7374 if (iov[seg].iov_base == iov[i].iov_base)
7375 goto out;
7376 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007377 }
7378 retval = 0;
7379out:
7380 return retval;
7381}
Josef Bacikeb838e72012-07-31 16:28:48 -04007382
Chris Mason16432982008-04-10 10:23:21 -04007383static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7384 const struct iovec *iov, loff_t offset,
7385 unsigned long nr_segs)
7386{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007387 struct file *file = iocb->ki_filp;
7388 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007389 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007390 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007391 bool wakeup = true;
7392 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007393 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007394
Chris Mason5a5f79b2010-05-26 21:33:37 -04007395 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007396 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007397 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007398
Miao Xie38851cc2013-02-08 07:04:11 +00007399 atomic_inc(&inode->i_dio_count);
7400 smp_mb__after_atomic_inc();
7401
Miao Xie09348562013-02-07 10:12:07 +00007402 if (rw & WRITE) {
7403 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007404 /*
7405 * If the write DIO is beyond the EOF, we need update
7406 * the isize, but it is protected by i_mutex. So we can
7407 * not unlock the i_mutex at this case.
7408 */
7409 if (offset + count <= inode->i_size) {
7410 mutex_unlock(&inode->i_mutex);
7411 relock = true;
7412 }
Miao Xie09348562013-02-07 10:12:07 +00007413 ret = btrfs_delalloc_reserve_space(inode, count);
7414 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007415 goto out;
7416 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7417 &BTRFS_I(inode)->runtime_flags))) {
7418 inode_dio_done(inode);
7419 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7420 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007421 }
7422
7423 ret = __blockdev_direct_IO(rw, iocb, inode,
7424 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7425 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007426 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007427 if (rw & WRITE) {
7428 if (ret < 0 && ret != -EIOCBQUEUED)
7429 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007430 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007431 btrfs_delalloc_release_space(inode,
7432 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007433 else
7434 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007435 }
Miao Xie38851cc2013-02-08 07:04:11 +00007436out:
Miao Xie2e60a512013-02-08 07:01:08 +00007437 if (wakeup)
7438 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007439 if (relock)
7440 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007441
7442 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007443}
7444
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007445#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7446
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007447static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7448 __u64 start, __u64 len)
7449{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007450 int ret;
7451
7452 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7453 if (ret)
7454 return ret;
7455
Chris Masonec29ed52011-02-23 16:23:20 -05007456 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007457}
7458
Chris Mason9ebefb182007-06-15 13:50:00 -04007459int btrfs_readpage(struct file *file, struct page *page)
7460{
Chris Masond1310b22008-01-24 16:13:08 -05007461 struct extent_io_tree *tree;
7462 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007463 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007464}
Chris Mason1832a6d2007-12-21 16:27:21 -05007465
Chris Mason39279cc2007-06-12 06:35:45 -04007466static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7467{
Chris Masond1310b22008-01-24 16:13:08 -05007468 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007469
7470
7471 if (current->flags & PF_MEMALLOC) {
7472 redirty_page_for_writepage(wbc, page);
7473 unlock_page(page);
7474 return 0;
7475 }
Chris Masond1310b22008-01-24 16:13:08 -05007476 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007477 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7478}
Chris Mason39279cc2007-06-12 06:35:45 -04007479
Eric Sandeen48a3b632013-04-25 20:41:01 +00007480static int btrfs_writepages(struct address_space *mapping,
7481 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007482{
Chris Masond1310b22008-01-24 16:13:08 -05007483 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007484
Chris Masond1310b22008-01-24 16:13:08 -05007485 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007486 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7487}
7488
Chris Mason3ab2fb52007-11-08 10:59:22 -05007489static int
7490btrfs_readpages(struct file *file, struct address_space *mapping,
7491 struct list_head *pages, unsigned nr_pages)
7492{
Chris Masond1310b22008-01-24 16:13:08 -05007493 struct extent_io_tree *tree;
7494 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007495 return extent_readpages(tree, mapping, pages, nr_pages,
7496 btrfs_get_extent);
7497}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007498static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007499{
Chris Masond1310b22008-01-24 16:13:08 -05007500 struct extent_io_tree *tree;
7501 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007502 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007503
Chris Masond1310b22008-01-24 16:13:08 -05007504 tree = &BTRFS_I(page->mapping->host)->io_tree;
7505 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007506 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007507 if (ret == 1) {
7508 ClearPagePrivate(page);
7509 set_page_private(page, 0);
7510 page_cache_release(page);
7511 }
7512 return ret;
7513}
Chris Mason39279cc2007-06-12 06:35:45 -04007514
Chris Masone6dcd2d2008-07-17 12:53:50 -04007515static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7516{
Chris Mason98509cfc2008-09-11 15:51:43 -04007517 if (PageWriteback(page) || PageDirty(page))
7518 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007519 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007520}
7521
Chris Masona52d9a82007-08-27 16:49:44 -04007522static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7523{
Josef Bacik5fd02042012-05-02 14:00:54 -04007524 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007525 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007526 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007527 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007528 u64 page_start = page_offset(page);
7529 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007530
Chris Mason8b62b722009-09-02 16:53:46 -04007531 /*
7532 * we have the page locked, so new writeback can't start,
7533 * and the dirty bit won't be cleared while we are here.
7534 *
7535 * Wait for IO on this page so that we can safely clear
7536 * the PagePrivate2 bit and do ordered accounting
7537 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007538 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007539
Josef Bacik5fd02042012-05-02 14:00:54 -04007540 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007541 if (offset) {
7542 btrfs_releasepage(page, GFP_NOFS);
7543 return;
7544 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007545 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007546 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007547 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007548 /*
7549 * IO on this page will never be started, so we need
7550 * to account for any ordered extents now
7551 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007552 clear_extent_bit(tree, page_start, page_end,
7553 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007554 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7555 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007556 /*
7557 * whoever cleared the private bit is responsible
7558 * for the finish_ordered_io
7559 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007560 if (TestClearPagePrivate2(page) &&
7561 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7562 PAGE_CACHE_SIZE, 1)) {
7563 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007564 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007565 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007566 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007567 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007568 }
7569 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007570 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007571 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7572 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007573 __btrfs_releasepage(page, GFP_NOFS);
7574
Chris Mason4a096752008-07-21 10:29:44 -04007575 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007576 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007577 ClearPagePrivate(page);
7578 set_page_private(page, 0);
7579 page_cache_release(page);
7580 }
Chris Mason39279cc2007-06-12 06:35:45 -04007581}
7582
Chris Mason9ebefb182007-06-15 13:50:00 -04007583/*
7584 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7585 * called from a page fault handler when a page is first dirtied. Hence we must
7586 * be careful to check for EOF conditions here. We set the page up correctly
7587 * for a written page which means we get ENOSPC checking when writing into
7588 * holes and correct delalloc and unwritten extent mapping on filesystems that
7589 * support these features.
7590 *
7591 * We are not allowed to take the i_mutex here so we have to play games to
7592 * protect against truncate races as the page could now be beyond EOF. Because
7593 * vmtruncate() writes the inode size before removing pages, once we have the
7594 * page lock we can determine safely if the page is beyond EOF. If it is not
7595 * beyond EOF, then the page is guaranteed safe against truncation until we
7596 * unlock the page.
7597 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007598int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007599{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007600 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007601 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007602 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007603 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7604 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007605 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007606 char *kaddr;
7607 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007608 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007609 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007610 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007611 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007612 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007613
Jan Karab2b5ef52012-06-12 16:20:45 +02007614 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007615 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007616 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007617 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007618 reserved = 1;
7619 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007620 if (ret) {
7621 if (ret == -ENOMEM)
7622 ret = VM_FAULT_OOM;
7623 else /* -ENOSPC, -EIO, etc */
7624 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007625 if (reserved)
7626 goto out;
7627 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007628 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007629
Nick Piggin56a76f82009-03-31 15:23:23 -07007630 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007631again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007632 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007633 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007634 page_start = page_offset(page);
7635 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007636
Chris Mason9ebefb182007-06-15 13:50:00 -04007637 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007638 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007639 /* page got truncated out from underneath us */
7640 goto out_unlock;
7641 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007642 wait_on_page_writeback(page);
7643
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007644 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007645 set_page_extent_mapped(page);
7646
Chris Masoneb84ae02008-07-17 13:53:27 -04007647 /*
7648 * we can't set the delalloc bits if there are pending ordered
7649 * extents. Drop our locks and wait for them to finish
7650 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007651 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7652 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007653 unlock_extent_cached(io_tree, page_start, page_end,
7654 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007655 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007656 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007657 btrfs_put_ordered_extent(ordered);
7658 goto again;
7659 }
7660
Josef Bacikfbf19082009-10-01 17:10:23 -04007661 /*
7662 * XXX - page_mkwrite gets called every time the page is dirtied, even
7663 * if it was already dirty, so for space accounting reasons we need to
7664 * clear any delalloc bits for the range we are fixing to save. There
7665 * is probably a better way to do this, but for now keep consistent with
7666 * prepare_pages in the normal write path.
7667 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007668 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007669 EXTENT_DIRTY | EXTENT_DELALLOC |
7670 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007671 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007672
Josef Bacik2ac55d42010-02-03 19:33:23 +00007673 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7674 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007675 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007676 unlock_extent_cached(io_tree, page_start, page_end,
7677 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007678 ret = VM_FAULT_SIGBUS;
7679 goto out_unlock;
7680 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007681 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007682
7683 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007684 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007685 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007686 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007687 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007688
Chris Masone6dcd2d2008-07-17 12:53:50 -04007689 if (zero_start != PAGE_CACHE_SIZE) {
7690 kaddr = kmap(page);
7691 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7692 flush_dcache_page(page);
7693 kunmap(page);
7694 }
Chris Mason247e7432008-07-17 12:53:51 -04007695 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007696 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007697 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007698
Chris Mason257c62e2009-10-13 13:21:08 -04007699 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7700 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007701 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007702
Josef Bacik2ac55d42010-02-03 19:33:23 +00007703 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007704
7705out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007706 if (!ret) {
7707 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007708 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007709 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007710 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007711out:
Josef Bacikec39e182012-01-12 19:10:12 -05007712 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007713out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007714 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007715 return ret;
7716}
7717
Josef Bacika41ad392011-01-31 15:30:16 -05007718static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007719{
7720 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007721 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007722 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007723 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007724 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007725 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007726 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007727
Josef Bacik2aaa6652012-08-29 14:27:18 -04007728 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007729 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007730 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007731
Chris Mason4a096752008-07-21 10:29:44 -04007732 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007733 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007734
Josef Bacikfcb80c22011-05-03 10:40:22 -04007735 /*
7736 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7737 * 3 things going on here
7738 *
7739 * 1) We need to reserve space for our orphan item and the space to
7740 * delete our orphan item. Lord knows we don't want to have a dangling
7741 * orphan item because we didn't reserve space to remove it.
7742 *
7743 * 2) We need to reserve space to update our inode.
7744 *
7745 * 3) We need to have something to cache all the space that is going to
7746 * be free'd up by the truncate operation, but also have some slack
7747 * space reserved in case it uses space during the truncate (thank you
7748 * very much snapshotting).
7749 *
7750 * And we need these to all be seperate. The fact is we can use alot of
7751 * space doing the truncate, and we have no earthly idea how much space
7752 * we will use, so we need the truncate reservation to be seperate so it
7753 * doesn't end up using space reserved for updating the inode or
7754 * removing the orphan item. We also need to be able to stop the
7755 * transaction and start a new one, which means we need to be able to
7756 * update the inode several times, and we have no idea of knowing how
7757 * many times that will be, so we can't just reserve 1 item for the
7758 * entirety of the opration, so that has to be done seperately as well.
7759 * Then there is the orphan item, which does indeed need to be held on
7760 * to for the whole operation, and we need nobody to touch this reserved
7761 * space except the orphan code.
7762 *
7763 * So that leaves us with
7764 *
7765 * 1) root->orphan_block_rsv - for the orphan deletion.
7766 * 2) rsv - for the truncate reservation, which we will steal from the
7767 * transaction reservation.
7768 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7769 * updating the inode.
7770 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007771 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007772 if (!rsv)
7773 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007774 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007775 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007776
Josef Bacik907cbce2011-08-08 13:46:15 -04007777 /*
Josef Bacik07127182011-08-19 10:29:59 -04007778 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007779 * 1 for updating the inode.
7780 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007781 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007782 if (IS_ERR(trans)) {
7783 err = PTR_ERR(trans);
7784 goto out;
7785 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007786
Josef Bacik907cbce2011-08-08 13:46:15 -04007787 /* Migrate the slack space for the truncate to our reserve */
7788 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7789 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007790 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007791
Chris Mason5a3f23d2009-03-31 13:27:11 -04007792 /*
7793 * setattr is responsible for setting the ordered_data_close flag,
7794 * but that is only tested during the last file release. That
7795 * could happen well after the next commit, leaving a great big
7796 * window where new writes may get lost if someone chooses to write
7797 * to this file after truncating to zero
7798 *
7799 * The inode doesn't have any dirty data here, and so if we commit
7800 * this is a noop. If someone immediately starts writing to the inode
7801 * it is very likely we'll catch some of their writes in this
7802 * transaction, and the commit will find this file on the ordered
7803 * data list with good things to send down.
7804 *
7805 * This is a best effort solution, there is still a window where
7806 * using truncate to replace the contents of the file will
7807 * end up with a zero length file after a crash.
7808 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007809 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7810 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007811 btrfs_add_ordered_operation(trans, root, inode);
7812
Josef Bacik5dc562c2012-08-17 13:14:17 -04007813 /*
7814 * So if we truncate and then write and fsync we normally would just
7815 * write the extents that changed, which is a problem if we need to
7816 * first truncate that entire inode. So set this flag so we write out
7817 * all of the extents in the inode to the sync log so we're completely
7818 * safe.
7819 */
7820 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007821 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007822
Yan, Zheng80825102009-11-12 09:35:36 +00007823 while (1) {
7824 ret = btrfs_truncate_inode_items(trans, root, inode,
7825 inode->i_size,
7826 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007827 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007828 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007829 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007830 }
Chris Mason39279cc2007-06-12 06:35:45 -04007831
Josef Bacikfcb80c22011-05-03 10:40:22 -04007832 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007833 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007834 if (ret) {
7835 err = ret;
7836 break;
7837 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007838
Yan, Zheng80825102009-11-12 09:35:36 +00007839 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007840 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007841
7842 trans = btrfs_start_transaction(root, 2);
7843 if (IS_ERR(trans)) {
7844 ret = err = PTR_ERR(trans);
7845 trans = NULL;
7846 break;
7847 }
7848
7849 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7850 rsv, min_size);
7851 BUG_ON(ret); /* shouldn't happen */
7852 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007853 }
7854
7855 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007856 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007857 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007858 if (ret)
7859 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007860 }
7861
Chris Mason917c16b2011-11-08 14:49:59 -05007862 if (trans) {
7863 trans->block_rsv = &root->fs_info->trans_block_rsv;
7864 ret = btrfs_update_inode(trans, root, inode);
7865 if (ret && !err)
7866 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007867
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007868 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007869 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007870 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007871
7872out:
7873 btrfs_free_block_rsv(root, rsv);
7874
Josef Bacik3893e332011-01-31 16:03:11 -05007875 if (ret && !err)
7876 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007877
Josef Bacik3893e332011-01-31 16:03:11 -05007878 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007879}
7880
Sven Wegener3b963622008-06-09 21:57:42 -04007881/*
Chris Masond352ac62008-09-29 15:18:18 -04007882 * create a new subvolume directory/inode (helper for the ioctl).
7883 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007884int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007885 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007886{
Chris Mason39279cc2007-06-12 06:35:45 -04007887 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007888 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007889 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007890
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007891 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7892 new_dirid, new_dirid,
7893 S_IFDIR | (~current_umask() & S_IRWXUGO),
7894 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007895 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007896 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007897 inode->i_op = &btrfs_dir_inode_operations;
7898 inode->i_fop = &btrfs_dir_file_operations;
7899
Miklos Szeredibfe86842011-10-28 14:13:29 +02007900 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007901 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007902
Yan, Zheng76dda932009-09-21 16:00:26 -04007903 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007904
Yan, Zheng76dda932009-09-21 16:00:26 -04007905 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007906 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007907}
7908
Chris Mason39279cc2007-06-12 06:35:45 -04007909struct inode *btrfs_alloc_inode(struct super_block *sb)
7910{
7911 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007912 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007913
7914 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7915 if (!ei)
7916 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007917
7918 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007919 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007920 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007921 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007922 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007923 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007924 ei->disk_i_size = 0;
7925 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007926 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007927 ei->index_cnt = (u64)-1;
7928 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007929 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007930
Josef Bacik9e0baf62011-07-15 15:16:44 +00007931 spin_lock_init(&ei->lock);
7932 ei->outstanding_extents = 0;
7933 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007934
Josef Bacik72ac3c02012-05-23 14:13:11 -04007935 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007936 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007937
Miao Xie16cdcec2011-04-22 18:12:22 +08007938 ei->delayed_node = NULL;
7939
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007940 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007941 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007942 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7943 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007944 ei->io_tree.track_uptodate = 1;
7945 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007946 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007947 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007948 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007949 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007950 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007951 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007952 RB_CLEAR_NODE(&ei->rb_node);
7953
7954 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007955}
7956
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007957static void btrfs_i_callback(struct rcu_head *head)
7958{
7959 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007960 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7961}
7962
Chris Mason39279cc2007-06-12 06:35:45 -04007963void btrfs_destroy_inode(struct inode *inode)
7964{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007965 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007966 struct btrfs_root *root = BTRFS_I(inode)->root;
7967
Al Virob3d9b7a2012-06-09 13:51:19 -04007968 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007969 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007970 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7971 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007972 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7973 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007974
Chris Mason5a3f23d2009-03-31 13:27:11 -04007975 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007976 * This can happen where we create an inode, but somebody else also
7977 * created the same inode and we need to destroy the one we already
7978 * created.
7979 */
7980 if (!root)
7981 goto free;
7982
7983 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007984 * Make sure we're properly removed from the ordered operation
7985 * lists.
7986 */
7987 smp_mb();
7988 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7989 spin_lock(&root->fs_info->ordered_extent_lock);
7990 list_del_init(&BTRFS_I(inode)->ordered_operations);
7991 spin_unlock(&root->fs_info->ordered_extent_lock);
7992 }
7993
Josef Bacik8a35d952012-05-23 14:26:42 -04007994 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7995 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007996 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7997 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007998 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007999 }
Josef Bacik7b128762008-07-24 12:17:14 -04008000
Chris Masond3977122009-01-05 21:25:51 -05008001 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008002 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8003 if (!ordered)
8004 break;
8005 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008006 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
8007 (unsigned long long)ordered->file_offset,
8008 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008009 btrfs_remove_ordered_extent(inode, ordered);
8010 btrfs_put_ordered_extent(ordered);
8011 btrfs_put_ordered_extent(ordered);
8012 }
8013 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008014 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008015 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008016free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008017 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008018}
8019
Al Viro45321ac2010-06-07 13:43:19 -04008020int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008021{
8022 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008023
Naohiro Aota6379ef92013-06-06 09:56:34 +00008024 if (root == NULL)
8025 return 1;
8026
Liu Bofa6ac872013-02-20 14:10:23 +00008027 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04008028 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00008029 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04008030 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008031 else
Al Viro45321ac2010-06-07 13:43:19 -04008032 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008033}
8034
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008035static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008036{
8037 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8038
8039 inode_init_once(&ei->vfs_inode);
8040}
8041
8042void btrfs_destroy_cachep(void)
8043{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008044 /*
8045 * Make sure all delayed rcu free inodes are flushed before we
8046 * destroy cache.
8047 */
8048 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008049 if (btrfs_inode_cachep)
8050 kmem_cache_destroy(btrfs_inode_cachep);
8051 if (btrfs_trans_handle_cachep)
8052 kmem_cache_destroy(btrfs_trans_handle_cachep);
8053 if (btrfs_transaction_cachep)
8054 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008055 if (btrfs_path_cachep)
8056 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008057 if (btrfs_free_space_cachep)
8058 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008059 if (btrfs_delalloc_work_cachep)
8060 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008061}
8062
8063int btrfs_init_cachep(void)
8064{
David Sterba837e1972012-09-07 03:00:48 -06008065 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008066 sizeof(struct btrfs_inode), 0,
8067 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008068 if (!btrfs_inode_cachep)
8069 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008070
David Sterba837e1972012-09-07 03:00:48 -06008071 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008072 sizeof(struct btrfs_trans_handle), 0,
8073 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008074 if (!btrfs_trans_handle_cachep)
8075 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008076
David Sterba837e1972012-09-07 03:00:48 -06008077 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008078 sizeof(struct btrfs_transaction), 0,
8079 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008080 if (!btrfs_transaction_cachep)
8081 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008082
David Sterba837e1972012-09-07 03:00:48 -06008083 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008084 sizeof(struct btrfs_path), 0,
8085 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008086 if (!btrfs_path_cachep)
8087 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008088
David Sterba837e1972012-09-07 03:00:48 -06008089 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008090 sizeof(struct btrfs_free_space), 0,
8091 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8092 if (!btrfs_free_space_cachep)
8093 goto fail;
8094
Miao Xie8ccf6f192012-10-25 09:28:04 +00008095 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8096 sizeof(struct btrfs_delalloc_work), 0,
8097 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8098 NULL);
8099 if (!btrfs_delalloc_work_cachep)
8100 goto fail;
8101
Chris Mason39279cc2007-06-12 06:35:45 -04008102 return 0;
8103fail:
8104 btrfs_destroy_cachep();
8105 return -ENOMEM;
8106}
8107
8108static int btrfs_getattr(struct vfsmount *mnt,
8109 struct dentry *dentry, struct kstat *stat)
8110{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008111 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008112 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008113 u32 blocksize = inode->i_sb->s_blocksize;
8114
Chris Mason39279cc2007-06-12 06:35:45 -04008115 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008116 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008117 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008118
8119 spin_lock(&BTRFS_I(inode)->lock);
8120 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8121 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008122 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008123 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008124 return 0;
8125}
8126
Chris Masond3977122009-01-05 21:25:51 -05008127static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8128 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008129{
8130 struct btrfs_trans_handle *trans;
8131 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008132 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008133 struct inode *new_inode = new_dentry->d_inode;
8134 struct inode *old_inode = old_dentry->d_inode;
8135 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008136 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008137 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008138 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008139 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008140
Li Zefan33345d012011-04-20 10:31:50 +08008141 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008142 return -EPERM;
8143
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008144 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008145 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008146 return -EXDEV;
8147
Li Zefan33345d012011-04-20 10:31:50 +08008148 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8149 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008150 return -ENOTEMPTY;
8151
Chris Mason39279cc2007-06-12 06:35:45 -04008152 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008153 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008154 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008155
8156
8157 /* check for collisions, even if the name isn't there */
Josef Bacika849b2f2013-10-09 12:24:04 -04008158 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008159 new_dentry->d_name.name,
8160 new_dentry->d_name.len);
8161
8162 if (ret) {
8163 if (ret == -EEXIST) {
8164 /* we shouldn't get
8165 * eexist without a new_inode */
8166 if (!new_inode) {
8167 WARN_ON(1);
8168 return ret;
8169 }
8170 } else {
8171 /* maybe -EOVERFLOW */
8172 return ret;
8173 }
8174 }
8175 ret = 0;
8176
Chris Mason5a3f23d2009-03-31 13:27:11 -04008177 /*
8178 * we're using rename to replace one file with another.
8179 * and the replacement file is large. Start IO on it now so
8180 * we don't add too much work to the end of the transaction
8181 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008182 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008183 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8184 filemap_flush(old_inode->i_mapping);
8185
Yan, Zheng76dda932009-09-21 16:00:26 -04008186 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008187 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008188 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008189 /*
8190 * We want to reserve the absolute worst case amount of items. So if
8191 * both inodes are subvols and we need to unlink them then that would
8192 * require 4 item modifications, but if they are both normal inodes it
8193 * would require 5 item modifications, so we'll assume their normal
8194 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8195 * should cover the worst case number of items we'll modify.
8196 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008197 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008198 if (IS_ERR(trans)) {
8199 ret = PTR_ERR(trans);
8200 goto out_notrans;
8201 }
Chris Mason5f39d392007-10-15 16:14:19 -04008202
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008203 if (dest != root)
8204 btrfs_record_root_in_trans(trans, dest);
8205
Yan, Zhenga5719522009-09-24 09:17:31 -04008206 ret = btrfs_set_inode_index(new_dir, &index);
8207 if (ret)
8208 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008209
Li Zefan33345d012011-04-20 10:31:50 +08008210 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008211 /* force full log commit if subvolume involved. */
8212 root->fs_info->last_trans_log_full_commit = trans->transid;
8213 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008214 ret = btrfs_insert_inode_ref(trans, dest,
8215 new_dentry->d_name.name,
8216 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008217 old_ino,
8218 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008219 if (ret)
8220 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008221 /*
8222 * this is an ugly little race, but the rename is required
8223 * to make sure that if we crash, the inode is either at the
8224 * old name or the new one. pinning the log transaction lets
8225 * us make sure we don't allow a log commit to come in after
8226 * we unlink the name but before we add the new name back in.
8227 */
8228 btrfs_pin_log_trans(root);
8229 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008230 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008231 * make sure the inode gets flushed if it is replacing
8232 * something.
8233 */
Li Zefan33345d012011-04-20 10:31:50 +08008234 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008235 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008236
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008237 inode_inc_iversion(old_dir);
8238 inode_inc_iversion(new_dir);
8239 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008240 old_dir->i_ctime = old_dir->i_mtime = ctime;
8241 new_dir->i_ctime = new_dir->i_mtime = ctime;
8242 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008243
Chris Mason12fcfd22009-03-24 10:24:20 -04008244 if (old_dentry->d_parent != new_dentry->d_parent)
8245 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8246
Li Zefan33345d012011-04-20 10:31:50 +08008247 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008248 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8249 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8250 old_dentry->d_name.name,
8251 old_dentry->d_name.len);
8252 } else {
Al Viro92986792011-03-04 17:14:37 +00008253 ret = __btrfs_unlink_inode(trans, root, old_dir,
8254 old_dentry->d_inode,
8255 old_dentry->d_name.name,
8256 old_dentry->d_name.len);
8257 if (!ret)
8258 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008259 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008260 if (ret) {
8261 btrfs_abort_transaction(trans, root, ret);
8262 goto out_fail;
8263 }
Chris Mason39279cc2007-06-12 06:35:45 -04008264
8265 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008266 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008267 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008268 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008269 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8270 root_objectid = BTRFS_I(new_inode)->location.objectid;
8271 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8272 root_objectid,
8273 new_dentry->d_name.name,
8274 new_dentry->d_name.len);
8275 BUG_ON(new_inode->i_nlink == 0);
8276 } else {
8277 ret = btrfs_unlink_inode(trans, dest, new_dir,
8278 new_dentry->d_inode,
8279 new_dentry->d_name.name,
8280 new_dentry->d_name.len);
8281 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008282 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008283 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008284 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008285 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008286 if (ret) {
8287 btrfs_abort_transaction(trans, root, ret);
8288 goto out_fail;
8289 }
Chris Mason39279cc2007-06-12 06:35:45 -04008290 }
Josef Bacikaec74772008-07-24 12:12:38 -04008291
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008292 ret = btrfs_add_link(trans, new_dir, old_inode,
8293 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008294 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008295 if (ret) {
8296 btrfs_abort_transaction(trans, root, ret);
8297 goto out_fail;
8298 }
Chris Mason39279cc2007-06-12 06:35:45 -04008299
Li Zefan33345d012011-04-20 10:31:50 +08008300 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008301 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008302 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008303 btrfs_end_log_trans(root);
8304 }
Chris Mason39279cc2007-06-12 06:35:45 -04008305out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008306 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008307out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008308 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008309 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008310
Chris Mason39279cc2007-06-12 06:35:45 -04008311 return ret;
8312}
8313
Miao Xie8ccf6f192012-10-25 09:28:04 +00008314static void btrfs_run_delalloc_work(struct btrfs_work *work)
8315{
8316 struct btrfs_delalloc_work *delalloc_work;
8317
8318 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8319 work);
8320 if (delalloc_work->wait)
8321 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8322 else
8323 filemap_flush(delalloc_work->inode->i_mapping);
8324
8325 if (delalloc_work->delay_iput)
8326 btrfs_add_delayed_iput(delalloc_work->inode);
8327 else
8328 iput(delalloc_work->inode);
8329 complete(&delalloc_work->completion);
8330}
8331
8332struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8333 int wait, int delay_iput)
8334{
8335 struct btrfs_delalloc_work *work;
8336
8337 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8338 if (!work)
8339 return NULL;
8340
8341 init_completion(&work->completion);
8342 INIT_LIST_HEAD(&work->list);
8343 work->inode = inode;
8344 work->wait = wait;
8345 work->delay_iput = delay_iput;
8346 work->work.func = btrfs_run_delalloc_work;
8347
8348 return work;
8349}
8350
8351void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8352{
8353 wait_for_completion(&work->completion);
8354 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8355}
8356
Chris Masond352ac62008-09-29 15:18:18 -04008357/*
8358 * some fairly slow code that needs optimization. This walks the list
8359 * of all the inodes with pending delalloc and forces them to disk.
8360 */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00008361int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008362{
Chris Masonea8c2812008-08-04 23:17:27 -04008363 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008364 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008365 struct btrfs_delalloc_work *work, *next;
8366 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008367 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008368 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008369
Yan Zhengc146afa2008-11-12 14:34:12 -05008370 if (root->fs_info->sb->s_flags & MS_RDONLY)
8371 return -EROFS;
8372
Miao Xie8ccf6f192012-10-25 09:28:04 +00008373 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008374 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008375
Chris Mason75eff682008-12-15 15:54:40 -05008376 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008377 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8378 while (!list_empty(&splice)) {
8379 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008380 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008381
8382 list_del_init(&binode->delalloc_inodes);
8383
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008384 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008385 if (!inode) {
8386 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8387 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008388 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008389 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008390
8391 list_add_tail(&binode->delalloc_inodes,
8392 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008393 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008394
8395 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8396 if (unlikely(!work)) {
8397 ret = -ENOMEM;
8398 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008399 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008400 list_add_tail(&work->list, &works);
8401 btrfs_queue_worker(&root->fs_info->flush_workers,
8402 &work->work);
8403
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008404 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008405 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008406 }
Chris Mason75eff682008-12-15 15:54:40 -05008407 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008408
Miao Xie1eafa6c2013-01-22 10:49:00 +00008409 list_for_each_entry_safe(work, next, &works, list) {
8410 list_del_init(&work->list);
8411 btrfs_wait_and_free_delalloc_work(work);
8412 }
8413
Chris Mason8c8bee12008-09-29 11:19:10 -04008414 /* the filemap_flush will queue IO into the worker threads, but
8415 * we have to make sure the IO is actually started and that
8416 * ordered extents get created before we return
8417 */
8418 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008419 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008420 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008421 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008422 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8423 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008424 }
8425 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008426 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008427out:
8428 list_for_each_entry_safe(work, next, &works, list) {
8429 list_del_init(&work->list);
8430 btrfs_wait_and_free_delalloc_work(work);
8431 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008432
8433 if (!list_empty_careful(&splice)) {
8434 spin_lock(&root->fs_info->delalloc_lock);
8435 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8436 spin_unlock(&root->fs_info->delalloc_lock);
8437 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008438 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008439}
8440
Chris Mason39279cc2007-06-12 06:35:45 -04008441static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8442 const char *symname)
8443{
8444 struct btrfs_trans_handle *trans;
8445 struct btrfs_root *root = BTRFS_I(dir)->root;
8446 struct btrfs_path *path;
8447 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008448 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008449 int err;
8450 int drop_inode = 0;
8451 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008452 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008453 int name_len;
8454 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008455 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008456 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008457 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008458
8459 name_len = strlen(symname) + 1;
8460 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8461 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008462
Josef Bacik9ed74f22009-09-11 16:12:44 -04008463 /*
8464 * 2 items for inode item and ref
8465 * 2 items for dir items
8466 * 1 item for xattr if selinux is on
8467 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008468 trans = btrfs_start_transaction(root, 5);
8469 if (IS_ERR(trans))
8470 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008471
Li Zefan581bb052011-04-20 10:06:11 +08008472 err = btrfs_find_free_ino(root, &objectid);
8473 if (err)
8474 goto out_unlock;
8475
Josef Bacikaec74772008-07-24 12:12:38 -04008476 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008477 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008478 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008479 if (IS_ERR(inode)) {
8480 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008481 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008482 }
Chris Mason39279cc2007-06-12 06:35:45 -04008483
Eric Paris2a7dba32011-02-01 11:05:39 -05008484 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008485 if (err) {
8486 drop_inode = 1;
8487 goto out_unlock;
8488 }
8489
Casey Schauflerad19db72011-12-15 10:09:07 -05008490 /*
8491 * If the active LSM wants to access the inode during
8492 * d_instantiate it needs these. Smack checks to see
8493 * if the filesystem supports xattrs by looking at the
8494 * ops vector.
8495 */
8496 inode->i_fop = &btrfs_file_operations;
8497 inode->i_op = &btrfs_file_inode_operations;
8498
Josef Bacika1b075d2010-11-19 20:36:11 +00008499 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008500 if (err)
8501 drop_inode = 1;
8502 else {
8503 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008504 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008505 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008506 }
Chris Mason39279cc2007-06-12 06:35:45 -04008507 if (drop_inode)
8508 goto out_unlock;
8509
8510 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008511 if (!path) {
8512 err = -ENOMEM;
8513 drop_inode = 1;
8514 goto out_unlock;
8515 }
Li Zefan33345d012011-04-20 10:31:50 +08008516 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008517 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008518 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8519 datasize = btrfs_file_extent_calc_inline_size(name_len);
8520 err = btrfs_insert_empty_item(trans, root, path, &key,
8521 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008522 if (err) {
8523 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008524 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008525 goto out_unlock;
8526 }
Chris Mason5f39d392007-10-15 16:14:19 -04008527 leaf = path->nodes[0];
8528 ei = btrfs_item_ptr(leaf, path->slots[0],
8529 struct btrfs_file_extent_item);
8530 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8531 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008532 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008533 btrfs_set_file_extent_encryption(leaf, ei, 0);
8534 btrfs_set_file_extent_compression(leaf, ei, 0);
8535 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8536 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8537
Chris Mason39279cc2007-06-12 06:35:45 -04008538 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008539 write_extent_buffer(leaf, symname, ptr, name_len);
8540 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008541 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008542
Chris Mason39279cc2007-06-12 06:35:45 -04008543 inode->i_op = &btrfs_symlink_inode_operations;
8544 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008545 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008546 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008547 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008548 err = btrfs_update_inode(trans, root, inode);
8549 if (err)
8550 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008551
8552out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008553 if (!err)
8554 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008555 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008556 if (drop_inode) {
8557 inode_dec_link_count(inode);
8558 iput(inode);
8559 }
Liu Bob53d3f52012-11-14 14:34:34 +00008560 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008561 return err;
8562}
Chris Mason16432982008-04-10 10:23:21 -04008563
Josef Bacik0af3d002010-06-21 14:48:16 -04008564static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8565 u64 start, u64 num_bytes, u64 min_size,
8566 loff_t actual_len, u64 *alloc_hint,
8567 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008568{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008569 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8570 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008571 struct btrfs_root *root = BTRFS_I(inode)->root;
8572 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008573 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008574 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008575 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008576 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008577 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008578
Josef Bacik0af3d002010-06-21 14:48:16 -04008579 if (trans)
8580 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008581 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008582 if (own_trans) {
8583 trans = btrfs_start_transaction(root, 3);
8584 if (IS_ERR(trans)) {
8585 ret = PTR_ERR(trans);
8586 break;
8587 }
Yan Zhengd899e052008-10-30 14:25:28 -04008588 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008589
Chris Mason154ea282013-03-05 11:11:26 -05008590 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8591 cur_bytes = max(cur_bytes, min_size);
8592 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008593 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008594 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008595 if (own_trans)
8596 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008597 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008598 }
8599
Yan Zhengd899e052008-10-30 14:25:28 -04008600 ret = insert_reserved_file_extent(trans, inode,
8601 cur_offset, ins.objectid,
8602 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008603 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008604 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008605 if (ret) {
8606 btrfs_abort_transaction(trans, root, ret);
8607 if (own_trans)
8608 btrfs_end_transaction(trans, root);
8609 break;
8610 }
Chris Masona1ed8352009-09-11 12:27:37 -04008611 btrfs_drop_extent_cache(inode, cur_offset,
8612 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008613
Josef Bacik5dc562c2012-08-17 13:14:17 -04008614 em = alloc_extent_map();
8615 if (!em) {
8616 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8617 &BTRFS_I(inode)->runtime_flags);
8618 goto next;
8619 }
8620
8621 em->start = cur_offset;
8622 em->orig_start = cur_offset;
8623 em->len = ins.offset;
8624 em->block_start = ins.objectid;
8625 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008626 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008627 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008628 em->bdev = root->fs_info->fs_devices->latest_bdev;
8629 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8630 em->generation = trans->transid;
8631
8632 while (1) {
8633 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008634 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008635 write_unlock(&em_tree->lock);
8636 if (ret != -EEXIST)
8637 break;
8638 btrfs_drop_extent_cache(inode, cur_offset,
8639 cur_offset + ins.offset - 1,
8640 0);
8641 }
8642 free_extent_map(em);
8643next:
Yan Zhengd899e052008-10-30 14:25:28 -04008644 num_bytes -= ins.offset;
8645 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008646 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008647
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008648 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008649 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008650 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008651 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008652 (actual_len > inode->i_size) &&
8653 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008654 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008655 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008656 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008657 i_size = cur_offset;
8658 i_size_write(inode, i_size);
8659 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008660 }
8661
Yan Zhengd899e052008-10-30 14:25:28 -04008662 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008663
8664 if (ret) {
8665 btrfs_abort_transaction(trans, root, ret);
8666 if (own_trans)
8667 btrfs_end_transaction(trans, root);
8668 break;
8669 }
Yan Zhengd899e052008-10-30 14:25:28 -04008670
Josef Bacik0af3d002010-06-21 14:48:16 -04008671 if (own_trans)
8672 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008673 }
Yan Zhengd899e052008-10-30 14:25:28 -04008674 return ret;
8675}
8676
Josef Bacik0af3d002010-06-21 14:48:16 -04008677int btrfs_prealloc_file_range(struct inode *inode, int mode,
8678 u64 start, u64 num_bytes, u64 min_size,
8679 loff_t actual_len, u64 *alloc_hint)
8680{
8681 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8682 min_size, actual_len, alloc_hint,
8683 NULL);
8684}
8685
8686int btrfs_prealloc_file_range_trans(struct inode *inode,
8687 struct btrfs_trans_handle *trans, int mode,
8688 u64 start, u64 num_bytes, u64 min_size,
8689 loff_t actual_len, u64 *alloc_hint)
8690{
8691 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8692 min_size, actual_len, alloc_hint, trans);
8693}
8694
Chris Masone6dcd2d2008-07-17 12:53:50 -04008695static int btrfs_set_page_dirty(struct page *page)
8696{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008697 return __set_page_dirty_nobuffers(page);
8698}
8699
Al Viro10556cb2011-06-20 19:28:19 -04008700static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008701{
Li Zefanb83cc962010-12-20 16:04:08 +08008702 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008703 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008704
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008705 if (mask & MAY_WRITE &&
8706 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8707 if (btrfs_root_readonly(root))
8708 return -EROFS;
8709 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8710 return -EACCES;
8711 }
Al Viro2830ba72011-06-20 19:16:29 -04008712 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008713}
Chris Mason39279cc2007-06-12 06:35:45 -04008714
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008715static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008716 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008717 .lookup = btrfs_lookup,
8718 .create = btrfs_create,
8719 .unlink = btrfs_unlink,
8720 .link = btrfs_link,
8721 .mkdir = btrfs_mkdir,
8722 .rmdir = btrfs_rmdir,
8723 .rename = btrfs_rename,
8724 .symlink = btrfs_symlink,
8725 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008726 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008727 .setxattr = btrfs_setxattr,
8728 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008729 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008730 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008731 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008732 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008733};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008734static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008735 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008736 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008737 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008738};
Yan, Zheng76dda932009-09-21 16:00:26 -04008739
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008740static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008741 .llseek = generic_file_llseek,
8742 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008743 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008744 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008745#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008746 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008747#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008748 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008749 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008750};
8751
Chris Masond1310b22008-01-24 16:13:08 -05008752static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008753 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008754 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008755 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008756 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008757 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008758 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008759 .set_bit_hook = btrfs_set_bit_hook,
8760 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008761 .merge_extent_hook = btrfs_merge_extent_hook,
8762 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008763};
8764
Chris Mason35054392009-01-21 13:11:13 -05008765/*
8766 * btrfs doesn't support the bmap operation because swapfiles
8767 * use bmap to make a mapping of extents in the file. They assume
8768 * these extents won't change over the life of the file and they
8769 * use the bmap result to do IO directly to the drive.
8770 *
8771 * the btrfs bmap call would return logical addresses that aren't
8772 * suitable for IO and they also will change frequently as COW
8773 * operations happen. So, swapfile + btrfs == corruption.
8774 *
8775 * For now we're avoiding this by dropping bmap.
8776 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008777static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008778 .readpage = btrfs_readpage,
8779 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008780 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008781 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008782 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008783 .invalidatepage = btrfs_invalidatepage,
8784 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008785 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008786 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008787};
8788
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008789static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008790 .readpage = btrfs_readpage,
8791 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008792 .invalidatepage = btrfs_invalidatepage,
8793 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008794};
8795
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008796static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008797 .getattr = btrfs_getattr,
8798 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008799 .setxattr = btrfs_setxattr,
8800 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008801 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008802 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008803 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008804 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008805 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008806 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008807};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008808static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008809 .getattr = btrfs_getattr,
8810 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008811 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008812 .setxattr = btrfs_setxattr,
8813 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008814 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008815 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008816 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008817 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008818};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008819static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008820 .readlink = generic_readlink,
8821 .follow_link = page_follow_link_light,
8822 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008823 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008824 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008825 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008826 .setxattr = btrfs_setxattr,
8827 .getxattr = btrfs_getxattr,
8828 .listxattr = btrfs_listxattr,
8829 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008830 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008831 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008832};
Yan, Zheng76dda932009-09-21 16:00:26 -04008833
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008834const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008835 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008836 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008837};