blob: 9b31b3b091fceb6536612b30e64f2fd191d47e89 [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();
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500718 if (!em)
719 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500720 em->start = async_extent->start;
721 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500722 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400723 em->mod_start = em->start;
724 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500725
726 em->block_start = ins.objectid;
727 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500728 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400729 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500730 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800731 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500732 set_bit(EXTENT_FLAG_PINNED, &em->flags);
733 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400734 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500735
Chris Masond3977122009-01-05 21:25:51 -0500736 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400737 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400738 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400739 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500740 if (ret != -EEXIST) {
741 free_extent_map(em);
742 break;
743 }
744 btrfs_drop_extent_cache(inode, async_extent->start,
745 async_extent->start +
746 async_extent->ram_size - 1, 0);
747 }
748
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500749 if (ret)
750 goto out_free_reserve;
751
Li Zefan261507a02010-12-17 14:21:50 +0800752 ret = btrfs_add_ordered_extent_compress(inode,
753 async_extent->start,
754 ins.objectid,
755 async_extent->ram_size,
756 ins.offset,
757 BTRFS_ORDERED_COMPRESSED,
758 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500759 if (ret)
760 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500761
Chris Mason771ed682008-11-06 22:02:51 -0500762 /*
763 * clear dirty, set writeback and unlock the pages.
764 */
765 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400766 &BTRFS_I(inode)->io_tree,
767 async_extent->start,
768 async_extent->start +
769 async_extent->ram_size - 1,
770 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
771 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400772 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400773 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500774
775 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500776 async_extent->start,
777 async_extent->ram_size,
778 ins.objectid,
779 ins.offset, async_extent->pages,
780 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500781 alloc_hint = ins.objectid + ins.offset;
782 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500783 if (ret)
784 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500785 cond_resched();
786 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100787 ret = 0;
788out:
789 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500790out_free_reserve:
791 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100792out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500793 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
794 async_extent->start,
795 async_extent->start +
796 async_extent->ram_size - 1,
797 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
798 EXTENT_CLEAR_UNLOCK |
799 EXTENT_CLEAR_DELALLOC |
800 EXTENT_CLEAR_DIRTY |
801 EXTENT_SET_WRITEBACK |
802 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100803 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500804 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500805}
806
Josef Bacik4b46fce2010-05-23 11:00:55 -0400807static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
808 u64 num_bytes)
809{
810 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
811 struct extent_map *em;
812 u64 alloc_hint = 0;
813
814 read_lock(&em_tree->lock);
815 em = search_extent_mapping(em_tree, start, num_bytes);
816 if (em) {
817 /*
818 * if block start isn't an actual block number then find the
819 * first block in this inode and use that as a hint. If that
820 * block is also bogus then just don't worry about it.
821 */
822 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
823 free_extent_map(em);
824 em = search_extent_mapping(em_tree, 0, 0);
825 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
826 alloc_hint = em->block_start;
827 if (em)
828 free_extent_map(em);
829 } else {
830 alloc_hint = em->block_start;
831 free_extent_map(em);
832 }
833 }
834 read_unlock(&em_tree->lock);
835
836 return alloc_hint;
837}
838
Chris Mason771ed682008-11-06 22:02:51 -0500839/*
840 * when extent_io.c finds a delayed allocation range in the file,
841 * the call backs end up in this code. The basic idea is to
842 * allocate extents on disk for the range, and create ordered data structs
843 * in ram to track those extents.
844 *
845 * locked_page is the page that writepage had locked already. We use
846 * it to make sure we don't do extra locks or unlocks.
847 *
848 * *page_started is set to one if we unlock locked_page and do everything
849 * required to start IO on it. It may be clean and already done with
850 * IO when we return.
851 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000852static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
853 struct inode *inode,
854 struct btrfs_root *root,
855 struct page *locked_page,
856 u64 start, u64 end, int *page_started,
857 unsigned long *nr_written,
858 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500859{
Chris Mason771ed682008-11-06 22:02:51 -0500860 u64 alloc_hint = 0;
861 u64 num_bytes;
862 unsigned long ram_size;
863 u64 disk_num_bytes;
864 u64 cur_alloc_size;
865 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500866 struct btrfs_key ins;
867 struct extent_map *em;
868 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
869 int ret = 0;
870
Liu Bo83eea1f2012-07-10 05:28:39 -0600871 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500872
Qu Wenruofda28322013-02-26 08:10:22 +0000873 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500874 num_bytes = max(blocksize, num_bytes);
875 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500876
Chris Mason4cb53002011-05-24 15:35:30 -0400877 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400878 if (num_bytes < 64 * 1024 &&
879 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400880 btrfs_add_inode_defrag(trans, inode);
881
Chris Mason771ed682008-11-06 22:02:51 -0500882 if (start == 0) {
883 /* lets try to make an inline extent */
884 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000885 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500886 if (ret == 0) {
887 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400888 &BTRFS_I(inode)->io_tree,
889 start, end, NULL,
890 EXTENT_CLEAR_UNLOCK_PAGE |
891 EXTENT_CLEAR_UNLOCK |
892 EXTENT_CLEAR_DELALLOC |
893 EXTENT_CLEAR_DIRTY |
894 EXTENT_SET_WRITEBACK |
895 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000896
Chris Mason771ed682008-11-06 22:02:51 -0500897 *nr_written = *nr_written +
898 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
899 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500900 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100901 } else if (ret < 0) {
902 btrfs_abort_transaction(trans, root, ret);
903 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500904 }
905 }
Chris Masonc8b97812008-10-29 14:49:59 -0400906
907 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200908 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400909
Josef Bacik4b46fce2010-05-23 11:00:55 -0400910 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400911 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400912
Chris Masond3977122009-01-05 21:25:51 -0500913 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400914 unsigned long op;
915
Josef Bacik287a0ab2010-03-19 18:07:23 +0000916 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400917 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500918 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500919 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100920 if (ret < 0) {
921 btrfs_abort_transaction(trans, root, ret);
922 goto out_unlock;
923 }
Chris Masond3977122009-01-05 21:25:51 -0500924
David Sterba172ddd62011-04-21 00:48:27 +0200925 em = alloc_extent_map();
Liu Boace68ba2013-04-22 10:53:47 +0000926 if (!em)
927 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400928 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500929 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500930 ram_size = ins.offset;
931 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400932 em->mod_start = em->start;
933 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400934
Chris Masone6dcd2d2008-07-17 12:53:50 -0400935 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400936 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500937 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400938 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400939 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400940 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400941 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400942
Chris Masond3977122009-01-05 21:25:51 -0500943 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400944 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400945 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400946 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400947 if (ret != -EEXIST) {
948 free_extent_map(em);
949 break;
950 }
951 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400952 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400953 }
Liu Boace68ba2013-04-22 10:53:47 +0000954 if (ret)
955 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400956
Chris Mason98d20f62008-04-14 09:46:10 -0400957 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400958 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500959 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000960 if (ret)
961 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400962
Yan Zheng17d217f2008-12-12 10:03:38 -0500963 if (root->root_key.objectid ==
964 BTRFS_DATA_RELOC_TREE_OBJECTID) {
965 ret = btrfs_reloc_clone_csums(inode, start,
966 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100967 if (ret) {
968 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000969 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100970 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500971 }
972
Chris Masond3977122009-01-05 21:25:51 -0500973 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400974 break;
Chris Masond3977122009-01-05 21:25:51 -0500975
Chris Masonc8b97812008-10-29 14:49:59 -0400976 /* we're not doing compressed IO, don't unlock the first
977 * page (which the caller expects to stay locked), don't
978 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400979 *
980 * Do set the Private2 bit so we know this page was properly
981 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400982 */
Chris Masona791e352009-10-08 11:27:10 -0400983 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
984 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
985 EXTENT_SET_PRIVATE2;
986
Chris Masonc8b97812008-10-29 14:49:59 -0400987 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
988 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400989 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400990 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500991 num_bytes -= cur_alloc_size;
992 alloc_hint = ins.objectid + ins.offset;
993 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400994 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100995out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500996 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000997
Liu Boace68ba2013-04-22 10:53:47 +0000998out_reserve:
999 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001000out_unlock:
1001 extent_clear_unlock_delalloc(inode,
1002 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -04001003 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 EXTENT_CLEAR_UNLOCK_PAGE |
1005 EXTENT_CLEAR_UNLOCK |
1006 EXTENT_CLEAR_DELALLOC |
1007 EXTENT_CLEAR_DIRTY |
1008 EXTENT_SET_WRITEBACK |
1009 EXTENT_END_WRITEBACK);
1010
1011 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001012}
Chris Masonc8b97812008-10-29 14:49:59 -04001013
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001014static noinline int cow_file_range(struct inode *inode,
1015 struct page *locked_page,
1016 u64 start, u64 end, int *page_started,
1017 unsigned long *nr_written,
1018 int unlock)
1019{
1020 struct btrfs_trans_handle *trans;
1021 struct btrfs_root *root = BTRFS_I(inode)->root;
1022 int ret;
1023
1024 trans = btrfs_join_transaction(root);
1025 if (IS_ERR(trans)) {
1026 extent_clear_unlock_delalloc(inode,
1027 &BTRFS_I(inode)->io_tree,
1028 start, end, locked_page,
1029 EXTENT_CLEAR_UNLOCK_PAGE |
1030 EXTENT_CLEAR_UNLOCK |
1031 EXTENT_CLEAR_DELALLOC |
1032 EXTENT_CLEAR_DIRTY |
1033 EXTENT_SET_WRITEBACK |
1034 EXTENT_END_WRITEBACK);
1035 return PTR_ERR(trans);
1036 }
1037 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1038
1039 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1040 page_started, nr_written, unlock);
1041
1042 btrfs_end_transaction(trans, root);
1043
1044 return ret;
1045}
1046
Chris Mason771ed682008-11-06 22:02:51 -05001047/*
1048 * work queue call back to started compression on a file and pages
1049 */
1050static noinline void async_cow_start(struct btrfs_work *work)
1051{
1052 struct async_cow *async_cow;
1053 int num_added = 0;
1054 async_cow = container_of(work, struct async_cow, work);
1055
1056 compress_file_range(async_cow->inode, async_cow->locked_page,
1057 async_cow->start, async_cow->end, async_cow,
1058 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001059 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001060 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001061 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001062 }
Chris Mason771ed682008-11-06 22:02:51 -05001063}
1064
1065/*
1066 * work queue call back to submit previously compressed pages
1067 */
1068static noinline void async_cow_submit(struct btrfs_work *work)
1069{
1070 struct async_cow *async_cow;
1071 struct btrfs_root *root;
1072 unsigned long nr_pages;
1073
1074 async_cow = container_of(work, struct async_cow, work);
1075
1076 root = async_cow->root;
1077 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1078 PAGE_CACHE_SHIFT;
1079
Josef Bacik66657b32012-08-01 15:36:24 -04001080 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001081 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001082 waitqueue_active(&root->fs_info->async_submit_wait))
1083 wake_up(&root->fs_info->async_submit_wait);
1084
Chris Masond3977122009-01-05 21:25:51 -05001085 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001086 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001087}
Chris Masonc8b97812008-10-29 14:49:59 -04001088
Chris Mason771ed682008-11-06 22:02:51 -05001089static noinline void async_cow_free(struct btrfs_work *work)
1090{
1091 struct async_cow *async_cow;
1092 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001093 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001094 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001095 kfree(async_cow);
1096}
1097
1098static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1099 u64 start, u64 end, int *page_started,
1100 unsigned long *nr_written)
1101{
1102 struct async_cow *async_cow;
1103 struct btrfs_root *root = BTRFS_I(inode)->root;
1104 unsigned long nr_pages;
1105 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001106 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001107
Chris Masona3429ab2009-10-08 12:30:20 -04001108 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1109 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001110 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001111 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001112 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001113 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001114 async_cow->root = root;
1115 async_cow->locked_page = locked_page;
1116 async_cow->start = start;
1117
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001118 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001119 cur_end = end;
1120 else
1121 cur_end = min(end, start + 512 * 1024 - 1);
1122
1123 async_cow->end = cur_end;
1124 INIT_LIST_HEAD(&async_cow->extents);
1125
1126 async_cow->work.func = async_cow_start;
1127 async_cow->work.ordered_func = async_cow_submit;
1128 async_cow->work.ordered_free = async_cow_free;
1129 async_cow->work.flags = 0;
1130
Chris Mason771ed682008-11-06 22:02:51 -05001131 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1132 PAGE_CACHE_SHIFT;
1133 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1134
1135 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1136 &async_cow->work);
1137
1138 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1139 wait_event(root->fs_info->async_submit_wait,
1140 (atomic_read(&root->fs_info->async_delalloc_pages) <
1141 limit));
1142 }
1143
Chris Masond3977122009-01-05 21:25:51 -05001144 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001145 atomic_read(&root->fs_info->async_delalloc_pages)) {
1146 wait_event(root->fs_info->async_submit_wait,
1147 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1148 0));
1149 }
1150
1151 *nr_written += nr_pages;
1152 start = cur_end + 1;
1153 }
1154 *page_started = 1;
1155 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001156}
1157
Chris Masond3977122009-01-05 21:25:51 -05001158static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001159 u64 bytenr, u64 num_bytes)
1160{
1161 int ret;
1162 struct btrfs_ordered_sum *sums;
1163 LIST_HEAD(list);
1164
Yan Zheng07d400a2009-01-06 11:42:00 -05001165 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001166 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001167 if (ret == 0 && list_empty(&list))
1168 return 0;
1169
1170 while (!list_empty(&list)) {
1171 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1172 list_del(&sums->list);
1173 kfree(sums);
1174 }
1175 return 1;
1176}
1177
Chris Masond352ac62008-09-29 15:18:18 -04001178/*
1179 * when nowcow writeback call back. This checks for snapshots or COW copies
1180 * of the extents that exist in the file, and COWs the file as required.
1181 *
1182 * If no cow copies or snapshots exist, we write directly to the existing
1183 * blocks on disk
1184 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001185static noinline int run_delalloc_nocow(struct inode *inode,
1186 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001187 u64 start, u64 end, int *page_started, int force,
1188 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001189{
Chris Masonbe20aa92007-12-17 20:14:01 -05001190 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001191 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001192 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001193 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001194 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001195 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001196 u64 cow_start;
1197 u64 cur_offset;
1198 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001199 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 u64 disk_bytenr;
1201 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001202 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001203 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001204 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001205 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001206 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 int nocow;
1208 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001209 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001210 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001211
1212 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001213 if (!path) {
1214 extent_clear_unlock_delalloc(inode,
1215 &BTRFS_I(inode)->io_tree,
1216 start, end, locked_page,
1217 EXTENT_CLEAR_UNLOCK_PAGE |
1218 EXTENT_CLEAR_UNLOCK |
1219 EXTENT_CLEAR_DELALLOC |
1220 EXTENT_CLEAR_DIRTY |
1221 EXTENT_SET_WRITEBACK |
1222 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001223 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001224 }
Li Zefan82d59022011-04-20 10:33:24 +08001225
Liu Bo83eea1f2012-07-10 05:28:39 -06001226 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001227
1228 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001229 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001230 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001231 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001232
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001233 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001234 extent_clear_unlock_delalloc(inode,
1235 &BTRFS_I(inode)->io_tree,
1236 start, end, locked_page,
1237 EXTENT_CLEAR_UNLOCK_PAGE |
1238 EXTENT_CLEAR_UNLOCK |
1239 EXTENT_CLEAR_DELALLOC |
1240 EXTENT_CLEAR_DIRTY |
1241 EXTENT_SET_WRITEBACK |
1242 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001243 btrfs_free_path(path);
1244 return PTR_ERR(trans);
1245 }
1246
Josef Bacik74b21072011-04-13 12:02:53 -04001247 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001248
Yan Zheng80ff3852008-10-30 14:20:02 -04001249 cow_start = (u64)-1;
1250 cur_offset = start;
1251 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001252 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001253 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001254 if (ret < 0) {
1255 btrfs_abort_transaction(trans, root, ret);
1256 goto error;
1257 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1259 leaf = path->nodes[0];
1260 btrfs_item_key_to_cpu(leaf, &found_key,
1261 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001262 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 found_key.type == BTRFS_EXTENT_DATA_KEY)
1264 path->slots[0]--;
1265 }
1266 check_prev = 0;
1267next_slot:
1268 leaf = path->nodes[0];
1269 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1270 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001271 if (ret < 0) {
1272 btrfs_abort_transaction(trans, root, ret);
1273 goto error;
1274 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 if (ret > 0)
1276 break;
1277 leaf = path->nodes[0];
1278 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001279
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 nocow = 0;
1281 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001282 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001284
Li Zefan33345d012011-04-20 10:31:50 +08001285 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1287 found_key.offset > end)
1288 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001289
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 if (found_key.offset > cur_offset) {
1291 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001292 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 goto out_check;
1294 }
Chris Masonc31f8832008-01-08 15:46:31 -05001295
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 fi = btrfs_item_ptr(leaf, path->slots[0],
1297 struct btrfs_file_extent_item);
1298 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001299
Josef Bacikcc95bef2013-04-04 14:31:27 -04001300 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001301 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1302 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001304 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 extent_end = found_key.offset +
1306 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001307 disk_num_bytes =
1308 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 if (extent_end <= start) {
1310 path->slots[0]++;
1311 goto next_slot;
1312 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001313 if (disk_bytenr == 0)
1314 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 if (btrfs_file_extent_compression(leaf, fi) ||
1316 btrfs_file_extent_encryption(leaf, fi) ||
1317 btrfs_file_extent_other_encoding(leaf, fi))
1318 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001319 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1320 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001321 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001323 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001324 found_key.offset -
1325 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001326 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001327 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001328 disk_bytenr += cur_offset - found_key.offset;
1329 num_bytes = min(end + 1, extent_end) - cur_offset;
1330 /*
1331 * force cow if csum exists in the range.
1332 * this ensure that csum for a given extent are
1333 * either valid or do not exist.
1334 */
1335 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1336 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001337 nocow = 1;
1338 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1339 extent_end = found_key.offset +
1340 btrfs_file_extent_inline_len(leaf, fi);
1341 extent_end = ALIGN(extent_end, root->sectorsize);
1342 } else {
1343 BUG_ON(1);
1344 }
1345out_check:
1346 if (extent_end <= start) {
1347 path->slots[0]++;
1348 goto next_slot;
1349 }
1350 if (!nocow) {
1351 if (cow_start == (u64)-1)
1352 cow_start = cur_offset;
1353 cur_offset = extent_end;
1354 if (cur_offset > end)
1355 break;
1356 path->slots[0]++;
1357 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001358 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001359
David Sterbab3b4aa72011-04-21 01:20:15 +02001360 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001362 ret = __cow_file_range(trans, inode, root, locked_page,
1363 cow_start, found_key.offset - 1,
1364 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001365 if (ret) {
1366 btrfs_abort_transaction(trans, root, ret);
1367 goto error;
1368 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001370 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001371
Yan Zhengd899e052008-10-30 14:25:28 -04001372 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1373 struct extent_map *em;
1374 struct extent_map_tree *em_tree;
1375 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001376 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001377 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001378 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001379 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001380 em->len = num_bytes;
1381 em->block_len = num_bytes;
1382 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001383 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001384 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001385 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001386 em->mod_start = em->start;
1387 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001388 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001389 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001390 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001391 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001392 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001393 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001394 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001395 if (ret != -EEXIST) {
1396 free_extent_map(em);
1397 break;
1398 }
1399 btrfs_drop_extent_cache(inode, em->start,
1400 em->start + em->len - 1, 0);
1401 }
1402 type = BTRFS_ORDERED_PREALLOC;
1403 } else {
1404 type = BTRFS_ORDERED_NOCOW;
1405 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001406
1407 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001408 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001409 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001410
Yan, Zhengefa56462010-05-16 10:49:59 -04001411 if (root->root_key.objectid ==
1412 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1413 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1414 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001415 if (ret) {
1416 btrfs_abort_transaction(trans, root, ret);
1417 goto error;
1418 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001419 }
1420
Yan Zhengd899e052008-10-30 14:25:28 -04001421 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001422 cur_offset, cur_offset + num_bytes - 1,
1423 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1424 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1425 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001426 cur_offset = extent_end;
1427 if (cur_offset > end)
1428 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001429 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001430 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001431
Josef Bacik17ca04a2012-05-31 15:58:55 -04001432 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001433 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001434 cur_offset = end;
1435 }
1436
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001438 ret = __cow_file_range(trans, inode, root, locked_page,
1439 cow_start, end,
1440 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001441 if (ret) {
1442 btrfs_abort_transaction(trans, root, ret);
1443 goto error;
1444 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001445 }
1446
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001447error:
Miao Xiea698d0752012-09-20 01:51:59 -06001448 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001449 if (!ret)
1450 ret = err;
1451
Josef Bacik17ca04a2012-05-31 15:58:55 -04001452 if (ret && cur_offset < end)
1453 extent_clear_unlock_delalloc(inode,
1454 &BTRFS_I(inode)->io_tree,
1455 cur_offset, end, locked_page,
1456 EXTENT_CLEAR_UNLOCK_PAGE |
1457 EXTENT_CLEAR_UNLOCK |
1458 EXTENT_CLEAR_DELALLOC |
1459 EXTENT_CLEAR_DIRTY |
1460 EXTENT_SET_WRITEBACK |
1461 EXTENT_END_WRITEBACK);
1462
Yan Zheng7ea394f2008-08-05 13:05:02 -04001463 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001464 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001465}
1466
Chris Masond352ac62008-09-29 15:18:18 -04001467/*
1468 * extent_io.c call back to do delayed allocation processing
1469 */
Chris Masonc8b97812008-10-29 14:49:59 -04001470static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001471 u64 start, u64 end, int *page_started,
1472 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001473{
Chris Masonbe20aa92007-12-17 20:14:01 -05001474 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001475 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001476
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001477 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001478 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001479 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001480 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001481 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001482 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001483 } else if (!btrfs_test_opt(root, COMPRESS) &&
1484 !(BTRFS_I(inode)->force_compress) &&
1485 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001486 ret = cow_file_range(inode, locked_page, start, end,
1487 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001488 } else {
1489 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1490 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001491 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001492 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001493 }
Chris Masonb888db22007-08-27 16:49:44 -04001494 return ret;
1495}
1496
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001497static void btrfs_split_extent_hook(struct inode *inode,
1498 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001499{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001500 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001501 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001502 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001503
Josef Bacik9e0baf62011-07-15 15:16:44 +00001504 spin_lock(&BTRFS_I(inode)->lock);
1505 BTRFS_I(inode)->outstanding_extents++;
1506 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001507}
1508
1509/*
1510 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1511 * extents so we can keep track of new extents that are just merged onto old
1512 * extents, such as when we are doing sequential writes, so we can properly
1513 * account for the metadata space we'll need.
1514 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001515static void btrfs_merge_extent_hook(struct inode *inode,
1516 struct extent_state *new,
1517 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001518{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001519 /* not delalloc, ignore it */
1520 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001521 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001522
Josef Bacik9e0baf62011-07-15 15:16:44 +00001523 spin_lock(&BTRFS_I(inode)->lock);
1524 BTRFS_I(inode)->outstanding_extents--;
1525 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001526}
1527
Chris Masond352ac62008-09-29 15:18:18 -04001528/*
1529 * extent_io.c set_bit_hook, used to track delayed allocation
1530 * bytes in this file, and to maintain the list of inodes that
1531 * have pending delalloc work to be done.
1532 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001533static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001534 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001535{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001536
Chris Mason75eff682008-12-15 15:54:40 -05001537 /*
1538 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001539 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001540 * bit, which is only set or cleared with irqs on
1541 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001542 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001543 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001544 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001545 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001546
Josef Bacik9e0baf62011-07-15 15:16:44 +00001547 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001548 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001549 } else {
1550 spin_lock(&BTRFS_I(inode)->lock);
1551 BTRFS_I(inode)->outstanding_extents++;
1552 spin_unlock(&BTRFS_I(inode)->lock);
1553 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001554
Miao Xie963d6782013-01-29 10:10:51 +00001555 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1556 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001557 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001558 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001559 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1560 &BTRFS_I(inode)->runtime_flags)) {
1561 spin_lock(&root->fs_info->delalloc_lock);
1562 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1563 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1564 &root->fs_info->delalloc_inodes);
1565 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1566 &BTRFS_I(inode)->runtime_flags);
1567 }
1568 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001569 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001570 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001571 }
Chris Mason291d6732008-01-29 15:55:23 -05001572}
1573
Chris Masond352ac62008-09-29 15:18:18 -04001574/*
1575 * extent_io.c clear_bit_hook, see set_bit_hook for why
1576 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001577static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001578 struct extent_state *state,
1579 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001580{
Chris Mason75eff682008-12-15 15:54:40 -05001581 /*
1582 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001583 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001584 * bit, which is only set or cleared with irqs on
1585 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001586 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001587 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001588 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001589 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001590
Josef Bacik9e0baf62011-07-15 15:16:44 +00001591 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001592 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001593 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1594 spin_lock(&BTRFS_I(inode)->lock);
1595 BTRFS_I(inode)->outstanding_extents--;
1596 spin_unlock(&BTRFS_I(inode)->lock);
1597 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001598
1599 if (*bits & EXTENT_DO_ACCOUNTING)
1600 btrfs_delalloc_release_metadata(inode, len);
1601
Josef Bacik0cb59c92010-07-02 12:14:14 -04001602 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1603 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001604 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001605
Miao Xie963d6782013-01-29 10:10:51 +00001606 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1607 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001608 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001609 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001610 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001611 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1612 &BTRFS_I(inode)->runtime_flags)) {
1613 spin_lock(&root->fs_info->delalloc_lock);
1614 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1615 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1616 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1617 &BTRFS_I(inode)->runtime_flags);
1618 }
1619 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001620 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001621 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001622 }
Chris Mason291d6732008-01-29 15:55:23 -05001623}
1624
Chris Masond352ac62008-09-29 15:18:18 -04001625/*
1626 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1627 * we don't create bios that span stripes or chunks
1628 */
David Woodhouse64a16702009-07-15 23:29:37 +01001629int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001630 size_t size, struct bio *bio,
1631 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001632{
1633 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001634 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001635 u64 length = 0;
1636 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001637 int ret;
1638
Chris Mason771ed682008-11-06 22:02:51 -05001639 if (bio_flags & EXTENT_BIO_COMPRESSED)
1640 return 0;
1641
Chris Masonf2d8d742008-04-21 10:03:05 -04001642 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001643 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001644 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001645 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001646 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001647 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001648 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001649 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001650 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001651}
1652
Chris Masond352ac62008-09-29 15:18:18 -04001653/*
1654 * in order to insert checksums into the metadata in large chunks,
1655 * we wait until bio submission time. All the pages in the bio are
1656 * checksummed and sums are attached onto the ordered extent record.
1657 *
1658 * At IO completion time the cums attached on the ordered extent record
1659 * are inserted into the btree
1660 */
Chris Masond3977122009-01-05 21:25:51 -05001661static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1662 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001663 unsigned long bio_flags,
1664 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001665{
Chris Mason065631f2008-02-20 12:07:25 -05001666 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001667 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001668
Chris Masond20f7042008-12-08 16:58:54 -05001669 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001670 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001671 return 0;
1672}
Chris Masone0156402008-04-16 11:15:20 -04001673
Chris Mason4a69a412008-11-06 22:03:00 -05001674/*
1675 * in order to insert checksums into the metadata in large chunks,
1676 * we wait until bio submission time. All the pages in the bio are
1677 * checksummed and sums are attached onto the ordered extent record.
1678 *
1679 * At IO completion time the cums attached on the ordered extent record
1680 * are inserted into the btree
1681 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001682static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001683 int mirror_num, unsigned long bio_flags,
1684 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001685{
1686 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001687 int ret;
1688
1689 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1690 if (ret)
1691 bio_endio(bio, ret);
1692 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001693}
1694
Chris Masond352ac62008-09-29 15:18:18 -04001695/*
Chris Masoncad321a2008-12-17 14:51:42 -05001696 * extent_io.c submission hook. This does the right thing for csum calculation
1697 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001698 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001699static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001700 int mirror_num, unsigned long bio_flags,
1701 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001702{
1703 struct btrfs_root *root = BTRFS_I(inode)->root;
1704 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001705 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001706 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001707 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001708
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001709 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001710
Liu Bo83eea1f2012-07-10 05:28:39 -06001711 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001712 metadata = 2;
1713
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001714 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001715 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1716 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001717 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001718
Chris Masond20f7042008-12-08 16:58:54 -05001719 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001720 ret = btrfs_submit_compressed_read(inode, bio,
1721 mirror_num,
1722 bio_flags);
1723 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001724 } else if (!skip_sum) {
1725 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1726 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001727 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001728 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001729 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001730 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001731 /* csum items have already been cloned */
1732 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1733 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001734 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001735 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001736 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001737 bio_flags, bio_offset,
1738 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001739 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001740 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001741 } else if (!skip_sum) {
1742 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1743 if (ret)
1744 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001745 }
1746
Chris Mason0b86a832008-03-24 15:01:56 -04001747mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001748 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1749
1750out:
1751 if (ret < 0)
1752 bio_endio(bio, ret);
1753 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001754}
Chris Mason6885f302008-02-20 16:11:05 -05001755
Chris Masond352ac62008-09-29 15:18:18 -04001756/*
1757 * given a list of ordered sums record them in the inode. This happens
1758 * at IO completion time based on sums calculated at bio submission time.
1759 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001760static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001761 struct inode *inode, u64 file_offset,
1762 struct list_head *list)
1763{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001764 struct btrfs_ordered_sum *sum;
1765
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001766 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001767 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001768 btrfs_csum_file_blocks(trans,
1769 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001770 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001771 }
1772 return 0;
1773}
1774
Josef Bacik2ac55d42010-02-03 19:33:23 +00001775int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1776 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001777{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001778 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001779 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001780 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001781}
1782
Chris Masond352ac62008-09-29 15:18:18 -04001783/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001784struct btrfs_writepage_fixup {
1785 struct page *page;
1786 struct btrfs_work work;
1787};
1788
Christoph Hellwigb2950862008-12-02 09:54:17 -05001789static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001790{
1791 struct btrfs_writepage_fixup *fixup;
1792 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001793 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001794 struct page *page;
1795 struct inode *inode;
1796 u64 page_start;
1797 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001798 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001799
1800 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1801 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001802again:
Chris Mason247e7432008-07-17 12:53:51 -04001803 lock_page(page);
1804 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1805 ClearPageChecked(page);
1806 goto out_page;
1807 }
1808
1809 inode = page->mapping->host;
1810 page_start = page_offset(page);
1811 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1812
Josef Bacik2ac55d42010-02-03 19:33:23 +00001813 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001814 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001815
1816 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001817 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001818 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001819
1820 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1821 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001822 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1823 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001824 unlock_page(page);
1825 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001826 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001827 goto again;
1828 }
Chris Mason247e7432008-07-17 12:53:51 -04001829
Jeff Mahoney87826df2012-02-15 16:23:57 +01001830 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1831 if (ret) {
1832 mapping_set_error(page->mapping, ret);
1833 end_extent_writepage(page, ret, page_start, page_end);
1834 ClearPageChecked(page);
1835 goto out;
1836 }
1837
Josef Bacik2ac55d42010-02-03 19:33:23 +00001838 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001839 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001840 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001841out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001842 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1843 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001844out_page:
1845 unlock_page(page);
1846 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001847 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001848}
1849
1850/*
1851 * There are a few paths in the higher layers of the kernel that directly
1852 * set the page dirty bit without asking the filesystem if it is a
1853 * good idea. This causes problems because we want to make sure COW
1854 * properly happens and the data=ordered rules are followed.
1855 *
Chris Masonc8b97812008-10-29 14:49:59 -04001856 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001857 * hasn't been properly setup for IO. We kick off an async process
1858 * to fix it up. The async helper will wait for ordered extents, set
1859 * the delalloc bit and make it safe to write the page.
1860 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001861static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001862{
1863 struct inode *inode = page->mapping->host;
1864 struct btrfs_writepage_fixup *fixup;
1865 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001866
Chris Mason8b62b722009-09-02 16:53:46 -04001867 /* this page is properly in the ordered list */
1868 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001869 return 0;
1870
1871 if (PageChecked(page))
1872 return -EAGAIN;
1873
1874 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1875 if (!fixup)
1876 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001877
Chris Mason247e7432008-07-17 12:53:51 -04001878 SetPageChecked(page);
1879 page_cache_get(page);
1880 fixup->work.func = btrfs_writepage_fixup_worker;
1881 fixup->page = page;
1882 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001883 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001884}
1885
Yan Zhengd899e052008-10-30 14:25:28 -04001886static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1887 struct inode *inode, u64 file_pos,
1888 u64 disk_bytenr, u64 disk_num_bytes,
1889 u64 num_bytes, u64 ram_bytes,
1890 u8 compression, u8 encryption,
1891 u16 other_encoding, int extent_type)
1892{
1893 struct btrfs_root *root = BTRFS_I(inode)->root;
1894 struct btrfs_file_extent_item *fi;
1895 struct btrfs_path *path;
1896 struct extent_buffer *leaf;
1897 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001898 int ret;
1899
1900 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001901 if (!path)
1902 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001903
Chris Masonb9473432009-03-13 11:00:37 -04001904 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001905
1906 /*
1907 * we may be replacing one extent in the tree with another.
1908 * The new extent is pinned in the extent map, and we don't want
1909 * to drop it from the cache until it is completely in the btree.
1910 *
1911 * So, tell btrfs_drop_extents to leave this extent in the cache.
1912 * the caller is expected to unpin it and allow it to be merged
1913 * with the others.
1914 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001915 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001916 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001917 if (ret)
1918 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001919
Li Zefan33345d012011-04-20 10:31:50 +08001920 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001921 ins.offset = file_pos;
1922 ins.type = BTRFS_EXTENT_DATA_KEY;
1923 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 if (ret)
1925 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001926 leaf = path->nodes[0];
1927 fi = btrfs_item_ptr(leaf, path->slots[0],
1928 struct btrfs_file_extent_item);
1929 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1930 btrfs_set_file_extent_type(leaf, fi, extent_type);
1931 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1932 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1933 btrfs_set_file_extent_offset(leaf, fi, 0);
1934 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1935 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1936 btrfs_set_file_extent_compression(leaf, fi, compression);
1937 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1938 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001939
Yan Zhengd899e052008-10-30 14:25:28 -04001940 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001941 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001942
1943 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001944
1945 ins.objectid = disk_bytenr;
1946 ins.offset = disk_num_bytes;
1947 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001948 ret = btrfs_alloc_reserved_file_extent(trans, root,
1949 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001950 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001951out:
Yan Zhengd899e052008-10-30 14:25:28 -04001952 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001953
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001954 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001955}
1956
Liu Bo38c227d2013-01-29 03:18:40 +00001957/* snapshot-aware defrag */
1958struct sa_defrag_extent_backref {
1959 struct rb_node node;
1960 struct old_sa_defrag_extent *old;
1961 u64 root_id;
1962 u64 inum;
1963 u64 file_pos;
1964 u64 extent_offset;
1965 u64 num_bytes;
1966 u64 generation;
1967};
1968
1969struct old_sa_defrag_extent {
1970 struct list_head list;
1971 struct new_sa_defrag_extent *new;
1972
1973 u64 extent_offset;
1974 u64 bytenr;
1975 u64 offset;
1976 u64 len;
1977 int count;
1978};
1979
1980struct new_sa_defrag_extent {
1981 struct rb_root root;
1982 struct list_head head;
1983 struct btrfs_path *path;
1984 struct inode *inode;
1985 u64 file_pos;
1986 u64 len;
1987 u64 bytenr;
1988 u64 disk_len;
1989 u8 compress_type;
1990};
1991
1992static int backref_comp(struct sa_defrag_extent_backref *b1,
1993 struct sa_defrag_extent_backref *b2)
1994{
1995 if (b1->root_id < b2->root_id)
1996 return -1;
1997 else if (b1->root_id > b2->root_id)
1998 return 1;
1999
2000 if (b1->inum < b2->inum)
2001 return -1;
2002 else if (b1->inum > b2->inum)
2003 return 1;
2004
2005 if (b1->file_pos < b2->file_pos)
2006 return -1;
2007 else if (b1->file_pos > b2->file_pos)
2008 return 1;
2009
2010 /*
2011 * [------------------------------] ===> (a range of space)
2012 * |<--->| |<---->| =============> (fs/file tree A)
2013 * |<---------------------------->| ===> (fs/file tree B)
2014 *
2015 * A range of space can refer to two file extents in one tree while
2016 * refer to only one file extent in another tree.
2017 *
2018 * So we may process a disk offset more than one time(two extents in A)
2019 * and locate at the same extent(one extent in B), then insert two same
2020 * backrefs(both refer to the extent in B).
2021 */
2022 return 0;
2023}
2024
2025static void backref_insert(struct rb_root *root,
2026 struct sa_defrag_extent_backref *backref)
2027{
2028 struct rb_node **p = &root->rb_node;
2029 struct rb_node *parent = NULL;
2030 struct sa_defrag_extent_backref *entry;
2031 int ret;
2032
2033 while (*p) {
2034 parent = *p;
2035 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2036
2037 ret = backref_comp(backref, entry);
2038 if (ret < 0)
2039 p = &(*p)->rb_left;
2040 else
2041 p = &(*p)->rb_right;
2042 }
2043
2044 rb_link_node(&backref->node, parent, p);
2045 rb_insert_color(&backref->node, root);
2046}
2047
2048/*
2049 * Note the backref might has changed, and in this case we just return 0.
2050 */
2051static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2052 void *ctx)
2053{
2054 struct btrfs_file_extent_item *extent;
2055 struct btrfs_fs_info *fs_info;
2056 struct old_sa_defrag_extent *old = ctx;
2057 struct new_sa_defrag_extent *new = old->new;
2058 struct btrfs_path *path = new->path;
2059 struct btrfs_key key;
2060 struct btrfs_root *root;
2061 struct sa_defrag_extent_backref *backref;
2062 struct extent_buffer *leaf;
2063 struct inode *inode = new->inode;
2064 int slot;
2065 int ret;
2066 u64 extent_offset;
2067 u64 num_bytes;
2068
2069 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2070 inum == btrfs_ino(inode))
2071 return 0;
2072
2073 key.objectid = root_id;
2074 key.type = BTRFS_ROOT_ITEM_KEY;
2075 key.offset = (u64)-1;
2076
2077 fs_info = BTRFS_I(inode)->root->fs_info;
2078 root = btrfs_read_fs_root_no_name(fs_info, &key);
2079 if (IS_ERR(root)) {
2080 if (PTR_ERR(root) == -ENOENT)
2081 return 0;
2082 WARN_ON(1);
2083 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2084 inum, offset, root_id);
2085 return PTR_ERR(root);
2086 }
2087
2088 key.objectid = inum;
2089 key.type = BTRFS_EXTENT_DATA_KEY;
2090 if (offset > (u64)-1 << 32)
2091 key.offset = 0;
2092 else
2093 key.offset = offset;
2094
2095 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2096 if (ret < 0) {
2097 WARN_ON(1);
2098 return ret;
2099 }
2100
2101 while (1) {
2102 cond_resched();
2103
2104 leaf = path->nodes[0];
2105 slot = path->slots[0];
2106
2107 if (slot >= btrfs_header_nritems(leaf)) {
2108 ret = btrfs_next_leaf(root, path);
2109 if (ret < 0) {
2110 goto out;
2111 } else if (ret > 0) {
2112 ret = 0;
2113 goto out;
2114 }
2115 continue;
2116 }
2117
2118 path->slots[0]++;
2119
2120 btrfs_item_key_to_cpu(leaf, &key, slot);
2121
2122 if (key.objectid > inum)
2123 goto out;
2124
2125 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2126 continue;
2127
2128 extent = btrfs_item_ptr(leaf, slot,
2129 struct btrfs_file_extent_item);
2130
2131 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2132 continue;
2133
2134 extent_offset = btrfs_file_extent_offset(leaf, extent);
2135 if (key.offset - extent_offset != offset)
2136 continue;
2137
2138 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2139 if (extent_offset >= old->extent_offset + old->offset +
2140 old->len || extent_offset + num_bytes <=
2141 old->extent_offset + old->offset)
2142 continue;
2143
2144 break;
2145 }
2146
2147 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2148 if (!backref) {
2149 ret = -ENOENT;
2150 goto out;
2151 }
2152
2153 backref->root_id = root_id;
2154 backref->inum = inum;
2155 backref->file_pos = offset + extent_offset;
2156 backref->num_bytes = num_bytes;
2157 backref->extent_offset = extent_offset;
2158 backref->generation = btrfs_file_extent_generation(leaf, extent);
2159 backref->old = old;
2160 backref_insert(&new->root, backref);
2161 old->count++;
2162out:
2163 btrfs_release_path(path);
2164 WARN_ON(ret);
2165 return ret;
2166}
2167
2168static noinline bool record_extent_backrefs(struct btrfs_path *path,
2169 struct new_sa_defrag_extent *new)
2170{
2171 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2172 struct old_sa_defrag_extent *old, *tmp;
2173 int ret;
2174
2175 new->path = path;
2176
2177 list_for_each_entry_safe(old, tmp, &new->head, list) {
2178 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2179 path, record_one_backref,
2180 old);
2181 BUG_ON(ret < 0 && ret != -ENOENT);
2182
2183 /* no backref to be processed for this extent */
2184 if (!old->count) {
2185 list_del(&old->list);
2186 kfree(old);
2187 }
2188 }
2189
2190 if (list_empty(&new->head))
2191 return false;
2192
2193 return true;
2194}
2195
2196static int relink_is_mergable(struct extent_buffer *leaf,
2197 struct btrfs_file_extent_item *fi,
2198 u64 disk_bytenr)
2199{
2200 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2201 return 0;
2202
2203 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2204 return 0;
2205
2206 if (btrfs_file_extent_compression(leaf, fi) ||
2207 btrfs_file_extent_encryption(leaf, fi) ||
2208 btrfs_file_extent_other_encoding(leaf, fi))
2209 return 0;
2210
2211 return 1;
2212}
2213
2214/*
2215 * Note the backref might has changed, and in this case we just return 0.
2216 */
2217static noinline int relink_extent_backref(struct btrfs_path *path,
2218 struct sa_defrag_extent_backref *prev,
2219 struct sa_defrag_extent_backref *backref)
2220{
2221 struct btrfs_file_extent_item *extent;
2222 struct btrfs_file_extent_item *item;
2223 struct btrfs_ordered_extent *ordered;
2224 struct btrfs_trans_handle *trans;
2225 struct btrfs_fs_info *fs_info;
2226 struct btrfs_root *root;
2227 struct btrfs_key key;
2228 struct extent_buffer *leaf;
2229 struct old_sa_defrag_extent *old = backref->old;
2230 struct new_sa_defrag_extent *new = old->new;
2231 struct inode *src_inode = new->inode;
2232 struct inode *inode;
2233 struct extent_state *cached = NULL;
2234 int ret = 0;
2235 u64 start;
2236 u64 len;
2237 u64 lock_start;
2238 u64 lock_end;
2239 bool merge = false;
2240 int index;
2241
2242 if (prev && prev->root_id == backref->root_id &&
2243 prev->inum == backref->inum &&
2244 prev->file_pos + prev->num_bytes == backref->file_pos)
2245 merge = true;
2246
2247 /* step 1: get root */
2248 key.objectid = backref->root_id;
2249 key.type = BTRFS_ROOT_ITEM_KEY;
2250 key.offset = (u64)-1;
2251
2252 fs_info = BTRFS_I(src_inode)->root->fs_info;
2253 index = srcu_read_lock(&fs_info->subvol_srcu);
2254
2255 root = btrfs_read_fs_root_no_name(fs_info, &key);
2256 if (IS_ERR(root)) {
2257 srcu_read_unlock(&fs_info->subvol_srcu, index);
2258 if (PTR_ERR(root) == -ENOENT)
2259 return 0;
2260 return PTR_ERR(root);
2261 }
2262 if (btrfs_root_refs(&root->root_item) == 0) {
2263 srcu_read_unlock(&fs_info->subvol_srcu, index);
2264 /* parse ENOENT to 0 */
2265 return 0;
2266 }
2267
2268 /* step 2: get inode */
2269 key.objectid = backref->inum;
2270 key.type = BTRFS_INODE_ITEM_KEY;
2271 key.offset = 0;
2272
2273 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2274 if (IS_ERR(inode)) {
2275 srcu_read_unlock(&fs_info->subvol_srcu, index);
2276 return 0;
2277 }
2278
2279 srcu_read_unlock(&fs_info->subvol_srcu, index);
2280
2281 /* step 3: relink backref */
2282 lock_start = backref->file_pos;
2283 lock_end = backref->file_pos + backref->num_bytes - 1;
2284 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2285 0, &cached);
2286
2287 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2288 if (ordered) {
2289 btrfs_put_ordered_extent(ordered);
2290 goto out_unlock;
2291 }
2292
2293 trans = btrfs_join_transaction(root);
2294 if (IS_ERR(trans)) {
2295 ret = PTR_ERR(trans);
2296 goto out_unlock;
2297 }
2298
2299 key.objectid = backref->inum;
2300 key.type = BTRFS_EXTENT_DATA_KEY;
2301 key.offset = backref->file_pos;
2302
2303 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2304 if (ret < 0) {
2305 goto out_free_path;
2306 } else if (ret > 0) {
2307 ret = 0;
2308 goto out_free_path;
2309 }
2310
2311 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2312 struct btrfs_file_extent_item);
2313
2314 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2315 backref->generation)
2316 goto out_free_path;
2317
2318 btrfs_release_path(path);
2319
2320 start = backref->file_pos;
2321 if (backref->extent_offset < old->extent_offset + old->offset)
2322 start += old->extent_offset + old->offset -
2323 backref->extent_offset;
2324
2325 len = min(backref->extent_offset + backref->num_bytes,
2326 old->extent_offset + old->offset + old->len);
2327 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2328
2329 ret = btrfs_drop_extents(trans, root, inode, start,
2330 start + len, 1);
2331 if (ret)
2332 goto out_free_path;
2333again:
2334 key.objectid = btrfs_ino(inode);
2335 key.type = BTRFS_EXTENT_DATA_KEY;
2336 key.offset = start;
2337
Liu Boa09a0a72013-03-11 09:20:58 +00002338 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002339 if (merge) {
2340 struct btrfs_file_extent_item *fi;
2341 u64 extent_len;
2342 struct btrfs_key found_key;
2343
2344 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2345 if (ret < 0)
2346 goto out_free_path;
2347
2348 path->slots[0]--;
2349 leaf = path->nodes[0];
2350 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2351
2352 fi = btrfs_item_ptr(leaf, path->slots[0],
2353 struct btrfs_file_extent_item);
2354 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2355
2356 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2357 extent_len + found_key.offset == start) {
2358 btrfs_set_file_extent_num_bytes(leaf, fi,
2359 extent_len + len);
2360 btrfs_mark_buffer_dirty(leaf);
2361 inode_add_bytes(inode, len);
2362
2363 ret = 1;
2364 goto out_free_path;
2365 } else {
2366 merge = false;
2367 btrfs_release_path(path);
2368 goto again;
2369 }
2370 }
2371
2372 ret = btrfs_insert_empty_item(trans, root, path, &key,
2373 sizeof(*extent));
2374 if (ret) {
2375 btrfs_abort_transaction(trans, root, ret);
2376 goto out_free_path;
2377 }
2378
2379 leaf = path->nodes[0];
2380 item = btrfs_item_ptr(leaf, path->slots[0],
2381 struct btrfs_file_extent_item);
2382 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2383 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2384 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2385 btrfs_set_file_extent_num_bytes(leaf, item, len);
2386 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2387 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2388 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2389 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2390 btrfs_set_file_extent_encryption(leaf, item, 0);
2391 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2392
2393 btrfs_mark_buffer_dirty(leaf);
2394 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002395 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002396
2397 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2398 new->disk_len, 0,
2399 backref->root_id, backref->inum,
2400 new->file_pos, 0); /* start - extent_offset */
2401 if (ret) {
2402 btrfs_abort_transaction(trans, root, ret);
2403 goto out_free_path;
2404 }
2405
2406 ret = 1;
2407out_free_path:
2408 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002409 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002410 btrfs_end_transaction(trans, root);
2411out_unlock:
2412 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2413 &cached, GFP_NOFS);
2414 iput(inode);
2415 return ret;
2416}
2417
2418static void relink_file_extents(struct new_sa_defrag_extent *new)
2419{
2420 struct btrfs_path *path;
2421 struct old_sa_defrag_extent *old, *tmp;
2422 struct sa_defrag_extent_backref *backref;
2423 struct sa_defrag_extent_backref *prev = NULL;
2424 struct inode *inode;
2425 struct btrfs_root *root;
2426 struct rb_node *node;
2427 int ret;
2428
2429 inode = new->inode;
2430 root = BTRFS_I(inode)->root;
2431
2432 path = btrfs_alloc_path();
2433 if (!path)
2434 return;
2435
2436 if (!record_extent_backrefs(path, new)) {
2437 btrfs_free_path(path);
2438 goto out;
2439 }
2440 btrfs_release_path(path);
2441
2442 while (1) {
2443 node = rb_first(&new->root);
2444 if (!node)
2445 break;
2446 rb_erase(node, &new->root);
2447
2448 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2449
2450 ret = relink_extent_backref(path, prev, backref);
2451 WARN_ON(ret < 0);
2452
2453 kfree(prev);
2454
2455 if (ret == 1)
2456 prev = backref;
2457 else
2458 prev = NULL;
2459 cond_resched();
2460 }
2461 kfree(prev);
2462
2463 btrfs_free_path(path);
2464
2465 list_for_each_entry_safe(old, tmp, &new->head, list) {
2466 list_del(&old->list);
2467 kfree(old);
2468 }
2469out:
2470 atomic_dec(&root->fs_info->defrag_running);
2471 wake_up(&root->fs_info->transaction_wait);
2472
2473 kfree(new);
2474}
2475
2476static struct new_sa_defrag_extent *
2477record_old_file_extents(struct inode *inode,
2478 struct btrfs_ordered_extent *ordered)
2479{
2480 struct btrfs_root *root = BTRFS_I(inode)->root;
2481 struct btrfs_path *path;
2482 struct btrfs_key key;
2483 struct old_sa_defrag_extent *old, *tmp;
2484 struct new_sa_defrag_extent *new;
2485 int ret;
2486
2487 new = kmalloc(sizeof(*new), GFP_NOFS);
2488 if (!new)
2489 return NULL;
2490
2491 new->inode = inode;
2492 new->file_pos = ordered->file_offset;
2493 new->len = ordered->len;
2494 new->bytenr = ordered->start;
2495 new->disk_len = ordered->disk_len;
2496 new->compress_type = ordered->compress_type;
2497 new->root = RB_ROOT;
2498 INIT_LIST_HEAD(&new->head);
2499
2500 path = btrfs_alloc_path();
2501 if (!path)
2502 goto out_kfree;
2503
2504 key.objectid = btrfs_ino(inode);
2505 key.type = BTRFS_EXTENT_DATA_KEY;
2506 key.offset = new->file_pos;
2507
2508 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2509 if (ret < 0)
2510 goto out_free_path;
2511 if (ret > 0 && path->slots[0] > 0)
2512 path->slots[0]--;
2513
2514 /* find out all the old extents for the file range */
2515 while (1) {
2516 struct btrfs_file_extent_item *extent;
2517 struct extent_buffer *l;
2518 int slot;
2519 u64 num_bytes;
2520 u64 offset;
2521 u64 end;
2522 u64 disk_bytenr;
2523 u64 extent_offset;
2524
2525 l = path->nodes[0];
2526 slot = path->slots[0];
2527
2528 if (slot >= btrfs_header_nritems(l)) {
2529 ret = btrfs_next_leaf(root, path);
2530 if (ret < 0)
2531 goto out_free_list;
2532 else if (ret > 0)
2533 break;
2534 continue;
2535 }
2536
2537 btrfs_item_key_to_cpu(l, &key, slot);
2538
2539 if (key.objectid != btrfs_ino(inode))
2540 break;
2541 if (key.type != BTRFS_EXTENT_DATA_KEY)
2542 break;
2543 if (key.offset >= new->file_pos + new->len)
2544 break;
2545
2546 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2547
2548 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2549 if (key.offset + num_bytes < new->file_pos)
2550 goto next;
2551
2552 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2553 if (!disk_bytenr)
2554 goto next;
2555
2556 extent_offset = btrfs_file_extent_offset(l, extent);
2557
2558 old = kmalloc(sizeof(*old), GFP_NOFS);
2559 if (!old)
2560 goto out_free_list;
2561
2562 offset = max(new->file_pos, key.offset);
2563 end = min(new->file_pos + new->len, key.offset + num_bytes);
2564
2565 old->bytenr = disk_bytenr;
2566 old->extent_offset = extent_offset;
2567 old->offset = offset - key.offset;
2568 old->len = end - offset;
2569 old->new = new;
2570 old->count = 0;
2571 list_add_tail(&old->list, &new->head);
2572next:
2573 path->slots[0]++;
2574 cond_resched();
2575 }
2576
2577 btrfs_free_path(path);
2578 atomic_inc(&root->fs_info->defrag_running);
2579
2580 return new;
2581
2582out_free_list:
2583 list_for_each_entry_safe(old, tmp, &new->head, list) {
2584 list_del(&old->list);
2585 kfree(old);
2586 }
2587out_free_path:
2588 btrfs_free_path(path);
2589out_kfree:
2590 kfree(new);
2591 return NULL;
2592}
2593
Chris Mason5d13a982009-03-13 11:41:46 -04002594/*
2595 * helper function for btrfs_finish_ordered_io, this
2596 * just reads in some of the csum leaves to prime them into ram
2597 * before we start the transaction. It limits the amount of btree
2598 * reads required while inside the transaction.
2599 */
Chris Masond352ac62008-09-29 15:18:18 -04002600/* as ordered data IO finishes, this gets called so we can finish
2601 * an ordered extent if the range of bytes in the file it covers are
2602 * fully written.
2603 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002604static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002605{
Josef Bacik5fd02042012-05-02 14:00:54 -04002606 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002607 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002608 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002609 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002610 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002611 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002612 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002613 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002614 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002615
Liu Bo83eea1f2012-07-10 05:28:39 -06002616 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002617
Josef Bacik5fd02042012-05-02 14:00:54 -04002618 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2619 ret = -EIO;
2620 goto out;
2621 }
2622
Yan, Zhengc2167752009-11-12 09:34:21 +00002623 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002624 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002625 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2626 if (nolock)
2627 trans = btrfs_join_transaction_nolock(root);
2628 else
2629 trans = btrfs_join_transaction(root);
2630 if (IS_ERR(trans)) {
2631 ret = PTR_ERR(trans);
2632 trans = NULL;
2633 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002634 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002635 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2636 ret = btrfs_update_inode_fallback(trans, root, inode);
2637 if (ret) /* -ENOMEM or corruption */
2638 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002639 goto out;
2640 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002641
Josef Bacik2ac55d42010-02-03 19:33:23 +00002642 lock_extent_bits(io_tree, ordered_extent->file_offset,
2643 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002644 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645
Liu Bo38c227d2013-01-29 03:18:40 +00002646 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2647 ordered_extent->file_offset + ordered_extent->len - 1,
2648 EXTENT_DEFRAG, 1, cached_state);
2649 if (ret) {
2650 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2651 if (last_snapshot >= BTRFS_I(inode)->generation)
2652 /* the inode is shared */
2653 new = record_old_file_extents(inode, ordered_extent);
2654
2655 clear_extent_bit(io_tree, ordered_extent->file_offset,
2656 ordered_extent->file_offset + ordered_extent->len - 1,
2657 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2658 }
2659
Josef Bacik0cb59c92010-07-02 12:14:14 -04002660 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002661 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002662 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002663 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002664 if (IS_ERR(trans)) {
2665 ret = PTR_ERR(trans);
2666 trans = NULL;
2667 goto out_unlock;
2668 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002669 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002670
Chris Masonc8b97812008-10-29 14:49:59 -04002671 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002672 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002673 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002674 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002675 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002676 ordered_extent->file_offset,
2677 ordered_extent->file_offset +
2678 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002679 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002680 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002681 ret = insert_reserved_file_extent(trans, inode,
2682 ordered_extent->file_offset,
2683 ordered_extent->start,
2684 ordered_extent->disk_len,
2685 ordered_extent->len,
2686 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002687 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002688 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002689 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002690 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2691 ordered_extent->file_offset, ordered_extent->len,
2692 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002693 if (ret < 0) {
2694 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002695 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002696 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002697
Chris Masone6dcd2d2008-07-17 12:53:50 -04002698 add_pending_csums(trans, inode, ordered_extent->file_offset,
2699 &ordered_extent->list);
2700
Josef Bacik6c760c02012-11-09 10:53:21 -05002701 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2702 ret = btrfs_update_inode_fallback(trans, root, inode);
2703 if (ret) { /* -ENOMEM or corruption */
2704 btrfs_abort_transaction(trans, root, ret);
2705 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002706 }
2707 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002708out_unlock:
2709 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2710 ordered_extent->file_offset +
2711 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002712out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002713 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002714 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002715 if (trans)
2716 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002717
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002718 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002719 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2720 ordered_extent->file_offset +
2721 ordered_extent->len - 1, NULL, GFP_NOFS);
2722
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002723 /*
2724 * If the ordered extent had an IOERR or something else went
2725 * wrong we need to return the space for this ordered extent
2726 * back to the allocator.
2727 */
2728 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2729 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2730 btrfs_free_reserved_extent(root, ordered_extent->start,
2731 ordered_extent->disk_len);
2732 }
2733
2734
Josef Bacik5fd02042012-05-02 14:00:54 -04002735 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002736 * This needs to be done to make sure anybody waiting knows we are done
2737 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002738 */
2739 btrfs_remove_ordered_extent(inode, ordered_extent);
2740
Liu Bo38c227d2013-01-29 03:18:40 +00002741 /* for snapshot-aware defrag */
2742 if (new)
2743 relink_file_extents(new);
2744
Chris Masone6dcd2d2008-07-17 12:53:50 -04002745 /* once for us */
2746 btrfs_put_ordered_extent(ordered_extent);
2747 /* once for the tree */
2748 btrfs_put_ordered_extent(ordered_extent);
2749
Josef Bacik5fd02042012-05-02 14:00:54 -04002750 return ret;
2751}
2752
2753static void finish_ordered_fn(struct btrfs_work *work)
2754{
2755 struct btrfs_ordered_extent *ordered_extent;
2756 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2757 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002758}
2759
Christoph Hellwigb2950862008-12-02 09:54:17 -05002760static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002761 struct extent_state *state, int uptodate)
2762{
Josef Bacik5fd02042012-05-02 14:00:54 -04002763 struct inode *inode = page->mapping->host;
2764 struct btrfs_root *root = BTRFS_I(inode)->root;
2765 struct btrfs_ordered_extent *ordered_extent = NULL;
2766 struct btrfs_workers *workers;
2767
liubo1abe9b82011-03-24 11:18:59 +00002768 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2769
Chris Mason8b62b722009-09-02 16:53:46 -04002770 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002771 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2772 end - start + 1, uptodate))
2773 return 0;
2774
2775 ordered_extent->work.func = finish_ordered_fn;
2776 ordered_extent->work.flags = 0;
2777
Liu Bo83eea1f2012-07-10 05:28:39 -06002778 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002779 workers = &root->fs_info->endio_freespace_worker;
2780 else
2781 workers = &root->fs_info->endio_write_workers;
2782 btrfs_queue_worker(workers, &ordered_extent->work);
2783
2784 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002785}
2786
Chris Masond352ac62008-09-29 15:18:18 -04002787/*
Chris Masond352ac62008-09-29 15:18:18 -04002788 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002789 * if there's a match, we allow the bio to finish. If not, the code in
2790 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002791 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002792static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002793 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002794{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002795 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002796 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002797 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002798 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002799 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002800 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002801 struct btrfs_root *root = BTRFS_I(inode)->root;
2802 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002803 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2804 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002805
Chris Masond20f7042008-12-08 16:58:54 -05002806 if (PageChecked(page)) {
2807 ClearPageChecked(page);
2808 goto good;
2809 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002810
2811 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002812 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002813
Yan Zheng17d217f2008-12-12 10:03:38 -05002814 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002815 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002816 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2817 GFP_NOFS);
2818 return 0;
2819 }
2820
Yanc2e639f2008-02-04 08:57:25 -05002821 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002822 private = state->private;
2823 ret = 0;
2824 } else {
2825 ret = get_state_private(io_tree, start, &private);
2826 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002827 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002828 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002829 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002830
Liu Bob0496682013-03-14 14:57:45 +00002831 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002832 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002833 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002834 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002835
Cong Wang7ac687d2011-11-25 23:14:28 +08002836 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002837good:
Chris Mason07157aa2007-08-30 08:50:51 -04002838 return 0;
2839
2840zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002841 if (__ratelimit(&_rs))
2842 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2843 (unsigned long long)btrfs_ino(page->mapping->host),
2844 (unsigned long long)start, csum,
2845 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002846 memset(kaddr + offset, 1, end - start + 1);
2847 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002848 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002849 if (private == 0)
2850 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002851 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002852}
Chris Masonb888db22007-08-27 16:49:44 -04002853
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002854struct delayed_iput {
2855 struct list_head list;
2856 struct inode *inode;
2857};
2858
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002859/* JDM: If this is fs-wide, why can't we add a pointer to
2860 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002861void btrfs_add_delayed_iput(struct inode *inode)
2862{
2863 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2864 struct delayed_iput *delayed;
2865
2866 if (atomic_add_unless(&inode->i_count, -1, 1))
2867 return;
2868
2869 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2870 delayed->inode = inode;
2871
2872 spin_lock(&fs_info->delayed_iput_lock);
2873 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2874 spin_unlock(&fs_info->delayed_iput_lock);
2875}
2876
2877void btrfs_run_delayed_iputs(struct btrfs_root *root)
2878{
2879 LIST_HEAD(list);
2880 struct btrfs_fs_info *fs_info = root->fs_info;
2881 struct delayed_iput *delayed;
2882 int empty;
2883
2884 spin_lock(&fs_info->delayed_iput_lock);
2885 empty = list_empty(&fs_info->delayed_iputs);
2886 spin_unlock(&fs_info->delayed_iput_lock);
2887 if (empty)
2888 return;
2889
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002890 spin_lock(&fs_info->delayed_iput_lock);
2891 list_splice_init(&fs_info->delayed_iputs, &list);
2892 spin_unlock(&fs_info->delayed_iput_lock);
2893
2894 while (!list_empty(&list)) {
2895 delayed = list_entry(list.next, struct delayed_iput, list);
2896 list_del(&delayed->list);
2897 iput(delayed->inode);
2898 kfree(delayed);
2899 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002900}
2901
Yan, Zhengd68fc572010-05-16 10:49:58 -04002902/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002903 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002904 * files in the subvolume, it removes orphan item and frees block_rsv
2905 * structure.
2906 */
2907void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2908 struct btrfs_root *root)
2909{
Josef Bacik90290e12011-12-02 15:44:12 -05002910 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002911 int ret;
2912
Josef Bacik8a35d952012-05-23 14:26:42 -04002913 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002914 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2915 return;
2916
Josef Bacik90290e12011-12-02 15:44:12 -05002917 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002918 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002919 spin_unlock(&root->orphan_lock);
2920 return;
2921 }
2922
2923 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2924 spin_unlock(&root->orphan_lock);
2925 return;
2926 }
2927
2928 block_rsv = root->orphan_block_rsv;
2929 root->orphan_block_rsv = NULL;
2930 spin_unlock(&root->orphan_lock);
2931
Yan, Zhengd68fc572010-05-16 10:49:58 -04002932 if (root->orphan_item_inserted &&
2933 btrfs_root_refs(&root->root_item) > 0) {
2934 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2935 root->root_key.objectid);
2936 BUG_ON(ret);
2937 root->orphan_item_inserted = 0;
2938 }
2939
Josef Bacik90290e12011-12-02 15:44:12 -05002940 if (block_rsv) {
2941 WARN_ON(block_rsv->size > 0);
2942 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002943 }
2944}
2945
2946/*
Josef Bacik7b128762008-07-24 12:17:14 -04002947 * This creates an orphan entry for the given inode in case something goes
2948 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002949 *
2950 * NOTE: caller of this function should reserve 5 units of metadata for
2951 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002952 */
2953int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2954{
2955 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002956 struct btrfs_block_rsv *block_rsv = NULL;
2957 int reserve = 0;
2958 int insert = 0;
2959 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002960
Yan, Zhengd68fc572010-05-16 10:49:58 -04002961 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002962 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002963 if (!block_rsv)
2964 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002965 }
2966
Yan, Zhengd68fc572010-05-16 10:49:58 -04002967 spin_lock(&root->orphan_lock);
2968 if (!root->orphan_block_rsv) {
2969 root->orphan_block_rsv = block_rsv;
2970 } else if (block_rsv) {
2971 btrfs_free_block_rsv(root, block_rsv);
2972 block_rsv = NULL;
2973 }
Josef Bacik7b128762008-07-24 12:17:14 -04002974
Josef Bacik8a35d952012-05-23 14:26:42 -04002975 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2976 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002977#if 0
2978 /*
2979 * For proper ENOSPC handling, we should do orphan
2980 * cleanup when mounting. But this introduces backward
2981 * compatibility issue.
2982 */
2983 if (!xchg(&root->orphan_item_inserted, 1))
2984 insert = 2;
2985 else
2986 insert = 1;
2987#endif
2988 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002989 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002990 }
Josef Bacik7b128762008-07-24 12:17:14 -04002991
Josef Bacik72ac3c02012-05-23 14:13:11 -04002992 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2993 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002994 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002995 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002996
Yan, Zhengd68fc572010-05-16 10:49:58 -04002997 /* grab metadata reservation from transaction handle */
2998 if (reserve) {
2999 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003000 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003001 }
3002
3003 /* insert an orphan item to track this unlinked/truncated file */
3004 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003005 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003006 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003007 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3008 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003009 btrfs_abort_transaction(trans, root, ret);
3010 return ret;
3011 }
3012 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003013 }
3014
3015 /* insert an orphan item to track subvolume contains orphan files */
3016 if (insert >= 2) {
3017 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3018 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003019 if (ret && ret != -EEXIST) {
3020 btrfs_abort_transaction(trans, root, ret);
3021 return ret;
3022 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003023 }
3024 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003025}
3026
3027/*
3028 * We have done the truncate/delete so we can go ahead and remove the orphan
3029 * item for this particular inode.
3030 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003031static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3032 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003033{
3034 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003035 int delete_item = 0;
3036 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003037 int ret = 0;
3038
Yan, Zhengd68fc572010-05-16 10:49:58 -04003039 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003040 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3041 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003042 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003043
Josef Bacik72ac3c02012-05-23 14:13:11 -04003044 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3045 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003046 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003047 spin_unlock(&root->orphan_lock);
3048
3049 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003050 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003051 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003052 }
3053
Josef Bacik8a35d952012-05-23 14:26:42 -04003054 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003055 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003056 atomic_dec(&root->orphan_inodes);
3057 }
Josef Bacik7b128762008-07-24 12:17:14 -04003058
Yan, Zhengd68fc572010-05-16 10:49:58 -04003059 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003060}
3061
3062/*
3063 * this cleans up any orphans that may be left on the list from the last use
3064 * of this root.
3065 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003066int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003067{
3068 struct btrfs_path *path;
3069 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003070 struct btrfs_key key, found_key;
3071 struct btrfs_trans_handle *trans;
3072 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003073 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003074 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3075
Yan, Zhengd68fc572010-05-16 10:49:58 -04003076 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003077 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003078
3079 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003080 if (!path) {
3081 ret = -ENOMEM;
3082 goto out;
3083 }
Josef Bacik7b128762008-07-24 12:17:14 -04003084 path->reada = -1;
3085
3086 key.objectid = BTRFS_ORPHAN_OBJECTID;
3087 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3088 key.offset = (u64)-1;
3089
Josef Bacik7b128762008-07-24 12:17:14 -04003090 while (1) {
3091 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003092 if (ret < 0)
3093 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003094
3095 /*
3096 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003097 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003098 * find the key and see if we have stuff that matches
3099 */
3100 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003101 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003102 if (path->slots[0] == 0)
3103 break;
3104 path->slots[0]--;
3105 }
3106
3107 /* pull out the item */
3108 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003109 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3110
3111 /* make sure the item matches what we want */
3112 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3113 break;
3114 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3115 break;
3116
3117 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003118 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003119
3120 /*
3121 * this is where we are basically btrfs_lookup, without the
3122 * crossing root thing. we store the inode number in the
3123 * offset of the orphan item.
3124 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003125
3126 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003127 btrfs_err(root->fs_info,
3128 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003129 ret = -EINVAL;
3130 goto out;
3131 }
3132
3133 last_objectid = found_key.offset;
3134
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003135 found_key.objectid = found_key.offset;
3136 found_key.type = BTRFS_INODE_ITEM_KEY;
3137 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003138 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003139 ret = PTR_RET(inode);
3140 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003141 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003142
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003143 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3144 struct btrfs_root *dead_root;
3145 struct btrfs_fs_info *fs_info = root->fs_info;
3146 int is_dead_root = 0;
3147
3148 /*
3149 * this is an orphan in the tree root. Currently these
3150 * could come from 2 sources:
3151 * a) a snapshot deletion in progress
3152 * b) a free space cache inode
3153 * We need to distinguish those two, as the snapshot
3154 * orphan must not get deleted.
3155 * find_dead_roots already ran before us, so if this
3156 * is a snapshot deletion, we should find the root
3157 * in the dead_roots list
3158 */
3159 spin_lock(&fs_info->trans_lock);
3160 list_for_each_entry(dead_root, &fs_info->dead_roots,
3161 root_list) {
3162 if (dead_root->root_key.objectid ==
3163 found_key.objectid) {
3164 is_dead_root = 1;
3165 break;
3166 }
3167 }
3168 spin_unlock(&fs_info->trans_lock);
3169 if (is_dead_root) {
3170 /* prevent this orphan from being found again */
3171 key.offset = found_key.objectid - 1;
3172 continue;
3173 }
3174 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003175 /*
3176 * Inode is already gone but the orphan item is still there,
3177 * kill the orphan item.
3178 */
3179 if (ret == -ESTALE) {
3180 trans = btrfs_start_transaction(root, 1);
3181 if (IS_ERR(trans)) {
3182 ret = PTR_ERR(trans);
3183 goto out;
3184 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003185 btrfs_debug(root->fs_info, "auto deleting %Lu",
3186 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003187 ret = btrfs_del_orphan_item(trans, root,
3188 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003189 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003190 btrfs_end_transaction(trans, root);
3191 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003192 }
Josef Bacik7b128762008-07-24 12:17:14 -04003193
Josef Bacik7b128762008-07-24 12:17:14 -04003194 /*
3195 * add this inode to the orphan list so btrfs_orphan_del does
3196 * the proper thing when we hit it
3197 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003198 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3199 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003200 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003201
Josef Bacik7b128762008-07-24 12:17:14 -04003202 /* if we have links, this was a truncate, lets do that */
3203 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003204 if (!S_ISREG(inode->i_mode)) {
3205 WARN_ON(1);
3206 iput(inode);
3207 continue;
3208 }
Josef Bacik7b128762008-07-24 12:17:14 -04003209 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003210
3211 /* 1 for the orphan item deletion. */
3212 trans = btrfs_start_transaction(root, 1);
3213 if (IS_ERR(trans)) {
3214 ret = PTR_ERR(trans);
3215 goto out;
3216 }
3217 ret = btrfs_orphan_add(trans, inode);
3218 btrfs_end_transaction(trans, root);
3219 if (ret)
3220 goto out;
3221
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003222 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003223 if (ret)
3224 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003225 } else {
3226 nr_unlink++;
3227 }
3228
3229 /* this will do delete_inode and everything for us */
3230 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003231 if (ret)
3232 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003233 }
Miao Xie3254c872011-11-10 20:45:05 -05003234 /* release the path since we're done with it */
3235 btrfs_release_path(path);
3236
Yan, Zhengd68fc572010-05-16 10:49:58 -04003237 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3238
3239 if (root->orphan_block_rsv)
3240 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3241 (u64)-1);
3242
3243 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003244 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003245 if (!IS_ERR(trans))
3246 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003247 }
Josef Bacik7b128762008-07-24 12:17:14 -04003248
3249 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003250 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003251 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003252 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003253
3254out:
3255 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003256 btrfs_crit(root->fs_info,
3257 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003258 btrfs_free_path(path);
3259 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003260}
3261
Chris Masond352ac62008-09-29 15:18:18 -04003262/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003263 * very simple check to peek ahead in the leaf looking for xattrs. If we
3264 * don't find any xattrs, we know there can't be any acls.
3265 *
3266 * slot is the slot the inode is in, objectid is the objectid of the inode
3267 */
3268static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3269 int slot, u64 objectid)
3270{
3271 u32 nritems = btrfs_header_nritems(leaf);
3272 struct btrfs_key found_key;
3273 int scanned = 0;
3274
3275 slot++;
3276 while (slot < nritems) {
3277 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3278
3279 /* we found a different objectid, there must not be acls */
3280 if (found_key.objectid != objectid)
3281 return 0;
3282
3283 /* we found an xattr, assume we've got an acl */
3284 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3285 return 1;
3286
3287 /*
3288 * we found a key greater than an xattr key, there can't
3289 * be any acls later on
3290 */
3291 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3292 return 0;
3293
3294 slot++;
3295 scanned++;
3296
3297 /*
3298 * it goes inode, inode backrefs, xattrs, extents,
3299 * so if there are a ton of hard links to an inode there can
3300 * be a lot of backrefs. Don't waste time searching too hard,
3301 * this is just an optimization
3302 */
3303 if (scanned >= 8)
3304 break;
3305 }
3306 /* we hit the end of the leaf before we found an xattr or
3307 * something larger than an xattr. We have to assume the inode
3308 * has acls
3309 */
3310 return 1;
3311}
3312
3313/*
Chris Masond352ac62008-09-29 15:18:18 -04003314 * read an inode from the btree into the in-memory inode
3315 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003316static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003317{
3318 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003319 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003320 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003321 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003322 struct btrfs_root *root = BTRFS_I(inode)->root;
3323 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003324 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003325 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003326 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003327 bool filled = false;
3328
3329 ret = btrfs_fill_inode(inode, &rdev);
3330 if (!ret)
3331 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003332
3333 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003334 if (!path)
3335 goto make_bad;
3336
Josef Bacikd90c7322011-05-17 09:50:54 -04003337 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003338 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003339
Chris Mason39279cc2007-06-12 06:35:45 -04003340 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003341 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003342 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003343
Chris Mason5f39d392007-10-15 16:14:19 -04003344 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003345
3346 if (filled)
3347 goto cache_acl;
3348
Chris Mason5f39d392007-10-15 16:14:19 -04003349 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3350 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003351 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003352 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003353 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3354 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003355 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003356
3357 tspec = btrfs_inode_atime(inode_item);
3358 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3359 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3360
3361 tspec = btrfs_inode_mtime(inode_item);
3362 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3363 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3364
3365 tspec = btrfs_inode_ctime(inode_item);
3366 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3367 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3368
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003369 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003370 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003371 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3372
3373 /*
3374 * If we were modified in the current generation and evicted from memory
3375 * and then re-read we need to do a full sync since we don't have any
3376 * idea about which extents were modified before we were evicted from
3377 * cache.
3378 */
3379 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3380 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3381 &BTRFS_I(inode)->runtime_flags);
3382
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003383 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003384 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003385 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003386 rdev = btrfs_inode_rdev(leaf, inode_item);
3387
Josef Bacikaec74772008-07-24 12:12:38 -04003388 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003389 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003390cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003391 /*
3392 * try to precache a NULL acl entry for files that don't have
3393 * any xattrs or acls
3394 */
Li Zefan33345d012011-04-20 10:31:50 +08003395 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3396 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003397 if (!maybe_acls)
3398 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003399
Chris Mason39279cc2007-06-12 06:35:45 -04003400 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003401
Chris Mason39279cc2007-06-12 06:35:45 -04003402 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003403 case S_IFREG:
3404 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003405 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003406 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003407 inode->i_fop = &btrfs_file_operations;
3408 inode->i_op = &btrfs_file_inode_operations;
3409 break;
3410 case S_IFDIR:
3411 inode->i_fop = &btrfs_dir_file_operations;
3412 if (root == root->fs_info->tree_root)
3413 inode->i_op = &btrfs_dir_ro_inode_operations;
3414 else
3415 inode->i_op = &btrfs_dir_inode_operations;
3416 break;
3417 case S_IFLNK:
3418 inode->i_op = &btrfs_symlink_inode_operations;
3419 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003420 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003421 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003422 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003423 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003424 init_special_inode(inode, inode->i_mode, rdev);
3425 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003426 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003427
3428 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003429 return;
3430
3431make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003432 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003433 make_bad_inode(inode);
3434}
3435
Chris Masond352ac62008-09-29 15:18:18 -04003436/*
3437 * given a leaf and an inode, copy the inode fields into the leaf
3438 */
Chris Masone02119d2008-09-05 16:13:11 -04003439static void fill_inode_item(struct btrfs_trans_handle *trans,
3440 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003441 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003442 struct inode *inode)
3443{
Liu Bo51fab692012-12-27 09:01:21 +00003444 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003445
Liu Bo51fab692012-12-27 09:01:21 +00003446 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003447
Liu Bo51fab692012-12-27 09:01:21 +00003448 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3449 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3450 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3451 &token);
3452 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3453 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003454
Liu Bo51fab692012-12-27 09:01:21 +00003455 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3456 inode->i_atime.tv_sec, &token);
3457 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3458 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003459
Liu Bo51fab692012-12-27 09:01:21 +00003460 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3461 inode->i_mtime.tv_sec, &token);
3462 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3463 inode->i_mtime.tv_nsec, &token);
3464
3465 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3466 inode->i_ctime.tv_sec, &token);
3467 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3468 inode->i_ctime.tv_nsec, &token);
3469
3470 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3471 &token);
3472 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3473 &token);
3474 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3475 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3476 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3477 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3478 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003479}
3480
Chris Masond352ac62008-09-29 15:18:18 -04003481/*
3482 * copy everything in the in-memory inode into the btree.
3483 */
Chris Mason21151332011-11-10 20:39:08 -05003484static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003485 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003486{
3487 struct btrfs_inode_item *inode_item;
3488 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003489 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003490 int ret;
3491
3492 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003493 if (!path)
3494 return -ENOMEM;
3495
Chris Masonb9473432009-03-13 11:00:37 -04003496 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003497 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3498 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003499 if (ret) {
3500 if (ret > 0)
3501 ret = -ENOENT;
3502 goto failed;
3503 }
3504
Chris Masonb4ce94d2009-02-04 09:25:08 -05003505 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003506 leaf = path->nodes[0];
3507 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003508 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003509
Chris Masone02119d2008-09-05 16:13:11 -04003510 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003511 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003512 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003513 ret = 0;
3514failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003515 btrfs_free_path(path);
3516 return ret;
3517}
3518
Chris Masond352ac62008-09-29 15:18:18 -04003519/*
Chris Mason21151332011-11-10 20:39:08 -05003520 * copy everything in the in-memory inode into the btree.
3521 */
3522noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3523 struct btrfs_root *root, struct inode *inode)
3524{
3525 int ret;
3526
3527 /*
3528 * If the inode is a free space inode, we can deadlock during commit
3529 * if we put it into the delayed code.
3530 *
3531 * The data relocation inode should also be directly updated
3532 * without delay
3533 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003534 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003535 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003536 btrfs_update_root_times(trans, root);
3537
Chris Mason21151332011-11-10 20:39:08 -05003538 ret = btrfs_delayed_update_inode(trans, root, inode);
3539 if (!ret)
3540 btrfs_set_inode_last_trans(trans, inode);
3541 return ret;
3542 }
3543
3544 return btrfs_update_inode_item(trans, root, inode);
3545}
3546
Josef Bacikbe6aef62012-10-22 15:43:12 -04003547noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3548 struct btrfs_root *root,
3549 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003550{
3551 int ret;
3552
3553 ret = btrfs_update_inode(trans, root, inode);
3554 if (ret == -ENOSPC)
3555 return btrfs_update_inode_item(trans, root, inode);
3556 return ret;
3557}
3558
3559/*
Chris Masond352ac62008-09-29 15:18:18 -04003560 * unlink helper that gets used here in inode.c and in the tree logging
3561 * recovery code. It remove a link in a directory with a given name, and
3562 * also drops the back refs in the inode to the directory
3563 */
Al Viro92986792011-03-04 17:14:37 +00003564static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3565 struct btrfs_root *root,
3566 struct inode *dir, struct inode *inode,
3567 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003568{
3569 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003570 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003571 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003572 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003573 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003574 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003575 u64 ino = btrfs_ino(inode);
3576 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003577
3578 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003579 if (!path) {
3580 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003581 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003582 }
3583
Chris Masonb9473432009-03-13 11:00:37 -04003584 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003585 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003586 name, name_len, -1);
3587 if (IS_ERR(di)) {
3588 ret = PTR_ERR(di);
3589 goto err;
3590 }
3591 if (!di) {
3592 ret = -ENOENT;
3593 goto err;
3594 }
Chris Mason5f39d392007-10-15 16:14:19 -04003595 leaf = path->nodes[0];
3596 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003597 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003598 if (ret)
3599 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003600 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003601
Li Zefan33345d012011-04-20 10:31:50 +08003602 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3603 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003604 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003605 btrfs_info(root->fs_info,
3606 "failed to delete reference to %.*s, inode %llu parent %llu",
3607 name_len, name,
3608 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003609 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003610 goto err;
3611 }
3612
Miao Xie16cdcec2011-04-22 18:12:22 +08003613 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003614 if (ret) {
3615 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003616 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003617 }
Chris Mason39279cc2007-06-12 06:35:45 -04003618
Chris Masone02119d2008-09-05 16:13:11 -04003619 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003620 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003621 if (ret != 0 && ret != -ENOENT) {
3622 btrfs_abort_transaction(trans, root, ret);
3623 goto err;
3624 }
Chris Masone02119d2008-09-05 16:13:11 -04003625
3626 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3627 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003628 if (ret == -ENOENT)
3629 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003630 else if (ret)
3631 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003632err:
3633 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003634 if (ret)
3635 goto out;
3636
3637 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003638 inode_inc_iversion(inode);
3639 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003640 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003641 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003642out:
Chris Mason39279cc2007-06-12 06:35:45 -04003643 return ret;
3644}
3645
Al Viro92986792011-03-04 17:14:37 +00003646int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3647 struct btrfs_root *root,
3648 struct inode *dir, struct inode *inode,
3649 const char *name, int name_len)
3650{
3651 int ret;
3652 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3653 if (!ret) {
3654 btrfs_drop_nlink(inode);
3655 ret = btrfs_update_inode(trans, root, inode);
3656 }
3657 return ret;
3658}
3659
3660
Yan, Zhenga22285a2010-05-16 10:48:46 -04003661/* helper to check if there is any shared block in the path */
3662static int check_path_shared(struct btrfs_root *root,
3663 struct btrfs_path *path)
3664{
3665 struct extent_buffer *eb;
3666 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00003667 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668
3669 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003670 int ret;
3671
Yan, Zhenga22285a2010-05-16 10:48:46 -04003672 if (!path->nodes[level])
3673 break;
3674 eb = path->nodes[level];
3675 if (!btrfs_block_can_be_shared(root, eb))
3676 continue;
Josef Bacik3173a182013-03-07 14:22:04 -05003677 ret = btrfs_lookup_extent_info(NULL, root, eb->start, level, 1,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003678 &refs, NULL);
3679 if (refs > 1)
3680 return 1;
3681 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003682 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003683}
3684
3685/*
3686 * helper to start transaction for unlink and rmdir.
3687 *
3688 * unlink and rmdir are special in btrfs, they do not always free space.
3689 * so in enospc case, we should make sure they will free space before
3690 * allowing them to use the global metadata reservation.
3691 */
3692static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3693 struct dentry *dentry)
3694{
3695 struct btrfs_trans_handle *trans;
3696 struct btrfs_root *root = BTRFS_I(dir)->root;
3697 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003698 struct btrfs_dir_item *di;
3699 struct inode *inode = dentry->d_inode;
3700 u64 index;
3701 int check_link = 1;
3702 int err = -ENOSPC;
3703 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003704 u64 ino = btrfs_ino(inode);
3705 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003706
Josef Bacike70bea52011-10-11 14:18:24 -04003707 /*
3708 * 1 for the possible orphan item
3709 * 1 for the dir item
3710 * 1 for the dir index
3711 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003712 * 1 for the inode
3713 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003714 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003715 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3716 return trans;
3717
Li Zefan33345d012011-04-20 10:31:50 +08003718 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003719 return ERR_PTR(-ENOSPC);
3720
3721 /* check if there is someone else holds reference */
3722 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3723 return ERR_PTR(-ENOSPC);
3724
3725 if (atomic_read(&inode->i_count) > 2)
3726 return ERR_PTR(-ENOSPC);
3727
3728 if (xchg(&root->fs_info->enospc_unlink, 1))
3729 return ERR_PTR(-ENOSPC);
3730
3731 path = btrfs_alloc_path();
3732 if (!path) {
3733 root->fs_info->enospc_unlink = 0;
3734 return ERR_PTR(-ENOMEM);
3735 }
3736
Josef Bacik3880a1b2011-10-14 14:46:51 -04003737 /* 1 for the orphan item */
3738 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003739 if (IS_ERR(trans)) {
3740 btrfs_free_path(path);
3741 root->fs_info->enospc_unlink = 0;
3742 return trans;
3743 }
3744
3745 path->skip_locking = 1;
3746 path->search_commit_root = 1;
3747
3748 ret = btrfs_lookup_inode(trans, root, path,
3749 &BTRFS_I(dir)->location, 0);
3750 if (ret < 0) {
3751 err = ret;
3752 goto out;
3753 }
3754 if (ret == 0) {
3755 if (check_path_shared(root, path))
3756 goto out;
3757 } else {
3758 check_link = 0;
3759 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003760 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003761
3762 ret = btrfs_lookup_inode(trans, root, path,
3763 &BTRFS_I(inode)->location, 0);
3764 if (ret < 0) {
3765 err = ret;
3766 goto out;
3767 }
3768 if (ret == 0) {
3769 if (check_path_shared(root, path))
3770 goto out;
3771 } else {
3772 check_link = 0;
3773 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003774 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003775
3776 if (ret == 0 && S_ISREG(inode->i_mode)) {
3777 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003778 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003779 if (ret < 0) {
3780 err = ret;
3781 goto out;
3782 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003783 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003784 if (check_path_shared(root, path))
3785 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003786 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003787 }
3788
3789 if (!check_link) {
3790 err = 0;
3791 goto out;
3792 }
3793
Li Zefan33345d012011-04-20 10:31:50 +08003794 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003795 dentry->d_name.name, dentry->d_name.len, 0);
3796 if (IS_ERR(di)) {
3797 err = PTR_ERR(di);
3798 goto out;
3799 }
3800 if (di) {
3801 if (check_path_shared(root, path))
3802 goto out;
3803 } else {
3804 err = 0;
3805 goto out;
3806 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003807 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003808
Mark Fashehf1863732012-08-08 11:32:27 -07003809 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3810 dentry->d_name.len, ino, dir_ino, 0,
3811 &index);
3812 if (ret) {
3813 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003814 goto out;
3815 }
Mark Fashehf1863732012-08-08 11:32:27 -07003816
Yan, Zhenga22285a2010-05-16 10:48:46 -04003817 if (check_path_shared(root, path))
3818 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003819
David Sterbab3b4aa72011-04-21 01:20:15 +02003820 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003821
Miao Xie16cdcec2011-04-22 18:12:22 +08003822 /*
3823 * This is a commit root search, if we can lookup inode item and other
3824 * relative items in the commit root, it means the transaction of
3825 * dir/file creation has been committed, and the dir index item that we
3826 * delay to insert has also been inserted into the commit root. So
3827 * we needn't worry about the delayed insertion of the dir index item
3828 * here.
3829 */
Li Zefan33345d012011-04-20 10:31:50 +08003830 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003831 dentry->d_name.name, dentry->d_name.len, 0);
3832 if (IS_ERR(di)) {
3833 err = PTR_ERR(di);
3834 goto out;
3835 }
3836 BUG_ON(ret == -ENOENT);
3837 if (check_path_shared(root, path))
3838 goto out;
3839
3840 err = 0;
3841out:
3842 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003843 /* Migrate the orphan reservation over */
3844 if (!err)
3845 err = btrfs_block_rsv_migrate(trans->block_rsv,
3846 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003847 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003848
Yan, Zhenga22285a2010-05-16 10:48:46 -04003849 if (err) {
3850 btrfs_end_transaction(trans, root);
3851 root->fs_info->enospc_unlink = 0;
3852 return ERR_PTR(err);
3853 }
3854
3855 trans->block_rsv = &root->fs_info->global_block_rsv;
3856 return trans;
3857}
3858
3859static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3860 struct btrfs_root *root)
3861{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003862 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003863 btrfs_block_rsv_release(root, trans->block_rsv,
3864 trans->bytes_reserved);
3865 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003866 BUG_ON(!root->fs_info->enospc_unlink);
3867 root->fs_info->enospc_unlink = 0;
3868 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003869 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003870}
3871
Chris Mason39279cc2007-06-12 06:35:45 -04003872static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3873{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003874 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003875 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003876 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003877 int ret;
3878
Yan, Zhenga22285a2010-05-16 10:48:46 -04003879 trans = __unlink_start_trans(dir, dentry);
3880 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003881 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003882
Chris Mason12fcfd22009-03-24 10:24:20 -04003883 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3884
Chris Masone02119d2008-09-05 16:13:11 -04003885 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3886 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003887 if (ret)
3888 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003889
Yan, Zhenga22285a2010-05-16 10:48:46 -04003890 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003891 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003892 if (ret)
3893 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003894 }
Josef Bacik7b128762008-07-24 12:17:14 -04003895
Tsutomu Itohb5324022011-07-19 07:27:20 +00003896out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003897 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003898 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003899 return ret;
3900}
3901
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003902int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3903 struct btrfs_root *root,
3904 struct inode *dir, u64 objectid,
3905 const char *name, int name_len)
3906{
3907 struct btrfs_path *path;
3908 struct extent_buffer *leaf;
3909 struct btrfs_dir_item *di;
3910 struct btrfs_key key;
3911 u64 index;
3912 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003913 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003914
3915 path = btrfs_alloc_path();
3916 if (!path)
3917 return -ENOMEM;
3918
Li Zefan33345d012011-04-20 10:31:50 +08003919 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003920 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003921 if (IS_ERR_OR_NULL(di)) {
3922 if (!di)
3923 ret = -ENOENT;
3924 else
3925 ret = PTR_ERR(di);
3926 goto out;
3927 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003928
3929 leaf = path->nodes[0];
3930 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3931 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3932 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003933 if (ret) {
3934 btrfs_abort_transaction(trans, root, ret);
3935 goto out;
3936 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003937 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003938
3939 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3940 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003941 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003942 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003943 if (ret != -ENOENT) {
3944 btrfs_abort_transaction(trans, root, ret);
3945 goto out;
3946 }
Li Zefan33345d012011-04-20 10:31:50 +08003947 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003948 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003949 if (IS_ERR_OR_NULL(di)) {
3950 if (!di)
3951 ret = -ENOENT;
3952 else
3953 ret = PTR_ERR(di);
3954 btrfs_abort_transaction(trans, root, ret);
3955 goto out;
3956 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003957
3958 leaf = path->nodes[0];
3959 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003960 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003961 index = key.offset;
3962 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003963 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003964
Miao Xie16cdcec2011-04-22 18:12:22 +08003965 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003966 if (ret) {
3967 btrfs_abort_transaction(trans, root, ret);
3968 goto out;
3969 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003970
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003971 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003972 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003973 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003974 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003975 if (ret)
3976 btrfs_abort_transaction(trans, root, ret);
3977out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003978 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003979 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003980}
3981
Chris Mason39279cc2007-06-12 06:35:45 -04003982static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3983{
3984 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003985 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003986 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003987 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003988
David Sterbab3ae2442012-09-13 16:04:34 -06003989 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003990 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003991 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3992 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003993
Yan, Zhenga22285a2010-05-16 10:48:46 -04003994 trans = __unlink_start_trans(dir, dentry);
3995 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003996 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003997
Li Zefan33345d012011-04-20 10:31:50 +08003998 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003999 err = btrfs_unlink_subvol(trans, root, dir,
4000 BTRFS_I(inode)->location.objectid,
4001 dentry->d_name.name,
4002 dentry->d_name.len);
4003 goto out;
4004 }
4005
Josef Bacik7b128762008-07-24 12:17:14 -04004006 err = btrfs_orphan_add(trans, inode);
4007 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004008 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004009
Chris Mason39279cc2007-06-12 06:35:45 -04004010 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004011 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4012 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004013 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004014 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004015out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04004016 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004017 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004018
Chris Mason39279cc2007-06-12 06:35:45 -04004019 return err;
4020}
4021
Chris Mason323ac952008-10-01 19:05:46 -04004022/*
Chris Mason39279cc2007-06-12 06:35:45 -04004023 * this can truncate away extent items, csum items and directory items.
4024 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004025 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004026 *
4027 * csum items that cross the new i_size are truncated to the new size
4028 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004029 *
4030 * min_type is the minimum key type to truncate down to. If set to 0, this
4031 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004032 */
Yan, Zheng80825102009-11-12 09:35:36 +00004033int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4034 struct btrfs_root *root,
4035 struct inode *inode,
4036 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004037{
Chris Mason39279cc2007-06-12 06:35:45 -04004038 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004039 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004040 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004041 struct btrfs_key key;
4042 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004043 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004044 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004045 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004046 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004047 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004048 int found_extent;
4049 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004050 int pending_del_nr = 0;
4051 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004052 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004053 int ret;
4054 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004055 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004056
4057 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004058
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004059 path = btrfs_alloc_path();
4060 if (!path)
4061 return -ENOMEM;
4062 path->reada = -1;
4063
Josef Bacik5dc562c2012-08-17 13:14:17 -04004064 /*
4065 * We want to drop from the next block forward in case this new size is
4066 * not block aligned since we will be keeping the last block of the
4067 * extent just the way it is.
4068 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004069 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004070 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4071 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004072
Miao Xie16cdcec2011-04-22 18:12:22 +08004073 /*
4074 * This function is also used to drop the items in the log tree before
4075 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4076 * it is used to drop the loged items. So we shouldn't kill the delayed
4077 * items.
4078 */
4079 if (min_type == 0 && root == BTRFS_I(inode)->root)
4080 btrfs_kill_delayed_inode_items(inode);
4081
Li Zefan33345d012011-04-20 10:31:50 +08004082 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004083 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004084 key.type = (u8)-1;
4085
Chris Mason85e21ba2008-01-29 15:11:36 -05004086search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004087 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004088 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004089 if (ret < 0) {
4090 err = ret;
4091 goto out;
4092 }
Chris Masond3977122009-01-05 21:25:51 -05004093
Chris Mason85e21ba2008-01-29 15:11:36 -05004094 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004095 /* there are no items in the tree for us to truncate, we're
4096 * done
4097 */
Yan, Zheng80825102009-11-12 09:35:36 +00004098 if (path->slots[0] == 0)
4099 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004100 path->slots[0]--;
4101 }
4102
Chris Masond3977122009-01-05 21:25:51 -05004103 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004104 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004105 leaf = path->nodes[0];
4106 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4107 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004108
Li Zefan33345d012011-04-20 10:31:50 +08004109 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004110 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004111
Chris Mason85e21ba2008-01-29 15:11:36 -05004112 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004113 break;
4114
Chris Mason5f39d392007-10-15 16:14:19 -04004115 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004116 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004117 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004118 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004119 extent_type = btrfs_file_extent_type(leaf, fi);
4120 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004121 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004122 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004123 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004124 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004125 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004126 }
Yan008630c2007-11-07 13:31:09 -05004127 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004128 }
Yan, Zheng80825102009-11-12 09:35:36 +00004129 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004130 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004131 } else {
4132 if (item_end < new_size)
4133 break;
4134 if (found_key.offset >= new_size)
4135 del_item = 1;
4136 else
4137 del_item = 0;
4138 }
Chris Mason39279cc2007-06-12 06:35:45 -04004139 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004140 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004141 if (found_type != BTRFS_EXTENT_DATA_KEY)
4142 goto delete;
4143
4144 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004145 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004146 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004147 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004148 u64 orig_num_bytes =
4149 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004150 extent_num_bytes = ALIGN(new_size -
4151 found_key.offset,
4152 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004153 btrfs_set_file_extent_num_bytes(leaf, fi,
4154 extent_num_bytes);
4155 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004156 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004157 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004158 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004159 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004160 } else {
Chris Masondb945352007-10-15 16:15:53 -04004161 extent_num_bytes =
4162 btrfs_file_extent_disk_num_bytes(leaf,
4163 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004164 extent_offset = found_key.offset -
4165 btrfs_file_extent_offset(leaf, fi);
4166
Chris Mason39279cc2007-06-12 06:35:45 -04004167 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004168 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004169 if (extent_start != 0) {
4170 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004171 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004172 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004173 }
4174 }
Chris Mason90692182008-02-08 13:49:28 -05004175 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004176 /*
4177 * we can't truncate inline items that have had
4178 * special encodings
4179 */
4180 if (!del_item &&
4181 btrfs_file_extent_compression(leaf, fi) == 0 &&
4182 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4183 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004184 u32 size = new_size - found_key.offset;
4185
4186 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004187 inode_sub_bytes(inode, item_end + 1 -
4188 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004189 }
4190 size =
4191 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004192 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004193 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004194 inode_sub_bytes(inode, item_end + 1 -
4195 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004196 }
Chris Mason39279cc2007-06-12 06:35:45 -04004197 }
Chris Mason179e29e2007-11-01 11:28:41 -04004198delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004199 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004200 if (!pending_del_nr) {
4201 /* no pending yet, add ourselves */
4202 pending_del_slot = path->slots[0];
4203 pending_del_nr = 1;
4204 } else if (pending_del_nr &&
4205 path->slots[0] + 1 == pending_del_slot) {
4206 /* hop on the pending chunk */
4207 pending_del_nr++;
4208 pending_del_slot = path->slots[0];
4209 } else {
Chris Masond3977122009-01-05 21:25:51 -05004210 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004211 }
Chris Mason39279cc2007-06-12 06:35:45 -04004212 } else {
4213 break;
4214 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004215 if (found_extent && (root->ref_cows ||
4216 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004217 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004218 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004219 extent_num_bytes, 0,
4220 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004221 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004222 BUG_ON(ret);
4223 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004224
Yan, Zheng80825102009-11-12 09:35:36 +00004225 if (found_type == BTRFS_INODE_ITEM_KEY)
4226 break;
4227
4228 if (path->slots[0] == 0 ||
4229 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004230 if (pending_del_nr) {
4231 ret = btrfs_del_items(trans, root, path,
4232 pending_del_slot,
4233 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004234 if (ret) {
4235 btrfs_abort_transaction(trans,
4236 root, ret);
4237 goto error;
4238 }
Yan, Zheng80825102009-11-12 09:35:36 +00004239 pending_del_nr = 0;
4240 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004241 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004242 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004243 } else {
4244 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004245 }
Chris Mason39279cc2007-06-12 06:35:45 -04004246 }
Yan, Zheng80825102009-11-12 09:35:36 +00004247out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004248 if (pending_del_nr) {
4249 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4250 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004251 if (ret)
4252 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004253 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004254error:
Chris Mason39279cc2007-06-12 06:35:45 -04004255 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004256 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004257}
4258
Chris Masona52d9a82007-08-27 16:49:44 -04004259/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004260 * btrfs_truncate_page - read, zero a chunk and write a page
4261 * @inode - inode that we're zeroing
4262 * @from - the offset to start zeroing
4263 * @len - the length to zero, 0 to zero the entire range respective to the
4264 * offset
4265 * @front - zero up to the offset instead of from the offset on
4266 *
4267 * This will find the page for the "from" offset and cow the page and zero the
4268 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004269 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004270int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4271 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004272{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004273 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004274 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004275 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4276 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004277 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004278 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004279 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004280 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4281 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4282 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004283 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004284 int ret = 0;
4285 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004286 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004287
Josef Bacik2aaa6652012-08-29 14:27:18 -04004288 if ((offset & (blocksize - 1)) == 0 &&
4289 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004290 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004291 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004292 if (ret)
4293 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004294
Chris Mason211c17f2008-05-15 09:13:45 -04004295again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004296 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004297 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004298 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004299 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004300 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004301 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004302
4303 page_start = page_offset(page);
4304 page_end = page_start + PAGE_CACHE_SIZE - 1;
4305
Chris Masona52d9a82007-08-27 16:49:44 -04004306 if (!PageUptodate(page)) {
4307 ret = btrfs_readpage(NULL, page);
4308 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004309 if (page->mapping != mapping) {
4310 unlock_page(page);
4311 page_cache_release(page);
4312 goto again;
4313 }
Chris Masona52d9a82007-08-27 16:49:44 -04004314 if (!PageUptodate(page)) {
4315 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004316 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004317 }
4318 }
Chris Mason211c17f2008-05-15 09:13:45 -04004319 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004320
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004321 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004322 set_page_extent_mapped(page);
4323
4324 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4325 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004326 unlock_extent_cached(io_tree, page_start, page_end,
4327 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004328 unlock_page(page);
4329 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004330 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004331 btrfs_put_ordered_extent(ordered);
4332 goto again;
4333 }
4334
Josef Bacik2ac55d42010-02-03 19:33:23 +00004335 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004336 EXTENT_DIRTY | EXTENT_DELALLOC |
4337 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004338 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004339
Josef Bacik2ac55d42010-02-03 19:33:23 +00004340 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4341 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004342 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004343 unlock_extent_cached(io_tree, page_start, page_end,
4344 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004345 goto out_unlock;
4346 }
4347
Chris Masone6dcd2d2008-07-17 12:53:50 -04004348 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004349 if (!len)
4350 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004351 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004352 if (front)
4353 memset(kaddr, 0, offset);
4354 else
4355 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004356 flush_dcache_page(page);
4357 kunmap(page);
4358 }
Chris Mason247e7432008-07-17 12:53:51 -04004359 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004360 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004361 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4362 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004363
Chris Mason89642222008-07-24 09:41:53 -04004364out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004365 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004366 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004367 unlock_page(page);
4368 page_cache_release(page);
4369out:
4370 return ret;
4371}
4372
Josef Bacik695a0d02011-03-04 15:46:53 -05004373/*
4374 * This function puts in dummy file extents for the area we're creating a hole
4375 * for. So if we are truncating this file to a larger size we need to insert
4376 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4377 * the range between oldsize and size
4378 */
Josef Bacika41ad392011-01-31 15:30:16 -05004379int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004380{
4381 struct btrfs_trans_handle *trans;
4382 struct btrfs_root *root = BTRFS_I(inode)->root;
4383 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004384 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004385 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004386 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004387 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4388 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004389 u64 last_byte;
4390 u64 cur_offset;
4391 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004392 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004393
4394 if (size <= hole_start)
4395 return 0;
4396
Yan Zheng9036c102008-10-30 14:19:41 -04004397 while (1) {
4398 struct btrfs_ordered_extent *ordered;
4399 btrfs_wait_ordered_range(inode, hole_start,
4400 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004401 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004402 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004403 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4404 if (!ordered)
4405 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004406 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4407 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004408 btrfs_put_ordered_extent(ordered);
4409 }
4410
Yan Zheng9036c102008-10-30 14:19:41 -04004411 cur_offset = hole_start;
4412 while (1) {
4413 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4414 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004415 if (IS_ERR(em)) {
4416 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004417 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004418 break;
4419 }
Yan Zheng9036c102008-10-30 14:19:41 -04004420 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004421 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004422 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004423 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004424 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004425
Miao Xie36423202011-12-14 20:12:02 -05004426 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004427 if (IS_ERR(trans)) {
4428 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004429 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004430 }
Yan, Zheng80825102009-11-12 09:35:36 +00004431
Josef Bacik5dc562c2012-08-17 13:14:17 -04004432 err = btrfs_drop_extents(trans, root, inode,
4433 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004434 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004435 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004436 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004437 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004438 break;
Miao Xie5b397372011-09-11 10:52:24 -04004439 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004440
Yan Zheng9036c102008-10-30 14:19:41 -04004441 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004442 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004443 0, hole_size, 0, hole_size,
4444 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004445 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004446 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004447 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004448 break;
Miao Xie5b397372011-09-11 10:52:24 -04004449 }
Yan, Zheng80825102009-11-12 09:35:36 +00004450
Josef Bacik5dc562c2012-08-17 13:14:17 -04004451 btrfs_drop_extent_cache(inode, cur_offset,
4452 cur_offset + hole_size - 1, 0);
4453 hole_em = alloc_extent_map();
4454 if (!hole_em) {
4455 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4456 &BTRFS_I(inode)->runtime_flags);
4457 goto next;
4458 }
4459 hole_em->start = cur_offset;
4460 hole_em->len = hole_size;
4461 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004462
Josef Bacik5dc562c2012-08-17 13:14:17 -04004463 hole_em->block_start = EXTENT_MAP_HOLE;
4464 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004465 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004466 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004467 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4468 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4469 hole_em->generation = trans->transid;
4470
4471 while (1) {
4472 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004473 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004474 write_unlock(&em_tree->lock);
4475 if (err != -EEXIST)
4476 break;
4477 btrfs_drop_extent_cache(inode, cur_offset,
4478 cur_offset +
4479 hole_size - 1, 0);
4480 }
4481 free_extent_map(hole_em);
4482next:
Miao Xie36423202011-12-14 20:12:02 -05004483 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004484 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004485 }
4486 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004487 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004488 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004489 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004490 break;
4491 }
4492
Yan, Zhenga22285a2010-05-16 10:48:46 -04004493 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004494 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4495 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004496 return err;
4497}
4498
Eric Sandeen3972f262013-01-12 02:57:22 +00004499static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004500{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004501 struct btrfs_root *root = BTRFS_I(inode)->root;
4502 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004503 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004504 loff_t newsize = attr->ia_size;
4505 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004506 int ret;
4507
Josef Bacika41ad392011-01-31 15:30:16 -05004508 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004509 return 0;
4510
Eric Sandeen3972f262013-01-12 02:57:22 +00004511 /*
4512 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4513 * special case where we need to update the times despite not having
4514 * these flags set. For all other operations the VFS set these flags
4515 * explicitly if it wants a timestamp update.
4516 */
4517 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4518 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4519
Josef Bacika41ad392011-01-31 15:30:16 -05004520 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004521 truncate_pagecache(inode, oldsize, newsize);
4522 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004523 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004524 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004525
Miao Xief4a2f4c2011-12-14 20:12:01 -05004526 trans = btrfs_start_transaction(root, 1);
4527 if (IS_ERR(trans))
4528 return PTR_ERR(trans);
4529
4530 i_size_write(inode, newsize);
4531 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4532 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004533 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004534 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004535
Josef Bacika41ad392011-01-31 15:30:16 -05004536 /*
4537 * We're truncating a file that used to have good data down to
4538 * zero. Make sure it gets into the ordered flush list so that
4539 * any new writes get down to disk quickly.
4540 */
4541 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004542 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4543 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004544
Josef Bacikf3fe8202013-01-07 17:03:21 -05004545 /*
4546 * 1 for the orphan item we're going to add
4547 * 1 for the orphan item deletion.
4548 */
4549 trans = btrfs_start_transaction(root, 2);
4550 if (IS_ERR(trans))
4551 return PTR_ERR(trans);
4552
4553 /*
4554 * We need to do this in case we fail at _any_ point during the
4555 * actual truncate. Once we do the truncate_setsize we could
4556 * invalidate pages which forces any outstanding ordered io to
4557 * be instantly completed which will give us extents that need
4558 * to be truncated. If we fail to get an orphan inode down we
4559 * could have left over extents that were never meant to live,
4560 * so we need to garuntee from this point on that everything
4561 * will be consistent.
4562 */
4563 ret = btrfs_orphan_add(trans, inode);
4564 btrfs_end_transaction(trans, root);
4565 if (ret)
4566 return ret;
4567
Josef Bacika41ad392011-01-31 15:30:16 -05004568 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4569 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004570
4571 /* Disable nonlocked read DIO to avoid the end less truncate */
4572 btrfs_inode_block_unlocked_dio(inode);
4573 inode_dio_wait(inode);
4574 btrfs_inode_resume_unlocked_dio(inode);
4575
Josef Bacika41ad392011-01-31 15:30:16 -05004576 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004577 if (ret && inode->i_nlink)
4578 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004579 }
4580
Josef Bacika41ad392011-01-31 15:30:16 -05004581 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004582}
4583
Chris Mason39279cc2007-06-12 06:35:45 -04004584static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4585{
4586 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004587 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004588 int err;
4589
Li Zefanb83cc962010-12-20 16:04:08 +08004590 if (btrfs_root_readonly(root))
4591 return -EROFS;
4592
Chris Mason39279cc2007-06-12 06:35:45 -04004593 err = inode_change_ok(inode, attr);
4594 if (err)
4595 return err;
4596
Chris Mason5a3f23d2009-03-31 13:27:11 -04004597 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004598 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004599 if (err)
4600 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004601 }
Yan Zheng9036c102008-10-30 14:19:41 -04004602
Christoph Hellwig10257742010-06-04 11:30:02 +02004603 if (attr->ia_valid) {
4604 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004605 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004606 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004607
Josef Bacik22c44fe2011-11-30 10:45:38 -05004608 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004609 err = btrfs_acl_chmod(inode);
4610 }
4611
Chris Mason39279cc2007-06-12 06:35:45 -04004612 return err;
4613}
Chris Mason61295eb2008-01-14 16:24:38 -05004614
Al Virobd555972010-06-07 11:35:40 -04004615void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004616{
4617 struct btrfs_trans_handle *trans;
4618 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004619 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004620 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004621 int ret;
4622
liubo1abe9b82011-03-24 11:18:59 +00004623 trace_btrfs_inode_evict(inode);
4624
Chris Mason39279cc2007-06-12 06:35:45 -04004625 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004626 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004627 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004628 goto no_delete;
4629
Chris Mason39279cc2007-06-12 06:35:45 -04004630 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004631 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004632 goto no_delete;
4633 }
Al Virobd555972010-06-07 11:35:40 -04004634 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004635 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004636
Yan, Zhengc71bf092009-11-12 09:34:40 +00004637 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004638 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004639 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004640 goto no_delete;
4641 }
4642
Yan, Zheng76dda932009-09-21 16:00:26 -04004643 if (inode->i_nlink > 0) {
4644 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4645 goto no_delete;
4646 }
4647
Miao Xie0e8c36a2012-12-19 06:59:51 +00004648 ret = btrfs_commit_inode_delayed_inode(inode);
4649 if (ret) {
4650 btrfs_orphan_del(NULL, inode);
4651 goto no_delete;
4652 }
4653
Miao Xie66d8f3d2012-09-06 04:02:28 -06004654 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004655 if (!rsv) {
4656 btrfs_orphan_del(NULL, inode);
4657 goto no_delete;
4658 }
Josef Bacik4a338542011-08-29 11:01:31 -04004659 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004660 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004661 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004662
Chris Masondbe674a2008-07-17 12:54:05 -04004663 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004664
Josef Bacik4289a662011-08-05 13:22:24 -04004665 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004666 * This is a bit simpler than btrfs_truncate since we've already
4667 * reserved our space for our orphan item in the unlink, so we just
4668 * need to reserve some slack space in case we add bytes and update
4669 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004670 */
Yan, Zheng80825102009-11-12 09:35:36 +00004671 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004672 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4673 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004674
Josef Bacik726c35f2011-09-26 15:46:06 -04004675 /*
4676 * Try and steal from the global reserve since we will
4677 * likely not use this space anyway, we want to try as
4678 * hard as possible to get this to work.
4679 */
4680 if (ret)
4681 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4682
Yan, Zhengd68fc572010-05-16 10:49:58 -04004683 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004684 btrfs_warn(root->fs_info,
4685 "Could not get space for a delete, will truncate on mount %d",
4686 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004687 btrfs_orphan_del(NULL, inode);
4688 btrfs_free_block_rsv(root, rsv);
4689 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004690 }
4691
Miao Xie0e8c36a2012-12-19 06:59:51 +00004692 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004693 if (IS_ERR(trans)) {
4694 btrfs_orphan_del(NULL, inode);
4695 btrfs_free_block_rsv(root, rsv);
4696 goto no_delete;
4697 }
4698
4699 trans->block_rsv = rsv;
4700
Yan, Zhengd68fc572010-05-16 10:49:58 -04004701 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004702 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004703 break;
4704
Miao Xie8407aa42012-09-07 01:43:32 -06004705 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004706 btrfs_end_transaction(trans, root);
4707 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004708 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004709 }
4710
Josef Bacik4289a662011-08-05 13:22:24 -04004711 btrfs_free_block_rsv(root, rsv);
4712
Yan, Zheng80825102009-11-12 09:35:36 +00004713 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004714 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004715 ret = btrfs_orphan_del(trans, inode);
4716 BUG_ON(ret);
4717 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004718
Josef Bacik4289a662011-08-05 13:22:24 -04004719 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004720 if (!(root == root->fs_info->tree_root ||
4721 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004722 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004723
Chris Mason54aa1f42007-06-22 14:16:25 -04004724 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004725 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004726no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004727 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004728 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004729}
4730
4731/*
4732 * this returns the key found in the dir entry in the location pointer.
4733 * If no dir entries were found, location->objectid is 0.
4734 */
4735static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4736 struct btrfs_key *location)
4737{
4738 const char *name = dentry->d_name.name;
4739 int namelen = dentry->d_name.len;
4740 struct btrfs_dir_item *di;
4741 struct btrfs_path *path;
4742 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004743 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004744
4745 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004746 if (!path)
4747 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004748
Li Zefan33345d012011-04-20 10:31:50 +08004749 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004750 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004751 if (IS_ERR(di))
4752 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004753
David Sterbac7040052011-04-19 18:00:01 +02004754 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004755 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004756
Chris Mason5f39d392007-10-15 16:14:19 -04004757 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004758out:
Chris Mason39279cc2007-06-12 06:35:45 -04004759 btrfs_free_path(path);
4760 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004761out_err:
4762 location->objectid = 0;
4763 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004764}
4765
4766/*
4767 * when we hit a tree root in a directory, the btrfs part of the inode
4768 * needs to be changed to reflect the root directory of the tree root. This
4769 * is kind of like crossing a mount point.
4770 */
4771static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004772 struct inode *dir,
4773 struct dentry *dentry,
4774 struct btrfs_key *location,
4775 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004776{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004777 struct btrfs_path *path;
4778 struct btrfs_root *new_root;
4779 struct btrfs_root_ref *ref;
4780 struct extent_buffer *leaf;
4781 int ret;
4782 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004783
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004784 path = btrfs_alloc_path();
4785 if (!path) {
4786 err = -ENOMEM;
4787 goto out;
4788 }
Chris Mason39279cc2007-06-12 06:35:45 -04004789
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004790 err = -ENOENT;
4791 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4792 BTRFS_I(dir)->root->root_key.objectid,
4793 location->objectid);
4794 if (ret) {
4795 if (ret < 0)
4796 err = ret;
4797 goto out;
4798 }
Chris Mason39279cc2007-06-12 06:35:45 -04004799
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004800 leaf = path->nodes[0];
4801 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004802 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004803 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4804 goto out;
4805
4806 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4807 (unsigned long)(ref + 1),
4808 dentry->d_name.len);
4809 if (ret)
4810 goto out;
4811
David Sterbab3b4aa72011-04-21 01:20:15 +02004812 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004813
4814 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4815 if (IS_ERR(new_root)) {
4816 err = PTR_ERR(new_root);
4817 goto out;
4818 }
4819
4820 if (btrfs_root_refs(&new_root->root_item) == 0) {
4821 err = -ENOENT;
4822 goto out;
4823 }
4824
4825 *sub_root = new_root;
4826 location->objectid = btrfs_root_dirid(&new_root->root_item);
4827 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004828 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004829 err = 0;
4830out:
4831 btrfs_free_path(path);
4832 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004833}
4834
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004835static void inode_tree_add(struct inode *inode)
4836{
4837 struct btrfs_root *root = BTRFS_I(inode)->root;
4838 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004839 struct rb_node **p;
4840 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004841 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004842again:
4843 p = &root->inode_tree.rb_node;
4844 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004845
Al Viro1d3382cb2010-10-23 15:19:20 -04004846 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004847 return;
4848
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004849 spin_lock(&root->inode_lock);
4850 while (*p) {
4851 parent = *p;
4852 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4853
Li Zefan33345d012011-04-20 10:31:50 +08004854 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004855 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004856 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004857 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004858 else {
4859 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004860 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004861 rb_erase(parent, &root->inode_tree);
4862 RB_CLEAR_NODE(parent);
4863 spin_unlock(&root->inode_lock);
4864 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004865 }
4866 }
4867 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4868 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4869 spin_unlock(&root->inode_lock);
4870}
4871
4872static void inode_tree_del(struct inode *inode)
4873{
4874 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004875 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004876
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004877 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004878 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004879 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004880 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004881 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004882 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004883 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004884
Josef Bacik0af3d002010-06-21 14:48:16 -04004885 /*
4886 * Free space cache has inodes in the tree root, but the tree root has a
4887 * root_refs of 0, so this could end up dropping the tree root as a
4888 * snapshot, so we need the extra !root->fs_info->tree_root check to
4889 * make sure we don't drop it.
4890 */
4891 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4892 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004893 synchronize_srcu(&root->fs_info->subvol_srcu);
4894 spin_lock(&root->inode_lock);
4895 empty = RB_EMPTY_ROOT(&root->inode_tree);
4896 spin_unlock(&root->inode_lock);
4897 if (empty)
4898 btrfs_add_dead_root(root);
4899 }
4900}
4901
Jeff Mahoney143bede2012-03-01 14:56:26 +01004902void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004903{
4904 struct rb_node *node;
4905 struct rb_node *prev;
4906 struct btrfs_inode *entry;
4907 struct inode *inode;
4908 u64 objectid = 0;
4909
4910 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4911
4912 spin_lock(&root->inode_lock);
4913again:
4914 node = root->inode_tree.rb_node;
4915 prev = NULL;
4916 while (node) {
4917 prev = node;
4918 entry = rb_entry(node, struct btrfs_inode, rb_node);
4919
Li Zefan33345d012011-04-20 10:31:50 +08004920 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004921 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004922 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004923 node = node->rb_right;
4924 else
4925 break;
4926 }
4927 if (!node) {
4928 while (prev) {
4929 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004930 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004931 node = prev;
4932 break;
4933 }
4934 prev = rb_next(prev);
4935 }
4936 }
4937 while (node) {
4938 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004939 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004940 inode = igrab(&entry->vfs_inode);
4941 if (inode) {
4942 spin_unlock(&root->inode_lock);
4943 if (atomic_read(&inode->i_count) > 1)
4944 d_prune_aliases(inode);
4945 /*
Al Viro45321ac2010-06-07 13:43:19 -04004946 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004947 * the inode cache when its usage count
4948 * hits zero.
4949 */
4950 iput(inode);
4951 cond_resched();
4952 spin_lock(&root->inode_lock);
4953 goto again;
4954 }
4955
4956 if (cond_resched_lock(&root->inode_lock))
4957 goto again;
4958
4959 node = rb_next(node);
4960 }
4961 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004962}
4963
Chris Masone02119d2008-09-05 16:13:11 -04004964static int btrfs_init_locked_inode(struct inode *inode, void *p)
4965{
4966 struct btrfs_iget_args *args = p;
4967 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004968 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004969 return 0;
4970}
4971
4972static int btrfs_find_actor(struct inode *inode, void *opaque)
4973{
4974 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004975 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004976 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004977}
4978
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004979static struct inode *btrfs_iget_locked(struct super_block *s,
4980 u64 objectid,
4981 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004982{
4983 struct inode *inode;
4984 struct btrfs_iget_args args;
4985 args.ino = objectid;
4986 args.root = root;
4987
4988 inode = iget5_locked(s, objectid, btrfs_find_actor,
4989 btrfs_init_locked_inode,
4990 (void *)&args);
4991 return inode;
4992}
4993
Balaji Rao1a54ef82008-07-21 02:01:04 +05304994/* Get an inode object given its location and corresponding root.
4995 * Returns in *is_new if the inode was read from disk
4996 */
4997struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004998 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304999{
5000 struct inode *inode;
5001
5002 inode = btrfs_iget_locked(s, location->objectid, root);
5003 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005004 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305005
5006 if (inode->i_state & I_NEW) {
5007 BTRFS_I(inode)->root = root;
5008 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
5009 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005010 if (!is_bad_inode(inode)) {
5011 inode_tree_add(inode);
5012 unlock_new_inode(inode);
5013 if (new)
5014 *new = 1;
5015 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005016 unlock_new_inode(inode);
5017 iput(inode);
5018 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005019 }
5020 }
5021
Balaji Rao1a54ef82008-07-21 02:01:04 +05305022 return inode;
5023}
5024
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005025static struct inode *new_simple_dir(struct super_block *s,
5026 struct btrfs_key *key,
5027 struct btrfs_root *root)
5028{
5029 struct inode *inode = new_inode(s);
5030
5031 if (!inode)
5032 return ERR_PTR(-ENOMEM);
5033
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005034 BTRFS_I(inode)->root = root;
5035 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005036 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005037
5038 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005039 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005040 inode->i_fop = &simple_dir_operations;
5041 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5042 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5043
5044 return inode;
5045}
5046
Chris Mason3de45862008-11-17 21:02:50 -05005047struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005048{
Chris Masond3977122009-01-05 21:25:51 -05005049 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005050 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005051 struct btrfs_root *sub_root = root;
5052 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005053 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005054 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005055
5056 if (dentry->d_name.len > BTRFS_NAME_LEN)
5057 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005058
Jeff Layton39e3c952012-11-28 11:30:53 -05005059 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005060 if (ret < 0)
5061 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005062
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005063 if (location.objectid == 0)
5064 return NULL;
5065
5066 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005067 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005068 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005069 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005070
5071 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5072
Yan, Zheng76dda932009-09-21 16:00:26 -04005073 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005074 ret = fixup_tree_root_location(root, dir, dentry,
5075 &location, &sub_root);
5076 if (ret < 0) {
5077 if (ret != -ENOENT)
5078 inode = ERR_PTR(ret);
5079 else
5080 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5081 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005082 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005083 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005084 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5085
Julia Lawall34d19ba2011-01-24 19:55:19 +00005086 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005087 down_read(&root->fs_info->cleanup_work_sem);
5088 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005089 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005090 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005091 if (ret)
5092 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005093 }
5094
Chris Mason3de45862008-11-17 21:02:50 -05005095 return inode;
5096}
5097
Nick Pigginfe15ce42011-01-07 17:49:23 +11005098static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005099{
5100 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005101 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005102
Li Zefan848cce02012-02-21 17:04:28 +08005103 if (!inode && !IS_ROOT(dentry))
5104 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005105
Li Zefan848cce02012-02-21 17:04:28 +08005106 if (inode) {
5107 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005108 if (btrfs_root_refs(&root->root_item) == 0)
5109 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005110
5111 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5112 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005113 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005114 return 0;
5115}
5116
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005117static void btrfs_dentry_release(struct dentry *dentry)
5118{
5119 if (dentry->d_fsdata)
5120 kfree(dentry->d_fsdata);
5121}
5122
Chris Mason3de45862008-11-17 21:02:50 -05005123static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005124 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005125{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005126 struct dentry *ret;
5127
5128 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005129 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005130}
5131
Miao Xie16cdcec2011-04-22 18:12:22 +08005132unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005133 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5134};
5135
David Woodhousecbdf5a22008-08-06 19:42:33 +01005136static int btrfs_real_readdir(struct file *filp, void *dirent,
5137 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005138{
Al Viro496ad9a2013-01-23 17:07:38 -05005139 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005140 struct btrfs_root *root = BTRFS_I(inode)->root;
5141 struct btrfs_item *item;
5142 struct btrfs_dir_item *di;
5143 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005144 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005145 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005146 struct list_head ins_list;
5147 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005148 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005149 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005150 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005151 unsigned char d_type;
5152 int over = 0;
5153 u32 di_cur;
5154 u32 di_total;
5155 u32 di_len;
5156 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005157 char tmp_name[32];
5158 char *name_ptr;
5159 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005160 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005161
5162 /* FIXME, use a real flag for deciding about the key type */
5163 if (root->fs_info->tree_root == root)
5164 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005165
Chris Mason39544012007-12-12 14:38:19 -05005166 /* special case for "." */
5167 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005168 over = filldir(dirent, ".", 1,
5169 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005170 if (over)
5171 return 0;
5172 filp->f_pos = 1;
5173 }
Chris Mason39544012007-12-12 14:38:19 -05005174 /* special case for .., just use the back ref */
5175 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005176 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005177 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005178 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005179 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005180 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005181 filp->f_pos = 2;
5182 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005183 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005184 if (!path)
5185 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005186
Josef Bacik026fd312011-05-13 10:32:11 -04005187 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005188
Miao Xie16cdcec2011-04-22 18:12:22 +08005189 if (key_type == BTRFS_DIR_INDEX_KEY) {
5190 INIT_LIST_HEAD(&ins_list);
5191 INIT_LIST_HEAD(&del_list);
5192 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5193 }
5194
Chris Mason39279cc2007-06-12 06:35:45 -04005195 btrfs_set_key_type(&key, key_type);
5196 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005197 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005198
Chris Mason39279cc2007-06-12 06:35:45 -04005199 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5200 if (ret < 0)
5201 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005202
5203 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005204 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005205 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005206 if (slot >= btrfs_header_nritems(leaf)) {
5207 ret = btrfs_next_leaf(root, path);
5208 if (ret < 0)
5209 goto err;
5210 else if (ret > 0)
5211 break;
5212 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005213 }
Chris Mason3de45862008-11-17 21:02:50 -05005214
Chris Mason5f39d392007-10-15 16:14:19 -04005215 item = btrfs_item_nr(leaf, slot);
5216 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5217
5218 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005219 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005220 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005221 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005222 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005223 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005224 if (key_type == BTRFS_DIR_INDEX_KEY &&
5225 btrfs_should_delete_dir_index(&del_list,
5226 found_key.offset))
5227 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005228
5229 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005230 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005231
Chris Mason39279cc2007-06-12 06:35:45 -04005232 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5233 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005234 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005235
5236 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005237 struct btrfs_key location;
5238
Josef Bacik22a94d42011-03-16 16:47:17 -04005239 if (verify_dir_item(root, leaf, di))
5240 break;
5241
Chris Mason5f39d392007-10-15 16:14:19 -04005242 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005243 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005244 name_ptr = tmp_name;
5245 } else {
5246 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005247 if (!name_ptr) {
5248 ret = -ENOMEM;
5249 goto err;
5250 }
Chris Mason5f39d392007-10-15 16:14:19 -04005251 }
5252 read_extent_buffer(leaf, name_ptr,
5253 (unsigned long)(di + 1), name_len);
5254
5255 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5256 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005257
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005258
Chris Mason3de45862008-11-17 21:02:50 -05005259 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005260 * skip it.
5261 *
5262 * In contrast to old kernels, we insert the snapshot's
5263 * dir item and dir index after it has been created, so
5264 * we won't find a reference to our own snapshot. We
5265 * still keep the following code for backward
5266 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005267 */
5268 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5269 location.objectid == root->root_key.objectid) {
5270 over = 0;
5271 goto skip;
5272 }
Chris Mason5f39d392007-10-15 16:14:19 -04005273 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005274 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005275 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005276
Chris Mason3de45862008-11-17 21:02:50 -05005277skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005278 if (name_ptr != tmp_name)
5279 kfree(name_ptr);
5280
Chris Mason39279cc2007-06-12 06:35:45 -04005281 if (over)
5282 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005283 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005284 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005285 di_cur += di_len;
5286 di = (struct btrfs_dir_item *)((char *)di + di_len);
5287 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005288next:
5289 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005290 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005291
Miao Xie16cdcec2011-04-22 18:12:22 +08005292 if (key_type == BTRFS_DIR_INDEX_KEY) {
5293 if (is_curr)
5294 filp->f_pos++;
5295 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5296 &ins_list);
5297 if (ret)
5298 goto nopos;
5299 }
5300
David Woodhouse49593bf2008-08-17 17:08:36 +01005301 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005302 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005303 /*
5304 * 32-bit glibc will use getdents64, but then strtol -
5305 * so the last number we can serve is this.
5306 */
5307 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005308 else
5309 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005310nopos:
5311 ret = 0;
5312err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005313 if (key_type == BTRFS_DIR_INDEX_KEY)
5314 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005315 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005316 return ret;
5317}
5318
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005319int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005320{
5321 struct btrfs_root *root = BTRFS_I(inode)->root;
5322 struct btrfs_trans_handle *trans;
5323 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005324 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005325
Josef Bacik72ac3c02012-05-23 14:13:11 -04005326 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005327 return 0;
5328
Liu Bo83eea1f2012-07-10 05:28:39 -06005329 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005330 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005331
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005332 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005333 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005334 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005335 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005336 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005337 if (IS_ERR(trans))
5338 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005339 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005340 }
5341 return ret;
5342}
5343
5344/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005345 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005346 * inode changes. But, it is most likely to find the inode in cache.
5347 * FIXME, needs more benchmarking...there are no reasons other than performance
5348 * to keep or drop this code.
5349 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005350static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005351{
5352 struct btrfs_root *root = BTRFS_I(inode)->root;
5353 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005354 int ret;
5355
Josef Bacik72ac3c02012-05-23 14:13:11 -04005356 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005357 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005358
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005359 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005360 if (IS_ERR(trans))
5361 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005362
5363 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005364 if (ret && ret == -ENOSPC) {
5365 /* whoops, lets try again with the full transaction */
5366 btrfs_end_transaction(trans, root);
5367 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005368 if (IS_ERR(trans))
5369 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005370
Chris Mason94b60442010-05-26 11:02:00 -04005371 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005372 }
Chris Mason39279cc2007-06-12 06:35:45 -04005373 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005374 if (BTRFS_I(inode)->delayed_node)
5375 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005376
5377 return ret;
5378}
5379
5380/*
5381 * This is a copy of file_update_time. We need this so we can return error on
5382 * ENOSPC for updating the inode in the case of file write and mmap writes.
5383 */
Josef Bacike41f9412012-03-26 09:46:47 -04005384static int btrfs_update_time(struct inode *inode, struct timespec *now,
5385 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005386{
Alexander Block2bc5565282012-06-15 09:49:33 +02005387 struct btrfs_root *root = BTRFS_I(inode)->root;
5388
5389 if (btrfs_root_readonly(root))
5390 return -EROFS;
5391
Josef Bacike41f9412012-03-26 09:46:47 -04005392 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005393 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005394 if (flags & S_CTIME)
5395 inode->i_ctime = *now;
5396 if (flags & S_MTIME)
5397 inode->i_mtime = *now;
5398 if (flags & S_ATIME)
5399 inode->i_atime = *now;
5400 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005401}
5402
Chris Masond352ac62008-09-29 15:18:18 -04005403/*
5404 * find the highest existing sequence number in a directory
5405 * and then set the in-memory index_cnt variable to reflect
5406 * free sequence numbers
5407 */
Josef Bacikaec74772008-07-24 12:12:38 -04005408static int btrfs_set_inode_index_count(struct inode *inode)
5409{
5410 struct btrfs_root *root = BTRFS_I(inode)->root;
5411 struct btrfs_key key, found_key;
5412 struct btrfs_path *path;
5413 struct extent_buffer *leaf;
5414 int ret;
5415
Li Zefan33345d012011-04-20 10:31:50 +08005416 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005417 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5418 key.offset = (u64)-1;
5419
5420 path = btrfs_alloc_path();
5421 if (!path)
5422 return -ENOMEM;
5423
5424 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5425 if (ret < 0)
5426 goto out;
5427 /* FIXME: we should be able to handle this */
5428 if (ret == 0)
5429 goto out;
5430 ret = 0;
5431
5432 /*
5433 * MAGIC NUMBER EXPLANATION:
5434 * since we search a directory based on f_pos we have to start at 2
5435 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5436 * else has to start at 2
5437 */
5438 if (path->slots[0] == 0) {
5439 BTRFS_I(inode)->index_cnt = 2;
5440 goto out;
5441 }
5442
5443 path->slots[0]--;
5444
5445 leaf = path->nodes[0];
5446 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5447
Li Zefan33345d012011-04-20 10:31:50 +08005448 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005449 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5450 BTRFS_I(inode)->index_cnt = 2;
5451 goto out;
5452 }
5453
5454 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5455out:
5456 btrfs_free_path(path);
5457 return ret;
5458}
5459
Chris Masond352ac62008-09-29 15:18:18 -04005460/*
5461 * helper to find a free sequence number in a given directory. This current
5462 * code is very simple, later versions will do smarter things in the btree
5463 */
Chris Mason3de45862008-11-17 21:02:50 -05005464int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005465{
5466 int ret = 0;
5467
5468 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005469 ret = btrfs_inode_delayed_dir_index_count(dir);
5470 if (ret) {
5471 ret = btrfs_set_inode_index_count(dir);
5472 if (ret)
5473 return ret;
5474 }
Josef Bacikaec74772008-07-24 12:12:38 -04005475 }
5476
Chris Mason00e4e6b2008-08-05 11:18:09 -04005477 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005478 BTRFS_I(dir)->index_cnt++;
5479
5480 return ret;
5481}
5482
Chris Mason39279cc2007-06-12 06:35:45 -04005483static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5484 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005485 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005486 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005487 u64 ref_objectid, u64 objectid,
5488 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005489{
5490 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005491 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005492 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005493 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005494 struct btrfs_inode_ref *ref;
5495 struct btrfs_key key[2];
5496 u32 sizes[2];
5497 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005498 int ret;
5499 int owner;
5500
Chris Mason5f39d392007-10-15 16:14:19 -04005501 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005502 if (!path)
5503 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005504
Chris Mason39279cc2007-06-12 06:35:45 -04005505 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005506 if (!inode) {
5507 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005508 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005509 }
Chris Mason39279cc2007-06-12 06:35:45 -04005510
Li Zefan581bb052011-04-20 10:06:11 +08005511 /*
5512 * we have to initialize this early, so we can reclaim the inode
5513 * number if we fail afterwards in this function.
5514 */
5515 inode->i_ino = objectid;
5516
Josef Bacikaec74772008-07-24 12:12:38 -04005517 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005518 trace_btrfs_inode_request(dir);
5519
Chris Mason3de45862008-11-17 21:02:50 -05005520 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005521 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005522 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005523 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005524 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005525 }
Josef Bacikaec74772008-07-24 12:12:38 -04005526 }
5527 /*
5528 * index_cnt is ignored for everything but a dir,
5529 * btrfs_get_inode_index_count has an explanation for the magic
5530 * number
5531 */
5532 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005533 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005534 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005535 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005536
Josef Bacik5dc562c2012-08-17 13:14:17 -04005537 /*
5538 * We could have gotten an inode number from somebody who was fsynced
5539 * and then removed in this same transaction, so let's just set full
5540 * sync since it will be a full sync anyway and this will blow away the
5541 * old info in the log.
5542 */
5543 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5544
Al Viro569254b2011-07-24 17:08:40 -04005545 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005546 owner = 0;
5547 else
5548 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005549
5550 key[0].objectid = objectid;
5551 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5552 key[0].offset = 0;
5553
Mark Fashehf1863732012-08-08 11:32:27 -07005554 /*
5555 * Start new inodes with an inode_ref. This is slightly more
5556 * efficient for small numbers of hard links since they will
5557 * be packed into one item. Extended refs will kick in if we
5558 * add more hard links than can fit in the ref item.
5559 */
Chris Mason9c583092008-01-29 15:15:18 -05005560 key[1].objectid = objectid;
5561 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5562 key[1].offset = ref_objectid;
5563
5564 sizes[0] = sizeof(struct btrfs_inode_item);
5565 sizes[1] = name_len + sizeof(*ref);
5566
Chris Masonb9473432009-03-13 11:00:37 -04005567 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005568 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5569 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005570 goto fail;
5571
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005572 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005573 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005574 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005575 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5576 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005577 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5578 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005579 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005580
5581 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5582 struct btrfs_inode_ref);
5583 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005584 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005585 ptr = (unsigned long)(ref + 1);
5586 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5587
Chris Mason5f39d392007-10-15 16:14:19 -04005588 btrfs_mark_buffer_dirty(path->nodes[0]);
5589 btrfs_free_path(path);
5590
Chris Mason39279cc2007-06-12 06:35:45 -04005591 location = &BTRFS_I(inode)->location;
5592 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005593 location->offset = 0;
5594 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5595
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005596 btrfs_inherit_iflags(inode, dir);
5597
Al Viro569254b2011-07-24 17:08:40 -04005598 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005599 if (btrfs_test_opt(root, NODATASUM))
5600 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005601 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005602 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5603 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005604 }
5605
Chris Mason39279cc2007-06-12 06:35:45 -04005606 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005608
5609 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005610 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005611
Alexander Block8ea05e32012-07-25 17:35:53 +02005612 btrfs_update_root_times(trans, root);
5613
Chris Mason39279cc2007-06-12 06:35:45 -04005614 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005615fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005616 if (dir)
5617 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005618 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005619 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005620 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005621}
5622
5623static inline u8 btrfs_inode_type(struct inode *inode)
5624{
5625 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5626}
5627
Chris Masond352ac62008-09-29 15:18:18 -04005628/*
5629 * utility function to add 'inode' into 'parent_inode' with
5630 * a give name and a given sequence number.
5631 * if 'add_backref' is true, also insert a backref from the
5632 * inode to the parent directory.
5633 */
Chris Masone02119d2008-09-05 16:13:11 -04005634int btrfs_add_link(struct btrfs_trans_handle *trans,
5635 struct inode *parent_inode, struct inode *inode,
5636 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005637{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005639 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005640 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005641 u64 ino = btrfs_ino(inode);
5642 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005643
Li Zefan33345d012011-04-20 10:31:50 +08005644 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005645 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5646 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005647 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005648 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5649 key.offset = 0;
5650 }
Chris Mason39279cc2007-06-12 06:35:45 -04005651
Li Zefan33345d012011-04-20 10:31:50 +08005652 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005653 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5654 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005655 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005656 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005657 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5658 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005659 }
5660
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005661 /* Nothing to clean up yet */
5662 if (ret)
5663 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005664
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005665 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5666 parent_inode, &key,
5667 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005668 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005669 goto fail_dir_item;
5670 else if (ret) {
5671 btrfs_abort_transaction(trans, root, ret);
5672 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005673 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005674
5675 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5676 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005677 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005678 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5679 ret = btrfs_update_inode(trans, root, parent_inode);
5680 if (ret)
5681 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005682 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005683
5684fail_dir_item:
5685 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5686 u64 local_index;
5687 int err;
5688 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5689 key.objectid, root->root_key.objectid,
5690 parent_ino, &local_index, name, name_len);
5691
5692 } else if (add_backref) {
5693 u64 local_index;
5694 int err;
5695
5696 err = btrfs_del_inode_ref(trans, root, name, name_len,
5697 ino, parent_ino, &local_index);
5698 }
5699 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005700}
5701
5702static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005703 struct inode *dir, struct dentry *dentry,
5704 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005705{
Josef Bacika1b075d2010-11-19 20:36:11 +00005706 int err = btrfs_add_link(trans, dir, inode,
5707 dentry->d_name.name, dentry->d_name.len,
5708 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005709 if (err > 0)
5710 err = -EEXIST;
5711 return err;
5712}
5713
Josef Bacik618e21d2007-07-11 10:18:17 -04005714static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005715 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005716{
5717 struct btrfs_trans_handle *trans;
5718 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005719 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005720 int err;
5721 int drop_inode = 0;
5722 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005723 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005724
5725 if (!new_valid_dev(rdev))
5726 return -EINVAL;
5727
Josef Bacik9ed74f22009-09-11 16:12:44 -04005728 /*
5729 * 2 for inode item and ref
5730 * 2 for dir items
5731 * 1 for xattr if selinux is on
5732 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005733 trans = btrfs_start_transaction(root, 5);
5734 if (IS_ERR(trans))
5735 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005736
Li Zefan581bb052011-04-20 10:06:11 +08005737 err = btrfs_find_free_ino(root, &objectid);
5738 if (err)
5739 goto out_unlock;
5740
Josef Bacikaec74772008-07-24 12:12:38 -04005741 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005742 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005743 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005744 if (IS_ERR(inode)) {
5745 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005746 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005747 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005748
Eric Paris2a7dba32011-02-01 11:05:39 -05005749 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005750 if (err) {
5751 drop_inode = 1;
5752 goto out_unlock;
5753 }
5754
Casey Schauflerad19db72011-12-15 10:09:07 -05005755 /*
5756 * If the active LSM wants to access the inode during
5757 * d_instantiate it needs these. Smack checks to see
5758 * if the filesystem supports xattrs by looking at the
5759 * ops vector.
5760 */
5761
5762 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005763 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005764 if (err)
5765 drop_inode = 1;
5766 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005767 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005768 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005769 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005770 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005771out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005772 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005773 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005774 if (drop_inode) {
5775 inode_dec_link_count(inode);
5776 iput(inode);
5777 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005778 return err;
5779}
5780
Chris Mason39279cc2007-06-12 06:35:45 -04005781static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005782 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005783{
5784 struct btrfs_trans_handle *trans;
5785 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005786 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005787 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005788 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005789 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005790 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005791
Josef Bacik9ed74f22009-09-11 16:12:44 -04005792 /*
5793 * 2 for inode item and ref
5794 * 2 for dir items
5795 * 1 for xattr if selinux is on
5796 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005797 trans = btrfs_start_transaction(root, 5);
5798 if (IS_ERR(trans))
5799 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005800
Li Zefan581bb052011-04-20 10:06:11 +08005801 err = btrfs_find_free_ino(root, &objectid);
5802 if (err)
5803 goto out_unlock;
5804
Josef Bacikaec74772008-07-24 12:12:38 -04005805 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005806 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005807 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005808 if (IS_ERR(inode)) {
5809 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005810 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005811 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005812 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005813
Eric Paris2a7dba32011-02-01 11:05:39 -05005814 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005815 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005816 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005817
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005818 err = btrfs_update_inode(trans, root, inode);
5819 if (err)
5820 goto out_unlock;
5821
Casey Schauflerad19db72011-12-15 10:09:07 -05005822 /*
5823 * If the active LSM wants to access the inode during
5824 * d_instantiate it needs these. Smack checks to see
5825 * if the filesystem supports xattrs by looking at the
5826 * ops vector.
5827 */
5828 inode->i_fop = &btrfs_file_operations;
5829 inode->i_op = &btrfs_file_inode_operations;
5830
Josef Bacika1b075d2010-11-19 20:36:11 +00005831 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005832 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005833 goto out_unlock;
5834
5835 inode->i_mapping->a_ops = &btrfs_aops;
5836 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5837 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5838 d_instantiate(dentry, inode);
5839
Chris Mason39279cc2007-06-12 06:35:45 -04005840out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005841 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005842 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005843 inode_dec_link_count(inode);
5844 iput(inode);
5845 }
Liu Bob53d3f52012-11-14 14:34:34 +00005846 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005847 return err;
5848}
5849
5850static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5851 struct dentry *dentry)
5852{
5853 struct btrfs_trans_handle *trans;
5854 struct btrfs_root *root = BTRFS_I(dir)->root;
5855 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005856 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005857 int err;
5858 int drop_inode = 0;
5859
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005860 /* do not allow sys_link's with other subvols of the same device */
5861 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005862 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005863
Mark Fashehf1863732012-08-08 11:32:27 -07005864 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005865 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005866
Chris Mason3de45862008-11-17 21:02:50 -05005867 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005868 if (err)
5869 goto fail;
5870
Yan, Zhenga22285a2010-05-16 10:48:46 -04005871 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005872 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005873 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005874 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005875 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005876 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005877 if (IS_ERR(trans)) {
5878 err = PTR_ERR(trans);
5879 goto fail;
5880 }
Chris Mason5f39d392007-10-15 16:14:19 -04005881
Miao Xie31534952011-04-13 13:19:21 +08005882 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005883 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005884 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005885 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005886 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005887
Josef Bacika1b075d2010-11-19 20:36:11 +00005888 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005889
Yan, Zhenga5719522009-09-24 09:17:31 -04005890 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005891 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005892 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005893 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005894 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005895 if (err)
5896 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005897 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005898 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005899 }
Chris Mason39279cc2007-06-12 06:35:45 -04005900
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005901 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005902fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005903 if (drop_inode) {
5904 inode_dec_link_count(inode);
5905 iput(inode);
5906 }
Liu Bob53d3f52012-11-14 14:34:34 +00005907 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005908 return err;
5909}
5910
Al Viro18bb1db2011-07-26 01:41:39 -04005911static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005912{
Chris Masonb9d86662008-05-02 16:13:49 -04005913 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005914 struct btrfs_trans_handle *trans;
5915 struct btrfs_root *root = BTRFS_I(dir)->root;
5916 int err = 0;
5917 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005918 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005919 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005920
Josef Bacik9ed74f22009-09-11 16:12:44 -04005921 /*
5922 * 2 items for inode and ref
5923 * 2 items for dir items
5924 * 1 for xattr if selinux is on
5925 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005926 trans = btrfs_start_transaction(root, 5);
5927 if (IS_ERR(trans))
5928 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005929
Li Zefan581bb052011-04-20 10:06:11 +08005930 err = btrfs_find_free_ino(root, &objectid);
5931 if (err)
5932 goto out_fail;
5933
Josef Bacikaec74772008-07-24 12:12:38 -04005934 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005935 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005936 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005937 if (IS_ERR(inode)) {
5938 err = PTR_ERR(inode);
5939 goto out_fail;
5940 }
Chris Mason5f39d392007-10-15 16:14:19 -04005941
Chris Mason39279cc2007-06-12 06:35:45 -04005942 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005943
Eric Paris2a7dba32011-02-01 11:05:39 -05005944 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005945 if (err)
5946 goto out_fail;
5947
Chris Mason39279cc2007-06-12 06:35:45 -04005948 inode->i_op = &btrfs_dir_inode_operations;
5949 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005950
Chris Masondbe674a2008-07-17 12:54:05 -04005951 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005952 err = btrfs_update_inode(trans, root, inode);
5953 if (err)
5954 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005955
Josef Bacika1b075d2010-11-19 20:36:11 +00005956 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5957 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005958 if (err)
5959 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005960
Chris Mason39279cc2007-06-12 06:35:45 -04005961 d_instantiate(dentry, inode);
5962 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005963
5964out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005965 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005966 if (drop_on_err)
5967 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005968 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005969 return err;
5970}
5971
Chris Masond352ac62008-09-29 15:18:18 -04005972/* helper for btfs_get_extent. Given an existing extent in the tree,
5973 * and an extent that you want to insert, deal with overlap and insert
5974 * the new extent into the tree.
5975 */
Chris Mason3b951512008-04-17 11:29:12 -04005976static int merge_extent_mapping(struct extent_map_tree *em_tree,
5977 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005978 struct extent_map *em,
5979 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005980{
5981 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005982
Chris Masone6dcd2d2008-07-17 12:53:50 -04005983 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5984 start_diff = map_start - em->start;
5985 em->start = map_start;
5986 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005987 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5988 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005989 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005990 em->block_len -= start_diff;
5991 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005992 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005993}
5994
Chris Masonc8b97812008-10-29 14:49:59 -04005995static noinline int uncompress_inline(struct btrfs_path *path,
5996 struct inode *inode, struct page *page,
5997 size_t pg_offset, u64 extent_offset,
5998 struct btrfs_file_extent_item *item)
5999{
6000 int ret;
6001 struct extent_buffer *leaf = path->nodes[0];
6002 char *tmp;
6003 size_t max_size;
6004 unsigned long inline_size;
6005 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006006 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006007
6008 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006009 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006010 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6011 inline_size = btrfs_file_extent_inline_item_len(leaf,
6012 btrfs_item_nr(leaf, path->slots[0]));
6013 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006014 if (!tmp)
6015 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006016 ptr = btrfs_file_extent_inline_start(item);
6017
6018 read_extent_buffer(leaf, tmp, ptr, inline_size);
6019
Chris Mason5b050f02008-11-11 09:34:41 -05006020 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006021 ret = btrfs_decompress(compress_type, tmp, page,
6022 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006023 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006024 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006025 unsigned long copy_size = min_t(u64,
6026 PAGE_CACHE_SIZE - pg_offset,
6027 max_size - extent_offset);
6028 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006029 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006030 }
6031 kfree(tmp);
6032 return 0;
6033}
6034
Chris Masond352ac62008-09-29 15:18:18 -04006035/*
6036 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006037 * the ugly parts come from merging extents from the disk with the in-ram
6038 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006039 * where the in-ram extents might be locked pending data=ordered completion.
6040 *
6041 * This also copies inline extents directly into the page.
6042 */
Chris Masond3977122009-01-05 21:25:51 -05006043
Chris Masona52d9a82007-08-27 16:49:44 -04006044struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006045 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006046 int create)
6047{
6048 int ret;
6049 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006050 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006051 u64 extent_start = 0;
6052 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006053 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006054 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006055 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006056 struct btrfs_root *root = BTRFS_I(inode)->root;
6057 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006058 struct extent_buffer *leaf;
6059 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006060 struct extent_map *em = NULL;
6061 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006062 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006063 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006064 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006065
Chris Masona52d9a82007-08-27 16:49:44 -04006066again:
Chris Mason890871b2009-09-02 16:24:52 -04006067 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006068 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006069 if (em)
6070 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006071 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006072
Chris Masona52d9a82007-08-27 16:49:44 -04006073 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006074 if (em->start > start || em->start + em->len <= start)
6075 free_extent_map(em);
6076 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006077 free_extent_map(em);
6078 else
6079 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006080 }
David Sterba172ddd62011-04-21 00:48:27 +02006081 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006082 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006083 err = -ENOMEM;
6084 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006085 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006086 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006087 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006088 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006089 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006090 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006091
6092 if (!path) {
6093 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006094 if (!path) {
6095 err = -ENOMEM;
6096 goto out;
6097 }
6098 /*
6099 * Chances are we'll be called again, so go ahead and do
6100 * readahead
6101 */
6102 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006103 }
6104
Chris Mason179e29e2007-11-01 11:28:41 -04006105 ret = btrfs_lookup_file_extent(trans, root, path,
6106 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006107 if (ret < 0) {
6108 err = ret;
6109 goto out;
6110 }
6111
6112 if (ret != 0) {
6113 if (path->slots[0] == 0)
6114 goto not_found;
6115 path->slots[0]--;
6116 }
6117
Chris Mason5f39d392007-10-15 16:14:19 -04006118 leaf = path->nodes[0];
6119 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006120 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006121 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006122 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6123 found_type = btrfs_key_type(&found_key);
6124 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006125 found_type != BTRFS_EXTENT_DATA_KEY) {
6126 goto not_found;
6127 }
6128
Chris Mason5f39d392007-10-15 16:14:19 -04006129 found_type = btrfs_file_extent_type(leaf, item);
6130 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006131 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006132 if (found_type == BTRFS_FILE_EXTENT_REG ||
6133 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006134 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006135 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006136 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6137 size_t size;
6138 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006139 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006140 }
6141
6142 if (start >= extent_end) {
6143 path->slots[0]++;
6144 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6145 ret = btrfs_next_leaf(root, path);
6146 if (ret < 0) {
6147 err = ret;
6148 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006149 }
Yan Zheng9036c102008-10-30 14:19:41 -04006150 if (ret > 0)
6151 goto not_found;
6152 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006153 }
Yan Zheng9036c102008-10-30 14:19:41 -04006154 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6155 if (found_key.objectid != objectid ||
6156 found_key.type != BTRFS_EXTENT_DATA_KEY)
6157 goto not_found;
6158 if (start + len <= found_key.offset)
6159 goto not_found;
6160 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006161 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006162 em->len = found_key.offset - start;
6163 goto not_found_em;
6164 }
6165
Josef Bacikcc95bef2013-04-04 14:31:27 -04006166 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006167 if (found_type == BTRFS_FILE_EXTENT_REG ||
6168 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006169 em->start = extent_start;
6170 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006171 em->orig_start = extent_start -
6172 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006173 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6174 item);
Chris Masondb945352007-10-15 16:15:53 -04006175 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6176 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006177 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006178 goto insert;
6179 }
Li Zefan261507a02010-12-17 14:21:50 +08006180 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006181 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006182 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006183 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006184 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006185 } else {
6186 bytenr += btrfs_file_extent_offset(leaf, item);
6187 em->block_start = bytenr;
6188 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006189 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6190 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006191 }
Chris Masona52d9a82007-08-27 16:49:44 -04006192 goto insert;
6193 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006194 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006195 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006196 size_t size;
6197 size_t extent_offset;
6198 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006199
Chris Masona52d9a82007-08-27 16:49:44 -04006200 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006201 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006202 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006203 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006204 goto out;
6205 }
6206
Yan Zheng9036c102008-10-30 14:19:41 -04006207 size = btrfs_file_extent_inline_len(leaf, item);
6208 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006209 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006210 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006211 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006212 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006213 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006214 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006215 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006216 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006217 em->compress_type = compress_type;
6218 }
Yan689f9342007-10-29 11:41:07 -04006219 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006220 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006221 if (btrfs_file_extent_compression(leaf, item) !=
6222 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006223 ret = uncompress_inline(path, inode, page,
6224 pg_offset,
6225 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006226 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006227 } else {
6228 map = kmap(page);
6229 read_extent_buffer(leaf, map + pg_offset, ptr,
6230 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006231 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6232 memset(map + pg_offset + copy_size, 0,
6233 PAGE_CACHE_SIZE - pg_offset -
6234 copy_size);
6235 }
Chris Masonc8b97812008-10-29 14:49:59 -04006236 kunmap(page);
6237 }
Chris Mason179e29e2007-11-01 11:28:41 -04006238 flush_dcache_page(page);
6239 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006240 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006241 if (!trans) {
6242 kunmap(page);
6243 free_extent_map(em);
6244 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006245
David Sterbab3b4aa72011-04-21 01:20:15 +02006246 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006247 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006248
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006249 if (IS_ERR(trans))
6250 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006251 goto again;
6252 }
Chris Masonc8b97812008-10-29 14:49:59 -04006253 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006254 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006255 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006256 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006257 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006258 }
Chris Masond1310b22008-01-24 16:13:08 -05006259 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006260 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006261 goto insert;
6262 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006263 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006264 }
6265not_found:
6266 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006267 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006268 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006269not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006270 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006271 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006272insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006273 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006274 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006275 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6276 (unsigned long long)em->start,
6277 (unsigned long long)em->len,
6278 (unsigned long long)start,
6279 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006280 err = -EIO;
6281 goto out;
6282 }
Chris Masond1310b22008-01-24 16:13:08 -05006283
6284 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006285 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006286 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006287 /* it is possible that someone inserted the extent into the tree
6288 * while we had the lock dropped. It is also possible that
6289 * an overlapping map exists in the tree
6290 */
Chris Masona52d9a82007-08-27 16:49:44 -04006291 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006292 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006293
6294 ret = 0;
6295
Chris Mason3b951512008-04-17 11:29:12 -04006296 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006297 if (existing && (existing->start > start ||
6298 existing->start + existing->len <= start)) {
6299 free_extent_map(existing);
6300 existing = NULL;
6301 }
Chris Mason3b951512008-04-17 11:29:12 -04006302 if (!existing) {
6303 existing = lookup_extent_mapping(em_tree, em->start,
6304 em->len);
6305 if (existing) {
6306 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006307 em, start,
6308 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006309 free_extent_map(existing);
6310 if (err) {
6311 free_extent_map(em);
6312 em = NULL;
6313 }
6314 } else {
6315 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006316 free_extent_map(em);
6317 em = NULL;
6318 }
6319 } else {
6320 free_extent_map(em);
6321 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006322 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006323 }
Chris Masona52d9a82007-08-27 16:49:44 -04006324 }
Chris Mason890871b2009-09-02 16:24:52 -04006325 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006326out:
liubo1abe9b82011-03-24 11:18:59 +00006327
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006328 if (em)
6329 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006330
Chris Masonf4219502008-07-22 11:18:09 -04006331 if (path)
6332 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006333 if (trans) {
6334 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006335 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006336 err = ret;
6337 }
Chris Masona52d9a82007-08-27 16:49:44 -04006338 if (err) {
6339 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006340 return ERR_PTR(err);
6341 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006342 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006343 return em;
6344}
6345
Chris Masonec29ed52011-02-23 16:23:20 -05006346struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6347 size_t pg_offset, u64 start, u64 len,
6348 int create)
6349{
6350 struct extent_map *em;
6351 struct extent_map *hole_em = NULL;
6352 u64 range_start = start;
6353 u64 end;
6354 u64 found;
6355 u64 found_end;
6356 int err = 0;
6357
6358 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6359 if (IS_ERR(em))
6360 return em;
6361 if (em) {
6362 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006363 * if our em maps to
6364 * - a hole or
6365 * - a pre-alloc extent,
6366 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006367 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006368 if (em->block_start != EXTENT_MAP_HOLE &&
6369 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006370 return em;
6371 else
6372 hole_em = em;
6373 }
6374
6375 /* check to see if we've wrapped (len == -1 or similar) */
6376 end = start + len;
6377 if (end < start)
6378 end = (u64)-1;
6379 else
6380 end -= 1;
6381
6382 em = NULL;
6383
6384 /* ok, we didn't find anything, lets look for delalloc */
6385 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6386 end, len, EXTENT_DELALLOC, 1);
6387 found_end = range_start + found;
6388 if (found_end < range_start)
6389 found_end = (u64)-1;
6390
6391 /*
6392 * we didn't find anything useful, return
6393 * the original results from get_extent()
6394 */
6395 if (range_start > end || found_end <= start) {
6396 em = hole_em;
6397 hole_em = NULL;
6398 goto out;
6399 }
6400
6401 /* adjust the range_start to make sure it doesn't
6402 * go backwards from the start they passed in
6403 */
6404 range_start = max(start,range_start);
6405 found = found_end - range_start;
6406
6407 if (found > 0) {
6408 u64 hole_start = start;
6409 u64 hole_len = len;
6410
David Sterba172ddd62011-04-21 00:48:27 +02006411 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006412 if (!em) {
6413 err = -ENOMEM;
6414 goto out;
6415 }
6416 /*
6417 * when btrfs_get_extent can't find anything it
6418 * returns one huge hole
6419 *
6420 * make sure what it found really fits our range, and
6421 * adjust to make sure it is based on the start from
6422 * the caller
6423 */
6424 if (hole_em) {
6425 u64 calc_end = extent_map_end(hole_em);
6426
6427 if (calc_end <= start || (hole_em->start > end)) {
6428 free_extent_map(hole_em);
6429 hole_em = NULL;
6430 } else {
6431 hole_start = max(hole_em->start, start);
6432 hole_len = calc_end - hole_start;
6433 }
6434 }
6435 em->bdev = NULL;
6436 if (hole_em && range_start > hole_start) {
6437 /* our hole starts before our delalloc, so we
6438 * have to return just the parts of the hole
6439 * that go until the delalloc starts
6440 */
6441 em->len = min(hole_len,
6442 range_start - hole_start);
6443 em->start = hole_start;
6444 em->orig_start = hole_start;
6445 /*
6446 * don't adjust block start at all,
6447 * it is fixed at EXTENT_MAP_HOLE
6448 */
6449 em->block_start = hole_em->block_start;
6450 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006451 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6452 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006453 } else {
6454 em->start = range_start;
6455 em->len = found;
6456 em->orig_start = range_start;
6457 em->block_start = EXTENT_MAP_DELALLOC;
6458 em->block_len = found;
6459 }
6460 } else if (hole_em) {
6461 return hole_em;
6462 }
6463out:
6464
6465 free_extent_map(hole_em);
6466 if (err) {
6467 free_extent_map(em);
6468 return ERR_PTR(err);
6469 }
6470 return em;
6471}
6472
Josef Bacik4b46fce2010-05-23 11:00:55 -04006473static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6474 u64 start, u64 len)
6475{
6476 struct btrfs_root *root = BTRFS_I(inode)->root;
6477 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006478 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006479 struct btrfs_key ins;
6480 u64 alloc_hint;
6481 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006482
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006483 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006484 if (IS_ERR(trans))
6485 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006486
6487 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6488
6489 alloc_hint = get_extent_allocation_hint(inode, start, len);
6490 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006491 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006492 if (ret) {
6493 em = ERR_PTR(ret);
6494 goto out;
6495 }
6496
Josef Bacik70c8a912012-10-11 16:54:30 -04006497 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006498 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006499 if (IS_ERR(em))
6500 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006501
6502 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6503 ins.offset, ins.offset, 0);
6504 if (ret) {
6505 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6506 em = ERR_PTR(ret);
6507 }
6508out:
6509 btrfs_end_transaction(trans, root);
6510 return em;
6511}
6512
Chris Mason46bfbb52010-05-26 11:04:10 -04006513/*
6514 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6515 * block must be cow'd
6516 */
6517static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
Josef Bacikeb384b52013-04-24 16:32:55 -04006518 struct inode *inode, u64 offset, u64 *len,
6519 u64 *orig_start, u64 *orig_block_len,
6520 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006521{
6522 struct btrfs_path *path;
6523 int ret;
6524 struct extent_buffer *leaf;
6525 struct btrfs_root *root = BTRFS_I(inode)->root;
6526 struct btrfs_file_extent_item *fi;
6527 struct btrfs_key key;
6528 u64 disk_bytenr;
6529 u64 backref_offset;
6530 u64 extent_end;
6531 u64 num_bytes;
6532 int slot;
6533 int found_type;
6534
6535 path = btrfs_alloc_path();
6536 if (!path)
6537 return -ENOMEM;
6538
Li Zefan33345d012011-04-20 10:31:50 +08006539 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006540 offset, 0);
6541 if (ret < 0)
6542 goto out;
6543
6544 slot = path->slots[0];
6545 if (ret == 1) {
6546 if (slot == 0) {
6547 /* can't find the item, must cow */
6548 ret = 0;
6549 goto out;
6550 }
6551 slot--;
6552 }
6553 ret = 0;
6554 leaf = path->nodes[0];
6555 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006556 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006557 key.type != BTRFS_EXTENT_DATA_KEY) {
6558 /* not our file or wrong item type, must cow */
6559 goto out;
6560 }
6561
6562 if (key.offset > offset) {
6563 /* Wrong offset, must cow */
6564 goto out;
6565 }
6566
6567 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6568 found_type = btrfs_file_extent_type(leaf, fi);
6569 if (found_type != BTRFS_FILE_EXTENT_REG &&
6570 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6571 /* not a regular extent, must cow */
6572 goto out;
6573 }
6574 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6575 backref_offset = btrfs_file_extent_offset(leaf, fi);
6576
Josef Bacikeb384b52013-04-24 16:32:55 -04006577 *orig_start = key.offset - backref_offset;
6578 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6579 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6580
Chris Mason46bfbb52010-05-26 11:04:10 -04006581 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikeb384b52013-04-24 16:32:55 -04006582 if (extent_end < offset + *len) {
Chris Mason46bfbb52010-05-26 11:04:10 -04006583 /* extent doesn't include our full range, must cow */
6584 goto out;
6585 }
6586
6587 if (btrfs_extent_readonly(root, disk_bytenr))
6588 goto out;
6589
6590 /*
6591 * look for other files referencing this extent, if we
6592 * find any we must cow
6593 */
Li Zefan33345d012011-04-20 10:31:50 +08006594 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006595 key.offset - backref_offset, disk_bytenr))
6596 goto out;
6597
6598 /*
6599 * adjust disk_bytenr and num_bytes to cover just the bytes
6600 * in this extent we are about to write. If there
6601 * are any csums in that range we have to cow in order
6602 * to keep the csums correct
6603 */
6604 disk_bytenr += backref_offset;
6605 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006606 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006607 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6608 goto out;
6609 /*
6610 * all of the above have passed, it is safe to overwrite this extent
6611 * without cow
6612 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006613 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006614 ret = 1;
6615out:
6616 btrfs_free_path(path);
6617 return ret;
6618}
6619
Josef Bacikeb838e72012-07-31 16:28:48 -04006620static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6621 struct extent_state **cached_state, int writing)
6622{
6623 struct btrfs_ordered_extent *ordered;
6624 int ret = 0;
6625
6626 while (1) {
6627 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6628 0, cached_state);
6629 /*
6630 * We're concerned with the entire range that we're going to be
6631 * doing DIO to, so we need to make sure theres no ordered
6632 * extents in this range.
6633 */
6634 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6635 lockend - lockstart + 1);
6636
6637 /*
6638 * We need to make sure there are no buffered pages in this
6639 * range either, we could have raced between the invalidate in
6640 * generic_file_direct_write and locking the extent. The
6641 * invalidate needs to happen so that reads after a write do not
6642 * get stale data.
6643 */
6644 if (!ordered && (!writing ||
6645 !test_range_bit(&BTRFS_I(inode)->io_tree,
6646 lockstart, lockend, EXTENT_UPTODATE, 0,
6647 *cached_state)))
6648 break;
6649
6650 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6651 cached_state, GFP_NOFS);
6652
6653 if (ordered) {
6654 btrfs_start_ordered_extent(inode, ordered, 1);
6655 btrfs_put_ordered_extent(ordered);
6656 } else {
6657 /* Screw you mmap */
6658 ret = filemap_write_and_wait_range(inode->i_mapping,
6659 lockstart,
6660 lockend);
6661 if (ret)
6662 break;
6663
6664 /*
6665 * If we found a page that couldn't be invalidated just
6666 * fall back to buffered.
6667 */
6668 ret = invalidate_inode_pages2_range(inode->i_mapping,
6669 lockstart >> PAGE_CACHE_SHIFT,
6670 lockend >> PAGE_CACHE_SHIFT);
6671 if (ret)
6672 break;
6673 }
6674
6675 cond_resched();
6676 }
6677
6678 return ret;
6679}
6680
Josef Bacik69ffb542012-09-11 15:40:07 -04006681static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6682 u64 len, u64 orig_start,
6683 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006684 u64 orig_block_len, u64 ram_bytes,
6685 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006686{
6687 struct extent_map_tree *em_tree;
6688 struct extent_map *em;
6689 struct btrfs_root *root = BTRFS_I(inode)->root;
6690 int ret;
6691
6692 em_tree = &BTRFS_I(inode)->extent_tree;
6693 em = alloc_extent_map();
6694 if (!em)
6695 return ERR_PTR(-ENOMEM);
6696
6697 em->start = start;
6698 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006699 em->mod_start = start;
6700 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006701 em->len = len;
6702 em->block_len = block_len;
6703 em->block_start = block_start;
6704 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006705 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006706 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006707 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006708 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6709 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006710 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006711
6712 do {
6713 btrfs_drop_extent_cache(inode, em->start,
6714 em->start + em->len - 1, 0);
6715 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006716 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006717 write_unlock(&em_tree->lock);
6718 } while (ret == -EEXIST);
6719
6720 if (ret) {
6721 free_extent_map(em);
6722 return ERR_PTR(ret);
6723 }
6724
6725 return em;
6726}
6727
6728
Josef Bacik4b46fce2010-05-23 11:00:55 -04006729static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6730 struct buffer_head *bh_result, int create)
6731{
6732 struct extent_map *em;
6733 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006734 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006735 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006736 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006737 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006738 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006739 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006740 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006741
Miao Xie172a5042013-02-21 02:48:22 -07006742 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006743 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006744 else
Josef Bacikc3298612012-08-03 16:49:19 -04006745 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006746
Josef Bacikc3298612012-08-03 16:49:19 -04006747 lockstart = start;
6748 lockend = start + len - 1;
6749
Josef Bacikeb838e72012-07-31 16:28:48 -04006750 /*
6751 * If this errors out it's because we couldn't invalidate pagecache for
6752 * this range and we need to fallback to buffered.
6753 */
6754 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6755 return -ENOTBLK;
6756
Josef Bacik4b46fce2010-05-23 11:00:55 -04006757 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006758 if (IS_ERR(em)) {
6759 ret = PTR_ERR(em);
6760 goto unlock_err;
6761 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006762
6763 /*
6764 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6765 * io. INLINE is special, and we could probably kludge it in here, but
6766 * it's still buffered so for safety lets just fall back to the generic
6767 * buffered path.
6768 *
6769 * For COMPRESSED we _have_ to read the entire extent in so we can
6770 * decompress it, so there will be buffering required no matter what we
6771 * do, so go ahead and fallback to buffered.
6772 *
6773 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6774 * to buffered IO. Don't blame me, this is the price we pay for using
6775 * the generic code.
6776 */
6777 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6778 em->block_start == EXTENT_MAP_INLINE) {
6779 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006780 ret = -ENOTBLK;
6781 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006782 }
6783
6784 /* Just a good old fashioned hole, return */
6785 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6786 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6787 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006788 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006789 }
6790
6791 /*
6792 * We don't allocate a new extent in the following cases
6793 *
6794 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6795 * existing extent.
6796 * 2) The extent is marked as PREALLOC. We're good to go here and can
6797 * just use the extent.
6798 *
6799 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006800 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006801 len = min(len, em->len - (start - em->start));
6802 lockstart = start + len;
6803 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006804 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006805
6806 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6807 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6808 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006809 int type;
6810 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006811 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006812
6813 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6814 type = BTRFS_ORDERED_PREALLOC;
6815 else
6816 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006817 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006818 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006819
6820 /*
6821 * we're not going to log anything, but we do need
6822 * to make sure the current transaction stays open
6823 * while we look for nocow cross refs
6824 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006825 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006826 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006827 goto must_cow;
6828
Josef Bacikeb384b52013-04-24 16:32:55 -04006829 if (can_nocow_odirect(trans, inode, start, &len, &orig_start,
6830 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006831 if (type == BTRFS_ORDERED_PREALLOC) {
6832 free_extent_map(em);
6833 em = create_pinned_em(inode, start, len,
6834 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006835 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006836 orig_block_len,
6837 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006838 if (IS_ERR(em)) {
6839 btrfs_end_transaction(trans, root);
6840 goto unlock_err;
6841 }
6842 }
6843
Chris Mason46bfbb52010-05-26 11:04:10 -04006844 ret = btrfs_add_ordered_extent_dio(inode, start,
6845 block_start, len, len, type);
6846 btrfs_end_transaction(trans, root);
6847 if (ret) {
6848 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006849 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006850 }
6851 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006852 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006853 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006854 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006855must_cow:
6856 /*
6857 * this will cow the extent, reset the len in case we changed
6858 * it above
6859 */
6860 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006861 free_extent_map(em);
6862 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006863 if (IS_ERR(em)) {
6864 ret = PTR_ERR(em);
6865 goto unlock_err;
6866 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006867 len = min(len, em->len - (start - em->start));
6868unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006869 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6870 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006871 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006872 bh_result->b_bdev = em->bdev;
6873 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006874 if (create) {
6875 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6876 set_buffer_new(bh_result);
6877
6878 /*
6879 * Need to update the i_size under the extent lock so buffered
6880 * readers will get the updated i_size when we unlock.
6881 */
6882 if (start + len > i_size_read(inode))
6883 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006884
Miao Xie172a5042013-02-21 02:48:22 -07006885 spin_lock(&BTRFS_I(inode)->lock);
6886 BTRFS_I(inode)->outstanding_extents++;
6887 spin_unlock(&BTRFS_I(inode)->lock);
6888
Miao Xie09348562013-02-07 10:12:07 +00006889 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6890 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6891 &cached_state, GFP_NOFS);
6892 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006893 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006894
Josef Bacikeb838e72012-07-31 16:28:48 -04006895 /*
6896 * In the case of write we need to clear and unlock the entire range,
6897 * in the case of read we need to unlock only the end area that we
6898 * aren't using if there is any left over space.
6899 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006900 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006901 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6902 lockend, unlock_bits, 1, 0,
6903 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006904 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006905 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006906 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006907
Josef Bacik4b46fce2010-05-23 11:00:55 -04006908 free_extent_map(em);
6909
6910 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006911
6912unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006913 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6914 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6915 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006916}
6917
6918struct btrfs_dio_private {
6919 struct inode *inode;
6920 u64 logical_offset;
6921 u64 disk_bytenr;
6922 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006923 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006924
6925 /* number of bios pending for this dio */
6926 atomic_t pending_bios;
6927
6928 /* IO errors */
6929 int errors;
6930
6931 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006932};
6933
6934static void btrfs_endio_direct_read(struct bio *bio, int err)
6935{
Miao Xiee65e1532010-11-22 03:04:43 +00006936 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006937 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6938 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006939 struct inode *inode = dip->inode;
6940 struct btrfs_root *root = BTRFS_I(inode)->root;
6941 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006942
6943 start = dip->logical_offset;
6944 do {
6945 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6946 struct page *page = bvec->bv_page;
6947 char *kaddr;
6948 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006949 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006950 unsigned long flags;
6951
Josef Bacikc3298612012-08-03 16:49:19 -04006952 if (get_state_private(&BTRFS_I(inode)->io_tree,
6953 start, &private))
6954 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006955 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006956 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006957 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006958 csum, bvec->bv_len);
6959 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006960 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006961 local_irq_restore(flags);
6962
6963 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006964 if (csum != private) {
6965failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006966 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6967 (unsigned long long)btrfs_ino(inode),
6968 (unsigned long long)start,
6969 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006970 err = -EIO;
6971 }
6972 }
6973
6974 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006975 bvec++;
6976 } while (bvec <= bvec_end);
6977
6978 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006979 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006980 bio->bi_private = dip->private;
6981
Josef Bacik4b46fce2010-05-23 11:00:55 -04006982 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006983
6984 /* If we had a csum failure make sure to clear the uptodate flag */
6985 if (err)
6986 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006987 dio_end_io(bio, err);
6988}
6989
6990static void btrfs_endio_direct_write(struct bio *bio, int err)
6991{
6992 struct btrfs_dio_private *dip = bio->bi_private;
6993 struct inode *inode = dip->inode;
6994 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006995 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006996 u64 ordered_offset = dip->logical_offset;
6997 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006998 int ret;
6999
7000 if (err)
7001 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007002again:
7003 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7004 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007005 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007006 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007007 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007008
Josef Bacik5fd02042012-05-02 14:00:54 -04007009 ordered->work.func = finish_ordered_fn;
7010 ordered->work.flags = 0;
7011 btrfs_queue_worker(&root->fs_info->endio_write_workers,
7012 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007013out_test:
7014 /*
7015 * our bio might span multiple ordered extents. If we haven't
7016 * completed the accounting for the whole dio, go back and try again
7017 */
7018 if (ordered_offset < dip->logical_offset + dip->bytes) {
7019 ordered_bytes = dip->logical_offset + dip->bytes -
7020 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007021 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007022 goto again;
7023 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007024out_done:
7025 bio->bi_private = dip->private;
7026
Josef Bacik4b46fce2010-05-23 11:00:55 -04007027 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007028
7029 /* If we had an error make sure to clear the uptodate flag */
7030 if (err)
7031 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007032 dio_end_io(bio, err);
7033}
7034
Chris Masoneaf25d92010-05-25 09:48:28 -04007035static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7036 struct bio *bio, int mirror_num,
7037 unsigned long bio_flags, u64 offset)
7038{
7039 int ret;
7040 struct btrfs_root *root = BTRFS_I(inode)->root;
7041 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007042 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007043 return 0;
7044}
7045
Miao Xiee65e1532010-11-22 03:04:43 +00007046static void btrfs_end_dio_bio(struct bio *bio, int err)
7047{
7048 struct btrfs_dio_private *dip = bio->bi_private;
7049
7050 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007051 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007052 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007053 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007054 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007055 dip->errors = 1;
7056
7057 /*
7058 * before atomic variable goto zero, we must make sure
7059 * dip->errors is perceived to be set.
7060 */
7061 smp_mb__before_atomic_dec();
7062 }
7063
7064 /* if there are more bios still pending for this dio, just exit */
7065 if (!atomic_dec_and_test(&dip->pending_bios))
7066 goto out;
7067
7068 if (dip->errors)
7069 bio_io_error(dip->orig_bio);
7070 else {
7071 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
7072 bio_endio(dip->orig_bio, 0);
7073 }
7074out:
7075 bio_put(bio);
7076}
7077
7078static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7079 u64 first_sector, gfp_t gfp_flags)
7080{
7081 int nr_vecs = bio_get_nr_vecs(bdev);
7082 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7083}
7084
7085static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7086 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007087 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007088{
7089 int write = rw & REQ_WRITE;
7090 struct btrfs_root *root = BTRFS_I(inode)->root;
7091 int ret;
7092
Josef Bacikb812ce22012-11-16 13:56:32 -05007093 if (async_submit)
7094 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7095
Miao Xiee65e1532010-11-22 03:04:43 +00007096 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007097
7098 if (!write) {
7099 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7100 if (ret)
7101 goto err;
7102 }
Miao Xiee65e1532010-11-22 03:04:43 +00007103
Josef Bacik1ae39932011-04-06 14:41:34 -04007104 if (skip_sum)
7105 goto map;
7106
7107 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007108 ret = btrfs_wq_submit_bio(root->fs_info,
7109 inode, rw, bio, 0, 0,
7110 file_offset,
7111 __btrfs_submit_bio_start_direct_io,
7112 __btrfs_submit_bio_done);
7113 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007114 } else if (write) {
7115 /*
7116 * If we aren't doing async submit, calculate the csum of the
7117 * bio now.
7118 */
7119 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7120 if (ret)
7121 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007122 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007123 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007124 if (ret)
7125 goto err;
7126 }
Miao Xiee65e1532010-11-22 03:04:43 +00007127
Josef Bacik1ae39932011-04-06 14:41:34 -04007128map:
7129 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007130err:
7131 bio_put(bio);
7132 return ret;
7133}
7134
7135static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7136 int skip_sum)
7137{
7138 struct inode *inode = dip->inode;
7139 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007140 struct bio *bio;
7141 struct bio *orig_bio = dip->orig_bio;
7142 struct bio_vec *bvec = orig_bio->bi_io_vec;
7143 u64 start_sector = orig_bio->bi_sector;
7144 u64 file_offset = dip->logical_offset;
7145 u64 submit_len = 0;
7146 u64 map_length;
7147 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007148 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007149 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007150
Miao Xiee65e1532010-11-22 03:04:43 +00007151 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007152 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007153 &map_length, NULL, 0);
7154 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007155 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007156 return -EIO;
7157 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007158 if (map_length >= orig_bio->bi_size) {
7159 bio = orig_bio;
7160 goto submit;
7161 }
7162
David Woodhouse53b381b2013-01-29 18:40:14 -05007163 /* async crcs make it difficult to collect full stripe writes. */
7164 if (btrfs_get_alloc_profile(root, 1) &
7165 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7166 async_submit = 0;
7167 else
7168 async_submit = 1;
7169
Josef Bacik02f57c72011-04-06 14:25:44 -04007170 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7171 if (!bio)
7172 return -ENOMEM;
7173 bio->bi_private = dip;
7174 bio->bi_end_io = btrfs_end_dio_bio;
7175 atomic_inc(&dip->pending_bios);
7176
Miao Xiee65e1532010-11-22 03:04:43 +00007177 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7178 if (unlikely(map_length < submit_len + bvec->bv_len ||
7179 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7180 bvec->bv_offset) < bvec->bv_len)) {
7181 /*
7182 * inc the count before we submit the bio so
7183 * we know the end IO handler won't happen before
7184 * we inc the count. Otherwise, the dip might get freed
7185 * before we're done setting it up
7186 */
7187 atomic_inc(&dip->pending_bios);
7188 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7189 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007190 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007191 if (ret) {
7192 bio_put(bio);
7193 atomic_dec(&dip->pending_bios);
7194 goto out_err;
7195 }
7196
Miao Xiee65e1532010-11-22 03:04:43 +00007197 start_sector += submit_len >> 9;
7198 file_offset += submit_len;
7199
7200 submit_len = 0;
7201 nr_pages = 0;
7202
7203 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7204 start_sector, GFP_NOFS);
7205 if (!bio)
7206 goto out_err;
7207 bio->bi_private = dip;
7208 bio->bi_end_io = btrfs_end_dio_bio;
7209
7210 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007211 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007212 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007213 &map_length, NULL, 0);
7214 if (ret) {
7215 bio_put(bio);
7216 goto out_err;
7217 }
7218 } else {
7219 submit_len += bvec->bv_len;
7220 nr_pages ++;
7221 bvec++;
7222 }
7223 }
7224
Josef Bacik02f57c72011-04-06 14:25:44 -04007225submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007226 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007227 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007228 if (!ret)
7229 return 0;
7230
7231 bio_put(bio);
7232out_err:
7233 dip->errors = 1;
7234 /*
7235 * before atomic variable goto zero, we must
7236 * make sure dip->errors is perceived to be set.
7237 */
7238 smp_mb__before_atomic_dec();
7239 if (atomic_dec_and_test(&dip->pending_bios))
7240 bio_io_error(dip->orig_bio);
7241
7242 /* bio_end_io() will handle error, so we needn't return it */
7243 return 0;
7244}
7245
Josef Bacik4b46fce2010-05-23 11:00:55 -04007246static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
7247 loff_t file_offset)
7248{
7249 struct btrfs_root *root = BTRFS_I(inode)->root;
7250 struct btrfs_dio_private *dip;
7251 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007253 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007254 int ret = 0;
7255
7256 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7257
7258 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7259 if (!dip) {
7260 ret = -ENOMEM;
7261 goto free_ordered;
7262 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263
7264 dip->private = bio->bi_private;
7265 dip->inode = inode;
7266 dip->logical_offset = file_offset;
7267
Josef Bacik4b46fce2010-05-23 11:00:55 -04007268 dip->bytes = 0;
7269 do {
7270 dip->bytes += bvec->bv_len;
7271 bvec++;
7272 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
7273
Chris Mason46bfbb52010-05-26 11:04:10 -04007274 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007275 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007276 dip->errors = 0;
7277 dip->orig_bio = bio;
7278 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007279
7280 if (write)
7281 bio->bi_end_io = btrfs_endio_direct_write;
7282 else
7283 bio->bi_end_io = btrfs_endio_direct_read;
7284
Miao Xiee65e1532010-11-22 03:04:43 +00007285 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7286 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007287 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007288free_ordered:
7289 /*
7290 * If this is a write, we need to clean up the reserved space and kill
7291 * the ordered extent.
7292 */
7293 if (write) {
7294 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007295 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007296 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7297 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7298 btrfs_free_reserved_extent(root, ordered->start,
7299 ordered->disk_len);
7300 btrfs_put_ordered_extent(ordered);
7301 btrfs_put_ordered_extent(ordered);
7302 }
7303 bio_endio(bio, ret);
7304}
7305
Chris Mason5a5f79b2010-05-26 21:33:37 -04007306static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7307 const struct iovec *iov, loff_t offset,
7308 unsigned long nr_segs)
7309{
7310 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007311 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007312 size_t size;
7313 unsigned long addr;
7314 unsigned blocksize_mask = root->sectorsize - 1;
7315 ssize_t retval = -EINVAL;
7316 loff_t end = offset;
7317
7318 if (offset & blocksize_mask)
7319 goto out;
7320
7321 /* Check the memory alignment. Blocks cannot straddle pages */
7322 for (seg = 0; seg < nr_segs; seg++) {
7323 addr = (unsigned long)iov[seg].iov_base;
7324 size = iov[seg].iov_len;
7325 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007326 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007327 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007328
7329 /* If this is a write we don't need to check anymore */
7330 if (rw & WRITE)
7331 continue;
7332
7333 /*
7334 * Check to make sure we don't have duplicate iov_base's in this
7335 * iovec, if so return EINVAL, otherwise we'll get csum errors
7336 * when reading back.
7337 */
7338 for (i = seg + 1; i < nr_segs; i++) {
7339 if (iov[seg].iov_base == iov[i].iov_base)
7340 goto out;
7341 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007342 }
7343 retval = 0;
7344out:
7345 return retval;
7346}
Josef Bacikeb838e72012-07-31 16:28:48 -04007347
Chris Mason16432982008-04-10 10:23:21 -04007348static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7349 const struct iovec *iov, loff_t offset,
7350 unsigned long nr_segs)
7351{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007352 struct file *file = iocb->ki_filp;
7353 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007354 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007355 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007356 bool wakeup = true;
7357 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007358 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007359
Chris Mason5a5f79b2010-05-26 21:33:37 -04007360 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007361 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007362 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007363
Miao Xie38851cc2013-02-08 07:04:11 +00007364 atomic_inc(&inode->i_dio_count);
7365 smp_mb__after_atomic_inc();
7366
Miao Xie09348562013-02-07 10:12:07 +00007367 if (rw & WRITE) {
7368 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007369 /*
7370 * If the write DIO is beyond the EOF, we need update
7371 * the isize, but it is protected by i_mutex. So we can
7372 * not unlock the i_mutex at this case.
7373 */
7374 if (offset + count <= inode->i_size) {
7375 mutex_unlock(&inode->i_mutex);
7376 relock = true;
7377 }
Miao Xie09348562013-02-07 10:12:07 +00007378 ret = btrfs_delalloc_reserve_space(inode, count);
7379 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007380 goto out;
7381 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7382 &BTRFS_I(inode)->runtime_flags))) {
7383 inode_dio_done(inode);
7384 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7385 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007386 }
7387
7388 ret = __blockdev_direct_IO(rw, iocb, inode,
7389 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7390 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007391 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007392 if (rw & WRITE) {
7393 if (ret < 0 && ret != -EIOCBQUEUED)
7394 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007395 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007396 btrfs_delalloc_release_space(inode,
7397 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007398 else
7399 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007400 }
Miao Xie38851cc2013-02-08 07:04:11 +00007401out:
Miao Xie2e60a512013-02-08 07:01:08 +00007402 if (wakeup)
7403 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007404 if (relock)
7405 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007406
7407 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007408}
7409
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007410#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7411
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007412static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7413 __u64 start, __u64 len)
7414{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007415 int ret;
7416
7417 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7418 if (ret)
7419 return ret;
7420
Chris Masonec29ed52011-02-23 16:23:20 -05007421 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007422}
7423
Chris Mason9ebefb182007-06-15 13:50:00 -04007424int btrfs_readpage(struct file *file, struct page *page)
7425{
Chris Masond1310b22008-01-24 16:13:08 -05007426 struct extent_io_tree *tree;
7427 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007428 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007429}
Chris Mason1832a6d2007-12-21 16:27:21 -05007430
Chris Mason39279cc2007-06-12 06:35:45 -04007431static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7432{
Chris Masond1310b22008-01-24 16:13:08 -05007433 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007434
7435
7436 if (current->flags & PF_MEMALLOC) {
7437 redirty_page_for_writepage(wbc, page);
7438 unlock_page(page);
7439 return 0;
7440 }
Chris Masond1310b22008-01-24 16:13:08 -05007441 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007442 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7443}
Chris Mason39279cc2007-06-12 06:35:45 -04007444
Eric Sandeen48a3b632013-04-25 20:41:01 +00007445static int btrfs_writepages(struct address_space *mapping,
7446 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007447{
Chris Masond1310b22008-01-24 16:13:08 -05007448 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007449
Chris Masond1310b22008-01-24 16:13:08 -05007450 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007451 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7452}
7453
Chris Mason3ab2fb52007-11-08 10:59:22 -05007454static int
7455btrfs_readpages(struct file *file, struct address_space *mapping,
7456 struct list_head *pages, unsigned nr_pages)
7457{
Chris Masond1310b22008-01-24 16:13:08 -05007458 struct extent_io_tree *tree;
7459 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007460 return extent_readpages(tree, mapping, pages, nr_pages,
7461 btrfs_get_extent);
7462}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007463static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007464{
Chris Masond1310b22008-01-24 16:13:08 -05007465 struct extent_io_tree *tree;
7466 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007467 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007468
Chris Masond1310b22008-01-24 16:13:08 -05007469 tree = &BTRFS_I(page->mapping->host)->io_tree;
7470 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007471 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007472 if (ret == 1) {
7473 ClearPagePrivate(page);
7474 set_page_private(page, 0);
7475 page_cache_release(page);
7476 }
7477 return ret;
7478}
Chris Mason39279cc2007-06-12 06:35:45 -04007479
Chris Masone6dcd2d2008-07-17 12:53:50 -04007480static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7481{
Chris Mason98509cf2008-09-11 15:51:43 -04007482 if (PageWriteback(page) || PageDirty(page))
7483 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007484 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007485}
7486
Chris Masona52d9a82007-08-27 16:49:44 -04007487static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7488{
Josef Bacik5fd02042012-05-02 14:00:54 -04007489 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007490 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007491 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007492 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007493 u64 page_start = page_offset(page);
7494 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007495
Chris Mason8b62b722009-09-02 16:53:46 -04007496 /*
7497 * we have the page locked, so new writeback can't start,
7498 * and the dirty bit won't be cleared while we are here.
7499 *
7500 * Wait for IO on this page so that we can safely clear
7501 * the PagePrivate2 bit and do ordered accounting
7502 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007503 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007504
Josef Bacik5fd02042012-05-02 14:00:54 -04007505 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007506 if (offset) {
7507 btrfs_releasepage(page, GFP_NOFS);
7508 return;
7509 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007510 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007511 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007512 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007513 /*
7514 * IO on this page will never be started, so we need
7515 * to account for any ordered extents now
7516 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007517 clear_extent_bit(tree, page_start, page_end,
7518 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007519 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7520 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007521 /*
7522 * whoever cleared the private bit is responsible
7523 * for the finish_ordered_io
7524 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007525 if (TestClearPagePrivate2(page) &&
7526 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7527 PAGE_CACHE_SIZE, 1)) {
7528 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007529 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007530 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007531 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007532 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007533 }
7534 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007535 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007536 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7537 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007538 __btrfs_releasepage(page, GFP_NOFS);
7539
Chris Mason4a096752008-07-21 10:29:44 -04007540 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007541 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007542 ClearPagePrivate(page);
7543 set_page_private(page, 0);
7544 page_cache_release(page);
7545 }
Chris Mason39279cc2007-06-12 06:35:45 -04007546}
7547
Chris Mason9ebefb182007-06-15 13:50:00 -04007548/*
7549 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7550 * called from a page fault handler when a page is first dirtied. Hence we must
7551 * be careful to check for EOF conditions here. We set the page up correctly
7552 * for a written page which means we get ENOSPC checking when writing into
7553 * holes and correct delalloc and unwritten extent mapping on filesystems that
7554 * support these features.
7555 *
7556 * We are not allowed to take the i_mutex here so we have to play games to
7557 * protect against truncate races as the page could now be beyond EOF. Because
7558 * vmtruncate() writes the inode size before removing pages, once we have the
7559 * page lock we can determine safely if the page is beyond EOF. If it is not
7560 * beyond EOF, then the page is guaranteed safe against truncation until we
7561 * unlock the page.
7562 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007563int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007564{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007565 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007566 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007567 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007568 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7569 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007570 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007571 char *kaddr;
7572 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007573 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007574 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007575 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007576 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007577 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007578
Jan Karab2b5ef52012-06-12 16:20:45 +02007579 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007580 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007581 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007582 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007583 reserved = 1;
7584 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007585 if (ret) {
7586 if (ret == -ENOMEM)
7587 ret = VM_FAULT_OOM;
7588 else /* -ENOSPC, -EIO, etc */
7589 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007590 if (reserved)
7591 goto out;
7592 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007593 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007594
Nick Piggin56a76f82009-03-31 15:23:23 -07007595 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007596again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007597 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007598 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007599 page_start = page_offset(page);
7600 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007601
Chris Mason9ebefb182007-06-15 13:50:00 -04007602 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007603 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007604 /* page got truncated out from underneath us */
7605 goto out_unlock;
7606 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007607 wait_on_page_writeback(page);
7608
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007609 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007610 set_page_extent_mapped(page);
7611
Chris Masoneb84ae02008-07-17 13:53:27 -04007612 /*
7613 * we can't set the delalloc bits if there are pending ordered
7614 * extents. Drop our locks and wait for them to finish
7615 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007616 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7617 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007618 unlock_extent_cached(io_tree, page_start, page_end,
7619 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007620 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007621 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007622 btrfs_put_ordered_extent(ordered);
7623 goto again;
7624 }
7625
Josef Bacikfbf19082009-10-01 17:10:23 -04007626 /*
7627 * XXX - page_mkwrite gets called every time the page is dirtied, even
7628 * if it was already dirty, so for space accounting reasons we need to
7629 * clear any delalloc bits for the range we are fixing to save. There
7630 * is probably a better way to do this, but for now keep consistent with
7631 * prepare_pages in the normal write path.
7632 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007633 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007634 EXTENT_DIRTY | EXTENT_DELALLOC |
7635 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007636 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007637
Josef Bacik2ac55d42010-02-03 19:33:23 +00007638 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7639 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007640 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007641 unlock_extent_cached(io_tree, page_start, page_end,
7642 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007643 ret = VM_FAULT_SIGBUS;
7644 goto out_unlock;
7645 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007646 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007647
7648 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007649 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007650 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007651 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007652 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007653
Chris Masone6dcd2d2008-07-17 12:53:50 -04007654 if (zero_start != PAGE_CACHE_SIZE) {
7655 kaddr = kmap(page);
7656 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7657 flush_dcache_page(page);
7658 kunmap(page);
7659 }
Chris Mason247e7432008-07-17 12:53:51 -04007660 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007661 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007662 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007663
Chris Mason257c62e2009-10-13 13:21:08 -04007664 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7665 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007666 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007667
Josef Bacik2ac55d42010-02-03 19:33:23 +00007668 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007669
7670out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007671 if (!ret) {
7672 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007673 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007674 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007675 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007676out:
Josef Bacikec39e182012-01-12 19:10:12 -05007677 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007678out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007679 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007680 return ret;
7681}
7682
Josef Bacika41ad392011-01-31 15:30:16 -05007683static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007684{
7685 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007686 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007687 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007688 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007689 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007690 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007691 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007692
Josef Bacik2aaa6652012-08-29 14:27:18 -04007693 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007694 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007695 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007696
Chris Mason4a096752008-07-21 10:29:44 -04007697 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007698 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007699
Josef Bacikfcb80c22011-05-03 10:40:22 -04007700 /*
7701 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7702 * 3 things going on here
7703 *
7704 * 1) We need to reserve space for our orphan item and the space to
7705 * delete our orphan item. Lord knows we don't want to have a dangling
7706 * orphan item because we didn't reserve space to remove it.
7707 *
7708 * 2) We need to reserve space to update our inode.
7709 *
7710 * 3) We need to have something to cache all the space that is going to
7711 * be free'd up by the truncate operation, but also have some slack
7712 * space reserved in case it uses space during the truncate (thank you
7713 * very much snapshotting).
7714 *
7715 * And we need these to all be seperate. The fact is we can use alot of
7716 * space doing the truncate, and we have no earthly idea how much space
7717 * we will use, so we need the truncate reservation to be seperate so it
7718 * doesn't end up using space reserved for updating the inode or
7719 * removing the orphan item. We also need to be able to stop the
7720 * transaction and start a new one, which means we need to be able to
7721 * update the inode several times, and we have no idea of knowing how
7722 * many times that will be, so we can't just reserve 1 item for the
7723 * entirety of the opration, so that has to be done seperately as well.
7724 * Then there is the orphan item, which does indeed need to be held on
7725 * to for the whole operation, and we need nobody to touch this reserved
7726 * space except the orphan code.
7727 *
7728 * So that leaves us with
7729 *
7730 * 1) root->orphan_block_rsv - for the orphan deletion.
7731 * 2) rsv - for the truncate reservation, which we will steal from the
7732 * transaction reservation.
7733 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7734 * updating the inode.
7735 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007736 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007737 if (!rsv)
7738 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007739 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007740 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007741
Josef Bacik907cbce2011-08-08 13:46:15 -04007742 /*
Josef Bacik07127182011-08-19 10:29:59 -04007743 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007744 * 1 for updating the inode.
7745 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007746 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007747 if (IS_ERR(trans)) {
7748 err = PTR_ERR(trans);
7749 goto out;
7750 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007751
Josef Bacik907cbce2011-08-08 13:46:15 -04007752 /* Migrate the slack space for the truncate to our reserve */
7753 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7754 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007755 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007756
Chris Mason5a3f23d2009-03-31 13:27:11 -04007757 /*
7758 * setattr is responsible for setting the ordered_data_close flag,
7759 * but that is only tested during the last file release. That
7760 * could happen well after the next commit, leaving a great big
7761 * window where new writes may get lost if someone chooses to write
7762 * to this file after truncating to zero
7763 *
7764 * The inode doesn't have any dirty data here, and so if we commit
7765 * this is a noop. If someone immediately starts writing to the inode
7766 * it is very likely we'll catch some of their writes in this
7767 * transaction, and the commit will find this file on the ordered
7768 * data list with good things to send down.
7769 *
7770 * This is a best effort solution, there is still a window where
7771 * using truncate to replace the contents of the file will
7772 * end up with a zero length file after a crash.
7773 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007774 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7775 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007776 btrfs_add_ordered_operation(trans, root, inode);
7777
Josef Bacik5dc562c2012-08-17 13:14:17 -04007778 /*
7779 * So if we truncate and then write and fsync we normally would just
7780 * write the extents that changed, which is a problem if we need to
7781 * first truncate that entire inode. So set this flag so we write out
7782 * all of the extents in the inode to the sync log so we're completely
7783 * safe.
7784 */
7785 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007786 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007787
Yan, Zheng80825102009-11-12 09:35:36 +00007788 while (1) {
7789 ret = btrfs_truncate_inode_items(trans, root, inode,
7790 inode->i_size,
7791 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007792 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007793 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007794 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007795 }
Chris Mason39279cc2007-06-12 06:35:45 -04007796
Josef Bacikfcb80c22011-05-03 10:40:22 -04007797 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007798 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007799 if (ret) {
7800 err = ret;
7801 break;
7802 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007803
Yan, Zheng80825102009-11-12 09:35:36 +00007804 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007805 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007806
7807 trans = btrfs_start_transaction(root, 2);
7808 if (IS_ERR(trans)) {
7809 ret = err = PTR_ERR(trans);
7810 trans = NULL;
7811 break;
7812 }
7813
7814 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7815 rsv, min_size);
7816 BUG_ON(ret); /* shouldn't happen */
7817 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007818 }
7819
7820 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007821 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007822 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007823 if (ret)
7824 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007825 }
7826
Chris Mason917c16b2011-11-08 14:49:59 -05007827 if (trans) {
7828 trans->block_rsv = &root->fs_info->trans_block_rsv;
7829 ret = btrfs_update_inode(trans, root, inode);
7830 if (ret && !err)
7831 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007832
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007833 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007834 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007835 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007836
7837out:
7838 btrfs_free_block_rsv(root, rsv);
7839
Josef Bacik3893e332011-01-31 16:03:11 -05007840 if (ret && !err)
7841 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007842
Josef Bacik3893e332011-01-31 16:03:11 -05007843 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007844}
7845
Sven Wegener3b963622008-06-09 21:57:42 -04007846/*
Chris Masond352ac62008-09-29 15:18:18 -04007847 * create a new subvolume directory/inode (helper for the ioctl).
7848 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007849int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007850 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007851{
Chris Mason39279cc2007-06-12 06:35:45 -04007852 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007853 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007854 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007855
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007856 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7857 new_dirid, new_dirid,
7858 S_IFDIR | (~current_umask() & S_IRWXUGO),
7859 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007860 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007861 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007862 inode->i_op = &btrfs_dir_inode_operations;
7863 inode->i_fop = &btrfs_dir_file_operations;
7864
Miklos Szeredibfe86842011-10-28 14:13:29 +02007865 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007866 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007867
Yan, Zheng76dda932009-09-21 16:00:26 -04007868 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007869
Yan, Zheng76dda932009-09-21 16:00:26 -04007870 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007871 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007872}
7873
Chris Mason39279cc2007-06-12 06:35:45 -04007874struct inode *btrfs_alloc_inode(struct super_block *sb)
7875{
7876 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007877 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007878
7879 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7880 if (!ei)
7881 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007882
7883 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007884 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007885 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007886 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007887 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007888 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007889 ei->disk_i_size = 0;
7890 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007891 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007892 ei->index_cnt = (u64)-1;
7893 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007894 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007895
Josef Bacik9e0baf62011-07-15 15:16:44 +00007896 spin_lock_init(&ei->lock);
7897 ei->outstanding_extents = 0;
7898 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007899
Josef Bacik72ac3c02012-05-23 14:13:11 -04007900 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007901 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007902
Miao Xie16cdcec2011-04-22 18:12:22 +08007903 ei->delayed_node = NULL;
7904
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007905 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007906 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007907 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7908 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007909 ei->io_tree.track_uptodate = 1;
7910 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007911 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007912 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007913 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007914 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007915 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007916 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007917 RB_CLEAR_NODE(&ei->rb_node);
7918
7919 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007920}
7921
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007922static void btrfs_i_callback(struct rcu_head *head)
7923{
7924 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007925 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7926}
7927
Chris Mason39279cc2007-06-12 06:35:45 -04007928void btrfs_destroy_inode(struct inode *inode)
7929{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007930 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007931 struct btrfs_root *root = BTRFS_I(inode)->root;
7932
Al Virob3d9b7a2012-06-09 13:51:19 -04007933 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007934 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007935 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7936 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007937 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7938 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007939
Chris Mason5a3f23d2009-03-31 13:27:11 -04007940 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007941 * This can happen where we create an inode, but somebody else also
7942 * created the same inode and we need to destroy the one we already
7943 * created.
7944 */
7945 if (!root)
7946 goto free;
7947
7948 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007949 * Make sure we're properly removed from the ordered operation
7950 * lists.
7951 */
7952 smp_mb();
7953 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7954 spin_lock(&root->fs_info->ordered_extent_lock);
7955 list_del_init(&BTRFS_I(inode)->ordered_operations);
7956 spin_unlock(&root->fs_info->ordered_extent_lock);
7957 }
7958
Josef Bacik8a35d952012-05-23 14:26:42 -04007959 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7960 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007961 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7962 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007963 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007964 }
Josef Bacik7b128762008-07-24 12:17:14 -04007965
Chris Masond3977122009-01-05 21:25:51 -05007966 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007967 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7968 if (!ordered)
7969 break;
7970 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007971 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7972 (unsigned long long)ordered->file_offset,
7973 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007974 btrfs_remove_ordered_extent(inode, ordered);
7975 btrfs_put_ordered_extent(ordered);
7976 btrfs_put_ordered_extent(ordered);
7977 }
7978 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007979 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007980 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007981free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007982 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007983 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007984}
7985
Al Viro45321ac2010-06-07 13:43:19 -04007986int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007987{
7988 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007989
Liu Bofa6ac872013-02-20 14:10:23 +00007990 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007991 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007992 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007993 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007994 else
Al Viro45321ac2010-06-07 13:43:19 -04007995 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007996}
7997
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007998static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007999{
8000 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8001
8002 inode_init_once(&ei->vfs_inode);
8003}
8004
8005void btrfs_destroy_cachep(void)
8006{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008007 /*
8008 * Make sure all delayed rcu free inodes are flushed before we
8009 * destroy cache.
8010 */
8011 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008012 if (btrfs_inode_cachep)
8013 kmem_cache_destroy(btrfs_inode_cachep);
8014 if (btrfs_trans_handle_cachep)
8015 kmem_cache_destroy(btrfs_trans_handle_cachep);
8016 if (btrfs_transaction_cachep)
8017 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008018 if (btrfs_path_cachep)
8019 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008020 if (btrfs_free_space_cachep)
8021 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008022 if (btrfs_delalloc_work_cachep)
8023 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008024}
8025
8026int btrfs_init_cachep(void)
8027{
David Sterba837e1972012-09-07 03:00:48 -06008028 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008029 sizeof(struct btrfs_inode), 0,
8030 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008031 if (!btrfs_inode_cachep)
8032 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008033
David Sterba837e1972012-09-07 03:00:48 -06008034 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008035 sizeof(struct btrfs_trans_handle), 0,
8036 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008037 if (!btrfs_trans_handle_cachep)
8038 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008039
David Sterba837e1972012-09-07 03:00:48 -06008040 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008041 sizeof(struct btrfs_transaction), 0,
8042 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008043 if (!btrfs_transaction_cachep)
8044 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008045
David Sterba837e1972012-09-07 03:00:48 -06008046 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008047 sizeof(struct btrfs_path), 0,
8048 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008049 if (!btrfs_path_cachep)
8050 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008051
David Sterba837e1972012-09-07 03:00:48 -06008052 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008053 sizeof(struct btrfs_free_space), 0,
8054 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8055 if (!btrfs_free_space_cachep)
8056 goto fail;
8057
Miao Xie8ccf6f192012-10-25 09:28:04 +00008058 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8059 sizeof(struct btrfs_delalloc_work), 0,
8060 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8061 NULL);
8062 if (!btrfs_delalloc_work_cachep)
8063 goto fail;
8064
Chris Mason39279cc2007-06-12 06:35:45 -04008065 return 0;
8066fail:
8067 btrfs_destroy_cachep();
8068 return -ENOMEM;
8069}
8070
8071static int btrfs_getattr(struct vfsmount *mnt,
8072 struct dentry *dentry, struct kstat *stat)
8073{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008074 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008075 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008076 u32 blocksize = inode->i_sb->s_blocksize;
8077
Chris Mason39279cc2007-06-12 06:35:45 -04008078 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008079 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008080 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008081
8082 spin_lock(&BTRFS_I(inode)->lock);
8083 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8084 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008085 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008086 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008087 return 0;
8088}
8089
Chris Masond3977122009-01-05 21:25:51 -05008090static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8091 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008092{
8093 struct btrfs_trans_handle *trans;
8094 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008095 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008096 struct inode *new_inode = new_dentry->d_inode;
8097 struct inode *old_inode = old_dentry->d_inode;
8098 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008099 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008100 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008101 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008102 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008103
Li Zefan33345d012011-04-20 10:31:50 +08008104 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008105 return -EPERM;
8106
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008107 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008108 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008109 return -EXDEV;
8110
Li Zefan33345d012011-04-20 10:31:50 +08008111 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8112 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008113 return -ENOTEMPTY;
8114
Chris Mason39279cc2007-06-12 06:35:45 -04008115 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008116 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008117 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008118
8119
8120 /* check for collisions, even if the name isn't there */
8121 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8122 new_dentry->d_name.name,
8123 new_dentry->d_name.len);
8124
8125 if (ret) {
8126 if (ret == -EEXIST) {
8127 /* we shouldn't get
8128 * eexist without a new_inode */
8129 if (!new_inode) {
8130 WARN_ON(1);
8131 return ret;
8132 }
8133 } else {
8134 /* maybe -EOVERFLOW */
8135 return ret;
8136 }
8137 }
8138 ret = 0;
8139
Chris Mason5a3f23d2009-03-31 13:27:11 -04008140 /*
8141 * we're using rename to replace one file with another.
8142 * and the replacement file is large. Start IO on it now so
8143 * we don't add too much work to the end of the transaction
8144 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008145 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008146 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8147 filemap_flush(old_inode->i_mapping);
8148
Yan, Zheng76dda932009-09-21 16:00:26 -04008149 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008150 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008151 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008152 /*
8153 * We want to reserve the absolute worst case amount of items. So if
8154 * both inodes are subvols and we need to unlink them then that would
8155 * require 4 item modifications, but if they are both normal inodes it
8156 * would require 5 item modifications, so we'll assume their normal
8157 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8158 * should cover the worst case number of items we'll modify.
8159 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008160 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008161 if (IS_ERR(trans)) {
8162 ret = PTR_ERR(trans);
8163 goto out_notrans;
8164 }
Chris Mason5f39d392007-10-15 16:14:19 -04008165
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008166 if (dest != root)
8167 btrfs_record_root_in_trans(trans, dest);
8168
Yan, Zhenga5719522009-09-24 09:17:31 -04008169 ret = btrfs_set_inode_index(new_dir, &index);
8170 if (ret)
8171 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008172
Li Zefan33345d012011-04-20 10:31:50 +08008173 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008174 /* force full log commit if subvolume involved. */
8175 root->fs_info->last_trans_log_full_commit = trans->transid;
8176 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008177 ret = btrfs_insert_inode_ref(trans, dest,
8178 new_dentry->d_name.name,
8179 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008180 old_ino,
8181 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008182 if (ret)
8183 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008184 /*
8185 * this is an ugly little race, but the rename is required
8186 * to make sure that if we crash, the inode is either at the
8187 * old name or the new one. pinning the log transaction lets
8188 * us make sure we don't allow a log commit to come in after
8189 * we unlink the name but before we add the new name back in.
8190 */
8191 btrfs_pin_log_trans(root);
8192 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008193 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008194 * make sure the inode gets flushed if it is replacing
8195 * something.
8196 */
Li Zefan33345d012011-04-20 10:31:50 +08008197 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008198 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008199
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008200 inode_inc_iversion(old_dir);
8201 inode_inc_iversion(new_dir);
8202 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008203 old_dir->i_ctime = old_dir->i_mtime = ctime;
8204 new_dir->i_ctime = new_dir->i_mtime = ctime;
8205 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008206
Chris Mason12fcfd22009-03-24 10:24:20 -04008207 if (old_dentry->d_parent != new_dentry->d_parent)
8208 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8209
Li Zefan33345d012011-04-20 10:31:50 +08008210 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008211 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8212 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8213 old_dentry->d_name.name,
8214 old_dentry->d_name.len);
8215 } else {
Al Viro92986792011-03-04 17:14:37 +00008216 ret = __btrfs_unlink_inode(trans, root, old_dir,
8217 old_dentry->d_inode,
8218 old_dentry->d_name.name,
8219 old_dentry->d_name.len);
8220 if (!ret)
8221 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008222 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008223 if (ret) {
8224 btrfs_abort_transaction(trans, root, ret);
8225 goto out_fail;
8226 }
Chris Mason39279cc2007-06-12 06:35:45 -04008227
8228 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008229 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008230 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008231 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008232 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8233 root_objectid = BTRFS_I(new_inode)->location.objectid;
8234 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8235 root_objectid,
8236 new_dentry->d_name.name,
8237 new_dentry->d_name.len);
8238 BUG_ON(new_inode->i_nlink == 0);
8239 } else {
8240 ret = btrfs_unlink_inode(trans, dest, new_dir,
8241 new_dentry->d_inode,
8242 new_dentry->d_name.name,
8243 new_dentry->d_name.len);
8244 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008245 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008246 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008247 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008248 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008249 if (ret) {
8250 btrfs_abort_transaction(trans, root, ret);
8251 goto out_fail;
8252 }
Chris Mason39279cc2007-06-12 06:35:45 -04008253 }
Josef Bacikaec74772008-07-24 12:12:38 -04008254
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008255 ret = btrfs_add_link(trans, new_dir, old_inode,
8256 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008257 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008258 if (ret) {
8259 btrfs_abort_transaction(trans, root, ret);
8260 goto out_fail;
8261 }
Chris Mason39279cc2007-06-12 06:35:45 -04008262
Li Zefan33345d012011-04-20 10:31:50 +08008263 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008264 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008265 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008266 btrfs_end_log_trans(root);
8267 }
Chris Mason39279cc2007-06-12 06:35:45 -04008268out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008269 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008270out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008271 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008272 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008273
Chris Mason39279cc2007-06-12 06:35:45 -04008274 return ret;
8275}
8276
Miao Xie8ccf6f192012-10-25 09:28:04 +00008277static void btrfs_run_delalloc_work(struct btrfs_work *work)
8278{
8279 struct btrfs_delalloc_work *delalloc_work;
8280
8281 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8282 work);
8283 if (delalloc_work->wait)
8284 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8285 else
8286 filemap_flush(delalloc_work->inode->i_mapping);
8287
8288 if (delalloc_work->delay_iput)
8289 btrfs_add_delayed_iput(delalloc_work->inode);
8290 else
8291 iput(delalloc_work->inode);
8292 complete(&delalloc_work->completion);
8293}
8294
8295struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8296 int wait, int delay_iput)
8297{
8298 struct btrfs_delalloc_work *work;
8299
8300 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8301 if (!work)
8302 return NULL;
8303
8304 init_completion(&work->completion);
8305 INIT_LIST_HEAD(&work->list);
8306 work->inode = inode;
8307 work->wait = wait;
8308 work->delay_iput = delay_iput;
8309 work->work.func = btrfs_run_delalloc_work;
8310
8311 return work;
8312}
8313
8314void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8315{
8316 wait_for_completion(&work->completion);
8317 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8318}
8319
Chris Masond352ac62008-09-29 15:18:18 -04008320/*
8321 * some fairly slow code that needs optimization. This walks the list
8322 * of all the inodes with pending delalloc and forces them to disk.
8323 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008324int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008325{
Chris Masonea8c2812008-08-04 23:17:27 -04008326 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008327 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008328 struct btrfs_delalloc_work *work, *next;
8329 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008330 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008331 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008332
Yan Zhengc146afa2008-11-12 14:34:12 -05008333 if (root->fs_info->sb->s_flags & MS_RDONLY)
8334 return -EROFS;
8335
Miao Xie8ccf6f192012-10-25 09:28:04 +00008336 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008337 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008338
Chris Mason75eff682008-12-15 15:54:40 -05008339 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008340 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8341 while (!list_empty(&splice)) {
8342 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008343 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008344
8345 list_del_init(&binode->delalloc_inodes);
8346
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008347 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008348 if (!inode) {
8349 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8350 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008351 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008352 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008353
8354 list_add_tail(&binode->delalloc_inodes,
8355 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008356 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008357
8358 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8359 if (unlikely(!work)) {
8360 ret = -ENOMEM;
8361 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008362 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008363 list_add_tail(&work->list, &works);
8364 btrfs_queue_worker(&root->fs_info->flush_workers,
8365 &work->work);
8366
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008367 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008368 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008369 }
Chris Mason75eff682008-12-15 15:54:40 -05008370 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008371
Miao Xie1eafa6c2013-01-22 10:49:00 +00008372 list_for_each_entry_safe(work, next, &works, list) {
8373 list_del_init(&work->list);
8374 btrfs_wait_and_free_delalloc_work(work);
8375 }
8376
Chris Mason8c8bee12008-09-29 11:19:10 -04008377 /* the filemap_flush will queue IO into the worker threads, but
8378 * we have to make sure the IO is actually started and that
8379 * ordered extents get created before we return
8380 */
8381 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008382 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008383 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008384 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008385 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8386 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008387 }
8388 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008389 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008390out:
8391 list_for_each_entry_safe(work, next, &works, list) {
8392 list_del_init(&work->list);
8393 btrfs_wait_and_free_delalloc_work(work);
8394 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008395
8396 if (!list_empty_careful(&splice)) {
8397 spin_lock(&root->fs_info->delalloc_lock);
8398 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8399 spin_unlock(&root->fs_info->delalloc_lock);
8400 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008401 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008402}
8403
Chris Mason39279cc2007-06-12 06:35:45 -04008404static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8405 const char *symname)
8406{
8407 struct btrfs_trans_handle *trans;
8408 struct btrfs_root *root = BTRFS_I(dir)->root;
8409 struct btrfs_path *path;
8410 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008411 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008412 int err;
8413 int drop_inode = 0;
8414 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008415 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008416 int name_len;
8417 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008418 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008419 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008420 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008421
8422 name_len = strlen(symname) + 1;
8423 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8424 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008425
Josef Bacik9ed74f22009-09-11 16:12:44 -04008426 /*
8427 * 2 items for inode item and ref
8428 * 2 items for dir items
8429 * 1 item for xattr if selinux is on
8430 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008431 trans = btrfs_start_transaction(root, 5);
8432 if (IS_ERR(trans))
8433 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008434
Li Zefan581bb052011-04-20 10:06:11 +08008435 err = btrfs_find_free_ino(root, &objectid);
8436 if (err)
8437 goto out_unlock;
8438
Josef Bacikaec74772008-07-24 12:12:38 -04008439 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008440 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008441 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008442 if (IS_ERR(inode)) {
8443 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008444 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008445 }
Chris Mason39279cc2007-06-12 06:35:45 -04008446
Eric Paris2a7dba32011-02-01 11:05:39 -05008447 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008448 if (err) {
8449 drop_inode = 1;
8450 goto out_unlock;
8451 }
8452
Casey Schauflerad19db72011-12-15 10:09:07 -05008453 /*
8454 * If the active LSM wants to access the inode during
8455 * d_instantiate it needs these. Smack checks to see
8456 * if the filesystem supports xattrs by looking at the
8457 * ops vector.
8458 */
8459 inode->i_fop = &btrfs_file_operations;
8460 inode->i_op = &btrfs_file_inode_operations;
8461
Josef Bacika1b075d2010-11-19 20:36:11 +00008462 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008463 if (err)
8464 drop_inode = 1;
8465 else {
8466 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008467 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008468 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008469 }
Chris Mason39279cc2007-06-12 06:35:45 -04008470 if (drop_inode)
8471 goto out_unlock;
8472
8473 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008474 if (!path) {
8475 err = -ENOMEM;
8476 drop_inode = 1;
8477 goto out_unlock;
8478 }
Li Zefan33345d012011-04-20 10:31:50 +08008479 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008480 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008481 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8482 datasize = btrfs_file_extent_calc_inline_size(name_len);
8483 err = btrfs_insert_empty_item(trans, root, path, &key,
8484 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008485 if (err) {
8486 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008487 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008488 goto out_unlock;
8489 }
Chris Mason5f39d392007-10-15 16:14:19 -04008490 leaf = path->nodes[0];
8491 ei = btrfs_item_ptr(leaf, path->slots[0],
8492 struct btrfs_file_extent_item);
8493 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8494 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008495 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008496 btrfs_set_file_extent_encryption(leaf, ei, 0);
8497 btrfs_set_file_extent_compression(leaf, ei, 0);
8498 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8499 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8500
Chris Mason39279cc2007-06-12 06:35:45 -04008501 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008502 write_extent_buffer(leaf, symname, ptr, name_len);
8503 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008504 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008505
Chris Mason39279cc2007-06-12 06:35:45 -04008506 inode->i_op = &btrfs_symlink_inode_operations;
8507 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008508 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008509 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008510 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008511 err = btrfs_update_inode(trans, root, inode);
8512 if (err)
8513 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008514
8515out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008516 if (!err)
8517 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008518 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008519 if (drop_inode) {
8520 inode_dec_link_count(inode);
8521 iput(inode);
8522 }
Liu Bob53d3f52012-11-14 14:34:34 +00008523 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008524 return err;
8525}
Chris Mason16432982008-04-10 10:23:21 -04008526
Josef Bacik0af3d002010-06-21 14:48:16 -04008527static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8528 u64 start, u64 num_bytes, u64 min_size,
8529 loff_t actual_len, u64 *alloc_hint,
8530 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008531{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008532 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8533 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008534 struct btrfs_root *root = BTRFS_I(inode)->root;
8535 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008536 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008537 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008538 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008539 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008540 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008541
Josef Bacik0af3d002010-06-21 14:48:16 -04008542 if (trans)
8543 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008544 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008545 if (own_trans) {
8546 trans = btrfs_start_transaction(root, 3);
8547 if (IS_ERR(trans)) {
8548 ret = PTR_ERR(trans);
8549 break;
8550 }
Yan Zhengd899e052008-10-30 14:25:28 -04008551 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008552
Chris Mason154ea282013-03-05 11:11:26 -05008553 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8554 cur_bytes = max(cur_bytes, min_size);
8555 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008556 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008557 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008558 if (own_trans)
8559 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008560 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008561 }
8562
Yan Zhengd899e052008-10-30 14:25:28 -04008563 ret = insert_reserved_file_extent(trans, inode,
8564 cur_offset, ins.objectid,
8565 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008566 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008567 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008568 if (ret) {
8569 btrfs_abort_transaction(trans, root, ret);
8570 if (own_trans)
8571 btrfs_end_transaction(trans, root);
8572 break;
8573 }
Chris Masona1ed8352009-09-11 12:27:37 -04008574 btrfs_drop_extent_cache(inode, cur_offset,
8575 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008576
Josef Bacik5dc562c2012-08-17 13:14:17 -04008577 em = alloc_extent_map();
8578 if (!em) {
8579 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8580 &BTRFS_I(inode)->runtime_flags);
8581 goto next;
8582 }
8583
8584 em->start = cur_offset;
8585 em->orig_start = cur_offset;
8586 em->len = ins.offset;
8587 em->block_start = ins.objectid;
8588 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008589 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008590 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008591 em->bdev = root->fs_info->fs_devices->latest_bdev;
8592 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8593 em->generation = trans->transid;
8594
8595 while (1) {
8596 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008597 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008598 write_unlock(&em_tree->lock);
8599 if (ret != -EEXIST)
8600 break;
8601 btrfs_drop_extent_cache(inode, cur_offset,
8602 cur_offset + ins.offset - 1,
8603 0);
8604 }
8605 free_extent_map(em);
8606next:
Yan Zhengd899e052008-10-30 14:25:28 -04008607 num_bytes -= ins.offset;
8608 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008609 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008610
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008611 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008612 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008613 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008614 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008615 (actual_len > inode->i_size) &&
8616 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008617 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008618 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008619 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008620 i_size = cur_offset;
8621 i_size_write(inode, i_size);
8622 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008623 }
8624
Yan Zhengd899e052008-10-30 14:25:28 -04008625 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008626
8627 if (ret) {
8628 btrfs_abort_transaction(trans, root, ret);
8629 if (own_trans)
8630 btrfs_end_transaction(trans, root);
8631 break;
8632 }
Yan Zhengd899e052008-10-30 14:25:28 -04008633
Josef Bacik0af3d002010-06-21 14:48:16 -04008634 if (own_trans)
8635 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008636 }
Yan Zhengd899e052008-10-30 14:25:28 -04008637 return ret;
8638}
8639
Josef Bacik0af3d002010-06-21 14:48:16 -04008640int btrfs_prealloc_file_range(struct inode *inode, int mode,
8641 u64 start, u64 num_bytes, u64 min_size,
8642 loff_t actual_len, u64 *alloc_hint)
8643{
8644 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8645 min_size, actual_len, alloc_hint,
8646 NULL);
8647}
8648
8649int btrfs_prealloc_file_range_trans(struct inode *inode,
8650 struct btrfs_trans_handle *trans, int mode,
8651 u64 start, u64 num_bytes, u64 min_size,
8652 loff_t actual_len, u64 *alloc_hint)
8653{
8654 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8655 min_size, actual_len, alloc_hint, trans);
8656}
8657
Chris Masone6dcd2d2008-07-17 12:53:50 -04008658static int btrfs_set_page_dirty(struct page *page)
8659{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008660 return __set_page_dirty_nobuffers(page);
8661}
8662
Al Viro10556cb2011-06-20 19:28:19 -04008663static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008664{
Li Zefanb83cc962010-12-20 16:04:08 +08008665 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008666 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008667
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008668 if (mask & MAY_WRITE &&
8669 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8670 if (btrfs_root_readonly(root))
8671 return -EROFS;
8672 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8673 return -EACCES;
8674 }
Al Viro2830ba72011-06-20 19:16:29 -04008675 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008676}
Chris Mason39279cc2007-06-12 06:35:45 -04008677
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008678static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008679 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008680 .lookup = btrfs_lookup,
8681 .create = btrfs_create,
8682 .unlink = btrfs_unlink,
8683 .link = btrfs_link,
8684 .mkdir = btrfs_mkdir,
8685 .rmdir = btrfs_rmdir,
8686 .rename = btrfs_rename,
8687 .symlink = btrfs_symlink,
8688 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008689 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008690 .setxattr = btrfs_setxattr,
8691 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008692 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008693 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008694 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008695 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008696};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008697static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008698 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008699 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008700 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008701};
Yan, Zheng76dda932009-09-21 16:00:26 -04008702
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008703static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008704 .llseek = generic_file_llseek,
8705 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008706 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008707 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008708#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008709 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008710#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008711 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008712 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008713};
8714
Chris Masond1310b22008-01-24 16:13:08 -05008715static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008716 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008717 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008718 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008719 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008720 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008721 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008722 .set_bit_hook = btrfs_set_bit_hook,
8723 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008724 .merge_extent_hook = btrfs_merge_extent_hook,
8725 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008726};
8727
Chris Mason35054392009-01-21 13:11:13 -05008728/*
8729 * btrfs doesn't support the bmap operation because swapfiles
8730 * use bmap to make a mapping of extents in the file. They assume
8731 * these extents won't change over the life of the file and they
8732 * use the bmap result to do IO directly to the drive.
8733 *
8734 * the btrfs bmap call would return logical addresses that aren't
8735 * suitable for IO and they also will change frequently as COW
8736 * operations happen. So, swapfile + btrfs == corruption.
8737 *
8738 * For now we're avoiding this by dropping bmap.
8739 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008740static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008741 .readpage = btrfs_readpage,
8742 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008743 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008744 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008745 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008746 .invalidatepage = btrfs_invalidatepage,
8747 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008748 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008749 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008750};
8751
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008752static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008753 .readpage = btrfs_readpage,
8754 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008755 .invalidatepage = btrfs_invalidatepage,
8756 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008757};
8758
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008759static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008760 .getattr = btrfs_getattr,
8761 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008762 .setxattr = btrfs_setxattr,
8763 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008764 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008765 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008766 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008767 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008768 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008769 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008770};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008771static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008772 .getattr = btrfs_getattr,
8773 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008774 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008775 .setxattr = btrfs_setxattr,
8776 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008777 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008778 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008779 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008780 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008781};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008782static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008783 .readlink = generic_readlink,
8784 .follow_link = page_follow_link_light,
8785 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008786 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008787 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008788 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008789 .setxattr = btrfs_setxattr,
8790 .getxattr = btrfs_getxattr,
8791 .listxattr = btrfs_listxattr,
8792 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008793 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008794 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008795};
Yan, Zheng76dda932009-09-21 16:00:26 -04008796
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008797const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008798 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008799 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008800};